-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopticonn.py
More file actions
70 lines (52 loc) · 2.03 KB
/
opticonn.py
File metadata and controls
70 lines (52 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""OptiConn CLI entrypoint.
This launcher ensures the repository's local virtual environment is used by
default. If a venv is found (e.g., ``braingraph_pipeline/`` or ``.venv``),
the script re-execs itself with that interpreter before handing off to the
central CLI hub in ``scripts/opticonn_hub.py``.
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
def _repo_root() -> Path:
return Path(__file__).resolve().parent
def _candidate_venvs(root: Path) -> list[Path]:
return [
root / "braingraph_pipeline", # project-named venv (observed in this repo)
root / ".venv",
root / "venv",
]
def _venv_python_path(venv_dir: Path) -> Path:
# POSIX vs Windows
posix = venv_dir / "bin" / "python"
win = venv_dir / "Scripts" / "python.exe"
return posix if posix.exists() else win
def _in_that_venv(target_venv: Path) -> bool:
# Heuristic: VIRTUAL_ENV matches or sys.prefix located under target venv
ve = os.environ.get("VIRTUAL_ENV")
if ve and Path(ve).resolve() == target_venv.resolve():
return True
try:
return target_venv.resolve() in Path(sys.prefix).resolve().parents
except Exception:
return False
def _bootstrap_venv_if_available() -> None:
if os.environ.get("OPTICONN_SKIP_VENV", "0") in ("1", "true", "yes"):
return
root = _repo_root()
for venv in _candidate_venvs(root):
py = _venv_python_path(venv)
if py.exists():
# Avoid infinite loop: if we already run with this interpreter, skip
if Path(sys.executable).resolve() == py.resolve() or _in_that_venv(venv):
return
# Re-exec current script with the venv interpreter
os.execv(str(py), [str(py), __file__, *sys.argv[1:]])
# No venv found: continue with current interpreter
def main() -> int:
from scripts.opticonn_hub import main as hub_main
return hub_main()
if __name__ == "__main__":
_bootstrap_venv_if_available()
raise SystemExit(main())