pyproject.py 7.0 KB

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