#!/usr/bin/env python3 """Create or synchronize permanent Echoion component working copies.""" from __future__ import annotations import argparse import json import subprocess from pathlib import Path def run(*args: str, cwd: Path | None = None) -> None: subprocess.run(args, cwd=cwd, check=True) def output(*args: str, cwd: Path) -> str: return subprocess.check_output(args, cwd=cwd, text=True).strip() def main() -> int: parser = argparse.ArgumentParser() parser.add_argument( "--workspace", type=Path, help="component checkout directory (default: parent of Echoion root)", ) parser.add_argument( "--update", action="store_true", help="fetch and check out exact revisions from components.lock.json", ) parser.add_argument( "--record", action="store_true", help="prepare and record a complete canonical release lock", ) args = parser.parse_args() if args.update and args.record: parser.error("--update and --record are mutually exclusive") root = Path(__file__).resolve().parents[1] workspace = (args.workspace or root.parent).resolve() registry = json.loads((root / "components.json").read_text()) lock = json.loads((root / "components.lock.json").read_text()) if registry.get("schemaVersion") != 1: raise SystemExit("unsupported components.json schema") if lock.get("schemaVersion") != 2: raise SystemExit("unsupported components.lock.json schema") if args.record: run( str(root / "scripts" / "prepare-release.sh"), "--workspace", str(workspace), cwd=root, ) return 0 for name, component in registry["components"].items(): destination = workspace / name revision = ( lock["components"][name]["revision"] if args.update else component.get("branch", "main") ) if not destination.exists(): run( "git", "clone", "--branch", component.get("branch", "main"), component["url"], str(destination), ) elif not (destination / ".git").exists(): raise SystemExit(f"{destination} exists but is not a Git repository") if not args.update: continue if output("git", "status", "--porcelain", cwd=destination): raise SystemExit(f"refusing to update dirty repository: {destination}") run("git", "fetch", "origin", revision, cwd=destination) run("git", "checkout", "--detach", revision, cwd=destination) return 0 if __name__ == "__main__": raise SystemExit(main())