-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure_python.py
More file actions
100 lines (73 loc) · 2.86 KB
/
configure_python.py
File metadata and controls
100 lines (73 loc) · 2.86 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
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
# setup a virtual environment with the proper packages for running DICE simulations
from __future__ import print_function
import subprocess
import sys
import os
if sys.version_info < (3, 0):
version_str = '.'.join(str(x) for x in sys.version_info[:3])
print(f"Must use Python 3, you're using version {version_str}")
sys.exit(0)
import venv
from types import SimpleNamespace
PACKAGES = [
'numpy',
'pandas',
'scipy'
]
done_msg = """
====================================================================================
\033[32;1mEverything is set up!\033[0m
To get started, you must first activate your Python virtual environment. You can
do this by using your terminal to run the command
\033[1m{}\033[0m
"""
remote_exec_fix_msg = """
===================================================================================
\033[31;1mError!\033[0m
It looks like you don't have the right permissions to create a virtual environemt.
To configure Python properly, please open a PowerShell prompt as administrator and run:
\033[1mSet-ExecutionPolicy -Scope CurrentUser RemoteSigned\033[0m
For details on why this is necessary, please visit:
\033[4mhttps://www.stanleyulili.com/powershell/solution-to-running-scripts-is-disabled-on-this-system-error-on-powershell/\033[0m
\033[4mhttps://tecadmin.net/powershell-running-scripts-is-disabled-system/\033[0m
"""
class DICEBuilder(venv.EnvBuilder):
def post_setup(self, context: SimpleNamespace) -> None:
is_windows = sys.platform == 'win32'
if is_windows:
python_exe = os.path.join(context.bin_path, 'python.exe')
else:
python_exe = os.path.join(context.bin_path, 'python')
cmd = [python_exe, '-m', 'pip', 'install', '--upgrade']
cmd.extend(['pip', 'setuptools'])
# install the right packages
cmd.extend(PACKAGES)
subprocess.check_call(cmd)
# ensure everything runs properly on windows
if is_windows:
exec_policy = subprocess.run(
'powershell Get-ExecutionPolicy', shell=True, capture_output=True
)
assert (
exec_policy.returncode == 0
), 'Unable to check execution level for Powershell'
policy = exec_policy.stdout.decode('utf-8').strip()
if policy.lower() in ['default', 'restriced', 'undefined']:
print(remote_exec_fix_msg)
sys.exit(1)
# setup environment
if __name__ == '__main__':
if sys.platform == 'win32':
os.system('') # enable escape codes for colorful output on windows
NAME = 'DICE'
DICEBuilder(
symlinks=(os.name != 'nt'),
with_pip=True,
).create(NAME)
print(
done_msg.format(
f".\\{NAME}\\Scripts\\activate"
if sys.platform == 'win32'
else f'source {NAME}/bin/activate'
)
)