|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2026 Carnegie Mellon University |
| 3 | +# SPDX-License-Identifier: BSD-3-Clause-Clear |
| 4 | +"""Sync trunk's committed module catalog from a registry checkout. |
| 5 | +
|
| 6 | +Module registration is TWO merges (registry PR + trunk PR — see the |
| 7 | +extract-module skill). This script produces the trunk half mechanically from |
| 8 | +a local checkout of castacks/airstack-modules-index: |
| 9 | +
|
| 10 | +1. mirror the registry's ``modules/*.yaml`` and ``stacks/*.yaml`` into the |
| 11 | + contract-test fixture ``tests/meta/fixtures/modules_index/`` (deleting |
| 12 | + entries the registry no longer has); |
| 13 | +2. regenerate the committed ``docs/modules/`` pages with |
| 14 | + ``tools/gen_docs_catalog.py`` against that fixture and an EMPTY |
| 15 | + fetched-modules dir (the committed-page variant the docs-catalog contract |
| 16 | + test expects); |
| 17 | +3. with ``--bump``, and only when step 1/2 changed anything: bump the ``.env`` |
| 18 | + VERSION pre-release counter (the check-version-increment gate requires a |
| 19 | + strict bump on every PR) and add a release-notes bullet. |
| 20 | +
|
| 21 | +Run by ``.github/workflows/sync-modules-index.yml``, which turns a non-empty |
| 22 | +diff into the trunk sync PR. Also runnable locally from the repo root: |
| 23 | +
|
| 24 | + git clone --depth 1 https://github.com/castacks/airstack-modules-index /tmp/idx |
| 25 | + python3 .github/workflows/scripts/registry_sync.py --index /tmp/idx |
| 26 | +
|
| 27 | +stdlib only (gen_docs_catalog.py itself needs PyYAML). |
| 28 | +""" |
| 29 | +from __future__ import annotations |
| 30 | + |
| 31 | +import argparse |
| 32 | +import re |
| 33 | +import shutil |
| 34 | +import subprocess |
| 35 | +import sys |
| 36 | +import tempfile |
| 37 | +from pathlib import Path |
| 38 | + |
| 39 | +TRUNK = Path(__file__).resolve().parents[3] |
| 40 | +FIXTURE = TRUNK / "tests" / "meta" / "fixtures" / "modules_index" |
| 41 | +GENERATOR = TRUNK / "tools" / "gen_docs_catalog.py" |
| 42 | +ENV_FILE = TRUNK / ".env" |
| 43 | +RELEASE_NOTES = TRUNK / "docs" / "release_notes" / "index.md" |
| 44 | + |
| 45 | + |
| 46 | +def mirror_entries(index: Path) -> None: |
| 47 | + """Mirror registry modules/ and stacks/ YAMLs into the fixture.""" |
| 48 | + for sub in ("modules", "stacks"): |
| 49 | + src, dst = index / sub, FIXTURE / sub |
| 50 | + if not src.is_dir(): |
| 51 | + sys.exit(f"registry checkout has no {sub}/ directory: {src}") |
| 52 | + if dst.is_dir(): |
| 53 | + shutil.rmtree(dst) |
| 54 | + dst.mkdir(parents=True) |
| 55 | + for entry in sorted(src.glob("*.yaml")): |
| 56 | + shutil.copy2(entry, dst / entry.name) |
| 57 | + |
| 58 | + |
| 59 | +def regenerate_catalog() -> None: |
| 60 | + with tempfile.TemporaryDirectory() as empty: |
| 61 | + subprocess.run( |
| 62 | + [sys.executable, str(GENERATOR), |
| 63 | + "--index", str(FIXTURE), |
| 64 | + "--modules-dir", f"{empty}/no-fetched-modules"], |
| 65 | + check=True, cwd=TRUNK, |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def changed_paths() -> "list[str]": |
| 70 | + out = subprocess.run( |
| 71 | + ["git", "status", "--porcelain", "--", |
| 72 | + str(FIXTURE.relative_to(TRUNK)), "docs/modules"], |
| 73 | + check=True, capture_output=True, text=True, cwd=TRUNK, |
| 74 | + ).stdout |
| 75 | + return [line[3:].strip() for line in out.splitlines() if line.strip()] |
| 76 | + |
| 77 | + |
| 78 | +def bump_version() -> "tuple[str, str]": |
| 79 | + """Increment the .env VERSION pre-release counter (or patch).""" |
| 80 | + text = ENV_FILE.read_text() |
| 81 | + m = re.search(r'^VERSION="([^"]+)"$', text, flags=re.M) |
| 82 | + if not m: |
| 83 | + sys.exit('.env has no VERSION="..." line') |
| 84 | + old = m.group(1) |
| 85 | + pre = re.fullmatch(r"(\d+\.\d+\.\d+-(?:alpha|beta|rc)\.)(\d+)", old) |
| 86 | + if pre: |
| 87 | + new = f"{pre.group(1)}{int(pre.group(2)) + 1}" |
| 88 | + else: |
| 89 | + rel = re.fullmatch(r"(\d+\.\d+\.)(\d+)", old) |
| 90 | + if not rel: |
| 91 | + sys.exit(f"unrecognized VERSION format: {old}") |
| 92 | + new = f"{rel.group(1)}{int(rel.group(2)) + 1}" |
| 93 | + ENV_FILE.write_text(text.replace(f'VERSION="{old}"', f'VERSION="{new}"', 1)) |
| 94 | + return old, new |
| 95 | + |
| 96 | + |
| 97 | +def add_release_note(entries: "list[str]") -> None: |
| 98 | + """Insert a bullet under the current '(Unreleased)' section heading.""" |
| 99 | + lines = RELEASE_NOTES.read_text().splitlines(keepends=True) |
| 100 | + names = ", ".join(f"`{e}`" for e in entries) or "registry entries" |
| 101 | + bullet = ( |
| 102 | + "- Module catalog synced from the registry " |
| 103 | + f"(`sync-modules-index`): {names}.\n" |
| 104 | + ) |
| 105 | + for i, line in enumerate(lines): |
| 106 | + if re.match(r"^## .*\(Unreleased\)", line): |
| 107 | + lines.insert(i + 2, bullet) |
| 108 | + lines.insert(i + 3, "\n") |
| 109 | + RELEASE_NOTES.write_text("".join(lines)) |
| 110 | + return |
| 111 | + print("warning: no '(Unreleased)' release-notes section — bullet skipped", |
| 112 | + file=sys.stderr) |
| 113 | + |
| 114 | + |
| 115 | +def main() -> int: |
| 116 | + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 117 | + parser.add_argument("--index", required=True, type=Path, |
| 118 | + help="local checkout of castacks/airstack-modules-index") |
| 119 | + parser.add_argument("--bump", action="store_true", |
| 120 | + help="on change, bump .env VERSION and add a " |
| 121 | + "release-notes bullet (for the sync PR)") |
| 122 | + args = parser.parse_args() |
| 123 | + |
| 124 | + mirror_entries(args.index.resolve()) |
| 125 | + regenerate_catalog() |
| 126 | + |
| 127 | + changed = changed_paths() |
| 128 | + if not changed: |
| 129 | + print("in-sync: committed catalog already matches the registry") |
| 130 | + return 0 |
| 131 | + |
| 132 | + print("changed:") |
| 133 | + for path in changed: |
| 134 | + print(f" {path}") |
| 135 | + if args.bump: |
| 136 | + # Only the registry-entry files (not the regenerated pages) make a |
| 137 | + # readable release note. |
| 138 | + entry_names = sorted( |
| 139 | + Path(p).stem for p in changed |
| 140 | + if "fixtures/modules_index" in p and p.endswith(".yaml") |
| 141 | + ) |
| 142 | + old, new = bump_version() |
| 143 | + add_release_note(entry_names) |
| 144 | + print(f"VERSION {old} -> {new}; release-notes bullet added") |
| 145 | + return 0 |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + sys.exit(main()) |
0 commit comments