legacy.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. import logging
  4. from pip._internal.build_env import BuildEnvironment
  5. from pip._internal.distributions.base import AbstractDistribution
  6. from pip._internal.exceptions import InstallationError
  7. from pip._internal.utils.subprocess import runner_with_spinner_message
  8. logger = logging.getLogger(__name__)
  9. class SourceDistribution(AbstractDistribution):
  10. """Represents a source distribution.
  11. The preparation step for these needs metadata for the packages to be
  12. generated, either using PEP 517 or using the legacy `setup.py egg_info`.
  13. NOTE from @pradyunsg (14 June 2019)
  14. I expect SourceDistribution class will need to be split into
  15. `legacy_source` (setup.py based) and `source` (PEP 517 based) when we start
  16. bringing logic for preparation out of InstallRequirement into this class.
  17. """
  18. def get_pkg_resources_distribution(self):
  19. return self.req.get_dist()
  20. def prepare_distribution_metadata(self, finder, build_isolation):
  21. # Prepare for building. We need to:
  22. # 1. Load pyproject.toml (if it exists)
  23. # 2. Set up the build environment
  24. self.req.load_pyproject_toml()
  25. should_isolate = self.req.use_pep517 and build_isolation
  26. if should_isolate:
  27. self._setup_isolation(finder)
  28. self.req.prepare_metadata()
  29. self.req.assert_source_matches_version()
  30. def _setup_isolation(self, finder):
  31. def _raise_conflicts(conflicting_with, conflicting_reqs):
  32. format_string = (
  33. "Some build dependencies for {requirement} "
  34. "conflict with {conflicting_with}: {description}."
  35. )
  36. error_message = format_string.format(
  37. requirement=self.req,
  38. conflicting_with=conflicting_with,
  39. description=', '.join(
  40. '%s is incompatible with %s' % (installed, wanted)
  41. for installed, wanted in sorted(conflicting)
  42. )
  43. )
  44. raise InstallationError(error_message)
  45. # Isolate in a BuildEnvironment and install the build-time
  46. # requirements.
  47. self.req.build_env = BuildEnvironment()
  48. self.req.build_env.install_requirements(
  49. finder, self.req.pyproject_requires, 'overlay',
  50. "Installing build dependencies"
  51. )
  52. conflicting, missing = self.req.build_env.check_requirements(
  53. self.req.requirements_to_check
  54. )
  55. if conflicting:
  56. _raise_conflicts("PEP 517/518 supported requirements",
  57. conflicting)
  58. if missing:
  59. logger.warning(
  60. "Missing build requirements in pyproject.toml for %s.",
  61. self.req,
  62. )
  63. logger.warning(
  64. "The project does not specify a build backend, and "
  65. "pip cannot fall back to setuptools without %s.",
  66. " and ".join(map(repr, sorted(missing)))
  67. )
  68. # Install any extra build dependencies that the backend requests.
  69. # This must be done in a second pass, as the pyproject.toml
  70. # dependencies must be installed before we can call the backend.
  71. with self.req.build_env:
  72. runner = runner_with_spinner_message(
  73. "Getting requirements to build wheel"
  74. )
  75. backend = self.req.pep517_backend
  76. with backend.subprocess_runner(runner):
  77. reqs = backend.get_requires_for_build_wheel()
  78. conflicting, missing = self.req.build_env.check_requirements(reqs)
  79. if conflicting:
  80. _raise_conflicts("the backend dependencies", conflicting)
  81. self.req.build_env.install_requirements(
  82. finder, missing, 'normal',
  83. "Installing backend dependencies"
  84. )