sdist.py 6.3 KB

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