desktop_build_reuse.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Reuse a packaged desktop app across commits that changed none of its build inputs.
  2. ``hermes update`` used to rebuild and repackage the Electron app (6-38 min on Windows) whenever HEAD
  3. moved, because ``_packaged_desktop_current_for_head`` requires the app's baked commit to BE HEAD. The
  4. freshness receipt already proves everything else the build reads is byte-identical; the baked
  5. install stamp is the one input it cannot see, since only a build rewrites it. This module answers
  6. the missing half: would a build at HEAD bake a stamp that differs in anything but commit provenance?
  7. The reused app keeps naming the commit it was really built from (never rewritten: the running app
  8. compares its boot stamp with the one on disk and would relaunch into the "swap" forever). Every
  9. reader of that commit tolerates an older one: bundle skew counts only desktop runtime-path commits
  10. since it, which identical inputs rule out, and bootstrap repair follows the checkout's own HEAD.
  11. """
  12. from __future__ import annotations
  13. import json
  14. import subprocess
  15. import sys
  16. from pathlib import Path
  17. from typing import Optional
  18. def _stamp_drift(project_root: Path, baked: Path, env: dict) -> Optional[list[str]]:
  19. """Fields a build at HEAD would bake differently, or None when that cannot be told."""
  20. from pm import installed_package
  21. installed = installed_package("node")
  22. if installed is None or installed.binary is None:
  23. return None
  24. try:
  25. result = subprocess.run(
  26. [str(installed.binary), str(project_root / "scripts/build/desktop-reuse.mjs"),
  27. "--source", str(project_root), "--baked", str(baked)],
  28. cwd=project_root, env=env, capture_output=True, text=True, encoding="utf-8",
  29. errors="replace", timeout=60, check=True,
  30. )
  31. drift = json.loads(result.stdout.strip().splitlines()[-1])
  32. except (OSError, subprocess.SubprocessError, ValueError, IndexError):
  33. return None
  34. return drift if isinstance(drift, list) else None
  35. def packaged_desktop_reusable(desktop_dir: Path, project_root: Path, env: dict) -> Optional[str]:
  36. """The commit the packaged app was built from, when it can stand for HEAD unchanged.
  37. Requires a current freshness receipt for the packaged bundle (inputs and output intact, not
  38. torn, native binary present) whose baked stamp is the prepared stamp that receipt covers, and a
  39. stamp a build at HEAD would bake that differs only in commit provenance. Anything unreadable
  40. means build.
  41. """
  42. from hermes_cli.main_desktop import _desktop_build_needed, _packaged_resources_dir
  43. resources = _packaged_resources_dir(desktop_dir)
  44. if resources is None:
  45. return None
  46. baked_path = resources / "install-stamp.json"
  47. try:
  48. baked = json.loads(baked_path.read_text(encoding="utf-8-sig"))
  49. prepared = json.loads((desktop_dir / "build/install-stamp.json").read_text(encoding="utf-8-sig"))
  50. except (OSError, ValueError):
  51. return None
  52. commit = baked.get("commit") if isinstance(baked, dict) else None
  53. if not isinstance(commit, str) or not commit or prepared != baked:
  54. return None
  55. if _desktop_build_needed(desktop_dir, project_root, source_mode=False):
  56. return None
  57. if sys.platform == "win32":
  58. # The build resolves HEAD and branch with PM's git (build_prepared_desktop); so must this.
  59. import pm
  60. env = pm.ensure("git", base_env=env).env
  61. return commit if _stamp_drift(project_root, baked_path, env) == [] else None