-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overrides.py
executable file
·256 lines (206 loc) · 6.93 KB
/
overrides.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/python3
import argparse
import json
import os
from typing import List
VALID_ARCHES = [
"x86_64",
"i686",
"aarch64",
"armv7hl",
"ppc64le",
"s390x",
]
VALID_RELEASES = [
"rawhide",
"41",
"40",
"39",
"38",
"37",
"36",
"35",
"34",
"33",
"32",
"31",
]
def main():
parser = argparse.ArgumentParser()
parser.set_defaults(action=None)
path_parser = argparse.ArgumentParser(add_help=False)
path_parser.add_argument(
"--path",
"-p",
required=False,
nargs="?",
default="./overrides.json",
help="override path to the 'overrides.json' file",
)
parsers = parser.add_subparsers()
insert_parser = parsers.add_parser(
"insert",
parents=[path_parser],
help="insert new value into the overrides file (may need resorting afterwards)",
)
insert_parser.set_defaults(action="insert")
insert_parser.add_argument(
"release",
choices=(VALID_RELEASES + ["all"]),
help="release to add override for",
)
insert_parser.add_argument(
"arch",
choices=(VALID_ARCHES + ["all"]),
help="architecture to add override for",
)
insert_parser.add_argument(
"dependency",
help="dependency to ignore for specified packages",
)
insert_parser.add_argument(
"packages",
nargs="+",
default=[],
help="packages to override missing dependencies for (singleton all has the special meaning)",
)
sortit_parser = parsers.add_parser(
"sortit",
parents=[path_parser],
help="sort overrides file for reproducible contents",
)
sortit_parser.set_defaults(action="sortit")
validate_parser = parsers.add_parser(
"validate",
parents=[path_parser],
help="validate contents of the overrides file for correct syntax",
)
validate_parser.set_defaults(action="validate")
cli_args = vars(parser.parse_args())
action = cli_args["action"]
if action is None:
print("No action specified.")
return 1
cli_path = cli_args["path"]
if os.path.isabs(cli_path):
path = cli_path
else:
path = os.path.abspath(cli_path)
if action == "insert":
release = cli_args["release"]
arch = cli_args["arch"]
dep = cli_args["dependency"]
value = cli_args["packages"]
return insert(path, release, arch, dep, value)
if action == "sortit":
return sortit(path)
if action == "validate":
return validate(path)
def insert(path: str, release: str, arch: str, dep: str, values: List[str]) -> int:
with open(path) as file:
overrides = json.loads(file.read())
inserted = False
current = overrides[release][arch]
if dep in current.keys():
if current[dep] == "all":
print(" → 'all' override subsumes individual overrides, this has no effect")
elif values == ["all"]:
print(f" → upgrading to 'all' override for /{release}/{arch}/{dep}")
current[dep] = "all"
inserted = True
else:
print(f" → adding new values for /{release}/{arch}/{dep}")
current[dep].extend(values)
inserted = True
else:
if values == ["all"]:
print(f" → adding 'all' override for /{release}/{arch}/{dep}")
current[dep] = "all"
inserted = True
else:
print(f" → adding {len(values)} overrides for /{release}/{arch}/{dep}")
current[dep] = values
inserted = True
with open(path, "w") as file:
file.write(json.dumps(overrides, indent=2, sort_keys=True))
if inserted:
sortit(path)
return 0
def sortit(path: str):
with open(path) as file:
overrides = json.loads(file.read())
for release, release_item in overrides.items():
if not isinstance(release_item, dict):
print(f" - /{release} does not contain a map of architectures.")
print(type(release_item))
break
for arch, arch_item in release_item.items():
if not isinstance(arch_item, dict):
print(f" - /{release}/{arch} does not contain a map of dependencies.")
break
for dep, dep_item in arch_item.items():
if isinstance(dep_item, list):
dep_item.sort()
with open(path, "w") as file:
file.write(json.dumps(overrides, indent=2, sort_keys=True))
return 0
def validate(path: str):
try:
with open(path) as file:
overrides = json.loads(file.read())
except json.JSONDecodeError as e:
print(" - File is not valid JSON")
print(e)
return 1
if not isinstance(overrides, dict):
print(" - Root element of JSON is not a map of releases.")
return 1
valid = True
broad = []
for release, release_item in overrides.items():
if release != "all" and release not in VALID_RELEASES:
print(f" - /{release} is not a valid value for a release.")
valid = False
if not isinstance(release_item, dict):
print(f" - /{release} does not contain a map of architectures.")
print(type(release_item))
valid = False
break
for arch, arch_item in release_item.items():
if arch != "all" and arch not in VALID_ARCHES:
print(f" - /{release}/{arch} is not a valid value for an architecture.")
valid = False
if not isinstance(arch_item, dict):
print(f" - /{release}/{arch} does not contain a map of dependencies.")
valid = False
break
for dep, dep_item in arch_item.items():
if isinstance(dep_item, str):
if dep_item == "all":
broad.append(f"/{release}/{arch}/{dep}")
else:
print(f" - /{release}/{arch}/{dep} contains invalid string '{dep_item}'.")
valid = False
elif isinstance(dep_item, list):
for package in dep_item:
if not isinstance(package, str):
print(f" - /{release}/{arch}/{dep} contains invalid element '{package}'.")
valid = False
else:
print(f" - /{release}/{arch}/{dep} has invalid type '{type(dep_item)}'.")
if broad:
print("Overrides file contains broad exceptions.")
print("Verify these manually and add individual overrides, if possible.")
for item in broad:
print(" -", item)
if valid:
print("File valid.")
return 0
else:
return 1
if __name__ == "__main__":
try:
exit(main())
except KeyboardInterrupt:
print("Cancelled by SIGINT.")
exit(0)