#!/usr/bin/env python3 """Build the product-bound MRDIMM offline GUI on Windows.""" from __future__ import annotations import argparse, hashlib, json, shutil, subprocess, sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def main() -> int: ap = argparse.ArgumentParser(); ap.add_argument("--output-dir", type=Path, required=True); ap.add_argument("--bitstream-sha256", default="NOT_BUILT"); a = ap.parse_args() contract_path = ROOT/"src/generated/host-contract.json" contract = json.loads(contract_path.read_text(encoding="utf-8")); out = a.output_dir.resolve(); out.mkdir(parents=True, exist_ok=True) contract_sha = contract.get("contract_sha256") or hashlib.sha256(contract_path.read_bytes()).hexdigest() commit = subprocess.check_output(["git","rev-parse","HEAD"], cwd=ROOT, text=True).strip() if subprocess.check_output(["git","status","--porcelain"], cwd=ROOT, text=True).strip(): raise SystemExit("GUI packaging requires clean commit") meta = {"product":"mrdimm-chamber","board":"FCB V1.2","commit":commit,"contract_sha256":contract_sha,"bitstream_sha256":a.bitstream_sha256} (out/"gui-build-metadata.json").write_text(json.dumps(meta, indent=2)+"\n", encoding="utf-8") name = f"Prod2U-MRDIMM-V1.2-HEX-GUI-{commit[:8]}-{meta['contract_sha256'][:8]}" work = out/("pyinstaller-"+name); sep = ";" if sys.platform == "win32" else ":" cmd=[sys.executable,"-m","PyInstaller","--noconfirm","--clean","--onefile","--windowed","--name",name,"--distpath",str(out),"--workpath",str(work),"--specpath",str(work),"--add-data",f"{contract_path}{sep}.","--add-data",f"{out/'gui-build-metadata.json'}{sep}.",str(ROOT/"tools/mrdimm_hex_gui.py")] subprocess.run(cmd,cwd=ROOT,check=True); exe=out/(name+(".exe" if sys.platform=="win32" else "")) if sys.platform=="win32" and exe.read_bytes()[:2] != b"MZ": raise SystemExit("not a Windows PE") zip_path=out/(name+".zip"); shutil.make_archive(str(zip_path.with_suffix("")),"zip",root_dir=out,base_dir=exe.name) print(json.dumps({"exe":str(exe),"zip":str(zip_path),"sha256":hashlib.sha256(exe.read_bytes()).hexdigest(),**meta},indent=2)); shutil.rmtree(work,ignore_errors=True); return 0 if __name__ == "__main__": raise SystemExit(main())