check_6400_closures.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. """Check and hash production-TD and analysis-replay closures separately."""
  3. from __future__ import annotations
  4. import argparse
  5. import hashlib
  6. import json
  7. from pathlib import Path
  8. ROOT = Path(__file__).resolve().parents[1]
  9. PRODUCT = ROOT / "products/6400-front-rear"
  10. PRODUCTION = PRODUCT / "source-closure.json"
  11. ANALYSIS = PRODUCT / "analysis-replay-closure.json"
  12. TD_INPUTS = (
  13. "constraints/prod2u_6400_front_back.adc",
  14. "constraints/prod2u_6400_front_back.sdc",
  15. "td/prod2u_6400_front_back.prj",
  16. "td/syn/prod2u_6400_front_back.tcl",
  17. "td/phy/prod2u_6400_front_back.tcl",
  18. "td/bitgen/prod2u_6400_front_back.tcl",
  19. "products/6400-front-rear/project.json",
  20. "products/6400-front-rear/source-closure.json",
  21. )
  22. def sha256(path: Path) -> str:
  23. digest = hashlib.sha256()
  24. with path.open("rb") as stream:
  25. for chunk in iter(lambda: stream.read(1024 * 1024), b""):
  26. digest.update(chunk)
  27. return digest.hexdigest()
  28. def safe_paths(values: object, label: str) -> list[str]:
  29. if not isinstance(values, list) or not values:
  30. raise ValueError(f"{label} closure must be a non-empty list")
  31. result: list[str] = []
  32. for value in values:
  33. if not isinstance(value, str) or not value:
  34. raise ValueError(f"{label} closure has invalid path")
  35. path = Path(value)
  36. if path.is_absolute() or ".." in path.parts or "\\" in value:
  37. raise ValueError(f"{label} closure has unsafe path: {value}")
  38. result.append(value)
  39. if len(result) != len(set(result)):
  40. raise ValueError(f"{label} closure has duplicate paths")
  41. return result
  42. def build_receipt() -> dict[str, object]:
  43. production_payload = json.loads(PRODUCTION.read_text(encoding="utf-8"))
  44. analysis_payload = json.loads(ANALYSIS.read_text(encoding="utf-8"))
  45. production_sources = safe_paths(production_payload.get("sources"), "production")
  46. analysis_paths = safe_paths(analysis_payload.get("paths"), "analysis")
  47. production_paths = list(dict.fromkeys([*production_sources, *TD_INPUTS]))
  48. missing_production = [path for path in production_paths if not (ROOT / path).is_file()]
  49. missing_analysis = [path for path in analysis_paths if not (ROOT / path).is_file()]
  50. if missing_production or missing_analysis:
  51. raise ValueError(
  52. f"closure missing files: production={missing_production} analysis={missing_analysis}"
  53. )
  54. return {
  55. "schema": "prod2u-6400-closure-check-v1",
  56. "project_id": "6400-front-rear",
  57. "production_source_closure": {
  58. "path": str(PRODUCTION.relative_to(ROOT)),
  59. "files": {path: sha256(ROOT / path) for path in production_paths},
  60. },
  61. "analysis_replay_closure": {
  62. "path": str(ANALYSIS.relative_to(ROOT)),
  63. "files": {path: sha256(ROOT / path) for path in analysis_paths},
  64. },
  65. }
  66. def main() -> int:
  67. parser = argparse.ArgumentParser()
  68. parser.add_argument("--output", type=Path)
  69. args = parser.parse_args()
  70. payload = build_receipt()
  71. text = json.dumps(payload, indent=2, sort_keys=True) + "\n"
  72. if args.output:
  73. args.output.parent.mkdir(parents=True, exist_ok=True)
  74. args.output.write_text(text, encoding="utf-8")
  75. else:
  76. print(text, end="")
  77. return 0
  78. if __name__ == "__main__":
  79. raise SystemExit(main())