#!/usr/bin/env python3 """Check and hash production-TD and analysis-replay closures separately.""" from __future__ import annotations import argparse import hashlib import json from pathlib import Path ROOT = Path(__file__).resolve().parents[1] PRODUCT = ROOT / "products/6400-front-rear" PRODUCTION = PRODUCT / "source-closure.json" ANALYSIS = PRODUCT / "analysis-replay-closure.json" TD_INPUTS = ( "constraints/prod2u_6400_front_back.adc", "constraints/prod2u_6400_front_back.sdc", "td/prod2u_6400_front_back.prj", "td/syn/prod2u_6400_front_back.tcl", "td/phy/prod2u_6400_front_back.tcl", "td/bitgen/prod2u_6400_front_back.tcl", "products/6400-front-rear/project.json", "products/6400-front-rear/source-closure.json", ) def sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as stream: for chunk in iter(lambda: stream.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def safe_paths(values: object, label: str) -> list[str]: if not isinstance(values, list) or not values: raise ValueError(f"{label} closure must be a non-empty list") result: list[str] = [] for value in values: if not isinstance(value, str) or not value: raise ValueError(f"{label} closure has invalid path") path = Path(value) if path.is_absolute() or ".." in path.parts or "\\" in value: raise ValueError(f"{label} closure has unsafe path: {value}") result.append(value) if len(result) != len(set(result)): raise ValueError(f"{label} closure has duplicate paths") return result def build_receipt() -> dict[str, object]: production_payload = json.loads(PRODUCTION.read_text(encoding="utf-8")) analysis_payload = json.loads(ANALYSIS.read_text(encoding="utf-8")) production_sources = safe_paths(production_payload.get("sources"), "production") analysis_paths = safe_paths(analysis_payload.get("paths"), "analysis") production_paths = list(dict.fromkeys([*production_sources, *TD_INPUTS])) missing_production = [path for path in production_paths if not (ROOT / path).is_file()] missing_analysis = [path for path in analysis_paths if not (ROOT / path).is_file()] if missing_production or missing_analysis: raise ValueError( f"closure missing files: production={missing_production} analysis={missing_analysis}" ) return { "schema": "prod2u-6400-closure-check-v1", "project_id": "6400-front-rear", "production_source_closure": { "path": str(PRODUCTION.relative_to(ROOT)), "files": {path: sha256(ROOT / path) for path in production_paths}, }, "analysis_replay_closure": { "path": str(ANALYSIS.relative_to(ROOT)), "files": {path: sha256(ROOT / path) for path in analysis_paths}, }, } def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--output", type=Path) args = parser.parse_args() payload = build_receipt() text = json.dumps(payload, indent=2, sort_keys=True) + "\n" if args.output: args.output.parent.mkdir(parents=True, exist_ok=True) args.output.write_text(text, encoding="utf-8") else: print(text, end="") return 0 if __name__ == "__main__": raise SystemExit(main())