-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgenerate_noarch_wheel_repodata.py
More file actions
306 lines (251 loc) · 10.1 KB
/
generate_noarch_wheel_repodata.py
File metadata and controls
306 lines (251 loc) · 10.1 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
Utility for generating test-specific local channel repodata.
This is test data generation logic for conda-pypi only; it is not intended for
production repodata generation.
Marker conversion policy for this test channel:
- Convert Python markers to `python...` matchspec fragments, including
`python_version not in "x, y"` -> `(python!=x and python!=y)`.
- Convert platform/os markers to virtual packages when feasible
(`__win`, `__linux`, `__osx`, `__unix`).
- Keep extras in `extra_depends`, with remaining non-extra marker logic
encoded via `[when="..."]`.
- Drop unsupported marker dimensions (for example interpreter/machine-specific
variants) for these noarch channel tests.
"""
import json
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
from enum import StrEnum
from packaging.markers import Marker
from packaging.requirements import Requirement
from typing import Any
class MarkerVar(StrEnum):
PYTHON_VERSION = "python_version"
PYTHON_FULL_VERSION = "python_full_version"
EXTRA = "extra"
SYS_PLATFORM = "sys_platform"
PLATFORM_SYSTEM = "platform_system"
OS_NAME = "os_name"
IMPLEMENTATION_NAME = "implementation_name"
PLATFORM_PYTHON_IMPLEMENTATION = "platform_python_implementation"
PLATFORM_MACHINE = "platform_machine"
class MarkerOp(StrEnum):
EQ = "=="
NE = "!="
NOT_IN = "not in"
SYSTEM_TO_VIRTUAL_PACKAGE = {
"windows": "__win",
"win32": "__win",
"linux": "__linux",
"darwin": "__osx",
"cygwin": "__unix",
}
OS_NAME_TO_VIRTUAL_PACKAGE = {
"nt": "__win",
"windows": "__win",
"posix": "__unix",
}
def normalize_name(name: str) -> str:
"""Normalize a package name to conda conventions (lowercase, _ -> -)."""
return name.lower().replace("_", "-")
def _marker_value(token: Any) -> str:
"""Extract the textual value from packaging marker tokens."""
return getattr(token, "value", str(token))
def _normalize_marker_atom(lhs: str, op: str, rhs: str) -> str | None:
"""Map a single PEP 508 marker atom to a MatchSpec-like fragment."""
lhs_l = lhs.lower()
rhs_l = rhs.lower()
if lhs_l in {MarkerVar.PYTHON_VERSION, MarkerVar.PYTHON_FULL_VERSION}:
if op == MarkerOp.NOT_IN:
excluded_versions = [version.strip() for version in rhs.split(",") if version.strip()]
if not excluded_versions:
return None
clauses = [f"python!={version}" for version in excluded_versions]
if len(clauses) == 1:
return clauses[0]
return f"({' and '.join(clauses)})"
return f"python{op}{rhs}"
if lhs_l == MarkerVar.EXTRA and op == MarkerOp.EQ:
return None
if lhs_l in {MarkerVar.SYS_PLATFORM, MarkerVar.PLATFORM_SYSTEM}:
mapped = SYSTEM_TO_VIRTUAL_PACKAGE.get(rhs_l)
if op == MarkerOp.EQ and mapped:
return mapped
if op == MarkerOp.NE and rhs_l in {"win32", "windows", "cygwin"}:
return "__unix"
if op == MarkerOp.NE and rhs_l == "emscripten":
return None
return None
if lhs_l == MarkerVar.OS_NAME:
mapped = OS_NAME_TO_VIRTUAL_PACKAGE.get(rhs_l)
if not mapped:
return None
if op == MarkerOp.EQ:
return mapped
if op == MarkerOp.NE:
return "__unix" if mapped == "__win" else "__win"
return None
if lhs_l in {MarkerVar.IMPLEMENTATION_NAME, MarkerVar.PLATFORM_PYTHON_IMPLEMENTATION}:
if rhs_l in {"cpython", "pypy", "jython"}:
return None
return None
if lhs_l == MarkerVar.PLATFORM_MACHINE:
return None
return None
def _combine_expr(left: str | None, op: str, right: str | None) -> str | None:
"""Combine optional left/right expressions with a boolean operator."""
if left is None:
return right
if right is None:
return left
if left == right:
return left
return f"({left} {op} {right})"
def extract_marker_condition_and_extras(marker: Marker) -> tuple[str | None, list[str]]:
"""Split a Marker into optional non-extra condition and extra group names."""
extras: list[str] = []
seen_extras: set[str] = set()
def visit(node: Any) -> str | None:
if isinstance(node, tuple) and len(node) == 3:
lhs = _marker_value(node[0])
op = _marker_value(node[1])
rhs = _marker_value(node[2])
if lhs.lower() == MarkerVar.EXTRA and op == MarkerOp.EQ:
extra_name = rhs.lower()
if extra_name not in seen_extras:
seen_extras.add(extra_name)
extras.append(extra_name)
return None
return _normalize_marker_atom(lhs, op, rhs)
if isinstance(node, list):
if not node:
return None
expr = visit(node[0])
i = 1
while i + 1 < len(node):
op = str(node[i]).lower()
rhs_expr = visit(node[i + 1])
expr = _combine_expr(expr, op, rhs_expr)
i += 2
return expr
return None
# Marker._markers is a private packaging attribute; keep access isolated here.
condition = visit(getattr(marker, "_markers", []))
return condition, extras
def pypi_to_repodata_noarch_whl_entry(
pypi_data: dict[str, Any],
) -> dict[str, Any] | None:
"""
Convert PyPI JSON endpoint data to a repodata.json v3.whl entry for a
pure Python (noarch) wheel.
Args:
pypi_data: Dictionary containing the complete info from PyPI JSON endpoint
Returns:
Dictionary representing the entry for v3.whl, or None if no pure
Python wheel (platform tag "none-any") is found
"""
# Find a pure Python wheel (platform tag "none-any")
wheel_url = None
for url_entry in pypi_data.get("urls", []):
if url_entry.get("packagetype") != "bdist_wheel":
continue
filename = url_entry.get("filename", "")
if not filename.endswith("-none-any.whl"):
continue
wheel_url = url_entry
break
if not wheel_url:
return None
pypi_info = pypi_data.get("info")
depends_list: list[str] = []
extra_depends_dict: dict[str, list[str]] = {}
for dep in pypi_info.get("requires_dist") or []:
req = Requirement(dep)
conda_dep = normalize_name(req.name) + str(req.specifier)
if req.marker:
non_extra_condition, extra_names = extract_marker_condition_and_extras(req.marker)
if extra_names:
for extra_name in extra_names:
extra_dep = conda_dep
if non_extra_condition:
marker_condition = json.dumps(non_extra_condition)
extra_dep = f"{extra_dep}[when={marker_condition}]"
extra_depends_dict.setdefault(extra_name, []).append(extra_dep)
else:
if non_extra_condition:
marker_condition = json.dumps(non_extra_condition)
depends_list.append(f"{conda_dep}[when={marker_condition}]")
else:
depends_list.append(conda_dep)
else:
depends_list.append(conda_dep)
python_requires = pypi_info.get("requires_python")
if python_requires:
depends_list.append(f"python {python_requires}")
else:
# Noarch python packages should still depend on python when PyPI omits requires_python
depends_list.append("python")
# Build the repodata entry
entry = {
"url": wheel_url.get("url", ""),
"record_version": 3,
"name": normalize_name(pypi_info.get("name", "")),
"version": pypi_info.get("version"),
"build": "py3_none_any_0",
"build_number": 0,
"depends": depends_list,
"extra_depends": extra_depends_dict,
"fn": f"{pypi_info.get('name')}-{pypi_info.get('version')}-py3-none-any.whl",
"sha256": wheel_url.get("digests", {}).get("sha256", ""),
"size": wheel_url.get("size", 0),
"subdir": "noarch",
# "timestamp": wheel_url.get("upload_time", 0),
"noarch": "python",
}
return entry
def get_repodata_entry(name: str, version: str) -> dict[str, Any] | None:
pypi_endpoint = f"https://pypi.org/pypi/{name}/{version}/json"
pypi_data = requests.get(pypi_endpoint)
if pypi_data.json() is None:
raise Exception(f"unable to process {name} {version}")
return pypi_to_repodata_noarch_whl_entry(pypi_data.json())
if __name__ == "__main__":
from pathlib import Path
HERE = Path(__file__).parent
wheel_repodata = HERE / "noarch/repodata.json"
pkg_whls = {}
repodata_packages = []
requested_wheel_packages_file = HERE / "wheel_packages.txt"
with open(requested_wheel_packages_file) as f:
pkgs_data = f.read()
for pkg in pkgs_data.splitlines():
repodata_packages.append(tuple(pkg.split("==")))
# Run in parallel using ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=25) as executor:
# Map each package to its repodata entry
futures = {
executor.submit(get_repodata_entry, pkg_tuple[0], pkg_tuple[1]): pkg_tuple
for pkg_tuple in repodata_packages
}
# Collect results as they complete
for future in as_completed(futures):
pkg_tuple = futures[future]
name, version = pkg_tuple
try:
result = future.result()
if result:
# Use the normalized name for the key
pkg_whls[f"{result['name']}-{version}-py3_none_any_0"] = result
except Exception as e:
print(f"Error processing {name} {version}: {e}")
repodata_output = {
"info": {"subdir": "noarch"},
"packages": {},
"packages.conda": {},
"removed": [],
"repodata_version": 3,
"signatures": {},
"v3": {"whl": {key: value for key, value in sorted(pkg_whls.items())}},
}
with open(wheel_repodata, "w") as f:
json.dump(repodata_output, f, indent=4)