_uv.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Pinned tool acquisition for PM's private Python engine; never worker API."""
  2. from __future__ import annotations
  3. from pathlib import Path
  4. from pm.filesystem import native
  5. from pm.package import InstallError
  6. from pm.registry import get_package
  7. from pm.store import current_target
  8. def _toolchain(*, realize: bool = True, explicit: bool = False) -> tuple[Path, Path] | None:
  9. """Resolve uv and Python without host discovery or recursive worker dispatch.
  10. A read-only probe never installs. A sealed Windows payload builds on a verified writable
  11. copy of its Python: uv starts a venv's ``Scripts\\python.exe`` to query it, and that
  12. redirector runs outside the package, so its target must too. A redirector naming the
  13. packaged interpreter dies with exit 101 (#135236). Hermes itself still enters those venvs
  14. on the packaged interpreter (``pm.environments.venv_command``).
  15. """
  16. from pm.install import _installed_location, _lockfile, ensure, sealed
  17. from pm.user_facing import writable_copy
  18. if realize:
  19. ensure("uv", explicit=explicit)
  20. lockfile = _lockfile()
  21. target = current_target()
  22. binaries = {}
  23. for name in ("uv", "python"):
  24. package = get_package(name)
  25. location = _installed_location(package, lockfile, target)
  26. if location is None:
  27. if not realize:
  28. return None
  29. raise InstallError(name, "pinned tool is unavailable", "run `hermes pm install`")
  30. if name == "python" and target.startswith("win32") and sealed():
  31. location = writable_copy(name, realize=realize, explicit=explicit,
  32. why="a writable Python is required for bundled builds")
  33. if location is None:
  34. return None
  35. facts, store = location
  36. binary = package.binary(store.entry(facts.get(name)["entry"]), target)
  37. if binary is None or not binary.is_file():
  38. if not realize:
  39. return None
  40. raise InstallError(name, "installed binary is missing", "run `hermes pm install`")
  41. binaries[name] = binary
  42. # Callers execute and compare these outside PM's own file calls: the ordinary spelling, not the store's.
  43. return Path(native(binaries["uv"])), Path(native(binaries["python"]))