|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Regenerate versions-manifest.json from the repository's GitHub releases. |
| 3 | +
|
| 4 | +Reads `GH_TOKEN` and `GITHUB_REPOSITORY` from the environment and paginates |
| 5 | +through every release on the repo. Each release is expected to have a tag of |
| 6 | +the form ``<normalised-version>-<run_id>`` (e.g. ``3.15.0-alpha.7-22913288817``). |
| 7 | +For each distinct version the release with the largest ``run_id`` wins, and an |
| 8 | +entry is emitted in the manifest with its assets. |
| 9 | +
|
| 10 | +Output matches the schema used by actions/python-versions. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import json |
| 16 | +import os |
| 17 | +import re |
| 18 | +import sys |
| 19 | +import urllib.error |
| 20 | +import urllib.parse |
| 21 | +import urllib.request |
| 22 | + |
| 23 | +API_ROOT = "https://api.github.com" |
| 24 | +TAG_RE = re.compile(r"^(?P<version>.+)-(?P<run_id>\d+)$") |
| 25 | +PRERELEASE_RE = re.compile(r"-(alpha|beta|rc)\.(\d+)$") |
| 26 | +FILENAME_PLATFORM_RE = re.compile( |
| 27 | + r"-linux-(?P<platform_version>\d+\.\d+)-(?P<arch>[^.]+?)(?P<ft>-freethreaded)?\.tar\.gz$" |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +def gh_get(path: str, token: str) -> list[dict]: |
| 32 | + """Paginate a GitHub REST API list endpoint.""" |
| 33 | + results: list[dict] = [] |
| 34 | + url: str | None = f"{API_ROOT}{path}" |
| 35 | + if "?" in url: |
| 36 | + url += "&per_page=100" |
| 37 | + else: |
| 38 | + url += "?per_page=100" |
| 39 | + while url: |
| 40 | + req = urllib.request.Request( |
| 41 | + url, |
| 42 | + headers={ |
| 43 | + "Authorization": f"Bearer {token}", |
| 44 | + "Accept": "application/vnd.github+json", |
| 45 | + "X-GitHub-Api-Version": "2022-11-28", |
| 46 | + "User-Agent": "update-manifest-script", |
| 47 | + }, |
| 48 | + ) |
| 49 | + with urllib.request.urlopen(req) as resp: |
| 50 | + payload = json.load(resp) |
| 51 | + results.extend(payload) |
| 52 | + link = resp.headers.get("Link", "") |
| 53 | + url = _next_link(link) |
| 54 | + return results |
| 55 | + |
| 56 | + |
| 57 | +def _next_link(link_header: str) -> str | None: |
| 58 | + for part in link_header.split(","): |
| 59 | + section = part.strip() |
| 60 | + if section.endswith('rel="next"'): |
| 61 | + return section.split(";", 1)[0].strip().lstrip("<").rstrip(">") |
| 62 | + return None |
| 63 | + |
| 64 | + |
| 65 | +def version_sort_key(version: str) -> tuple[int, int, int, int, int]: |
| 66 | + """Return a tuple suitable for descending version sort. |
| 67 | +
|
| 68 | + Order: (major, minor, patch, stage_order, stage_num) |
| 69 | + where stage_order is 0=alpha, 1=beta, 2=rc, 3=final. |
| 70 | + """ |
| 71 | + match = PRERELEASE_RE.search(version) |
| 72 | + if match: |
| 73 | + stage_map = {"alpha": 0, "beta": 1, "rc": 2} |
| 74 | + stage_order = stage_map[match.group(1)] |
| 75 | + stage_num = int(match.group(2)) |
| 76 | + base = version[: match.start()] |
| 77 | + else: |
| 78 | + stage_order = 3 |
| 79 | + stage_num = 0 |
| 80 | + base = version |
| 81 | + parts = base.split(".") |
| 82 | + major = int(parts[0]) |
| 83 | + minor = int(parts[1]) if len(parts) > 1 else 0 |
| 84 | + patch = int(parts[2]) if len(parts) > 2 else 0 |
| 85 | + return (major, minor, patch, stage_order, stage_num) |
| 86 | + |
| 87 | + |
| 88 | +def is_stable(version: str) -> bool: |
| 89 | + return PRERELEASE_RE.search(version) is None |
| 90 | + |
| 91 | + |
| 92 | +def build_file_entry(asset: dict) -> dict | None: |
| 93 | + filename = asset["name"] |
| 94 | + match = FILENAME_PLATFORM_RE.search(filename) |
| 95 | + if not match: |
| 96 | + return None |
| 97 | + arch = match.group("arch") |
| 98 | + if match.group("ft"): |
| 99 | + arch = f"{arch}-freethreaded" |
| 100 | + return { |
| 101 | + "filename": filename, |
| 102 | + "arch": arch, |
| 103 | + "platform": "linux", |
| 104 | + "platform_version": match.group("platform_version"), |
| 105 | + "download_url": asset["browser_download_url"], |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | +def main() -> int: |
| 110 | + token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") |
| 111 | + repo = os.environ.get("GITHUB_REPOSITORY") |
| 112 | + if not token or not repo: |
| 113 | + print("GH_TOKEN and GITHUB_REPOSITORY must be set", file=sys.stderr) |
| 114 | + return 1 |
| 115 | + |
| 116 | + releases = gh_get(f"/repos/{repo}/releases", token) |
| 117 | + |
| 118 | + # Pick the release with the highest run_id per version. |
| 119 | + best: dict[str, dict] = {} |
| 120 | + for release in releases: |
| 121 | + if release.get("draft"): |
| 122 | + continue |
| 123 | + tag = release.get("tag_name") or "" |
| 124 | + match = TAG_RE.match(tag) |
| 125 | + if not match: |
| 126 | + continue |
| 127 | + version = match.group("version") |
| 128 | + run_id = int(match.group("run_id")) |
| 129 | + existing = best.get(version) |
| 130 | + if existing is None or run_id > existing["_run_id"]: |
| 131 | + best[version] = {"_run_id": run_id, "release": release} |
| 132 | + |
| 133 | + entries: list[dict] = [] |
| 134 | + for version, picked in best.items(): |
| 135 | + release = picked["release"] |
| 136 | + tag = release["tag_name"] |
| 137 | + files = [] |
| 138 | + for asset in release.get("assets") or []: |
| 139 | + entry = build_file_entry(asset) |
| 140 | + if entry is not None: |
| 141 | + files.append(entry) |
| 142 | + entries.append( |
| 143 | + { |
| 144 | + "version": version, |
| 145 | + "stable": is_stable(version), |
| 146 | + "release_url": f"https://github.com/{repo}/releases/tag/{tag}", |
| 147 | + "files": files, |
| 148 | + } |
| 149 | + ) |
| 150 | + |
| 151 | + entries.sort(key=lambda e: version_sort_key(e["version"]), reverse=True) |
| 152 | + |
| 153 | + out = json.dumps(entries, indent=2) |
| 154 | + with open("versions-manifest.json", "w", encoding="utf-8") as f: |
| 155 | + f.write(out) |
| 156 | + print(f"Wrote versions-manifest.json with {len(entries)} entries", file=sys.stderr) |
| 157 | + return 0 |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + sys.exit(main()) |
0 commit comments