-
-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathgenerate-version-metadata.py
More file actions
executable file
·100 lines (86 loc) · 2.97 KB
/
generate-version-metadata.py
File metadata and controls
executable file
·100 lines (86 loc) · 2.97 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
# /// script
# requires-python = ">=3.11"
# ///
"""Generate versions payload for python-build-standalone releases."""
from __future__ import annotations
import json
import os
import re
from collections import defaultdict
from datetime import datetime, timezone
from pathlib import Path
from urllib.parse import quote
FILENAME_RE = re.compile(
r"""(?x)
^
cpython-
(?P<py>\d+\.\d+\.\d+(?:(?:a|b|rc)\d+)?)(?:\+\d+)?\+
(?P<tag>\d+)-
(?P<triple>[a-z\d_]+-[a-z\d]+(?:-[a-z\d]+)?-[a-z\d_]+)-
(?:(?P<build>.+)-)?
(?P<flavor>[a-z_]+)?
\.tar\.(?:gz|zst)
$
"""
)
def main() -> None:
tag = os.environ["GITHUB_EVENT_INPUTS_TAG"]
repo = os.environ["GITHUB_REPOSITORY"]
dist = Path("dist")
checksums = dist / "SHA256SUMS"
if not checksums.exists():
raise SystemExit("SHA256SUMS not found in dist/")
# Parse filenames and checksums directly from SHA256SUMS to avoid downloading
# all release artifacts (tens of GB).
entries: list[tuple[str, str]] = []
for line in checksums.read_text().splitlines():
line = line.strip()
if not line:
continue
checksum, filename = line.split(maxsplit=1)
filename = filename.lstrip("*")
entries.append((filename, checksum))
versions: dict[str, list[dict[str, str]]] = defaultdict(list)
for filename, checksum in sorted(entries):
match = FILENAME_RE.match(filename)
if match is None:
continue
python_version = match.group("py")
build_version = match.group("tag")
version = f"{python_version}+{build_version}"
build = match.group("build")
flavor = match.group("flavor")
variant_parts: list[str] = []
if build:
variant_parts.extend(build.split("+"))
if flavor:
variant_parts.append(flavor)
variant = "+".join(variant_parts) if variant_parts else ""
url_prefix = f"https://github.com/{repo}/releases/download/{tag}/"
url = url_prefix + quote(filename, safe="")
archive_format = "tar.zst" if filename.endswith(".tar.zst") else "tar.gz"
artifact = {
"platform": match.group("triple"),
"variant": variant,
"url": url,
"archive_format": archive_format,
"sha256": checksum,
}
versions[version].append(artifact)
payload_versions: list[dict[str, object]] = []
now = datetime.now(timezone.utc).isoformat()
for version, artifacts in sorted(versions.items(), reverse=True):
artifacts.sort(
key=lambda artifact: (artifact["platform"], artifact.get("variant", ""))
)
payload_versions.append(
{
"version": version,
"date": now,
"artifacts": artifacts,
}
)
for version in payload_versions:
print(json.dumps(version, separators=(",", ":")))
if __name__ == "__main__":
main()