-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
65 lines (52 loc) · 1.78 KB
/
install.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
from subprocess import check_output, CalledProcessError
from shlex import split
import os
import sys
import re
def run(command, allow_fail=False):
print(command)
try:
output = check_output(split(command))
print(output.decode())
except CalledProcessError as e:
if not allow_fail:
raise e
def get_packages_path():
try:
_clear_local_directory_from_path()
import createserial
p = os.path.dirname(os.path.dirname(createserial.__file__))
print("Found existing installation...", p)
return p
except ImportError:
for p in sys.path:
if re.match(r'(/\w*)*python3\.\d+/\w+-packages', p):
print("Found packages directory...", p)
return p
raise FileNotFoundError("Could not find system python packages directory")
def _clear_local_directory_from_path():
cwd = os.getcwd()
sys.path = [p for p in sys.path if p != cwd]
def check_if_root():
try:
run("sudo echo Testing for root access...")
except CalledProcessError:
raise PermissionError("Please run as root")
def verify_install():
_clear_local_directory_from_path()
try:
import createserial
print("Successfully found createserial!")
except ImportError as e:
if "createserial" in e.msg:
raise ImportError("Failed to find the createserial library installed on the system")
else:
print(e.msg, 'but otherwise looking ok...')
def main():
check_if_root()
packages_path = get_packages_path()
run(f"sudo rm -r {os.path.join(packages_path, 'createserial')}", allow_fail=True)
run(f"sudo cp -r {os.path.join(os.getcwd(), 'createserial')} {packages_path}")
verify_install()
if __name__ == '__main__':
main()