"""Verify the engine source inventories used by the release runner.""" from __future__ import annotations import argparse import hashlib import json from pathlib import Path def digest(path: Path) -> str: sha = hashlib.sha256() with path.open("rb") as stream: for block in iter(lambda: stream.read(1024 * 1024), b""): sha.update(block) return sha.hexdigest() def check(root: Path) -> dict: engine = root / "engine" inventory = engine / "BASE_SHA256SUMS" revision = engine / "REPAIR_V2_REVISION.json" mismatches = [] inventory_count = 0 revision_count = 0 if not inventory.is_file(): mismatches.append({"source": "BASE_SHA256SUMS", "reason": "missing_inventory"}) else: for line_number, line in enumerate(inventory.read_text(encoding="utf-8").splitlines(), 1): if not line.strip(): continue if " " not in line: mismatches.append({"source": "BASE_SHA256SUMS", "line": line_number, "reason": "invalid_format"}) continue expected, name = line.split(" ", 1) inventory_count += 1 path = engine / name actual = digest(path) if path.is_file() else None if actual != expected: mismatches.append({"source": "BASE_SHA256SUMS", "path": name, "expected": expected, "actual": actual}) if not revision.is_file(): mismatches.append({"source": "REPAIR_V2_REVISION.json", "reason": "missing_revision"}) else: try: data = json.loads(revision.read_text(encoding="utf-8")) except (OSError, json.JSONDecodeError) as exc: mismatches.append({"source": "REPAIR_V2_REVISION.json", "reason": "invalid_json", "detail": str(exc)}) else: recorded = data.get("implementation_sha256", {}) if not isinstance(recorded, dict): mismatches.append({"source": "REPAIR_V2_REVISION.json", "reason": "invalid_implementation_sha256"}) else: for name, expected in recorded.items(): revision_count += 1 path = engine / name actual = digest(path) if path.is_file() else None if actual != expected: mismatches.append({"source": "REPAIR_V2_REVISION.json", "path": name, "expected": expected, "actual": actual}) expected_inventory = data.get("inventory_sha256") actual_inventory = digest(inventory) if inventory.is_file() else None if expected_inventory != actual_inventory: mismatches.append({"source": "REPAIR_V2_REVISION.json", "path": "inventory_sha256", "expected": expected_inventory, "actual": actual_inventory}) return { "root": str(root), "inventory_files": inventory_count, "revision_files": revision_count, "mismatches": mismatches, "passed": not mismatches, } def main(argv=None) -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[2], help="tree root containing engine/") args = parser.parse_args(argv) result = check(args.root.resolve()) print(json.dumps(result, indent=2, sort_keys=True)) return 0 if result["passed"] else 1 if __name__ == "__main__": raise SystemExit(main())