-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_patch_json.py
288 lines (230 loc) · 9.21 KB
/
gen_patch_json.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
from collections import defaultdict
import copy
import json
import os
from os.path import join, isdir
import sys
import tqdm
import re
import requests
import pkg_resources
CHANNEL_NAME = "bioconda"
CHANNEL_ALIAS = "https://conda.anaconda.org"
SUBDIRS = (
"noarch",
"linux-64",
"osx-64",
)
REMOVALS = {
"noarch": (
),
"linux-64": (
),
"osx-64": (
),
}
OPERATORS = ["==", ">=", "<=", ">", "<", "!="]
def _add_removals(instructions, subdir):
r = requests.get(
"https://conda.anaconda.org/bioconda/"
"label/broken/%s/repodata.json" % subdir
)
if r.status_code != 200:
r.raise_for_status()
data = r.json()
currvals = list(REMOVALS.get(subdir, []))
for pkg_name in data["packages"]:
currvals.append(pkg_name)
instructions["remove"].extend(tuple(set(currvals)))
def _gen_patch_instructions(index, new_index, subdir):
instructions = {
"patch_instructions_version": 1,
"packages": defaultdict(dict),
"revoke": [],
"remove": [],
}
#_add_removals(instructions, subdir)
# diff all items in the index and put any differences in the instructions
for fn in index:
assert fn in new_index
# replace any old keys
for key in index[fn]:
assert key in new_index[fn], (key, index[fn], new_index[fn])
if index[fn][key] != new_index[fn][key]:
instructions['packages'][fn][key] = new_index[fn][key]
# add any new keys
for key in new_index[fn]:
if key not in index[fn]:
instructions['packages'][fn][key] = new_index[fn][key]
return instructions
def has_dep(record, name):
return any(dep.split(' ')[0] == name for dep in record.get('depends', ()))
changes = set([])
def _gen_new_index(repodata, subdir):
"""Make any changes to the index by adjusting the values directly.
This function returns the new index with the adjustments.
Finally, the new and old indices are then diff'ed to produce the repo
data patches.
"""
index = copy.deepcopy(repodata["packages"])
for fn, record in index.items():
record_name = record["name"]
version = record['version']
deps = record.get("depends", ())
# TBB 2021 (oneTBB 2021) is incompatible with previous releases.
if has_dep(record, "tbb") and record.get('timestamp', 0) < 1614809400000:
for i, dep in enumerate(deps):
if dep == "tbb":
deps[i] = "tbb <2021.0.0a0"
break
elif any(dep.startswith(f"tbb >={i}") for i in range(2017, 2021)) or dep.startswith("tbb >=4.4"):
deps[i] = "{},<2021.0.0a0".format(dep)
#deps.append("tbb <2021.0.0a0")
break
# All R packages require a maximum version, so >=A.B,<A.C rather than >=A.B.D
if (record_name.startswith('bioconductor-') or record_name.startswith('r-')) and has_dep(record, "r-base"):
for i, dep in enumerate(deps):
if dep.startswith('r-base >=') and '<' not in dep:
minVersion = dep.split('=')[1]
_ = minVersion.split('.')
if len(_) >= 2:
minor = str(int(_[1]) + 1)
minVersion = '.'.join([_[0], _[1]])
maxVersion = '.'.join([_[0], minor])
deps[i] = 'r-base >={},<{}'.format(minVersion, maxVersion)
break
return index
def _replace_pin(old_pin, new_pin, deps, record):
"""Replace an exact pin with a new one."""
if old_pin in deps:
i = record['depends'].index(old_pin)
record['depends'][i] = new_pin
def _rename_dependency(fn, record, old_name, new_name):
depends = record["depends"]
dep_idx = next(
(q for q, dep in enumerate(depends)
if dep.split(' ')[0] == old_name),
None
)
if dep_idx is not None:
parts = depends[dep_idx].split(" ")
remainder = (" " + " ".join(parts[1:])) if len(parts) > 1 else ""
depends[dep_idx] = new_name + remainder
record['depends'] = depends
def pad_list(l, num):
if len(l) >= num:
return l
return l + ["0"]*(num - len(l))
def get_upper_bound(version, max_pin):
num_x = max_pin.count("x")
ver = pad_list(version.split("."), num_x)
ver[num_x:] = ["0"]*(len(ver)-num_x)
ver[num_x-1] = str(int(ver[num_x-1])+1)
return ".".join(ver)
def _relax_exact(fn, record, fix_dep, max_pin=None):
depends = record.get("depends", ())
dep_idx = next(
(q for q, dep in enumerate(depends)
if dep.split(' ')[0] == fix_dep),
None
)
if dep_idx is not None:
dep_parts = depends[dep_idx].split(" ")
if (len(dep_parts) == 3 and \
not any(dep_parts[1].startswith(op) for op in OPERATORS)):
if max_pin is not None:
upper_bound = get_upper_bound(dep_parts[1], max_pin) + "a0"
depends[dep_idx] = "{} >={},<{}".format(*dep_parts[:2], upper_bound)
else:
depends[dep_idx] = "{} >={}".format(*dep_parts[:2])
record['depends'] = depends
cb_pin_regex = re.compile(r"^>=(?P<lower>\d(\.\d+)*a?),<(?P<upper>\d(\.\d+)*)a0$")
def _pin_stricter(fn, record, fix_dep, max_pin, upper_bound=None):
depends = record.get("depends", ())
dep_indices = [q for q, dep in enumerate(depends) if dep.split(' ')[0] == fix_dep]
for dep_idx in dep_indices:
dep_parts = depends[dep_idx].split(" ")
if len(dep_parts) not in [2, 3]:
continue
m = cb_pin_regex.match(dep_parts[1])
if m is None:
continue
lower = m.group("lower")
upper = m.group("upper").split(".")
if upper_bound is None:
new_upper = get_upper_bound(lower, max_pin).split(".")
else:
new_upper = upper_bound.split(".")
upper = pad_list(upper, len(new_upper))
new_upper = pad_list(new_upper, len(upper))
if tuple(upper) > tuple(new_upper):
if str(new_upper[-1]) != "0":
new_upper += ["0"]
depends[dep_idx] = "{} >={},<{}a0".format(dep_parts[0], lower, ".".join(new_upper))
if len(dep_parts) == 3:
depends[dep_idx] = "{} {}".format(depends[dep_idx], dep_parts[2])
record['depends'] = depends
def _pin_looser(fn, record, fix_dep, max_pin=None, upper_bound=None):
depends = record.get("depends", ())
dep_indices = [q for q, dep in enumerate(depends) if dep.split(' ')[0] == fix_dep]
for dep_idx in dep_indices:
dep_parts = depends[dep_idx].split(" ")
if len(dep_parts) not in [2, 3]:
continue
m = cb_pin_regex.match(dep_parts[1])
if m is None:
continue
lower = m.group("lower")
upper = m.group("upper").split(".")
if upper_bound is None:
new_upper = get_upper_bound(lower, max_pin).split(".")
else:
new_upper = upper_bound.split(".")
upper = pad_list(upper, len(new_upper))
new_upper = pad_list(new_upper, len(upper))
if tuple(upper) < tuple(new_upper):
if str(new_upper[-1]) != "0":
new_upper += ["0"]
depends[dep_idx] = "{} >={},<{}a0".format(dep_parts[0], lower, ".".join(new_upper))
if len(dep_parts) == 3:
depends[dep_idx] = "{} {}".format(depends[dep_idx], dep_parts[2])
record['depends'] = depends
def _extract_feature(record, feature_name):
features = record.get('features', '').split()
features.remove(feature_name)
return " ".join(features) or None
def _extract_track_feature(record, feature_name):
features = record.get('track_features', '').split()
features.remove(feature_name)
return " ".join(features) or None
def main():
# Step 1. Collect initial repodata for all subdirs.
repodatas = {}
for subdir in tqdm.tqdm(SUBDIRS, desc="Downloading repodata"):
repodata_url = "/".join(
(CHANNEL_ALIAS, CHANNEL_NAME, subdir, "repodata_from_packages.json"))
response = requests.get(repodata_url)
response.raise_for_status()
repodatas[subdir] = response.json()
# Step 2. Create all patch instructions.
prefix_dir = os.getenv("PREFIX", "patches")
for subdir in SUBDIRS:
prefix_subdir = join(prefix_dir, subdir)
if not isdir(prefix_subdir):
os.makedirs(prefix_subdir)
# Step 2a. Generate a new index.
new_index = _gen_new_index(repodatas[subdir], subdir)
# Step 2b. Generate the instructions by diff'ing the indices.
instructions = _gen_patch_instructions(
repodatas[subdir]['packages'], new_index, subdir)
# Step 2c. Output this to $PREFIX so that we bundle the JSON files.
patch_instructions_path = join(
prefix_subdir, "patch_instructions.json")
with open(patch_instructions_path, 'w') as fh:
json.dump(
instructions, fh, indent=2,
sort_keys=True, separators=(',', ': '))
if __name__ == "__main__":
sys.exit(main())