-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetupPython.py
47 lines (39 loc) · 1.77 KB
/
SetupPython.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
import sys
import subprocess
import importlib.util as importlib_util
class PythonConfiguration:
@classmethod
def Validate(cls):
if not cls.__ValidatePython():
return # cannot validate further
for packageName in ["requests"]:
if not cls.__ValidatePackage(packageName):
return # cannot validate further
@classmethod
def __ValidatePython(cls, versionMajor = 3, versionMinor = 3):
if sys.version is not None:
print("Python version {0:d}.{1:d}.{2:d} detected.".format( \
sys.version_info.major, sys.version_info.minor, sys.version_info.micro))
if sys.version_info.major < versionMajor or (sys.version_info.major == versionMajor and sys.version_info.minor < versionMinor):
print("Python version too low, expected version {0:d}.{1:d} or higher.".format( \
versionMajor, versionMinor))
return False
return True
@classmethod
def __ValidatePackage(cls, packageName):
if importlib_util.find_spec(packageName) is None:
return cls.__InstallPackage(packageName)
return True
@classmethod
def __InstallPackage(cls, packageName):
permissionGranted = False
while not permissionGranted:
reply = str(input("Would you like to install Python package '{0:s}'? [Y/N]: ".format(packageName))).lower().strip()[:1]
if reply == 'n':
return False
permissionGranted = (reply == 'y')
print(f"Installing {packageName} module...")
subprocess.check_call(['python', '-m', 'pip', 'install', packageName])
return cls.__ValidatePackage(packageName)
if __name__ == "__main__":
PythonConfiguration.Validate()