update_owning_install.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Send ``hermes update`` back to the install whose interpreter it is running on.
  2. An install's in-tree venv can end up importing another checkout's code: an editable
  3. install recorded against a dev tree (what ``project_venv_dir`` used to cause when a dev
  4. checkout ran on the app install's interpreter) turns ``<install>/venv/bin/hermes`` into
  5. the dev tree's CLI. Every update the Desktop hands to that launcher then pulls the dev
  6. tree, the install never moves, and the app keeps relaunching its stale build.
  7. Running the install's own updater repairs it: it pulls the install and reinstalls the
  8. install into its venv, which rewrites the editable record to point home again.
  9. """
  10. from __future__ import annotations
  11. import os
  12. from pathlib import Path
  13. import subprocess
  14. import sys
  15. def owning_install_root(project_root: Path) -> Path | None:
  16. """The checkout whose in-tree venv this interpreter is, when that is not *project_root*.
  17. ``None`` when this process runs on its own checkout's interpreter, a venv outside any
  18. checkout, or when *project_root* was put on ``PYTHONPATH`` on purpose (a wrapper that
  19. runs a dev tree on another interpreter chose that tree; it is not a redirected install).
  20. """
  21. if sys.prefix == sys.base_prefix:
  22. return None
  23. venv = Path(sys.prefix).resolve()
  24. root = Path(project_root).resolve()
  25. if venv.name not in ("venv", ".venv"):
  26. return None
  27. owner = venv.parent if (venv.parent / "hermes_cli" / "main.py").is_file() else _pm_environment_owner(venv)
  28. if owner is None or owner == root:
  29. return None
  30. chosen = (Path(p).resolve() for p in os.environ.get("PYTHONPATH", "").split(os.pathsep) if p)
  31. if root in chosen:
  32. return None
  33. return owner
  34. def _pm_environment_owner(venv: Path) -> Path | None:
  35. """The checkout a PM generation venv (``<installs>/<key>/environments/<gen>/venv``) belongs to.
  36. Its ``hermes_cli`` is the generation's ``workspace/`` snapshot, which has no ``.git``, so an
  37. update run from it must go to the checkout (#122627). The install key hashes the checkout
  38. path, which proves the path recorded beside it.
  39. """
  40. from pm.environments import install_key
  41. state = venv.parent.parent.parent
  42. if venv.parent.parent.name != "environments":
  43. return None
  44. try:
  45. # pm.environments.record_activation_inputs writes it on every full install.
  46. owner = Path((state / "inputs" / ".project-root").read_text(encoding="utf-8-sig").strip())
  47. except OSError:
  48. return None
  49. if install_key(owner) != state.name or not (owner / "hermes_cli" / "main.py").is_file():
  50. return None
  51. return owner
  52. def retarget_to_owning_install(project_root: Path) -> None:
  53. """Re-run this command with the owning install's code; returns only when nothing is redirected."""
  54. owner = owning_install_root(project_root)
  55. if owner is None:
  56. return
  57. print(f"⚠ {owner / Path(sys.prefix).name} is running the checkout at {project_root}, not its own code.")
  58. print(f"→ Updating {owner} with its own updater; this also points its venv back at it.")
  59. sys.stdout.flush()
  60. # PYTHONPATH entries resolve before the editable finder on sys.meta_path, so the
  61. # owner's hermes_cli wins; in the child, project_root == owner and this is a no-op.
  62. env = dict(os.environ, PYTHONPATH=str(owner))
  63. code = subprocess.call([sys.executable, "-m", "hermes_cli.main", *sys.argv[1:]], cwd=owner, env=env)
  64. raise SystemExit(code)