build_6400_hex_gui.py 3.2 KB

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