-
Notifications
You must be signed in to change notification settings - Fork 1
/
env_setup.py
132 lines (98 loc) · 3.7 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
This script should ensure you have a functioning Python environment to run FLAVOENZYME SCRAPER
"""
import os
from os import path
from pathlib import Path, PureWindowsPath
import platform
import subprocess
import shutil
from modules.helpers.logger import log
VENV_DIR_NAME = 'flav_env'
def execute_system_command(command, show_command=True, show_output=True):
if show_command:
print(command)
output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
# Convert from bytes to string
output = str(output, "utf-8")
output = output.strip()
if show_output:
print(output)
return output
def print_instructions(venv_path):
print("\n")
# if Windows
if os.name == "nt":
win_path = Path(venv_path) / "Scripts/activate.bat"
ps_path = Path(venv_path) / "Scripts/Activate.ps1"
win_path = PureWindowsPath(win_path)
print(f'➡︎ Now execute: "{win_path}"\nIf you are using powershell type this instead {ps_path}')
print(f'If you get an error you might need to enable execution policy, follow this guide: https://docs.python.org/3/library/venv.html')
print(f'To allow PowerShell to activate virtual env run this: "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser"')
# If posix
if os.name == "posix":
print(f'➡︎ Now execute: "source {venv_path}/bin/activate"')
def print_os_info():
log(f'OS: {os.name}','info')
log(f'Platform: {platform.system()}\t{platform.release()}','info')
def create_venv(venv_path):
"""
venv_path: this must be a path, don't screw this up, yo!
"""
execute_system_command(f"python -m venv {venv_path}")
# execute_system_command(f'virtualenv {venv_path}')
# execute_system_command(f'source {venv_path}/bin/activate')
def confirm(venv_path):
user_confiremed = (
input(
f'\n\nYou already have virtual env "{venv_path}".\nDelete and recreate? (y/n): '
)
.lower()
.strip()[:1]
== "y"
)
if user_confiremed:
return True
else:
return False
def delete_venv(venv_path):
shutil.rmtree(venv_path)
log(f'Deleting {venv_path}','info')
def check_for_virtual_env(venv_dir_name):
# https://stackoverflow.com/a/58026969/872328
running_in_virtualenv = "VIRTUAL_ENV" in os.environ
# print(f'Currently running in venv? {running_in_virtualenv}')
# alternative ways to write this, also supporting the case where
# the variable is set but contains an empty string to indicate
# 'not in a virtual environment':
# running_in_virtualenv = bool(os.environ.get("VIRTUAL_ENV"))
# running_in_virtualenv = bool(os.getenv("VIRTUAL_ENV"))
cwd = os.getcwd()
# print(cwd)
venv_path = path.join(cwd, venv_dir_name)
# print(f'Checking {venv_path}')
venv_path_exists = path.exists(venv_path)
if venv_path_exists:
log(f'Virtual environment path does exist! 🎉','success')
else:
log(f'Virtual environment path does not exist! ❌','warning')
return venv_path_exists, venv_path
def main():
print_os_info()
venv_path_exists, venv_path = check_for_virtual_env(VENV_DIR_NAME)
if venv_path_exists:
if confirm(venv_path):
delete_venv(venv_path)
create_venv(venv_path)
else:
log("virtual env not deleted. If you are experiencing issues, try deleting it.",'info')
else:
create_venv(venv_path)
print_instructions(venv_path)
# activate_venv(venv_path)
# requirements_file_path = 'requirements.txt'
# execute_system_command(f'pip3 install -r {requirements_file_path}')
# activate
# install
if __name__ == "__main__":
main()