-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpackage.py
103 lines (79 loc) · 2.92 KB
/
package.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
"""Package management for LeTP."""
import os
import shutil
import argparse
import hashlib
__copyright__ = "Copyright (C) Sierra Wireless Inc."
def _parse_args():
"""Parse user arguments."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--clean",
"-c",
action="store_true",
help="Remove the required packages for LeTP",
)
parser.add_argument(
"--install",
"-i",
action="store_true",
help="Install the required packages for LeTP",
)
parser.add_argument(
"--path", "-p", default=os.getcwd(), help="Path to the LeTP root"
)
return parser.parse_args()
def _get_python_cmd():
"""Determine the python cmd and return the compatible one."""
python = "python3"
if not shutil.which(python) or not os.system(f"{python} --version") == 0:
python = "python"
return python
def remove_pkg(letp_path=None):
"""Remove the required packages for LeTP."""
if letp_path:
req_cache = os.path.join(letp_path, ".requirements")
python_cache = os.path.join(letp_path, "__pycache__")
requirement = os.path.join(letp_path, "requirements.txt")
if os.path.isdir(req_cache):
shutil.rmtree(req_cache)
if os.path.isdir(python_cache):
shutil.rmtree(python_cache)
if os.path.exists(requirement):
os.system(
"{} -m pip uninstall -y -r {}".format(_get_python_cmd(), requirement)
)
print("Packages were removed")
def install_pkg(letp_path=None):
"""Install the required packages for LeTP."""
if letp_path:
requirement = os.path.join(letp_path, "requirements.txt")
req_cache = os.path.join(letp_path, ".requirements")
if os.path.exists(requirement):
# make a hash object
req_hash = hashlib.sha1()
# open file for reading in binary mode
with open(requirement, "rb") as file:
# loop till the end of the file
chunk = 0
while chunk != b"":
# read only 1024 bytes at a time
chunk = file.read(1024)
req_hash.update(chunk)
req_hash = req_hash.hexdigest()
req_hash_path = os.path.join(req_cache, req_hash)
if os.path.exists(req_cache) and os.path.exists(req_hash_path):
return
print("LeTP dependencies: install {}".format(requirement))
os.system(
"{} -m pip install --user -r {}".format(_get_python_cmd(), requirement)
)
# Not failing on purpose in case the source folder is read-only
os.makedirs(req_cache, exist_ok=True)
open(req_hash_path, "w").close()
if __name__ == "__main__":
args = _parse_args()
if args.clean:
remove_pkg(args.path)
elif args.install:
install_pkg(args.path)