-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_env.py
More file actions
71 lines (47 loc) · 1.87 KB
/
run_env.py
File metadata and controls
71 lines (47 loc) · 1.87 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
71
import hashlib
import os
import platform
import subprocess
import sys
from pathlib import Path
PROJECT_DIR = Path(__file__).resolve().parent
IN_REPO_VENV_DIR = PROJECT_DIR / "venv"
def get_venv_dir():
project_hash = hashlib.sha1(str(PROJECT_DIR).encode("utf-8")).hexdigest()[:12]
if platform.system() == "Windows":
base_dir = Path(os.environ.get("LOCALAPPDATA", Path.home() / "AppData" / "Local"))
else:
base_dir = Path(os.environ.get("XDG_DATA_HOME", Path.home() / ".local" / "share"))
return base_dir / "PythonProjectVenvs" / f"{PROJECT_DIR.name}-{project_hash}"
VENV_DIR = get_venv_dir()
def get_venv_python():
if platform.system() == "Windows":
return VENV_DIR / "Scripts" / "python.exe"
return VENV_DIR / "bin" / "python"
def venv_exists():
return get_venv_python().exists()
def create_venv():
print(f"Creating virtual environment in: {VENV_DIR}")
VENV_DIR.parent.mkdir(parents=True, exist_ok=True)
subprocess.check_call([sys.executable, "-m", "venv", str(VENV_DIR)], cwd=PROJECT_DIR)
def install_requirements():
print("Installing dependencies...")
subprocess.check_call(
[str(get_venv_python()), "-m", "pip", "install", "-r", str(PROJECT_DIR / "requirements.txt")],
cwd=PROJECT_DIR,
)
def run_script():
print("Running Excel Generator in virtual environment...")
subprocess.check_call([str(get_venv_python()), str(PROJECT_DIR / "ExcelGenerator.py")], cwd=PROJECT_DIR)
def warn_about_in_repo_venv():
if IN_REPO_VENV_DIR.exists():
print(
"Warning: found an old 'venv' folder inside the project. "
"run_env.py no longer uses it, so you can delete it to stop Nextcloud from syncing it."
)
if __name__ == "__main__":
warn_about_in_repo_venv()
if not venv_exists():
create_venv()
install_requirements()
run_script()