forked from opensight-cv/opensight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requirements.py
executable file
·220 lines (167 loc) · 5.25 KB
/
requirements.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/python3
import argparse
import json
import pathlib
import subprocess
import sys
parser = argparse.ArgumentParser(
description="Install OpenSight dependencies based on required feature-sets"
)
parser.add_argument(
"-y",
"--yes",
dest="autoconfirm",
action="store_true",
help="automatically confirm all prompts",
)
parser.add_argument(
"-l",
"--list",
dest="list",
action="store_true",
help="list packages to be installed and exit",
)
parser.add_argument(
"-r",
"--requirements",
dest="requirements",
default=None,
help="specify alternative requirements.txt",
)
parser.add_argument(
"-f",
"--filename",
dest="requirements_extra",
default=None,
help="specify alternative requirements_extra.json",
)
parser.add_argument(
"-n",
"--no-requirements",
dest="exclude_requirements",
action="store_true",
help="exclude requirements.txt",
)
parser.add_argument(
"-e", "--exclude", dest="excludes", action="append", help="exclude features",
)
parser.add_argument(
"-p",
"--print",
dest="print_overlays",
action="store_true",
help="print available overlays and exit",
)
def get_default_overlays():
defaults = ["base"]
if sys.platform.startswith("linux"):
defaults.append("linux")
return defaults
parser.add_argument(
"overlays",
action="append",
nargs="*",
help="overlays to install",
default=get_default_overlays(),
)
def parse_args(*args, **kwargs):
args = parser.parse_args(*args, **kwargs)
args.excludes = args.excludes or []
# the format is [*defaults, [*items]]
args.overlays = args.overlays[-1]
return args
def open_file(filename_arg, default, **kwargs):
if filename_arg is None:
# open file with default name, from the script's dir, not cwd
return open(pathlib.Path(__file__).parent.absolute() / default, **kwargs)
return open(filename_arg, **kwargs)
def prompt_user(prompt, default=False, autoconfirm=False):
if autoconfirm:
return True
if default:
query = "[Y/n]"
else:
query = "[y/N]"
prompt = f"{prompt} {query} "
while True:
out = input(prompt).strip().lower()
if not out:
return default
if out.startswith("y"):
return True
if out.startswith("n"):
return False
def process_overlay(data, overlay):
features = set()
parent = overlay.get("extends")
if parent:
features.update(process_overlay(data, data["overlays"][parent]))
if overlay.get("add"):
features.update(overlay.get("add"))
if overlay.get("remove"):
features.difference_update(overlay.get("remove"))
return features
def process_requirements(data, overlays, excludes):
features = set()
for overlay in overlays:
features.update(process_overlay(data, data["overlays"][overlay]))
features.difference_update(excludes)
packages = set()
descriptions = {}
for feature in features:
feature_data = data["features"][feature]
packages.update(feature_data["packages"])
descriptions[feature] = feature_data["description"]
return packages, descriptions
def check_args_exist(data, overlays, excludes):
for overlay in overlays:
if overlay not in data["overlays"]:
return f"overlay '{overlay}' to be added does not exist"
for exclude in excludes:
if exclude not in data["features"]:
return f"feature '{exclude}' to be excluded does not exist"
def print_descriptions(descriptions):
for description, values in sorted(descriptions.items()):
print(f" {description}:")
for line in values:
print(f" {line}")
def install_requirements(packages):
subprocess.check_call([sys.executable, "-m", "pip", "install", *packages])
def main(*args, **kwargs):
args = parse_args(*args, **kwargs)
with open_file(args.requirements_extra, "requirements_extra.json") as f:
data = json.load(f)
if args.print_overlays:
print("Available overlays:")
for overlay in data["overlays"].keys():
print(" ", overlay)
print()
return
error = check_args_exist(data, args.overlays, args.excludes)
if error:
return error
packages, descriptions = process_requirements(data, args.overlays, args.excludes)
if not args.exclude_requirements:
with open_file(args.requirements, "requirements.txt") as f:
for line in f:
requirement = line.split("#")[0].strip()
if requirement:
packages.add(requirement)
# sort ignoring case, like `pip freeze`
packages = sorted(packages, key=str.casefold)
if args.list:
print("\n".join(packages))
return
print("Features to be installed:")
print_descriptions(descriptions)
if not args.exclude_requirements:
print(" All minimal requirements from requirements.txt")
print()
print(f"Packages to be installed: {' '.join(packages)}")
print()
if prompt_user("Install packages?", autoconfirm=args.autoconfirm):
install_requirements(packages)
if __name__ == "__main__":
error = main()
if error:
exit(f"Error: {error}")