#!/usr/bin/env python3 """Project public catalog fields from a pinned root and its domain gitlinks.""" import json from pathlib import Path import re import subprocess import sys import tomllib OUTPUT = Path(__file__).resolve().parents[1] / "src/config/catalog.json" def git(repo, *args): return subprocess.check_output(["git", "-C", str(repo), *args], text=True) def project(repo, sha, nested=False): modules = git(repo, "show", f"{sha}:.gitmodules") remotes = dict(re.findall(r"(?m)^\s*path = (\S+)\n\s*url = (https://git\.cflmy\.cn/[\w.-]+/[\w.-]+)\.git$", modules)) catalog = tomllib.loads(git(repo, "show", f"{sha}:components.toml")) for component in catalog["component"]: entry = {key: component[key] for key in ("id", "kind", "state", "scope", "requires", "checks_with", "release")} path = component["path"] entry["repository"] = remotes.get(path) yield entry if component["kind"] == "domain" and path in remotes: if nested: raise ValueError(f"Unexpected nested domain {path}") tree = git(repo, "ls-tree", sha, "--", path) if tree: mode, kind, child_sha, _ = tree.split() if (mode, kind) != ("160000", "commit"): raise ValueError(f"Expected a gitlink for {path}") yield from project(repo / path, child_sha, nested=True) if __name__ == "__main__": root, ref = sys.argv[1:] repo = Path(root).resolve() source = git(repo, "rev-parse", "--verify", ref + "^{commit}").strip() components = list(project(repo, source)) if len({row["id"] for row in components}) != len(components): raise ValueError("Duplicate component IDs in the upstream catalog") OUTPUT.write_text(json.dumps({"source": source, "components": components}, ensure_ascii=False, indent=2) + "\n")