You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pyproject.py 6.3KB

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