-
-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy path_pyproject_toml.py
More file actions
339 lines (277 loc) · 9.3 KB
/
_pyproject_toml.py
File metadata and controls
339 lines (277 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# This module should be replaced with the equivalent functionality
# in the PyPI "packaging" package (once it's added there).
__all__ = [
"load_pyproject_toml",
"parse_classifier",
"parse_entry_point",
"parse_person",
"parse_pyproject_toml",
]
import os.path
import re
import urllib.parse
import packaging.requirements
import packaging.specifiers
import packaging.utils
import packaging.version
try:
import tomllib # type: ignore[import-not-found] # tomllib doesn't exist on 3.7-3.10
except ImportError:
import tomli as tomllib
from ._utils import check_name
NAME_RE = re.compile("^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", re.IGNORECASE)
def parse_person(text):
# XXX
return text
def parse_classifier(text):
# XXX Use https://pypi.org/project/packaging-classifiers.
return text
def parse_entry_point(text):
# See:
# * https://packaging.python.org/specifications/entry-points/#data-model
# * https://www.python.org/dev/peps/pep-0517/#source-trees
module, sep, qualname = text.partition(":")
if all(p.isidentifier() for p in module.split(".")):
if not sep or all(p.isidentifier() for p in qualname.split(".")):
return module, qualname
raise ValueError(f"invalid entry point {text!r}")
def parse_pyproject_toml(
text,
rootdir,
name=None,
*,
tools=None,
requirefiles=True,
):
data = tomllib.loads(text)
unused = list(data)
for section, normalize in SECTIONS.items():
try:
secdata = data[section]
except KeyError:
data[section] = None
else:
data[section] = normalize(
secdata,
name=name,
tools=tools,
rootdir=rootdir,
requirefiles=requirefiles,
)
unused.remove(section)
if unused:
raise ValueError(f"unsupported sections ({', '.join(sorted(unused))})")
return data
def load_pyproject_toml(filename, *, name=None, tools=None, requirefiles=True):
if os.path.isdir(filename):
rootdir = filename
filename = os.path.join(rootdir, "pyproject.toml")
else:
rootdir = os.path.dirname(filename)
with open(filename, encoding="utf-8") as infile:
text = infile.read()
data = parse_pyproject_toml(
text,
rootdir,
name,
tools=tools,
requirefiles=requirefiles,
)
return data, filename
#######################################
# internal implementation
def _check_relfile(relname, rootdir, kind):
if os.path.isabs(relname):
raise ValueError(f"{relname!r} is absolute, expected relative")
actual = os.path.join(rootdir, relname)
if kind == "dir":
if not os.path.isdir(actual):
raise ValueError(f"directory {actual!r} does not exist")
elif kind == "file":
if not os.path.isfile(actual):
raise ValueError(f"file {actual!r} does not exist")
elif kind == "any":
if not os.path.exists(actual):
raise ValueError(f"{actual!r} does not exist")
elif kind:
raise NotImplementedError(kind)
def _check_file_or_text(table, rootdir, requirefiles, extra=None):
unsupported = set(table) - set(["file", "text"]) - set(extra or ())
if unsupported:
raise ValueError(f"unsupported license data {table!r}")
if "file" in table:
if "text" in table:
raise ValueError('"file" and "text" are mutually exclusive')
kind = "file" if requirefiles else None
_check_relfile(table["file"], rootdir, kind)
def _normalize_project(data, rootdir, name, requirefiles, **_ignored):
# See PEP 621.
unused = set(data)
##########
# First handle the required fields.
name = data.get("name", name)
if name:
if not NAME_RE.match(name):
raise ValueError(f"invalid name {name!r}")
name = packaging.utils.canonicalize_name(name)
data["name"] = name
if "name" in unused:
unused.remove("name")
else:
if "name" not in data.get("dynamic", []):
raise ValueError('missing required "name" field')
try:
version = data["version"]
except KeyError:
if "version" not in data.get("dynamic", []):
raise ValueError('missing required "version" field')
else:
# We keep the full version string rather than
# the canonicalized form. However, we still validate and
# (effectively) normalize it.
version = packaging.version.parse(version)
data["version"] = str(version)
unused.remove("version")
##########
# Now we handle the optional fields.
# We leave "description" as-is.
key = "readme"
if key in data:
readme = data[key]
if isinstance(readme, str):
readme = data[key] = {"file": readme}
# XXX Check the suffix.
# XXX Handle 'content-type'.
# XXX Handle "charset" parameter.
_check_file_or_text(
data[key], rootdir, requirefiles, ["content-type", "charset"]
)
unused.remove(key)
key = "requires-python"
if key in data:
# We keep it as a string.
data[key] = str(packaging.specifiers.SpecifierSet(data[key]))
unused.remove(key)
key = "license"
if key in data:
_check_file_or_text(data[key], rootdir, requirefiles)
unused.remove(key)
key = "keywords"
if key in data:
for keyword in data[key]:
# XXX Is this the right check?
check_name(name, loose=True)
unused.remove(key)
key = "authors"
if key in data:
for person in data[key]:
# We only make sure it is valid.
parse_person(person)
unused.remove(key)
key = "maintainers"
if key in data:
for person in data[key]:
# We only make sure it is valid.
parse_person(person)
unused.remove(key)
key = "classifiers"
if key in data:
for classifier in data[key]:
# We only make sure it is valid.
parse_classifier(classifier)
unused.remove(key)
key = "dependencies"
if key in data:
for dep in data[key]:
# We only make sure it is valid.
packaging.requirements.Requirement(dep)
unused.remove(key)
key = "optional-dependencies"
if key in data:
# XXX
unused.remove(key)
key = "urls"
if key in data:
for name, url in data[key].items():
# XXX Is there a stricter check?
check_name(name, loose=True)
# We only make sure it is valid.
urllib.parse.urlparse(url)
unused.remove(key)
key = "scripts"
if key in data:
for name, value in data[key].items():
# XXX Is there a stricter check?
check_name(name, loose=True)
# We only make sure it is valid.
parse_entry_point(value)
unused.remove(key)
key = "gui-scripts"
if key in data:
for _, value in data[key].items():
# XXX Is there a stricter check?
check_name(name, loose=True)
# We only make sure it is valid.
parse_entry_point(value)
unused.remove(key)
key = "entry-points"
if key in data:
for groupname, group in data[key].items():
# XXX Is there a stricter check?
check_name(groupname, loose=True)
for epname, value in group.items():
# XXX Is there a stricter check?
check_name(epname, loose=True)
# We only make sure it is valid.
parse_entry_point(value)
unused.remove(key)
key = "dynamic"
if key in data:
for field in data[key]:
check_name(field, loose=True)
# XXX Fail it isn't one of the supported fields.
unused.remove(key)
return data
def _normalize_build_system(data, rootdir, requirefiles, **_ignored):
# See PEP 518 and 517.
unused = set(data)
key = "requires"
if key in data:
reqs = data[key]
for i, raw in enumerate(reqs):
# We only make sure it is valid.
packaging.requirements.Requirement(raw)
unused.remove(key)
else:
raise ValueError('missing "requires" field')
key = "build-backend"
if key in data:
# We only make sure it is valid.
parse_entry_point(data[key])
unused.remove(key)
key = "backend-path"
if key in data:
if "build-backend" not in data:
raise ValueError('missing "build-backend" field')
kind = "dir" if requirefiles else None
for dirname in data[key]:
_check_relfile(dirname, rootdir, kind=kind)
unused.remove(key)
if unused:
raise ValueError(f"unsupported keys ({', '.join(sorted(unused))})")
return data
def _normalize_tool(data, tools, rootdir, **_ignored):
# See PEP 518.
tools = tools or {}
for name, tooldata in list(data.items()):
if name in tools:
normalize = tools[name]
data[name] = normalize(name, tooldata, rootdir=rootdir)
if data[name] is None:
del data[name]
return data
SECTIONS = {
"project": _normalize_project,
"build-system": _normalize_build_system,
"tool": _normalize_tool,
}