comparison planemo/lib/python3.7/site-packages/pip/_internal/pyproject.py @ 1:56ad4e20f292 draft

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