pyproject.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import os
  2. from collections import namedtuple
  3. from typing import Any, List, Optional
  4. from pip._vendor import tomli
  5. from pip._vendor.packaging.requirements import InvalidRequirement, Requirement
  6. from pip._internal.exceptions import (
  7. InstallationError,
  8. InvalidPyProjectBuildRequires,
  9. MissingPyProjectBuildRequires,
  10. )
  11. def _is_list_of_str(obj: Any) -> bool:
  12. return isinstance(obj, list) and all(isinstance(item, str) for item in obj)
  13. def make_pyproject_path(unpacked_source_directory: str) -> str:
  14. return os.path.join(unpacked_source_directory, "pyproject.toml")
  15. BuildSystemDetails = namedtuple(
  16. "BuildSystemDetails", ["requires", "backend", "check", "backend_path"]
  17. )
  18. def load_pyproject_toml(
  19. use_pep517: Optional[bool], pyproject_toml: str, setup_py: str, req_name: str
  20. ) -> Optional[BuildSystemDetails]:
  21. """Load the pyproject.toml file.
  22. Parameters:
  23. use_pep517 - Has the user requested PEP 517 processing? None
  24. means the user hasn't explicitly specified.
  25. pyproject_toml - Location of the project's pyproject.toml file
  26. setup_py - Location of the project's setup.py file
  27. req_name - The name of the requirement we're processing (for
  28. error reporting)
  29. Returns:
  30. None if we should use the legacy code path, otherwise a tuple
  31. (
  32. requirements from pyproject.toml,
  33. name of PEP 517 backend,
  34. requirements we should check are installed after setting
  35. up the build environment
  36. directory paths to import the backend from (backend-path),
  37. relative to the project root.
  38. )
  39. """
  40. has_pyproject = os.path.isfile(pyproject_toml)
  41. has_setup = os.path.isfile(setup_py)
  42. if not has_pyproject and not has_setup:
  43. raise InstallationError(
  44. f"{req_name} does not appear to be a Python project: "
  45. f"neither 'setup.py' nor 'pyproject.toml' found."
  46. )
  47. if has_pyproject:
  48. with open(pyproject_toml, encoding="utf-8") as f:
  49. pp_toml = tomli.loads(f.read())
  50. build_system = pp_toml.get("build-system")
  51. else:
  52. build_system = None
  53. # The following cases must use PEP 517
  54. # We check for use_pep517 being non-None and falsey because that means
  55. # the user explicitly requested --no-use-pep517. The value 0 as
  56. # opposed to False can occur when the value is provided via an
  57. # environment variable or config file option (due to the quirk of
  58. # strtobool() returning an integer in pip's configuration code).
  59. if has_pyproject and not has_setup:
  60. if use_pep517 is not None and not use_pep517:
  61. raise InstallationError(
  62. "Disabling PEP 517 processing is invalid: "
  63. "project does not have a setup.py"
  64. )
  65. use_pep517 = True
  66. elif build_system and "build-backend" in build_system:
  67. if use_pep517 is not None and not use_pep517:
  68. raise InstallationError(
  69. "Disabling PEP 517 processing is invalid: "
  70. "project specifies a build backend of {} "
  71. "in pyproject.toml".format(build_system["build-backend"])
  72. )
  73. use_pep517 = True
  74. # If we haven't worked out whether to use PEP 517 yet,
  75. # and the user hasn't explicitly stated a preference,
  76. # we do so if the project has a pyproject.toml file.
  77. elif use_pep517 is None:
  78. use_pep517 = has_pyproject
  79. # At this point, we know whether we're going to use PEP 517.
  80. assert use_pep517 is not None
  81. # If we're using the legacy code path, there is nothing further
  82. # for us to do here.
  83. if not use_pep517:
  84. return None
  85. if build_system is None:
  86. # Either the user has a pyproject.toml with no build-system
  87. # section, or the user has no pyproject.toml, but has opted in
  88. # explicitly via --use-pep517.
  89. # In the absence of any explicit backend specification, we
  90. # assume the setuptools backend that most closely emulates the
  91. # traditional direct setup.py execution, and require wheel and
  92. # a version of setuptools that supports that backend.
  93. build_system = {
  94. "requires": ["setuptools>=40.8.0", "wheel"],
  95. "build-backend": "setuptools.build_meta:__legacy__",
  96. }
  97. # If we're using PEP 517, we have build system information (either
  98. # from pyproject.toml, or defaulted by the code above).
  99. # Note that at this point, we do not know if the user has actually
  100. # specified a backend, though.
  101. assert build_system is not None
  102. # Ensure that the build-system section in pyproject.toml conforms
  103. # to PEP 518.
  104. # Specifying the build-system table but not the requires key is invalid
  105. if "requires" not in build_system:
  106. raise MissingPyProjectBuildRequires(package=req_name)
  107. # Error out if requires is not a list of strings
  108. requires = build_system["requires"]
  109. if not _is_list_of_str(requires):
  110. raise InvalidPyProjectBuildRequires(
  111. package=req_name,
  112. reason="It is not a list of strings.",
  113. )
  114. # Each requirement must be valid as per PEP 508
  115. for requirement in requires:
  116. try:
  117. Requirement(requirement)
  118. except InvalidRequirement as error:
  119. raise InvalidPyProjectBuildRequires(
  120. package=req_name,
  121. reason=f"It contains an invalid requirement: {requirement!r}",
  122. ) from error
  123. backend = build_system.get("build-backend")
  124. backend_path = build_system.get("backend-path", [])
  125. check: List[str] = []
  126. if backend is None:
  127. # If the user didn't specify a backend, we assume they want to use
  128. # the setuptools backend. But we can't be sure they have included
  129. # a version of setuptools which supplies the backend, or wheel
  130. # (which is needed by the backend) in their requirements. So we
  131. # make a note to check that those requirements are present once
  132. # we have set up the environment.
  133. # This is quite a lot of work to check for a very specific case. But
  134. # the problem is, that case is potentially quite common - projects that
  135. # adopted PEP 518 early for the ability to specify requirements to
  136. # execute setup.py, but never considered needing to mention the build
  137. # tools themselves. The original PEP 518 code had a similar check (but
  138. # implemented in a different way).
  139. backend = "setuptools.build_meta:__legacy__"
  140. check = ["setuptools>=40.8.0", "wheel"]
  141. return BuildSystemDetails(requires, backend, check, backend_path)