build_6400_hex_gui.py 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. """Build one profile-specific Windows HEX-only GUI from generated contracts."""
  3. from __future__ import annotations
  4. import argparse
  5. import hashlib
  6. import json
  7. import shutil
  8. import subprocess
  9. import sys
  10. from pathlib import Path
  11. ROOT = Path(__file__).resolve().parents[1]
  12. def sha(path: Path) -> str:
  13. return hashlib.sha256(path.read_bytes()).hexdigest()
  14. def main() -> int:
  15. parser = argparse.ArgumentParser()
  16. parser.add_argument("--profile", choices=("6400-front-rear",), default="6400-front-rear")
  17. parser.add_argument("--output-dir", type=Path, required=True)
  18. parser.add_argument("--bitstream", type=Path, required=True,
  19. help="real TD bitstream file; its SHA-256 is computed here")
  20. parser.add_argument("--wsl-source-root")
  21. parser.add_argument('--td-receipt',type=Path,required=True)
  22. args = parser.parse_args()
  23. bitstream = args.bitstream.resolve()
  24. if not bitstream.is_file():
  25. raise SystemExit(f"bitstream file not found: {bitstream}")
  26. bitstream_sha = hashlib.sha256(bitstream.read_bytes()).hexdigest()
  27. td_receipt=json.loads(args.td_receipt.read_text(encoding='utf-8'))
  28. if td_receipt.get('status')!='PASS' or td_receipt.get('bitstream_sha256')!=bitstream_sha:
  29. raise SystemExit('BIT does not match a passing TD receipt')
  30. changed=[name for name,value in td_receipt['inputs'].items() if not (ROOT/name).is_file() or sha(ROOT/name)!=value]
  31. if changed:raise SystemExit('production inputs changed since TD: '+', '.join(changed))
  32. contract_path = ROOT / "generated/6400/manual-uart-contract.json"
  33. golden_path = ROOT / "generated/6400/golden-vectors.json"
  34. contract = json.loads(contract_path.read_text(encoding="utf-8"))
  35. golden = json.loads(golden_path.read_text(encoding="utf-8"))
  36. if contract["contract_sha256"] != golden["contract_sha256"]:
  37. raise SystemExit("contract/golden SHA mismatch")
  38. git_prefix = ["git"]
  39. git_cwd = ROOT
  40. if args.wsl_source_root:
  41. if sys.platform != "win32":
  42. raise SystemExit("--wsl-source-root is only valid for the Windows GUI build")
  43. git_prefix = ["wsl.exe", "-d", "Debian13", "--cd", args.wsl_source_root, "git"]
  44. git_cwd = None
  45. commit = subprocess.check_output([*git_prefix, "rev-parse", "HEAD"], cwd=git_cwd, text=True).strip()
  46. if subprocess.check_output([*git_prefix, "status", "--porcelain"], cwd=git_cwd, text=True).strip():
  47. raise SystemExit("GUI packaging requires an exact clean commit")
  48. output = args.output_dir.resolve()
  49. output.mkdir(parents=True, exist_ok=True)
  50. meta = {"profile": args.profile, "commit": commit, "contract_sha256": contract["contract_sha256"], "bitstream_sha256": bitstream_sha}
  51. meta['bitstream_source_commit']=td_receipt['commit']
  52. meta['source_manifest_sha256']=td_receipt['source_manifest_sha256']
  53. meta_path = output / "gui-build-metadata.json"
  54. meta_path.write_text(json.dumps(meta, indent=2) + "\n", encoding="utf-8")
  55. name = f"6400-HEX-GUI-{args.profile}-{commit[:8]}-{contract['contract_sha256'][:8]}"
  56. work = output / ("pyinstaller-" + args.profile)
  57. sep = ";" if sys.platform == "win32" else ":"
  58. command = [sys.executable, "-m", "PyInstaller", "--noconfirm", "--clean", "--onefile", "--windowed",
  59. "--name", name, "--distpath", str(output), "--workpath", str(work), "--specpath", str(work),
  60. "--add-data", f"{contract_path}{sep}generated/6400", "--add-data", f"{golden_path}{sep}generated/6400",
  61. "--add-data", f"{meta_path}{sep}.",
  62. "--add-data", f"{ROOT/'contracts/ct7428-format.json'}{sep}generated/6400",
  63. "--paths", str(ROOT / "tools/_vendor"), "--hidden-import", "hth_protocol", "--hidden-import", "prod2u_status",
  64. str(ROOT / "tools/6400_hex_gui.py")]
  65. subprocess.run(command, cwd=ROOT, check=True)
  66. exe = output / (name + (".exe" if sys.platform == "win32" else ""))
  67. if sys.platform != "win32" or exe.read_bytes()[:2] != b"MZ":
  68. raise SystemExit("Windows x64 Python/PyInstaller is required")
  69. embedded = json.loads(meta_path.read_text(encoding="utf-8"))
  70. if embedded["contract_sha256"] != contract["contract_sha256"]:
  71. raise SystemExit("embedded GUI contract SHA mismatch")
  72. print(json.dumps({"exe": str(exe), "sha256": sha(exe), **meta}, indent=2))
  73. shutil.rmtree(work, ignore_errors=True)
  74. return 0
  75. if __name__ == "__main__":
  76. raise SystemExit(main())