-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathultimate.py
More file actions
60 lines (40 loc) · 1.65 KB
/
Copy pathultimate.py
File metadata and controls
60 lines (40 loc) · 1.65 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
#!/usr/bin/env python3
"""Convenience launcher for Ultimate LAB5 Flasher.
It creates a local virtual environment on first run, installs this project in
editable mode, and then forwards all arguments to ``python -m lab5_flasher``.
"""
from __future__ import annotations
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
VENV_DIR = ROOT / ".venv"
MARKER = VENV_DIR / ".ultimate-lab5-installed"
def in_virtualenv() -> bool:
return sys.prefix != getattr(sys, "base_prefix", sys.prefix) or hasattr(sys, "real_prefix")
def venv_python() -> Path:
if os.name == "nt":
return VENV_DIR / "Scripts" / "python.exe"
return VENV_DIR / "bin" / "python"
def ensure_venv() -> Path:
python = venv_python()
if not python.exists():
subprocess.check_call([sys.executable, "-m", "venv", str(VENV_DIR)])
pyproject = ROOT / "pyproject.toml"
needs_install = not MARKER.exists()
if MARKER.exists() and pyproject.exists():
needs_install = MARKER.stat().st_mtime < pyproject.stat().st_mtime
if needs_install:
subprocess.check_call([str(python), "-m", "pip", "install", "--upgrade", "pip"])
subprocess.check_call([str(python), "-m", "pip", "install", "-e", str(ROOT)])
MARKER.write_text("installed\n", encoding="utf-8")
return python
def main() -> None:
if os.environ.get("ULTIMATE_FLASHER_NO_VENV") or in_virtualenv():
from lab5_flasher.cli import main as cli_main
raise SystemExit(cli_main())
python = ensure_venv()
os.execv(str(python), [str(python), "-m", "lab5_flasher", *sys.argv[1:]])
if __name__ == "__main__":
main()