#!/usr/bin/env python3 """Refresh docs/REFERENCE-INVENTORY.{json,md} from live reference + board-viewer assets.""" from __future__ import annotations import argparse import hashlib import json import sys from datetime import datetime, timezone from pathlib import Path from typing import Any ROOT = Path(__file__).resolve().parents[2] EXPECTED_BRD_SHA256 = "269b5bab2f2e1215b9392079a003badc34238ae3cbe8e5094c9a11bba8b84ebb" EXPECTED_PDF_SHA256 = "7ebd0dced194338ceb97ff5a0abcee2d9790e7f6e7a90d9d4be7de593dca7eb5" CANONICAL_BRD = ( "reference/original/board/FCB_FPGA_MCU_V1.2_20260717/" "FCB_FPGA+MCU_V1.2_20260717.brd" ) ARCHIVE_BRD = "reference/original/local_archives/board/FCB_FPGA+MCU_V1.2_20260717.brd" def sha256_file(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as stream: while chunk := stream.read(1024 * 1024): digest.update(chunk) return digest.hexdigest() def item_for( root: Path, relative: str, *, expected_sha256: str | None = None, confidence: str = "present", notes: dict[str, Any] | None = None, ) -> dict[str, Any]: path = root / relative payload: dict[str, Any] = { "path": relative, "exists": path.is_file(), "size": path.stat().st_size if path.is_file() else 0, } if path.is_file(): payload["sha256"] = sha256_file(path) if expected_sha256 is not None: payload["expected_sha256"] = expected_sha256 payload["hash_match"] = bool( path.is_file() and payload.get("sha256") == expected_sha256 ) payload["confidence"] = ( "confirmed" if payload.get("hash_match") else ("missing" if not path.is_file() else "hash_fail") ) else: payload["confidence"] = confidence if path.is_file() else "missing" if notes: payload.update(notes) return payload def build_inventory(root: Path) -> dict[str, Any]: generated = root / "tools/board-viewer/generated" build_info: dict[str, Any] = {} if (generated / "build-info.json").is_file(): build_info = json.loads((generated / "build-info.json").read_text(encoding="utf-8")) items = [ item_for(root, "reference/original/MANIFEST.zh-CN.md"), item_for( root, CANONICAL_BRD, expected_sha256=EXPECTED_BRD_SHA256, notes={ "role": "canonical_brd", "packaging_note": ( "Canonical physical BRD for analysis packages; " "prefer this path over local_archives duplicate" ), }, ), item_for( root, ARCHIVE_BRD, expected_sha256=EXPECTED_BRD_SHA256, notes={ "role": "duplicate_archive_mirror", "byte_identical_to_canonical": True, "packaging_recommendation": ( "Keep one physical copy in analysis packages: include " f"{CANONICAL_BRD} and omit {ARCHIVE_BRD} (or replace the " "archive entry with a provenance pointer to the canonical path)." ), }, ), item_for( root, "reference/original/schematics/8000高温罩电气图V2.0.pdf", expected_sha256=EXPECTED_PDF_SHA256, ), item_for(root, "reference/original/schematics/FCB_FPGA+MCU0611.pdf"), item_for(root, "reference/original/diagrams/上位机逻辑.svg"), item_for(root, "reference/original/diagrams/接长鑫水滴系统.svg"), item_for(root, "config/board_viewer_annotations.fcb_fpga_mcu_v1_2.json"), item_for( root, "tools/board-viewer/generated/board-data.json", notes={ "evidence_class": "derived_analysis_from_brd_not_rtl_td_source_closure", }, ), item_for( root, "tools/board-viewer/generated/build-info.json", notes={ "evidence_class": "derived_analysis_from_brd_not_rtl_td_source_closure", }, ), item_for( root, "tools/board-viewer/generated/import-report.json", notes={ "evidence_class": "derived_analysis_from_brd_not_rtl_td_source_closure", }, ), item_for( root, "tools/board-viewer/generated/board-copper.svg", notes={ "evidence_class": "derived_analysis_from_brd_not_rtl_td_source_closure", "layer": "F.Cu,Edge.Cuts", }, ), item_for( root, "tools/board-viewer/generated/board-silkscreen.svg", notes={ "evidence_class": "derived_analysis_from_brd_not_rtl_td_source_closure", "layer": "F.SilkS,Edge.Cuts", }, ), item_for( root, "tools/board-viewer/generated/board-mask-edge.svg", notes={ "evidence_class": "derived_analysis_from_brd_not_rtl_td_source_closure", "layer": "F.Mask,Edge.Cuts", }, ), ] present = sum(1 for item in items if item.get("exists")) missing = sum(1 for item in items if not item.get("exists")) hash_ok = sum(1 for item in items if item.get("hash_match") is True) hash_fail = sum(1 for item in items if item.get("hash_match") is False) return { "schema": "prod2u-reference-inventory-v1", "generated_at_utc": datetime.now(timezone.utc).isoformat(), "repository_root": str(root.resolve()), "evidence_boundaries": { "board_viewer_svgs_and_json": ( "Derived analysis evidence from the Allegro BRD via KiCad. " "Not RTL/TD/ADC/bitstream source closure." ), "canonical_brd": CANONICAL_BRD, "duplicate_brd_archive": ARCHIVE_BRD, "packaging_recommendation": ( "Analysis packages should ship one physical BRD copy at the " "canonical path; treat local_archives/board as a local-only mirror." ), "expected_brd_sha256": EXPECTED_BRD_SHA256, }, "items": items, "board_viewer_build_info": build_info, "summary": { "present": present, "missing": missing, "hash_ok": hash_ok, "hash_fail": hash_fail, }, } def render_markdown(inventory: dict[str, Any]) -> str: bounds = inventory.get("evidence_boundaries") or {} summary = inventory.get("summary") or {} lines = [ "# Reference Inventory", "", f"Generated: {inventory.get('generated_at_utc')}", "", f"- present: {summary.get('present', 0)}", f"- missing: {summary.get('missing', 0)}", f"- hash_ok: {summary.get('hash_ok', 0)}", f"- hash_fail: {summary.get('hash_fail', 0)}", "", "## Evidence boundaries", "", f"- Board-viewer JSON/SVG: {bounds.get('board_viewer_svgs_and_json')}", f"- Canonical BRD: `{bounds.get('canonical_brd')}`", f"- Duplicate archive BRD: `{bounds.get('duplicate_brd_archive')}` " "(byte-identical local mirror; packaging should keep one physical copy)", f"- Expected BRD SHA-256: `{bounds.get('expected_brd_sha256')}`", f"- Packager recommendation: {bounds.get('packaging_recommendation')}", "", "| Path | Exists | Size | Confidence | Hash notes | Evidence |", "|------|--------|------|------------|------------|----------|", ] for item in inventory.get("items") or []: sha = item.get("sha256") or "" if item.get("hash_match") is True: hash_notes = "MATCH" elif item.get("hash_match") is False: hash_notes = "FAIL" elif sha: hash_notes = sha[:16] + "..." else: hash_notes = "-" evidence = item.get("evidence_class") or item.get("role") or "source/reference" lines.append( f"| `{item.get('path')}` | {item.get('exists')} | {item.get('size')} | " f"{item.get('confidence')} | {hash_notes} | {evidence} |" ) lines.append("") return "\n".join(lines) def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--root", type=Path, default=ROOT) parser.add_argument( "--json-out", type=Path, default=ROOT / "docs" / "REFERENCE-INVENTORY.json", ) parser.add_argument( "--md-out", type=Path, default=ROOT / "docs" / "REFERENCE-INVENTORY.md", ) args = parser.parse_args(argv) inventory = build_inventory(args.root.resolve()) args.json_out.parent.mkdir(parents=True, exist_ok=True) args.json_out.write_text( json.dumps(inventory, indent=2, ensure_ascii=False) + "\n", encoding="utf-8", ) args.md_out.write_text(render_markdown(inventory), encoding="utf-8") print(f"wrote {args.json_out}") print(f"wrote {args.md_out}") print(json.dumps(inventory["summary"], ensure_ascii=False)) return 0 if inventory["summary"].get("hash_fail", 0) == 0 else 1 if __name__ == "__main__": sys.exit(main())