features.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """The enabled-features file: the exact extras the install carries.
  2. The bundle is built with `uv sync --all-extras` minus the declared
  3. opt-in extras (`opt_in_extras`), but WHICH extras
  4. actually resolve differs per platform (markers gate some off). The
  5. shipped default is the EXACT list that installed — written at bundle
  6. time beside the payload, read at sync time:
  7. - lazy installs OFF (security.allow_lazy_installs: false): the feature
  8. list is FROZEN to this file; pm sync never deviates from it and never
  9. installs a plugin dep.
  10. - lazy installs ON: the file is the baseline; extras union on top.
  11. """
  12. from __future__ import annotations
  13. import json
  14. from pathlib import Path
  15. from typing import Optional
  16. FEATURES_FILENAME = "enabled-features.json"
  17. class FeatureProbeError(RuntimeError):
  18. """A failed inventory must not become an empty feature list."""
  19. def features_path(base_dir: Optional[Path] = None) -> Path:
  20. """Where the enabled-features file lives. In a bundle, the payload
  21. root (beside manifest.json — bundle-written, sealed-shipped). At
  22. runtime, the runtime dir (beside the byte store — per-install,
  23. writable on every install kind). Same relative location on both:
  24. a sealed install's store_root() is <payload>/tools, and the file
  25. sits at the payload root = store_root().parent."""
  26. if base_dir is not None:
  27. return base_dir / FEATURES_FILENAME
  28. from pm.paths import store_root
  29. return store_root().parent / FEATURES_FILENAME
  30. def write_features(extras: list[str], base_dir: Optional[Path] = None) -> Path:
  31. """Record the exact extras an install carries. Sorted, deduped."""
  32. path = features_path(base_dir)
  33. path.parent.mkdir(parents=True, exist_ok=True)
  34. path.write_text(
  35. json.dumps({"schema": 1, "extras": sorted(set(extras))}, indent=2) + "\n",
  36. encoding="utf-8",
  37. )
  38. return path
  39. def read_features() -> Optional[list[str]]:
  40. """The frozen/baseline feature list; None when no file exists (source
  41. installs without a bundle — the baseline is the recorded venv
  42. state)."""
  43. try:
  44. data = json.loads(features_path().read_text(encoding="utf-8-sig"))
  45. except (OSError, ValueError):
  46. return None
  47. extras = data.get("extras")
  48. if not isinstance(extras, list):
  49. return None
  50. return sorted({str(e) for e in extras})
  51. def declared_extras(repo_dir: Path) -> list[str]:
  52. """Every extra the repo's pyproject declares."""
  53. import tomllib
  54. with (repo_dir / "pyproject.toml").open("rb") as f:
  55. data = tomllib.load(f)
  56. return sorted(data.get("project", {}).get("optional-dependencies", {}))
  57. def opt_in_extras(repo_dir: Path) -> list[str]:
  58. """Extras that only an explicit selection installs, never an all-extras build.
  59. ``[tool.hermes] opt-in-extras`` in the repo's pyproject. They stay
  60. installable through ``sync_venv([extra])``; bundles and other
  61. ``--all-extras`` builds leave them out, so their closures are absent
  62. from shipped payloads and from the recorded feature list.
  63. """
  64. import tomllib
  65. with (repo_dir / "pyproject.toml").open("rb") as f:
  66. data = tomllib.load(f)
  67. names = data.get("tool", {}).get("hermes", {}).get("opt-in-extras", [])
  68. declared = set(data.get("project", {}).get("optional-dependencies", {}))
  69. unknown = sorted(set(names) - declared)
  70. if unknown:
  71. raise ValueError(f"[tool.hermes] opt-in-extras names undeclared extras: {unknown}")
  72. return sorted(set(names))
  73. def installed_extras(repo_dir: Path, venv_dir: Path, *, python_exe: Path) -> list[str]:
  74. """Inventory every required anchor in one isolated target process.
  75. The staged interpreter owns the site layout. Only the selected tree's
  76. .pth files are processed, so editable packages keep their launch behavior.
  77. """
  78. import subprocess
  79. from pm.extras import _anchors
  80. required = {extra: _anchors(extra) for extra in declared_extras(repo_dir)}
  81. anchors = sorted({anchor for group in required.values() for anchor in group})
  82. probe = """
  83. import contextlib, importlib.util, json, os, site, sys, sysconfig
  84. base, anchors = sys.argv[1], json.loads(sys.argv[2])
  85. paths = sysconfig.get_paths(vars={"base": base, "platbase": base})
  86. sites = dict.fromkeys(paths[key] for key in ("purelib", "platlib"))
  87. if not any(os.path.isdir(path) for path in sites):
  88. raise RuntimeError("target dependency tree has no site-packages")
  89. result = {}
  90. with contextlib.redirect_stdout(sys.stderr):
  91. for path in sites:
  92. site.addsitedir(path)
  93. for anchor in anchors:
  94. try:
  95. result[anchor] = importlib.util.find_spec(anchor) is not None
  96. except (ImportError, ValueError):
  97. result[anchor] = False
  98. print(json.dumps(result))
  99. """
  100. try:
  101. child = subprocess.run(
  102. [str(python_exe), "-B", "-I", "-S", "-c", probe,
  103. str(venv_dir.resolve()), json.dumps(anchors)],
  104. cwd=repo_dir, capture_output=True, text=True, encoding="utf-8",
  105. errors="replace", timeout=60, check=True,
  106. )
  107. except (OSError, subprocess.SubprocessError) as exc:
  108. detail = (getattr(exc, "stderr", None) or str(exc)).strip()
  109. raise FeatureProbeError(f"feature inventory failed on {python_exe}: {detail}") from exc
  110. try:
  111. resolved = json.loads(child.stdout)
  112. except ValueError as exc:
  113. raise FeatureProbeError("feature inventory returned invalid JSON") from exc
  114. if (not isinstance(resolved, dict) or set(resolved) != set(anchors)
  115. or any(type(value) is not bool for value in resolved.values())):
  116. raise FeatureProbeError("feature inventory returned incomplete anchor results")
  117. return sorted(extra for extra, group in required.items() if all(resolved[anchor] for anchor in group))