|
| 1 | +"""Patch code.json with correct version info for programs fetched from releases.json. |
| 2 | +
|
| 3 | +make-code-json reads versions from pymake's internal program catalog, which may |
| 4 | +be out of date relative to the versions actually fetched. This script overwrites |
| 5 | +the version (and url) for each program listed in releases.json with the values |
| 6 | +from the releases.json manifest. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python patch_code_json.py --manifest releases.json --ostag linux --zip linux.zip |
| 10 | + python patch_code_json.py --manifest releases.json --ostag linux code.json |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import datetime |
| 15 | +import json |
| 16 | +import sys |
| 17 | +import urllib.request |
| 18 | +import zipfile |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +GITHUB_URL = "https://github.com/{repo}/releases/download/{tag}/{asset}" |
| 22 | + |
| 23 | + |
| 24 | +def _get_url_date(url): |
| 25 | + """Fetch Last-Modified date from a URL's response headers.""" |
| 26 | + try: |
| 27 | + req = urllib.request.Request(url, method="HEAD") |
| 28 | + with urllib.request.urlopen(req, timeout=10) as resp: |
| 29 | + for key in ("Last-Modified", "Date"): |
| 30 | + val = resp.headers.get(key) |
| 31 | + if val: |
| 32 | + dt = datetime.datetime.strptime(val, "%a, %d %b %Y %H:%M:%S %Z") |
| 33 | + return dt.strftime("%m/%d/%Y") |
| 34 | + except Exception: |
| 35 | + pass |
| 36 | + return None |
| 37 | + |
| 38 | + |
| 39 | +def patch(manifest_path, code_json_path, ostag, zip_path=None): |
| 40 | + with open(manifest_path) as f: |
| 41 | + manifest = json.load(f) |
| 42 | + |
| 43 | + code_json_path = Path(code_json_path) |
| 44 | + if not code_json_path.exists(): |
| 45 | + print(f"code.json not found: {code_json_path}", file=sys.stderr) |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + with open(code_json_path) as f: |
| 49 | + code = json.load(f) |
| 50 | + |
| 51 | + changed = [] |
| 52 | + for entry in manifest: |
| 53 | + repo = entry["repo"] |
| 54 | + tag = entry["tag"] |
| 55 | + version = tag.lstrip("v") |
| 56 | + asset = entry["assets"].get(ostag) |
| 57 | + url = GITHUB_URL.format(repo=repo, tag=tag, asset=asset) if asset else None |
| 58 | + |
| 59 | + for prog_name in entry["programs"]: |
| 60 | + if prog_name not in code: |
| 61 | + continue |
| 62 | + prev_version = code[prog_name].get("version") |
| 63 | + if prev_version == version: |
| 64 | + continue |
| 65 | + code[prog_name]["version"] = version |
| 66 | + if url: |
| 67 | + code[prog_name]["url"] = url |
| 68 | + date = _get_url_date(url) |
| 69 | + if date: |
| 70 | + code[prog_name]["url_download_asset_date"] = date |
| 71 | + changed.append(f" {prog_name}: {prev_version!r} -> {version!r}") |
| 72 | + |
| 73 | + if not changed: |
| 74 | + print("code.json already up to date") |
| 75 | + return |
| 76 | + |
| 77 | + for line in changed: |
| 78 | + print(line) |
| 79 | + |
| 80 | + code_json_path.write_text(json.dumps(code, indent=4) + "\n") |
| 81 | + print(f"wrote {code_json_path}") |
| 82 | + |
| 83 | + if zip_path and Path(zip_path).exists(): |
| 84 | + with zipfile.ZipFile(zip_path, "a", zipfile.ZIP_DEFLATED) as zf: |
| 85 | + zf.write(code_json_path, "code.json") |
| 86 | + print(f"updated code.json in {zip_path}") |
| 87 | + |
| 88 | + |
| 89 | +def main(): |
| 90 | + parser = argparse.ArgumentParser(description=__doc__.split("\n")[0]) |
| 91 | + parser.add_argument( |
| 92 | + "code_json", |
| 93 | + nargs="?", |
| 94 | + default="code.json", |
| 95 | + help="path to code.json to patch (default: code.json)", |
| 96 | + ) |
| 97 | + parser.add_argument( |
| 98 | + "--manifest", |
| 99 | + default="releases.json", |
| 100 | + help="path to releases.json manifest (default: releases.json)", |
| 101 | + ) |
| 102 | + parser.add_argument( |
| 103 | + "--ostag", |
| 104 | + required=True, |
| 105 | + help="platform tag (linux, mac, macarm, win64)", |
| 106 | + ) |
| 107 | + parser.add_argument( |
| 108 | + "--zip", |
| 109 | + dest="zip_path", |
| 110 | + help="zip file to update with the patched code.json", |
| 111 | + ) |
| 112 | + args = parser.parse_args() |
| 113 | + patch(args.manifest, args.code_json, args.ostag, args.zip_path) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments