72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
"""Configura ODBC portable desde config/odbc/."""
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from ..constants import IS_WINDOWS, ODBC_DIR
|
|
from ..utils.logger import app_logger
|
|
|
|
|
|
def _write_portable_odbc_ini(odbc_dir: Path) -> None:
|
|
"""Genera odbcinst.ini mínimo si no existe."""
|
|
inst = odbc_dir / "odbcinst.ini"
|
|
if inst.exists():
|
|
return
|
|
|
|
if IS_WINDOWS:
|
|
driver_path = odbc_dir / "msodbcsql18.dll"
|
|
if not driver_path.exists():
|
|
for candidate in odbc_dir.glob("msodbcsql*.dll"):
|
|
driver_path = candidate
|
|
break
|
|
driver_line = str(driver_path.resolve()) if driver_path.exists() else "msodbcsql18.dll"
|
|
content = f"""[ODBC Driver 18 for SQL Server]
|
|
Description=Microsoft ODBC Driver 18 for SQL Server
|
|
Driver={driver_line}
|
|
UsageCount=1
|
|
"""
|
|
else:
|
|
so_path = None
|
|
for pattern in ("libmsodbcsql-18*.so*", "msodbcsql-18*.so*"):
|
|
matches = list(odbc_dir.glob(pattern))
|
|
if matches:
|
|
so_path = matches[0]
|
|
break
|
|
driver_line = str(so_path.resolve()) if so_path else "libmsodbcsql-18.so"
|
|
content = f"""[ODBC Driver 18 for SQL Server]
|
|
Description=Microsoft ODBC Driver 18 for SQL Server
|
|
Driver={driver_line}
|
|
UsageCount=1
|
|
"""
|
|
inst.write_text(content, encoding="utf-8")
|
|
|
|
ini = odbc_dir / "odbc.ini"
|
|
if not ini.exists():
|
|
ini.write_text("[ODBC Data Sources]\n", encoding="utf-8")
|
|
|
|
|
|
def configure_odbc_environment() -> None:
|
|
"""Apunta pyodbc al driver embebido en config/odbc si está disponible."""
|
|
if not ODBC_DIR.is_dir():
|
|
return
|
|
|
|
lib_dir = ODBC_DIR / "lib"
|
|
if lib_dir.is_dir():
|
|
existing = os.environ.get("LD_LIBRARY_PATH", "")
|
|
lib_path = str(lib_dir.resolve())
|
|
if lib_path not in existing.split(":"):
|
|
os.environ["LD_LIBRARY_PATH"] = (
|
|
f"{lib_path}:{existing}" if existing else lib_path
|
|
)
|
|
|
|
_write_portable_odbc_ini(ODBC_DIR)
|
|
odbc_sys = str(ODBC_DIR.resolve())
|
|
os.environ["ODBCSYSINI"] = odbc_sys
|
|
os.environ["ODBCINI"] = str((ODBC_DIR / "odbc.ini").resolve())
|
|
|
|
if IS_WINDOWS:
|
|
os.environ["PATH"] = odbc_sys + os.pathsep + os.environ.get("PATH", "")
|
|
|
|
app_logger.debug(f"ODBC configurado: ODBCSYSINI={odbc_sys}")
|