#!/usr/bin/env python3 """Build one profile-specific Windows HEX-only GUI from generated contracts.""" from __future__ import annotations import argparse import hashlib import json import shutil import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def sha(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--profile", choices=("6400-front-rear",), default="6400-front-rear") parser.add_argument("--output-dir", type=Path, required=True) parser.add_argument("--bitstream-sha256", required=True) parser.add_argument("--wsl-source-root") args = parser.parse_args() contract_path = ROOT / "generated/6400/host-contract.json" golden_path = ROOT / "generated/6400/golden-vectors.json" contract = json.loads(contract_path.read_text(encoding="utf-8")) golden = json.loads(golden_path.read_text(encoding="utf-8")) if contract["contract_sha256"] != golden["contract_sha256"]: raise SystemExit("contract/golden SHA mismatch") git_prefix = ["git"] git_cwd = ROOT if args.wsl_source_root: if sys.platform != "win32": raise SystemExit("--wsl-source-root is only valid for the Windows GUI build") git_prefix = ["wsl.exe", "-d", "Debian13", "--cd", args.wsl_source_root, "git"] git_cwd = None commit = subprocess.check_output([*git_prefix, "rev-parse", "HEAD"], cwd=git_cwd, text=True).strip() if subprocess.check_output([*git_prefix, "status", "--porcelain"], cwd=git_cwd, text=True).strip(): raise SystemExit("GUI packaging requires an exact clean commit") output = args.output_dir.resolve() output.mkdir(parents=True, exist_ok=True) meta = {"profile": args.profile, "commit": commit, "contract_sha256": contract["contract_sha256"], "bitstream_sha256": args.bitstream_sha256} meta_path = output / "gui-build-metadata.json" meta_path.write_text(json.dumps(meta, indent=2) + "\n", encoding="utf-8") name = f"6400-HEX-GUI-{args.profile}-{commit[:8]}-{contract['contract_sha256'][:8]}" work = output / ("pyinstaller-" + args.profile) sep = ";" if sys.platform == "win32" else ":" command = [sys.executable, "-m", "PyInstaller", "--noconfirm", "--clean", "--onefile", "--windowed", "--name", name, "--distpath", str(output), "--workpath", str(work), "--specpath", str(work), "--add-data", f"{contract_path}{sep}generated/6400", "--add-data", f"{golden_path}{sep}generated/6400", "--add-data", f"{meta_path}{sep}.", str(ROOT / "tools/6400_hex_gui.py")] subprocess.run(command, cwd=ROOT, check=True) exe = output / (name + (".exe" if sys.platform == "win32" else "")) if sys.platform != "win32" or exe.read_bytes()[:2] != b"MZ": raise SystemExit("Windows x64 Python/PyInstaller is required") embedded = json.loads(meta_path.read_text(encoding="utf-8")) if embedded["contract_sha256"] != contract["contract_sha256"]: raise SystemExit("embedded GUI contract SHA mismatch") print(json.dumps({"exe": str(exe), "sha256": sha(exe), **meta}, indent=2)) shutil.rmtree(work, ignore_errors=True) return 0 if __name__ == "__main__": raise SystemExit(main())