Repository navigation
Expand file tree
/
Copy pathbuild_metadata.py
More file actions
65 lines (52 loc) · 2.45 KB
/
Copy pathbuild_metadata.py
File metadata and controls
65 lines (52 loc) · 2.45 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
"""Versioned package metadata shared by source and official wheel builds.
Keep this module standard-library-only: setuptools loads it before installing
runtime dependencies, and Docker also uses it to resolve source extras.
"""
import ast
import json
from pathlib import Path
import tomllib
ROOT = Path(__file__).resolve().parent
def project_version(root: Path = ROOT) -> str:
source = ast.parse((root / "deeptutor/__version__.py").read_text(encoding="utf-8"))
for node in source.body:
if isinstance(node, ast.Assign) and any(
isinstance(target, ast.Name) and target.id == "__version__" for target in node.targets
):
return str(ast.literal_eval(node.value))
raise ValueError("Missing DeepTutor version")
def optional_dependencies(root: Path = ROOT) -> dict[str, list[str]]:
data = tomllib.loads((root / "pyproject.toml").read_text(encoding="utf-8"))
templates = data["tool"]["deeptutor"]["optional-dependencies"]
version = project_version(root)
return {name: [raw.format(version=version) for raw in rows] for name, rows in templates.items()}
def source_optional_dependencies(root: Path = ROOT) -> dict[str, list[str]]:
"""Source deployments already own module code; install only its dependencies.
Installing a feature wheel over a Docker/source tree would add a second
code owner and require the unreleased version to exist on PyPI.
"""
extras = optional_dependencies(root)
modules = {
path.stem: json.loads(path.read_text(encoding="utf-8"))
for path in (root / "deeptutor/modules/manifests").glob("*.json")
}
distributions = {row["distribution"]: name for name, row in modules.items()}
def expand_module(name: str) -> list[str]:
row = modules[name]
return [
*row["requirements"],
*(raw for dep in row["requires"] for raw in expand_module(dep)),
]
def expand_extra(name: str) -> list[str]:
result = []
for raw in extras[name]:
package = raw.split("==", 1)[0]
if package in distributions:
result.extend(expand_module(distributions[package]))
elif package.startswith("deeptutor["):
for dep in package[len("deeptutor[") : -1].split(","):
result.extend(expand_extra(dep))
else:
result.append(raw)
return list(dict.fromkeys(result))
return {name: expand_extra(name) for name in extras}