sdist.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from __future__ import annotations
  2. import logging
  3. from collections.abc import Iterable
  4. from typing import TYPE_CHECKING
  5. from pip._internal.build_env import BuildEnvironment
  6. from pip._internal.distributions.base import AbstractDistribution
  7. from pip._internal.exceptions import InstallationError
  8. from pip._internal.metadata import BaseDistribution
  9. from pip._internal.utils.subprocess import runner_with_spinner_message
  10. if TYPE_CHECKING:
  11. from pip._internal.build_env import BuildEnvironmentInstaller
  12. logger = logging.getLogger(__name__)
  13. class SourceDistribution(AbstractDistribution):
  14. """Represents a source distribution.
  15. The preparation step for these needs metadata for the packages to be
  16. generated.
  17. """
  18. @property
  19. def build_tracker_id(self) -> str | None:
  20. """Identify this requirement uniquely by its link."""
  21. assert self.req.link
  22. return self.req.link.url_without_fragment
  23. def get_metadata_distribution(self) -> BaseDistribution:
  24. return self.req.get_dist()
  25. def prepare_distribution_metadata(
  26. self,
  27. build_env_installer: BuildEnvironmentInstaller,
  28. build_isolation: bool,
  29. check_build_deps: bool,
  30. ) -> None:
  31. # Load pyproject.toml
  32. self.req.load_pyproject_toml()
  33. # Set up the build isolation, if this requirement should be isolated
  34. if build_isolation:
  35. # Setup an isolated environment and install the build backend static
  36. # requirements in it.
  37. self._prepare_build_backend(build_env_installer)
  38. # Check that the build backend supports PEP 660. This cannot be done
  39. # earlier because we need to setup the build backend to verify it
  40. # supports build_editable, nor can it be done later, because we want
  41. # to avoid installing build requirements needlessly.
  42. self.req.editable_sanity_check()
  43. # Install the dynamic build requirements.
  44. self._install_build_reqs(build_env_installer)
  45. else:
  46. # When not using build isolation, we still need to check that
  47. # the build backend supports PEP 660.
  48. self.req.editable_sanity_check()
  49. # Check if the current environment provides build dependencies
  50. if check_build_deps:
  51. pyproject_requires = self.req.pyproject_requires
  52. assert pyproject_requires is not None
  53. conflicting, missing = self.req.build_env.check_requirements(
  54. pyproject_requires
  55. )
  56. if conflicting:
  57. self._raise_conflicts("the backend dependencies", conflicting)
  58. if missing:
  59. self._raise_missing_reqs(missing)
  60. self.req.prepare_metadata()
  61. def _prepare_build_backend(
  62. self, build_env_installer: BuildEnvironmentInstaller
  63. ) -> None:
  64. # Isolate in a BuildEnvironment and install the build-time
  65. # requirements.
  66. pyproject_requires = self.req.pyproject_requires
  67. assert pyproject_requires is not None
  68. self.req.build_env = BuildEnvironment(build_env_installer)
  69. self.req.build_env.install_requirements(
  70. pyproject_requires, "overlay", kind="build dependencies", for_req=self.req
  71. )
  72. conflicting, missing = self.req.build_env.check_requirements(
  73. self.req.requirements_to_check
  74. )
  75. if conflicting:
  76. self._raise_conflicts("PEP 517/518 supported requirements", conflicting)
  77. if missing:
  78. logger.warning(
  79. "Missing build requirements in pyproject.toml for %s.",
  80. self.req,
  81. )
  82. logger.warning(
  83. "The project does not specify a build backend, and "
  84. "pip cannot fall back to setuptools without %s.",
  85. " and ".join(map(repr, sorted(missing))),
  86. )
  87. def _get_build_requires_wheel(self) -> Iterable[str]:
  88. with self.req.build_env:
  89. runner = runner_with_spinner_message("Getting requirements to build wheel")
  90. backend = self.req.pep517_backend
  91. assert backend is not None
  92. with backend.subprocess_runner(runner):
  93. return backend.get_requires_for_build_wheel()
  94. def _get_build_requires_editable(self) -> Iterable[str]:
  95. with self.req.build_env:
  96. runner = runner_with_spinner_message(
  97. "Getting requirements to build editable"
  98. )
  99. backend = self.req.pep517_backend
  100. assert backend is not None
  101. with backend.subprocess_runner(runner):
  102. return backend.get_requires_for_build_editable()
  103. def _install_build_reqs(
  104. self, build_env_installer: BuildEnvironmentInstaller
  105. ) -> None:
  106. # Install any extra build dependencies that the backend requests.
  107. # This must be done in a second pass, as the pyproject.toml
  108. # dependencies must be installed before we can call the backend.
  109. if (
  110. self.req.editable
  111. and self.req.permit_editable_wheels
  112. and self.req.supports_pyproject_editable
  113. ):
  114. build_reqs = self._get_build_requires_editable()
  115. else:
  116. build_reqs = self._get_build_requires_wheel()
  117. conflicting, missing = self.req.build_env.check_requirements(build_reqs)
  118. if conflicting:
  119. self._raise_conflicts("the backend dependencies", conflicting)
  120. self.req.build_env.install_requirements(
  121. missing, "normal", kind="backend dependencies", for_req=self.req
  122. )
  123. def _raise_conflicts(
  124. self, conflicting_with: str, conflicting_reqs: set[tuple[str, str]]
  125. ) -> None:
  126. format_string = (
  127. "Some build dependencies for {requirement} "
  128. "conflict with {conflicting_with}: {description}."
  129. )
  130. error_message = format_string.format(
  131. requirement=self.req,
  132. conflicting_with=conflicting_with,
  133. description=", ".join(
  134. f"{installed} is incompatible with {wanted}"
  135. for installed, wanted in sorted(conflicting_reqs)
  136. ),
  137. )
  138. raise InstallationError(error_message)
  139. def _raise_missing_reqs(self, missing: set[str]) -> None:
  140. format_string = (
  141. "Some build dependencies for {requirement} are missing: {missing}."
  142. )
  143. error_message = format_string.format(
  144. requirement=self.req, missing=", ".join(map(repr, sorted(missing)))
  145. )
  146. raise InstallationError(error_message)