|
| 1 | +#! /usr/bin/env nix-shell |
| 2 | +#! nix-shell -i python3 -p python3 git nix-prefetch-git python3Packages.packaging |
| 3 | + |
| 4 | +import subprocess |
| 5 | +import json |
| 6 | +import argparse |
| 7 | +from pathlib import Path |
| 8 | +from typing import Dict, List, Union |
| 9 | +from packaging.version import parse as parse_version, InvalidVersion |
| 10 | + |
| 11 | +Schema = Dict[str, Dict[str, Dict[str, Union[List[str], str, bool]]]] |
| 12 | + |
| 13 | +POSTGRES_VERSIONS: List[str] = ["15", "17"] |
| 14 | +VERSIONS_JSON_PATH = "versions.json" |
| 15 | + |
| 16 | + |
| 17 | +def run(cmd: List[str]) -> str: |
| 18 | + result = subprocess.run(cmd, capture_output=True, text=True, check=True) |
| 19 | + return result.stdout.strip() |
| 20 | + |
| 21 | + |
| 22 | +def get_tags(url: str) -> Dict[str, str]: |
| 23 | + output = run(["git", "ls-remote", "--tags", url]) |
| 24 | + tags: Dict[str, str] = {} |
| 25 | + for line in output.splitlines(): |
| 26 | + if "^{}" not in line: |
| 27 | + parts = line.split("\t") |
| 28 | + if len(parts) == 2: |
| 29 | + commit_hash, ref = parts |
| 30 | + if ref.startswith("refs/tags/"): |
| 31 | + tag = ref.removeprefix("refs/tags/") |
| 32 | + try: |
| 33 | + parse_version(tag) |
| 34 | + except InvalidVersion: |
| 35 | + continue |
| 36 | + tags[tag] = commit_hash |
| 37 | + return tags |
| 38 | + |
| 39 | + |
| 40 | +def get_sri_hash(url: str, commit_hash: str) -> str: |
| 41 | + output = run(["nix-prefetch-git", "--quiet", "--url", url, "--rev", commit_hash]) |
| 42 | + nix_hash = json.loads(output)["sha256"] |
| 43 | + return "sha256-" + run(["nix", "hash", "to-base64", "--type", "sha256", nix_hash]) |
| 44 | + |
| 45 | + |
| 46 | +def load() -> Schema: |
| 47 | + if not Path(VERSIONS_JSON_PATH).exists(): |
| 48 | + return {} |
| 49 | + with open(VERSIONS_JSON_PATH, "r", encoding="utf-8") as f: |
| 50 | + return json.load(f) |
| 51 | + |
| 52 | + |
| 53 | +def build(name: str, url: str, data: Schema, ignore: bool = False) -> Schema: |
| 54 | + tags = get_tags(url) |
| 55 | + versions = data.get(name, {}) |
| 56 | + for tag, commit_hash in tags.items(): |
| 57 | + if tag in versions: |
| 58 | + continue |
| 59 | + if ignore: |
| 60 | + versions[tag] = {"ignore": True} |
| 61 | + else: |
| 62 | + sri_hash = get_sri_hash(url, commit_hash) |
| 63 | + versions[tag] = {"postgresql": POSTGRES_VERSIONS, "hash": sri_hash} |
| 64 | + data[name] = versions |
| 65 | + return data |
| 66 | + |
| 67 | + |
| 68 | +def save(data: Schema) -> None: |
| 69 | + sorted_data = {} |
| 70 | + for name, versions in data.items(): |
| 71 | + sorted_data[name] = dict( |
| 72 | + sorted(versions.items(), key=lambda item: parse_version(item[0])) |
| 73 | + ) |
| 74 | + with open(VERSIONS_JSON_PATH, "w", encoding="utf-8") as f: |
| 75 | + json.dump(sorted_data, f, indent=2) |
| 76 | + f.write("\n") |
| 77 | + |
| 78 | + |
| 79 | +def main() -> None: |
| 80 | + parser = argparse.ArgumentParser() |
| 81 | + parser.add_argument("extension_name") |
| 82 | + parser.add_argument("git_repo_url") |
| 83 | + parser.add_argument("--ignore", action="store_true") |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + save(build(args.extension_name, args.git_repo_url, load(), ignore=args.ignore)) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments