build_6400_hex_gui.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. args = parser.parse_args()
  22. bitstream = args.bitstream.resolve()
  23. if not bitstream.is_file():
  24. raise SystemExit(f"bitstream file not found: {bitstream}")
  25. bitstream_sha = hashlib.sha256(bitstream.read_bytes()).hexdigest()
  26. contract_path = ROOT / "generated/6400/host-contract.json"
  27. golden_path = ROOT / "generated/6400/golden-vectors.json"
  28. contract = json.loads(contract_path.read_text(encoding="utf-8"))
  29. golden = json.loads(golden_path.read_text(encoding="utf-8"))
  30. if contract["contract_sha256"] != golden["contract_sha256"]:
  31. raise SystemExit("contract/golden SHA mismatch")
  32. git_prefix = ["git"]
  33. git_cwd = ROOT
  34. if args.wsl_source_root:
  35. if sys.platform != "win32":
  36. raise SystemExit("--wsl-source-root is only valid for the Windows GUI build")
  37. git_prefix = ["wsl.exe", "-d", "Debian13", "--cd", args.wsl_source_root, "git"]
  38. git_cwd = None
  39. commit = subprocess.check_output([*git_prefix, "rev-parse", "HEAD"], cwd=git_cwd, text=True).strip()
  40. if subprocess.check_output([*git_prefix, "status", "--porcelain"], cwd=git_cwd, text=True).strip():
  41. raise SystemExit("GUI packaging requires an exact clean commit")
  42. output = args.output_dir.resolve()
  43. output.mkdir(parents=True, exist_ok=True)
  44. meta = {"profile": args.profile, "commit": commit, "contract_sha256": contract["contract_sha256"], "bitstream_sha256": bitstream_sha}
  45. meta_path = output / "gui-build-metadata.json"
  46. meta_path.write_text(json.dumps(meta, indent=2) + "\n", encoding="utf-8")
  47. name = f"6400-HEX-GUI-{args.profile}-{commit[:8]}-{contract['contract_sha256'][:8]}"
  48. work = output / ("pyinstaller-" + args.profile)
  49. sep = ";" if sys.platform == "win32" else ":"
  50. command = [sys.executable, "-m", "PyInstaller", "--noconfirm", "--clean", "--onefile", "--windowed",
  51. "--name", name, "--distpath", str(output), "--workpath", str(work), "--specpath", str(work),
  52. "--add-data", f"{contract_path}{sep}generated/6400", "--add-data", f"{golden_path}{sep}generated/6400",
  53. "--add-data", f"{meta_path}{sep}.",
  54. "--paths", str(ROOT / "tools/_vendor"), "--hidden-import", "hth_protocol",
  55. str(ROOT / "tools/6400_hex_gui.py")]
  56. subprocess.run(command, cwd=ROOT, check=True)
  57. exe = output / (name + (".exe" if sys.platform == "win32" else ""))
  58. if sys.platform != "win32" or exe.read_bytes()[:2] != b"MZ":
  59. raise SystemExit("Windows x64 Python/PyInstaller is required")
  60. embedded = json.loads(meta_path.read_text(encoding="utf-8"))
  61. if embedded["contract_sha256"] != contract["contract_sha256"]:
  62. raise SystemExit("embedded GUI contract SHA mismatch")
  63. print(json.dumps({"exe": str(exe), "sha256": sha(exe), **meta}, indent=2))
  64. shutil.rmtree(work, ignore_errors=True)
  65. return 0
  66. if __name__ == "__main__":
  67. raise SystemExit(main())