#!/usr/bin/env python3 import argparse import json import re import sys import urllib.request from pathlib import Path VERSION_RE = re.compile(r"[0-9]+(?:\.[0-9]+)*") def fetch_latest_tag(repo: str) -> str: url = f"https://api.github.com/repos/{repo}/releases/latest" req = urllib.request.Request( url, headers={ "Accept": "application/vnd.github+json", "User-Agent": "static-musl-builds-updater", }, ) with urllib.request.urlopen(req, timeout=30) as resp: data = json.load(resp) return data["tag_name"].strip() def normalize_version(tag: str, strip_prefix: str) -> str: version = tag if strip_prefix and version.startswith(strip_prefix): version = version[len(strip_prefix):] version = version.strip() if not VERSION_RE.fullmatch(version): raise ValueError(f"Unexpected version format: {tag!r} -> {version!r}") return version def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--repo", required=True, help="owner/repo on GitHub") parser.add_argument("--version-file", required=True, type=Path) parser.add_argument("--strip-prefix", default="v") parser.add_argument("--update-file", action="store_true") args = parser.parse_args() latest_tag = fetch_latest_tag(args.repo) latest = normalize_version(latest_tag, args.strip_prefix) current = args.version_file.read_text(encoding="utf-8").strip() if args.version_file.exists() else "" print(f"repo={args.repo}") print(f"current={current or ''}") print(f"latest={latest}") if current == latest: print("status=up-to-date") return 0 print("status=update-available") if args.update_file: args.version_file.parent.mkdir(parents=True, exist_ok=True) args.version_file.write_text(latest + "\n", encoding="utf-8") print(f"updated_file={args.version_file}") return 0 if __name__ == "__main__": sys.exit(main())