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
|
#!/usr/bin/env nix-shell
#!nix-shell -i python -p "python3.withPackages (ps: [ ps.httpx ] )"
import json
import httpx
import re
import subprocess
from pathlib import Path
from typing import Dict
from collections import defaultdict
def get_metadata(package) -> Dict:
response = httpx.get(
f"https://pypi.org/pypi/{package}/json",
headers={"user-agent": "nixpkgs/pypi-updater/unstable"},
)
return response.json()
def get_hash(url) -> str:
result = subprocess.run(["nix-prefetch-url", url], stdout=subprocess.PIPE)
base32_hash = result.stdout.decode().strip()
result = subprocess.run(
["nix", "hash", "to-sri", "--type", "sha256", base32_hash],
stdout=subprocess.PIPE,
)
sri_hash = result.stdout.decode().strip()
return sri_hash
def get_platform(platform: str) -> str:
result = re.match(r"^(?P<target>macosx|manylinux)[\d+_]+(?P<arch>x86_64|aarch64)", platform)
if not result:
raise RuntimeError(f"Unable to parse platform string: {platform}")
target = {
"macosx": "darwin",
"manylinux": "linux",
}[result.group("target")]
arch = result.group("arch")
return f"{arch}-{target}"
def get_python_version(python: str) -> str:
result = re.match(r"^cp(?P<major>\d)(?P<minor>\d+)$", python)
if not result:
raise RuntimeError(f"Unable to disect python compat tag: {python}")
return f"{result.group('major')}.{result.group('minor')}"
def main(package: str):
metadata = get_metadata(package)
info = metadata.get("info")
if info is None:
raise RuntimeError("Package metadata has no info attribute")
version = info.get("version")
assert not info.get("yanked"), (
f"Latest release was yanked: {info.get('yanked_reason')}"
)
releases = metadata["releases"][version]
out = {
"version": version,
"src": defaultdict(dict),
}
for release in releases:
if release.get("packagetype") != "bdist_wheel":
# the package expects the binary wheels
continue
print(json.dumps(release, indent=2))
filename = release.get("filename")
result = re.match(
rf"(?P<pname>\w+)-{re.escape(version)}-(?P<python>\w+)-(?P<dist>\w+)-(?P<platform>\w+)\.whl$",
filename,
)
if not result:
raise RuntimeError(f"Unable to disect wheel filename: {filename}")
platform = get_platform(result.group("platform"))
python_version = get_python_version(release["python_version"])
out["src"][platform][python_version] = {
"url": release.get("url"),
"hash": get_hash(release.get("url")),
}
with open(Path(__file__).with_name("release.json"), "w") as fd:
json.dump(out, fd, indent=2)
if __name__ == "__main__":
main("ai-edge-litert")
|