-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_setup.py
63 lines (48 loc) · 1.64 KB
/
env_setup.py
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
import subprocess
import os
import sys
from pathlib import Path
import shutil
def run_setup():
""" sets up a base project
Creates a virtual environment.
Uppgrades pip
Installs wheel
Installs anything the requirements.txt file
"""
# find the base path and set path to python binary
base_path = Path(__file__).parent
python_bin = f"{base_path}/venv/Scripts/python" if os.name == 'nt' else f"{base_path}/venv/bin/python3"
if Path(f"{base_path}/venv").exists():
print("Virtual Environment already exists what should we do?")
print("d: Delete if exists and recreate it (default)")
print("n: do nothing and stop the script")
print("r: run the installs anyway without deleting first")
answer = input("d/n/r (d): ") or 'd'
if answer.upper() == 'N':
print("Exiting")
sys.exit()
elif answer.upper() == 'D':
print("Deleting and recreating")
shutil.rmtree(f"{base_path}/venv", ignore_errors=True)
# create virtual environment if not exists
make_venv = "python -m venv venv"
os.system(make_venv)
elif answer.upper() == 'R':
print("Running anyway")
# commands to run
cmds = [
'-m pip install --upgrade pip',
'-m pip install wheel',
'-m pip install -r requirements.txt',
]
for cmd in cmds:
cmd = [python_bin] + cmd.split()
print(' '.join(cmd))
process = subprocess.Popen(cmd, shell=True)
# wait for process to run
process.wait()
# kill the process
process.kill()
if __name__ == '__main__':
run_setup()