-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathbuilder.py
377 lines (292 loc) · 12.5 KB
/
builder.py
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from __future__ import annotations
import logging
import sys
import textwrap
from functools import cached_property
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from poetry.core.masonry.utils.module import Module
from poetry.core.poetry import Poetry
METADATA_BASE = """\
Metadata-Version: 2.3
Name: {name}
Version: {version}
Summary: {summary}
"""
logger = logging.getLogger(__name__)
class Builder:
format: str | None = None
def __init__(self, poetry: Poetry, executable: Path | None = None) -> None:
from poetry.core.masonry.metadata import Metadata
if not poetry.is_package_mode:
raise RuntimeError(
"Building a package is not possible in non-package mode."
)
self._poetry = poetry
self._package = poetry.package
self._path: Path = poetry.pyproject_path.parent
self._excluded_files: set[str] | None = None
self._executable = Path(executable or sys.executable)
self._meta = Metadata.from_package(self._package)
@cached_property
def _module(self) -> Module:
from poetry.core.masonry.utils.module import Module
packages: list[dict[str, str | dict[str, str]]] = []
includes: list[dict[str, str | dict[str, str]]] = []
for source_list, target_list, default in [
(self._package.packages, packages, ["sdist", "wheel"]),
(self._package.include, includes, ["sdist"]),
]:
for item in source_list:
# Default to including in both sdist & wheel
# if the `format` key is not provided.
formats = item.get("format", default)
if not isinstance(formats, list):
formats = [formats]
if self.format and self.format not in formats:
continue
target_list.append({**item, "format": formats})
return Module(
self._package.name,
self._path.as_posix(),
packages=packages,
includes=includes,
)
@property
def executable(self) -> Path:
return self._executable
@property
def default_target_dir(self) -> Path:
return self._path / "dist"
def build(self, target_dir: Path | None) -> Path:
raise NotImplementedError
def find_excluded_files(self, fmt: str | None = None) -> set[str]:
if self._excluded_files is None:
from poetry.core.vcs import get_vcs
# Checking VCS
vcs = get_vcs(self._path)
vcs_ignored_files = set(vcs.get_ignored_files()) if vcs else set()
explicitly_excluded = set()
for excluded_glob in self._package.exclude:
for excluded in self._path.glob(str(excluded_glob)):
explicitly_excluded.add(
Path(excluded).relative_to(self._path).as_posix()
)
explicitly_included = set()
for inc in self._module.explicit_includes:
if fmt and fmt not in inc.formats:
continue
for included in inc.elements:
explicitly_included.add(included.relative_to(self._path).as_posix())
ignored = (vcs_ignored_files | explicitly_excluded) - explicitly_included
for ignored_file in ignored:
logger.debug(f"Ignoring: {ignored_file}")
self._excluded_files = ignored
return self._excluded_files
def is_excluded(self, filepath: str | Path) -> bool:
exclude_path = Path(filepath)
while True:
if exclude_path.as_posix() in self.find_excluded_files(fmt=self.format):
return True
if len(exclude_path.parts) > 1:
exclude_path = exclude_path.parent
else:
break
return False
def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]:
"""
Finds all files to add to the tarball
"""
from poetry.core.masonry.utils.package_include import PackageInclude
to_add = set()
for include in self._module.includes:
include.refresh()
formats = include.formats
for file in include.elements:
if "__pycache__" in str(file):
continue
if (
isinstance(include, PackageInclude)
and include.source
and self.format == "wheel"
):
source_root = include.base
else:
source_root = self._path
if (
isinstance(include, PackageInclude)
and include.target
and self.format == "wheel"
):
target_dir = include.target
else:
target_dir = None
if file.is_dir():
if self.format in formats:
for current_file in file.glob("**/*"):
include_file = BuildIncludeFile(
path=current_file,
project_root=self._path,
source_root=source_root,
target_dir=target_dir,
)
if not (
current_file.is_dir()
or self.is_excluded(
include_file.relative_to_project_root()
)
):
to_add.add(include_file)
continue
include_file = BuildIncludeFile(
path=file,
project_root=self._path,
source_root=source_root,
target_dir=target_dir,
)
if self.is_excluded(
include_file.relative_to_project_root()
) and isinstance(include, PackageInclude):
continue
if file.suffix == ".pyc":
continue
logger.debug(f"Adding: {file}")
to_add.add(include_file)
# add build script if it is specified and explicitly required
if self._package.build_script and not exclude_build:
to_add.add(
BuildIncludeFile(
path=self._package.build_script,
project_root=self._path,
source_root=self._path,
)
)
return to_add
def get_metadata_content(self) -> str:
content = METADATA_BASE.format(
name=self._meta.name,
version=self._meta.version,
summary=str(self._meta.summary),
)
# Optional fields
if self._meta.home_page:
content += f"Home-page: {self._meta.home_page}\n"
if self._meta.license:
license_field = "License: "
# Indentation is not only for readability, but required
# so that the line break is not treated as end of field.
# The exact indentation does not matter,
# but it is essential to also indent empty lines.
escaped_license = textwrap.indent(
self._meta.license, " " * len(license_field), lambda line: True
).strip()
content += f"{license_field}{escaped_license}\n"
if self._meta.keywords:
content += f"Keywords: {self._meta.keywords}\n"
if self._meta.author:
content += f"Author: {self._meta.author}\n"
if self._meta.author_email:
content += f"Author-email: {self._meta.author_email}\n"
if self._meta.maintainer:
content += f"Maintainer: {self._meta.maintainer}\n"
if self._meta.maintainer_email:
content += f"Maintainer-email: {self._meta.maintainer_email}\n"
if self._meta.requires_python:
content += f"Requires-Python: {self._meta.requires_python}\n"
for classifier in self._meta.classifiers:
content += f"Classifier: {classifier}\n"
for extra in sorted(self._meta.provides_extra):
content += f"Provides-Extra: {extra}\n"
for dep in sorted(self._meta.requires_dist):
content += f"Requires-Dist: {dep}\n"
for url in sorted(self._meta.project_urls, key=lambda u: u[0]):
content += f"Project-URL: {url}\n"
if self._meta.description_content_type:
content += (
f"Description-Content-Type: {self._meta.description_content_type}\n"
)
if self._meta.description is not None:
content += f"\n{self._meta.description}\n"
return content
def convert_entry_points(self) -> dict[str, list[str]]:
result: dict[str, list[str]] = {}
for group_name, group in self._poetry.package.entry_points.items():
if group_name == "console-scripts":
group_name = "console_scripts"
elif group_name == "gui-scripts":
group_name = "gui_scripts"
result[group_name] = sorted(
f"{name} = {specification}" for name, specification in group.items()
)
return result
def convert_script_files(self) -> list[Path]:
script_files: list[Path] = []
for name, specification in self._poetry.local_config.get("scripts", {}).items():
if isinstance(specification, dict) and specification.get("type") == "file":
source = specification["reference"]
if Path(source).is_absolute():
raise RuntimeError(
f"{source} in {name} is an absolute path. Expected relative"
" path."
)
abs_path = Path.joinpath(self._path, source)
if not self._package.build_script:
# scripts can be generated by build_script, in this case they do not exist here
if not abs_path.exists():
raise RuntimeError(
f"{abs_path} in script specification ({name}) is not found."
)
if not abs_path.is_file():
raise RuntimeError(
f"{abs_path} in script specification ({name}) is not a file."
)
script_files.append(abs_path)
return script_files
def _get_legal_files(self) -> set[Path]:
include_files_patterns = {"COPYING*", "LICEN[SC]E*", "AUTHORS*", "NOTICE*"}
files: set[Path] = set()
for pattern in include_files_patterns:
files.update(self._path.glob(pattern))
files.update(self._path.joinpath("LICENSES").glob("**/*"))
return files
class BuildIncludeFile:
def __init__(
self,
path: Path | str,
project_root: Path | str,
source_root: Path | str,
target_dir: Path | str | None = None,
) -> None:
"""
:param project_root: the full path of the project's root
:param path: a full path to the file to be included
:param source_root: the full root path to resolve to
:param target_dir: the relative target root to resolve to
"""
self.path = Path(path)
self.project_root = Path(project_root).resolve()
self.source_root = Path(source_root).resolve()
self.target_dir = None if not target_dir else Path(target_dir)
if not self.path.is_absolute():
self.path = self.source_root / self.path
else:
self.path = self.path
self.path = self.path.resolve()
def __eq__(self, other: object) -> bool:
if not isinstance(other, BuildIncludeFile):
return False
return self.path == other.path
def __hash__(self) -> int:
return hash(self.path)
def __repr__(self) -> str:
return str(self.path)
def relative_to_project_root(self) -> Path:
return self.path.relative_to(self.project_root)
def relative_to_source_root(self) -> Path:
return self.path.relative_to(self.source_root)
def relative_to_target_root(self) -> Path:
path = self.relative_to_source_root()
if self.target_dir is not None:
return self.target_dir / path
return path