-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathfactory.py
450 lines (371 loc) · 15.7 KB
/
factory.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
from __future__ import annotations
import logging
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Dict
from typing import List
from typing import Mapping
from typing import Union
from typing import cast
from warnings import warn
from poetry.core.utils.helpers import combine_unicode
from poetry.core.utils.helpers import readme_content_type
if TYPE_CHECKING:
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import DependencyGroup
from poetry.core.packages.project_package import ProjectPackage
from poetry.core.poetry import Poetry
from poetry.core.spdx.license import License
DependencyConstraint = Union[str, Dict[str, Any]]
DependencyConfig = Mapping[
str, Union[List[DependencyConstraint], DependencyConstraint]
]
logger = logging.getLogger(__name__)
class Factory:
"""
Factory class to create various elements needed by Poetry.
"""
def create_poetry(
self, cwd: Path | None = None, with_groups: bool = True
) -> Poetry:
from poetry.core.poetry import Poetry
from poetry.core.pyproject.toml import PyProjectTOML
poetry_file = self.locate(cwd)
local_config = PyProjectTOML(path=poetry_file).poetry_config
# Checking validity
check_result = self.validate(local_config)
if check_result["errors"]:
message = ""
for error in check_result["errors"]:
message += f" - {error}\n"
raise RuntimeError("The Poetry configuration is invalid:\n" + message)
# Load package
name = cast(str, local_config["name"])
version = cast(str, local_config["version"])
package = self.get_package(name, version)
package = self.configure_package(
package, local_config, poetry_file.parent, with_groups=with_groups
)
return Poetry(poetry_file, local_config, package)
@classmethod
def get_package(cls, name: str, version: str) -> ProjectPackage:
from poetry.core.packages.project_package import ProjectPackage
return ProjectPackage(name, version, version)
@classmethod
def _add_package_group_dependencies(
cls,
package: ProjectPackage,
group: str | DependencyGroup,
dependencies: DependencyConfig,
) -> None:
from poetry.core.packages.dependency_group import MAIN_GROUP
if isinstance(group, str):
if package.has_dependency_group(group):
group = package.dependency_group(group)
else:
from poetry.core.packages.dependency_group import DependencyGroup
group = DependencyGroup(group)
for name, constraints in dependencies.items():
_constraints = (
constraints if isinstance(constraints, list) else [constraints]
)
for _constraint in _constraints:
if name.lower() == "python":
if group.name == MAIN_GROUP and isinstance(_constraint, str):
package.python_versions = _constraint
continue
group.add_dependency(
cls.create_dependency(
name,
_constraint,
groups=[group.name],
root_dir=package.root_dir,
)
)
package.add_dependency_group(group)
@classmethod
def configure_package(
cls,
package: ProjectPackage,
config: dict[str, Any],
root: Path,
with_groups: bool = True,
) -> ProjectPackage:
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.dependency_group import DependencyGroup
from poetry.core.spdx.helpers import license_by_id
package.root_dir = root
for author in config["authors"]:
package.authors.append(combine_unicode(author))
for maintainer in config.get("maintainers", []):
package.maintainers.append(combine_unicode(maintainer))
package.description = config.get("description", "")
package.homepage = config.get("homepage")
package.repository_url = config.get("repository")
package.documentation_url = config.get("documentation")
try:
license_: License | None = license_by_id(config.get("license", ""))
except ValueError:
license_ = None
package.license = license_
package.keywords = config.get("keywords", [])
package.classifiers = config.get("classifiers", [])
if "readme" in config:
if isinstance(config["readme"], str):
package.readmes = (root / config["readme"],)
else:
package.readmes = tuple(root / readme for readme in config["readme"])
if "platform" in config:
package.platform = config["platform"]
if "dependencies" in config:
cls._add_package_group_dependencies(
package=package, group=MAIN_GROUP, dependencies=config["dependencies"]
)
if with_groups and "group" in config:
for group_name, group_config in config["group"].items():
group = DependencyGroup(
group_name, optional=group_config.get("optional", False)
)
cls._add_package_group_dependencies(
package=package,
group=group,
dependencies=group_config["dependencies"],
)
if with_groups and "dev-dependencies" in config:
cls._add_package_group_dependencies(
package=package, group="dev", dependencies=config["dev-dependencies"]
)
extras = config.get("extras", {})
for extra_name, requirements in extras.items():
package.extras[extra_name] = []
# Checking for dependency
for req in requirements:
req = Dependency(req, "*")
for dep in package.requires:
if dep.name == req.name:
dep.in_extras.append(extra_name)
package.extras[extra_name].append(dep)
break
if "build" in config:
build = config["build"]
if not isinstance(build, dict):
build = {"script": build}
package.build_config = build or {}
if "include" in config:
package.include = []
for include in config["include"]:
if not isinstance(include, dict):
include = {"path": include}
formats = include.get("format", [])
if formats and not isinstance(formats, list):
formats = [formats]
include["format"] = formats
package.include.append(include)
if "exclude" in config:
package.exclude = config["exclude"]
if "packages" in config:
package.packages = config["packages"]
# Custom urls
if "urls" in config:
package.custom_urls = config["urls"]
return package
@classmethod
def create_dependency(
cls,
name: str,
constraint: DependencyConstraint,
groups: list[str] | None = None,
root_dir: Path | None = None,
) -> Dependency:
from poetry.core.packages.constraints import (
parse_constraint as parse_generic_constraint,
)
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.packages.url_dependency import URLDependency
from poetry.core.packages.utils.utils import create_nested_marker
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.semver.helpers import parse_constraint
from poetry.core.version.markers import AnyMarker
from poetry.core.version.markers import parse_marker
if groups is None:
groups = [MAIN_GROUP]
if constraint is None:
constraint = "*"
if isinstance(constraint, dict):
optional = constraint.get("optional", False)
python_versions = constraint.get("python")
platform = constraint.get("platform")
markers = constraint.get("markers")
if "allows-prereleases" in constraint:
message = (
f'The "{name}" dependency specifies '
'the "allows-prereleases" property, which is deprecated. '
'Use "allow-prereleases" instead.'
)
warn(message, DeprecationWarning)
logger.warning(message)
allows_prereleases = constraint.get(
"allow-prereleases", constraint.get("allows-prereleases", False)
)
dependency: Dependency
if "git" in constraint:
# VCS dependency
dependency = VCSDependency(
name,
"git",
constraint["git"],
branch=constraint.get("branch", None),
tag=constraint.get("tag", None),
rev=constraint.get("rev", None),
directory=constraint.get("subdirectory", None),
groups=groups,
optional=optional,
develop=constraint.get("develop", False),
extras=constraint.get("extras", []),
)
elif "file" in constraint:
file_path = Path(constraint["file"])
dependency = FileDependency(
name,
file_path,
groups=groups,
base=root_dir,
extras=constraint.get("extras", []),
)
elif "path" in constraint:
path = Path(constraint["path"])
if root_dir:
is_file = root_dir.joinpath(path).is_file()
else:
is_file = path.is_file()
if is_file:
dependency = FileDependency(
name,
path,
groups=groups,
optional=optional,
base=root_dir,
extras=constraint.get("extras", []),
)
else:
dependency = DirectoryDependency(
name,
path,
groups=groups,
optional=optional,
base=root_dir,
develop=constraint.get("develop", False),
extras=constraint.get("extras", []),
)
elif "url" in constraint:
dependency = URLDependency(
name,
constraint["url"],
groups=groups,
optional=optional,
extras=constraint.get("extras", []),
)
else:
version = constraint["version"]
dependency = Dependency(
name,
version,
optional=optional,
groups=groups,
allows_prereleases=allows_prereleases,
extras=constraint.get("extras", []),
)
marker = parse_marker(markers) if markers else AnyMarker()
if python_versions:
marker = marker.intersect(
parse_marker(
create_nested_marker(
"python_version", parse_constraint(python_versions)
)
)
)
if platform:
marker = marker.intersect(
parse_marker(
create_nested_marker(
"sys_platform", parse_generic_constraint(platform)
)
)
)
if not marker.is_any():
dependency.marker = marker
dependency.source_name = constraint.get("source")
else:
dependency = Dependency(name, constraint, groups=groups)
return dependency
@classmethod
def validate(
cls, config: dict[str, Any], strict: bool = False
) -> dict[str, list[str]]:
"""
Checks the validity of a configuration
"""
from poetry.core.json import validate_object
result: dict[str, list[str]] = {"errors": [], "warnings": []}
# Schema validation errors
validation_errors = validate_object(config, "poetry-schema")
result["errors"] += validation_errors
if strict:
# If strict, check the file more thoroughly
if "dependencies" in config:
python_versions = config["dependencies"]["python"]
if python_versions == "*":
result["warnings"].append(
"A wildcard Python dependency is ambiguous. "
"Consider specifying a more explicit one."
)
for name, constraint in config["dependencies"].items():
if not isinstance(constraint, dict):
continue
if "allows-prereleases" in constraint:
result["warnings"].append(
f'The "{name}" dependency specifies '
'the "allows-prereleases" property, which is deprecated. '
'Use "allow-prereleases" instead.'
)
# Checking for scripts with extras
if "scripts" in config:
scripts = config["scripts"]
config_extras = config.get("extras", {})
for name, script in scripts.items():
if not isinstance(script, dict):
continue
extras = script.get("extras", [])
for extra in extras:
if extra not in config_extras:
result["errors"].append(
f'Script "{name}" requires extra "{extra}" which is not'
" defined."
)
# Checking types of all readme files (must match)
if "readme" in config and not isinstance(config["readme"], str):
readme_types = {readme_content_type(r) for r in config["readme"]}
if len(readme_types) > 1:
result["errors"].append(
"Declared README files must be of same type: found"
f" {', '.join(sorted(readme_types))}"
)
return result
@classmethod
def locate(cls, cwd: Path | None = None) -> Path:
cwd = Path(cwd or Path.cwd())
candidates = [cwd]
candidates.extend(cwd.parents)
for path in candidates:
poetry_file = path / "pyproject.toml"
if poetry_file.exists():
return poetry_file
else:
raise RuntimeError(
f"Poetry could not find a pyproject.toml file in {cwd} or its parents"
)