From 4b5ad9b33805cc7e81032766608154f7dee8df84 Mon Sep 17 00:00:00 2001 From: team 1 Date: Wed, 25 Feb 2026 09:23:02 +0100 Subject: [PATCH] optimize py control --- .gitignore | 1 + python/vector/vector_control.py | 63 ++++++++++++++++++++++++++------- requirements.txt | 5 +++ 3 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index c5088ee..8413271 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ /var/locks /var/agent-history /var/cache +/var/run/* .env.local test.* # ---> Symfony diff --git a/python/vector/vector_control.py b/python/vector/vector_control.py index a08959f..9945242 100644 --- a/python/vector/vector_control.py +++ b/python/vector/vector_control.py @@ -18,7 +18,6 @@ from typing import Dict, List, Optional, Tuple BASE_PATH = Path(__file__).resolve().parents[2] VENV_DIR = BASE_PATH / ".venv" VENV_PY = VENV_DIR / "bin" / "python" -VENV_PIP = VENV_DIR / "bin" / "pip" UVICORN_BIN = VENV_DIR / "bin" / "uvicorn" PID_DIR = BASE_PATH / "var" / "run" @@ -112,9 +111,29 @@ def _curl(url: str, method: str = "GET", timeout_seconds: int = 3) -> Tuple[int, # ============================================================ -# Dependency Handling +# Dependency Handling (robust & self-healing) # ============================================================ +def _venv_python_ok() -> bool: + return VENV_PY.exists() and os.access(VENV_PY, os.X_OK) + + +def _ensure_pip() -> subprocess.CompletedProcess: + return subprocess.run( + [str(VENV_PY), "-m", "ensurepip", "--upgrade"], + capture_output=True, + text=True, + ) + + +def _pip_install(pkgs: List[str]) -> subprocess.CompletedProcess: + return subprocess.run( + [str(VENV_PY), "-m", "pip", "install", *pkgs], + capture_output=True, + text=True, + ) + + def check_modules() -> List[str]: missing = [] for module in REQUIRED_MODULES: @@ -126,6 +145,12 @@ def check_modules() -> List[str]: def install_missing_modules(missing: List[str]) -> Dict[str, str]: + if not _venv_python_ok(): + return { + "status": "error", + "detail": "venv python not found or not executable" + } + mod_to_pkg = { "fastapi": "fastapi", "uvicorn": "uvicorn", @@ -136,16 +161,28 @@ def install_missing_modules(missing: List[str]) -> Dict[str, str]: pkgs = [mod_to_pkg.get(m, m) for m in missing] - if not VENV_PIP.exists(): - return {"status": "error", "detail": "pip not found in .venv"} + # Try normal pip install + p = _pip_install(pkgs) + if p.returncode == 0: + return {"status": "ok", "detail": "installed: " + " ".join(pkgs)} - cmd = [str(VENV_PIP), "install", *pkgs] - p = subprocess.run(cmd, capture_output=True, text=True) + # Attempt pip repair via ensurepip + ensure = _ensure_pip() + if ensure.returncode != 0: + return { + "status": "error", + "detail": "ensurepip failed: " + (ensure.stderr or ensure.stdout).strip() + } - if p.returncode != 0: - return {"status": "error", "detail": (p.stderr or p.stdout).strip()} + # Retry install after repair + retry = _pip_install(pkgs) + if retry.returncode != 0: + return { + "status": "error", + "detail": (retry.stderr or retry.stdout).strip() + } - return {"status": "ok", "detail": "installed: " + " ".join(pkgs)} + return {"status": "ok", "detail": "installed after pip repair: " + " ".join(pkgs)} # ============================================================ @@ -173,14 +210,16 @@ def service_status(port: int) -> Dict: def start_service(host: str, port: int) -> Dict: - if not UVICORN_BIN.exists(): - return {"status": "error", "detail": "uvicorn not found in .venv"} + if not _venv_python_ok(): + return {"status": "error", "detail": "venv python not executable"} if _is_port_open("127.0.0.1", port): return {"status": "error", "detail": f"port {port} already in use"} cmd = [ - str(UVICORN_BIN), + str(VENV_PY), + "-m", + "uvicorn", "python.vector.vector_service:app", "--host", host, "--port", str(port), diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..48e2287 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +fastapi +uvicorn +faiss-cpu +sentence-transformers +numpy \ No newline at end of file