recovery.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """Restore dependency generations without importing the damaged environment."""
  2. from __future__ import annotations
  3. import contextlib
  4. import subprocess
  5. import sys
  6. from pathlib import Path
  7. from pm.package import InstallError
  8. STARTUP_IMPORTS = (
  9. ("ruamel.yaml", "ruamel.yaml", "YAML"),
  10. ("python-dotenv", "dotenv", "load_dotenv"),
  11. ("click", "click", "Command"),
  12. ("certifi", "certifi", "contents"),
  13. ("rich", "rich", "print"),
  14. ("cryptography", "cryptography.hazmat.bindings._rust", "openssl"),
  15. ("PyJWT", "jwt", "encode"),
  16. )
  17. def validate_environment(python: Path | list[str], *, env: dict, cwd: Path) -> None:
  18. """Run startup import checks in the candidate, never the repairing process.
  19. *python* is the candidate's interpreter or its argv prefix (``venv_command``)."""
  20. script = (
  21. "import importlib, importlib.metadata, pathlib, re, tomllib\n"
  22. "project = tomllib.loads(pathlib.Path('pyproject.toml').read_text(encoding='utf-8-sig'))['project']\n"
  23. "required = {re.split(r'[\\[<>=!~; @]', dep, 1)[0].lower().replace('_', '-')\n"
  24. " for dep in project.get('dependencies', [])}\n"
  25. f"checks = {STARTUP_IMPORTS!r}\n"
  26. "for distribution, module, attribute in checks:\n"
  27. " try:\n"
  28. " importlib.metadata.distribution(distribution)\n"
  29. " except importlib.metadata.PackageNotFoundError:\n"
  30. " if distribution.lower().replace('_', '-') in required:\n"
  31. " raise\n"
  32. " continue\n"
  33. " loaded = importlib.import_module(module)\n"
  34. " getattr(loaded, attribute)\n"
  35. " if module == 'certifi':\n"
  36. " bundle = pathlib.Path(loaded.where())\n"
  37. " assert bundle.is_file() and bundle.stat().st_size >= 1024, 'CA bundle is missing'\n"
  38. )
  39. command = [str(python), "-I"] if isinstance(python, (str, Path)) else [*python]
  40. result = subprocess.run([*command, "-c", script], cwd=cwd, env=env,
  41. capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=60, check=False)
  42. if result.returncode:
  43. raise InstallError("venv", f"startup validation failed: {result.stderr.strip()[-1000:]}")
  44. def repair_dependencies(project_root: Path) -> None:
  45. """Restore this installation's recorded set; never repair a foreign tree."""
  46. from hermes_cli.venv_sync import collect_superseded_generations
  47. from pm.client import sync_venv
  48. from pm.paths import repo_root
  49. if Path(project_root).resolve() != repo_root().resolve():
  50. raise InstallError("venv", "recovery root does not match this PM installation")
  51. with contextlib.redirect_stdout(sys.stderr):
  52. sync_venv(repair=True)
  53. collect_superseded_generations(project_root)
  54. def _baseline_gaps(root: Path) -> list[str]:
  55. """Extras the store's shipped selection carries that the recorded selection lacks.
  56. A volume whose first generation was built before the image recorded its selection holds
  57. only what was installed on use (``["fal"]``, or ``[]``), and that generation replaces the
  58. image's environment with one missing everything the image ships.
  59. """
  60. from pm.environments import runtime_facts_path
  61. from pm.install import _carried, _extra_key, _facts
  62. from pm.lock import Facts
  63. from pm.packages import Venv
  64. fact = Facts(runtime_facts_path(root), strict=True).get("venv")
  65. if fact is None:
  66. return []
  67. # The same checks venv_is_current applies; a gap must not skip them.
  68. recorded = fact.get("extras") if isinstance(fact, dict) else None
  69. stamp = fact.get("stamp") if isinstance(fact, dict) else None
  70. if (not isinstance(recorded, list) or any(not isinstance(extra, str) for extra in recorded)
  71. or not isinstance(stamp, str) or not stamp):
  72. raise ValueError("invalid recorded dependency state")
  73. shipped = (_facts().get("venv") or {}).get("extras") or []
  74. have = {_extra_key(extra) for extra in recorded}
  75. return sorted({extra for extra in _carried(Venv(root), shipped) if _extra_key(extra) not in have})
  76. def refresh_dependencies(project_root: Path) -> str:
  77. """Re-resolve the durable selection against inputs an external update replaced.
  78. A container image swaps the code and lock under a selection recorded on the data volume; a
  79. generation resolved against the previous lock must never boot the new code. Rebuilds the
  80. recorded extras and plugins, or on failure boots the image's own environment while keeping
  81. them recorded, so the next boot or install rebuilds them. A recorded selection missing
  82. extras the image ships gets them back. Returns what happened.
  83. """
  84. from hermes_cli.runtime_state import runtime_lock
  85. from pm.client import sync_venv
  86. from pm.environments import runtime_facts_path
  87. from pm.install import venv_is_current
  88. from pm.lock import Facts
  89. from pm.paths import repo_root
  90. root = Path(project_root).resolve()
  91. if root != repo_root().resolve():
  92. raise InstallError("venv", "refresh root does not match this PM installation")
  93. if not runtime_facts_path(root).is_file():
  94. return "base"
  95. missing = _baseline_gaps(root)
  96. if not missing and venv_is_current(project_root=root):
  97. return "current"
  98. try:
  99. with contextlib.redirect_stdout(sys.stderr):
  100. # The image's baseline, not a user request: it never undoes a `--without`.
  101. sync_venv(missing or None, explicit=True, automatic=missing)
  102. return f"restored {', '.join(missing)}" if missing else "rebuilt"
  103. except Exception as exc:
  104. print(f"dependency refresh failed: {exc}", file=sys.stderr)
  105. with runtime_lock(root, timeout=None):
  106. facts = Facts(runtime_facts_path(root), strict=True)
  107. fact = facts.get("venv") or {}
  108. # Keep the restored extras recorded too: a lazy install before the next boot
  109. # unions onto this selection and must not publish a generation without them.
  110. recorded = list(fact.get("extras") or [])
  111. facts.record_state("venv", fact.get("stamp") or "stale",
  112. recorded + [extra for extra in missing if extra not in recorded])
  113. return "fallback"