build_env.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """Build and inspect explicit Python dependency environments through PM."""
  2. from __future__ import annotations
  3. import argparse
  4. from collections.abc import Sequence
  5. from pathlib import Path
  6. import sys
  7. def _record_selection(source: Path, extras: list[str]) -> None:
  8. """Publish the built extras as the store's venv selection, as a bundle build does.
  9. A sync that finds no runtime selection starts from this one, so the first opt-in
  10. extra installed on use extends the shipped environment instead of replacing it
  11. with a generation that carries only that extra. The frozen build installed exactly
  12. this list from the lock, so it is recorded as given; an anchor inventory
  13. (``installed_extras``) misses meta extras such as ``all`` and claims extras whose
  14. anchors other packages happen to provide.
  15. """
  16. from pm.lock import Facts
  17. from pm.packages import Venv
  18. from pm.paths import facts_path
  19. selected = sorted(set(extras))
  20. stamp = Venv(source.resolve()).expected_stamp(selected, plugin_dirs=[])
  21. Facts(facts_path()).record_state("venv", stamp, selected)
  22. def _check_record_selection(parser: argparse.ArgumentParser, args: argparse.Namespace, *,
  23. build: bool, requirements: list[str]) -> None:
  24. """Only a project build from an explicit --extra list has a selection to record."""
  25. if args.record_selection and (not build or requirements or args.manager_runtime
  26. or args.all_extras or args.groups or not args.extras):
  27. parser.error("--record-selection records the --extra list of a project environment build")
  28. def main(argv: Sequence[str] | None = None) -> int:
  29. import pm
  30. parser = argparse.ArgumentParser(description=__doc__)
  31. parser.add_argument("--source", type=Path)
  32. parser.add_argument("--out", type=Path)
  33. parser.add_argument("--python", type=Path)
  34. parser.add_argument("--cache", type=Path)
  35. parser.add_argument("--extra", dest="extras", action="append", default=[])
  36. parser.add_argument("--group", dest="groups", action="append", default=[])
  37. parser.add_argument("--all-extras", action="store_true")
  38. parser.add_argument("--no-install-project", action="store_true")
  39. parser.add_argument("--resolve", action="store_true", help="resolve the source lock before building")
  40. operation = parser.add_mutually_exclusive_group()
  41. operation.add_argument("--lock-only", action="store_true")
  42. operation.add_argument("--check-lock", action="store_true")
  43. operation.add_argument("--export-requirements", type=Path)
  44. operation.add_argument("--prune-cache", action="store_true")
  45. operation.add_argument("--exact-lock", action="store_true",
  46. help="prune to the project lock: entries the uv.lock cannot resolve are deleted")
  47. operation.add_argument("--manager-runtime", action="store_true")
  48. parser.add_argument("--lock-source", type=Path, default=None,
  49. help="repo whose uv.lock selects the kept entries for --exact-lock (default: cwd)")
  50. parser.add_argument("--upgrade", action="store_true")
  51. parser.add_argument("--ci", action="store_true")
  52. parser.add_argument("--sealed", action="store_true")
  53. parser.add_argument("--offline", action="store_true")
  54. parser.add_argument("--explicit", action="store_true", help="CLI builds are always explicit requests")
  55. parser.add_argument("--record-selection", action="store_true",
  56. help="record the --extra list as this install's baseline dependency selection")
  57. parser.add_argument("--wheelhouse", type=Path)
  58. parser.add_argument("--requirements", type=Path)
  59. parser.add_argument("--requirement", action="append", default=[])
  60. parser.add_argument("--lock", type=Path,
  61. help="uv.lock the requirements were exported from; its overrides apply")
  62. args = parser.parse_args(argv)
  63. requirements = list(args.requirement)
  64. if args.requirements is not None:
  65. requirements.extend(line.strip() for line in args.requirements.read_text(encoding="utf-8-sig").splitlines()
  66. if line.strip() and not line.lstrip().startswith("#"))
  67. if args.exact_lock:
  68. if args.ci or args.prune_cache:
  69. parser.error("--exact-lock replaces --ci/--prune-cache: downloaded wheels the lock keeps must survive")
  70. if args.lock_source is None:
  71. parser.error("--exact-lock requires --lock-source")
  72. if args.cache is None:
  73. parser.error("--exact-lock requires --cache")
  74. elif args.prune_cache:
  75. if args.cache is None:
  76. parser.error("--prune-cache requires --cache")
  77. elif not requirements and args.source is None:
  78. parser.error("--source is required for project operations")
  79. build = not (args.lock_only or args.check_lock or args.export_requirements
  80. or args.prune_cache or args.exact_lock)
  81. if build and args.out is None:
  82. parser.error("--out is required when building an environment")
  83. if args.manager_runtime and args.python is None:
  84. parser.error("--manager-runtime requires the target --python")
  85. _check_record_selection(parser, args, build=build, requirements=requirements)
  86. try:
  87. if args.exact_lock:
  88. from pm.uv_cache_prune import prune_uv_cache_to_lock
  89. pruned = prune_uv_cache_to_lock(args.cache, args.lock_source)
  90. print(f"pruned {pruned} cache entries outside the lock")
  91. return 0
  92. if args.prune_cache:
  93. pm.prune_cache(args.cache, ci=args.ci)
  94. return 0
  95. if args.check_lock:
  96. pm.check_project_lock(args.source, python=args.python, cache=args.cache,
  97. offline=args.offline, explicit=True)
  98. return 0
  99. if args.export_requirements:
  100. pm.export_requirements(args.source, args.export_requirements, extras=args.extras,
  101. python=args.python, cache=args.cache, explicit=True)
  102. return 0
  103. if args.lock_only:
  104. pm.lock_project(args.source, upgrade=args.upgrade, python=args.python,
  105. cache=args.cache, offline=args.offline, explicit=True)
  106. return 0
  107. if args.manager_runtime:
  108. executable = pm.stage_manager_runtime(python=args.python, destination=args.out,
  109. project=args.source, cache=args.cache,
  110. offline=args.offline, wheelhouse=args.wheelhouse)
  111. elif requirements:
  112. executable = pm.build_requirements_environment(
  113. requirements, out=args.out, python=args.python, cache=args.cache,
  114. wheelhouse=args.wheelhouse, offline=args.offline, sealed=args.sealed, explicit=True,
  115. lock=args.lock,
  116. )
  117. else:
  118. executable = pm.build_environment(
  119. source=args.source, out=args.out, python=args.python, cache=args.cache,
  120. extras=args.extras, groups=args.groups, all_extras=args.all_extras,
  121. no_install_project=args.no_install_project, frozen=not args.resolve,
  122. sealed=args.sealed, offline=args.offline, explicit=True,
  123. )
  124. if args.record_selection:
  125. _record_selection(args.source, args.extras)
  126. except (pm.InstallError, OSError, ValueError) as exc:
  127. print(f"python environment: {exc}", file=sys.stderr)
  128. return 1
  129. print(executable)
  130. return 0
  131. if __name__ == "__main__":
  132. raise SystemExit(main())