build_mrdimm_hex_gui.py 2.5 KB

1234567891011121314151617181920212223242526
  1. #!/usr/bin/env python3
  2. """Build the product-bound MRDIMM offline GUI on Windows."""
  3. from __future__ import annotations
  4. import argparse, hashlib, json, shutil, subprocess, sys
  5. from pathlib import Path
  6. ROOT = Path(__file__).resolve().parents[1]
  7. def main() -> int:
  8. ap = argparse.ArgumentParser(); ap.add_argument("--output-dir", type=Path, required=True); ap.add_argument("--bitstream", type=Path, required=True, help="real TD bitstream file"); a = ap.parse_args()
  9. bitstream = a.bitstream.resolve()
  10. if not bitstream.is_file(): raise SystemExit(f"bitstream file not found: {bitstream}")
  11. bitstream_sha = hashlib.sha256(bitstream.read_bytes()).hexdigest()
  12. contract_path = ROOT/"src/generated/host-contract.json"
  13. contract = json.loads(contract_path.read_text(encoding="utf-8")); out = a.output_dir.resolve(); out.mkdir(parents=True, exist_ok=True)
  14. contract_sha = contract.get("contract_sha256") or hashlib.sha256(contract_path.read_bytes()).hexdigest()
  15. commit = subprocess.check_output(["git","rev-parse","HEAD"], cwd=ROOT, text=True).strip()
  16. if subprocess.check_output(["git","status","--porcelain"], cwd=ROOT, text=True).strip(): raise SystemExit("GUI packaging requires clean commit")
  17. meta = {"product":"mrdimm-chamber","board":"FCB V1.2","commit":commit,"contract_sha256":contract_sha,"bitstream_sha256":bitstream_sha}
  18. (out/"gui-build-metadata.json").write_text(json.dumps(meta, indent=2)+"\n", encoding="utf-8")
  19. name = f"Prod2U-MRDIMM-V1.2-HEX-GUI-{commit[:8]}-{meta['contract_sha256'][:8]}"
  20. work = out/("pyinstaller-"+name); sep = ";" if sys.platform == "win32" else ":"
  21. 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}.","--paths",str(ROOT/"tools/_vendor"),"--hidden-import","hth_protocol",str(ROOT/"tools/mrdimm_hex_gui.py")]
  22. subprocess.run(cmd,cwd=ROOT,check=True); exe=out/(name+(".exe" if sys.platform=="win32" else ""))
  23. if sys.platform=="win32" and exe.read_bytes()[:2] != b"MZ": raise SystemExit("not a Windows PE")
  24. zip_path=out/(name+".zip"); shutil.make_archive(str(zip_path.with_suffix("")),"zip",root_dir=out,base_dir=exe.name)
  25. 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
  26. if __name__ == "__main__": raise SystemExit(main())