"""Read-only RC plan/ledger consistency checks; not artifact qualification.""" from __future__ import annotations import argparse import json import re from pathlib import Path from typing import Any ROOT = Path(__file__).resolve().parents[1] CONFIG = "config/release/rc-program.json" LEDGER = "docs/quality/RC_GAP_MATRIX.md" ROADMAP = "docs/product/RC_EXECUTION_ROADMAP.md" GAP_ID = re.compile(r"RC-[A-Z]+-[0-9]{2}") PACKET_ID = re.compile(r"P[0-9]{2}[A-Z]?") CLASSIFICATIONS = { "COMPLETE", "NEEDS POLISH", "FUNCTIONALLY INCOMPLETE", "UX INCOMPLETE", "OPERATIONALLY INCOMPLETE", "SECURITY BLOCKER", "RC BLOCKER", "POST-RC", } STATUSES = {"OPEN", "IN PROGRESS", "RESOLVED", "VERIFIED", "DEFERRED"} class InvalidPlan(ValueError): """The plan cannot safely be interpreted.""" def _object(pairs: list[tuple[str, Any]]) -> dict[str, Any]: result: dict[str, Any] = {} for key, value in pairs: if key in result: raise InvalidPlan(f"duplicate JSON field: {key}") result[key] = value return result def _read(root: Path, relative: str) -> str: path = (root / relative).resolve() if not path.is_relative_to(root.resolve()): raise InvalidPlan(f"repository path escapes root: {relative}") return path.read_text(encoding="utf-8") def _keys(value: Any, expected: set[str], label: str) -> None: if not isinstance(value, dict) or set(value) != expected: raise InvalidPlan(f"invalid {label} fields") def _ids(value: Any, pattern: re.Pattern[str], label: str) -> list[str]: if not isinstance(value, list) or any( not isinstance(item, str) or pattern.fullmatch(item) is None for item in value ): raise InvalidPlan(f"invalid {label} IDs") if len(set(value)) != len(value): raise InvalidPlan(f"duplicate {label} IDs") return value def validate(root: Path = ROOT) -> dict[str, Any]: plan = json.loads(_read(root, CONFIG), object_pairs_hook=_object) _keys(plan, {"schema_version", "target_version", "baseline_commit", "ledger", "roadmap", "packets", "deferred_gaps"}, "configuration") if type(plan["schema_version"]) is not int or plan["schema_version"] != 1: raise InvalidPlan("unsupported schema_version") if not isinstance(plan["target_version"], str) or not re.fullmatch( r"[0-9]+\.[0-9]+\.[0-9]+-rc\.[1-9][0-9]*", plan["target_version"] ): raise InvalidPlan("invalid target_version") if not isinstance(plan["baseline_commit"], str) or not re.fullmatch( r"[0-9a-f]{40}", plan["baseline_commit"] ): raise InvalidPlan("invalid baseline_commit") if plan["ledger"] != LEDGER or plan["roadmap"] != ROADMAP: raise InvalidPlan("ledger and roadmap must use the fixed repository paths") if not _read(root, ROADMAP).strip(): raise InvalidPlan("roadmap is empty") rows: dict[str, tuple[str, str]] = {} for line in _read(root, LEDGER).splitlines(): if not line.startswith("| RC-"): continue cells = [cell.strip() for cell in re.split(r"(? int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--require-verified", action="store_true") args = parser.parse_args(argv) try: result = validate() except (ValueError, OSError) as exc: print(json.dumps({"error": str(exc), "all_required_rows_verified": False})) return 2 print(json.dumps(result, sort_keys=True)) return int(args.require_verified and not result["all_required_rows_verified"]) if __name__ == "__main__": raise SystemExit(main())