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