environments_adopt.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """Adopt a newly selected dependency generation inside a running process.
  2. A plugin that installs a requirement at runtime wants it importable without a restart. That
  3. is safe only when nothing this process already loaded would change underneath it; otherwise
  4. the new generation waits for the next process, which boots straight into it.
  5. """
  6. from __future__ import annotations
  7. import importlib
  8. import importlib.metadata
  9. import os
  10. import re
  11. import site
  12. import sys
  13. from pathlib import Path
  14. from pm.environments import install_state_dir, selected_venv, site_packages, venv_bin_dir
  15. def running_environment(project_root: Path) -> Path | None:
  16. """This install's generation, selected or not, whose site-packages this process imports from.
  17. A generation that is no longer selected still counts: an update landed under this process,
  18. and a request from it is recorded and built but only the next process can load the result.
  19. """
  20. entries = {Path(entry).resolve() for entry in sys.path if entry}
  21. try:
  22. selected = selected_venv(project_root)
  23. except (OSError, RuntimeError, ValueError):
  24. return None
  25. for environment in (selected, *sorted((install_state_dir(project_root) / "environments").glob("*/venv"))):
  26. if site_packages(environment).resolve() in entries:
  27. return environment
  28. return None
  29. def _distributions(environment: Path) -> dict[str, importlib.metadata.Distribution]:
  30. return {re.sub(r"[-_.]+", "-", dist.metadata["Name"]).lower(): dist
  31. for dist in importlib.metadata.distributions(path=[str(site_packages(environment))])}
  32. def _loaded_from_changed(previous: Path, selected: Path) -> list[str]:
  33. """Modules loaded from a distribution whose version changed or disappeared, read from RECORD."""
  34. new = _distributions(selected)
  35. changed = set()
  36. for name, dist in _distributions(previous).items():
  37. if name not in new or new[name].version != dist.version:
  38. changed.update(Path(str(dist.locate_file(file))).resolve() for file in dist.files or ())
  39. roots = (str(site_packages(previous)), str(site_packages(previous).resolve()))
  40. loaded = []
  41. for name, module in list(sys.modules.items()):
  42. file = getattr(module, "__file__", None)
  43. if isinstance(file, str) and file.startswith(roots) and Path(file).resolve() in changed:
  44. loaded.append(name)
  45. return loaded
  46. def _replace(value: str, old: Path, new: Path) -> str:
  47. return os.pathsep.join(str(new) if part and Path(part).resolve() == old.resolve() else part
  48. for part in value.split(os.pathsep))
  49. def adopt(previous: Path, selected: Path, running: Path) -> bool:
  50. """Swap this process from ``previous`` onto ``selected``; False means only a restart can load it."""
  51. from hermes_cli.runtime_state import lease_generation
  52. if running.resolve() != previous.resolve():
  53. return False # An update landed under this process; its code no longer matches the selection.
  54. if selected.resolve() == previous.resolve():
  55. return True
  56. if _loaded_from_changed(previous, selected):
  57. return False
  58. lease_generation(selected) # The old lease stays: loaded modules keep reading from it.
  59. old, new = site_packages(previous), site_packages(selected)
  60. sys.path[:] = [str(new) if entry and Path(entry).resolve() == old.resolve() else entry for entry in sys.path]
  61. known = {pth.name for pth in old.glob("*.pth")}
  62. for pth in sorted(new.glob("*.pth")):
  63. if pth.name not in known:
  64. site.addpackage(str(new), pth.name, None)
  65. if os.environ.get("PYTHONPATH"):
  66. os.environ["PYTHONPATH"] = _replace(os.environ["PYTHONPATH"], old, new)
  67. os.environ["PATH"] = _replace(os.environ.get("PATH", ""), venv_bin_dir(previous), venv_bin_dir(selected))
  68. importlib.invalidate_caches()
  69. return True
  70. def _running_and_selected(project_root: Path) -> tuple[Path, Path] | None:
  71. running = running_environment(project_root)
  72. if running is None:
  73. return None
  74. try:
  75. return running, selected_venv(project_root)
  76. except (OSError, RuntimeError, ValueError):
  77. return None
  78. def restart_needed(project_root: Path) -> str | None:
  79. """Why this process must restart to load the selected generation, or None when it runs it.
  80. Also None when the process does not run from one of this install's generations (a developer
  81. venv, Nix): it never loads a PM selection, so a restart would not change what it imports.
  82. The reason names the selected generation, so it changes with every new publication.
  83. """
  84. pair = _running_and_selected(project_root)
  85. if pair is None or pair[0].resolve() == pair[1].resolve():
  86. return None
  87. running, selected = pair
  88. return (f"dependency generation {selected.parent.name} was published after this process "
  89. f"loaded {running.parent.name}")
  90. _restart_pending: str | None = None
  91. def note_restart_pending(reason: str) -> None:
  92. """Record that an install this process ran can only load in a new process.
  93. An auto-started gateway that nothing restarts reads it (``gateway.run_idle_exit``) and retires,
  94. so the next client starts one that boots the new generation instead of reattaching to this one.
  95. """
  96. global _restart_pending
  97. _restart_pending = reason
  98. def restart_pending() -> str | None:
  99. return _restart_pending
  100. def adopt_selected(project_root: Path) -> bool:
  101. """Move this process onto the selected generation. True when it already runs it, adopted it,
  102. or runs from no generation of this install at all (there is nothing to adopt)."""
  103. pair = _running_and_selected(project_root)
  104. return pair is None or adopt(pair[0], pair[1], pair[0])