build_6400_hex_gui.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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-sha256", required=True)
  19. args = parser.parse_args()
  20. contract_path = ROOT / "generated/6400/host-contract.json"
  21. golden_path = ROOT / "generated/6400/golden-vectors.json"
  22. contract = json.loads(contract_path.read_text(encoding="utf-8"))
  23. golden = json.loads(golden_path.read_text(encoding="utf-8"))
  24. if contract["contract_sha256"] != golden["contract_sha256"]:
  25. raise SystemExit("contract/golden SHA mismatch")
  26. commit = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip()
  27. if subprocess.check_output(["git", "status", "--porcelain"], cwd=ROOT, text=True).strip():
  28. raise SystemExit("GUI packaging requires an exact clean commit")
  29. output = args.output_dir.resolve()
  30. output.mkdir(parents=True, exist_ok=True)
  31. meta = {"profile": args.profile, "commit": commit, "contract_sha256": contract["contract_sha256"], "bitstream_sha256": args.bitstream_sha256}
  32. meta_path = output / "gui-build-metadata.json"
  33. meta_path.write_text(json.dumps(meta, indent=2) + "\n", encoding="utf-8")
  34. name = f"6400-HEX-GUI-{args.profile}-{commit[:8]}-{contract['contract_sha256'][:8]}"
  35. work = output / ("pyinstaller-" + args.profile)
  36. sep = ";" if sys.platform == "win32" else ":"
  37. command = [sys.executable, "-m", "PyInstaller", "--noconfirm", "--clean", "--onefile", "--windowed",
  38. "--name", name, "--distpath", str(output), "--workpath", str(work), "--specpath", str(work),
  39. "--add-data", f"{contract_path}{sep}generated/6400", "--add-data", f"{golden_path}{sep}generated/6400",
  40. "--add-data", f"{meta_path}{sep}.", str(ROOT / "tools/6400_hex_gui.py")]
  41. subprocess.run(command, cwd=ROOT, check=True)
  42. exe = output / (name + (".exe" if sys.platform == "win32" else ""))
  43. if sys.platform != "win32" or exe.read_bytes()[:2] != b"MZ":
  44. raise SystemExit("Windows x64 Python/PyInstaller is required")
  45. embedded = json.loads(meta_path.read_text(encoding="utf-8"))
  46. if embedded["contract_sha256"] != contract["contract_sha256"]:
  47. raise SystemExit("embedded GUI contract SHA mismatch")
  48. print(json.dumps({"exe": str(exe), "sha256": sha(exe), **meta}, indent=2))
  49. shutil.rmtree(work, ignore_errors=True)
  50. return 0
  51. if __name__ == "__main__":
  52. raise SystemExit(main())