-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_backend.py
More file actions
150 lines (123 loc) · 5.25 KB
/
build_backend.py
File metadata and controls
150 lines (123 loc) · 5.25 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
"""Minimal PEP 517 backend for offline wheel builds.
This backend avoids a dependency on setuptools so the package can be built and
installed in constrained local environments without network access.
"""
from __future__ import annotations
import base64
import hashlib
import tempfile
import tomllib
import zipfile
from dataclasses import dataclass
from pathlib import Path
ROOT = Path(__file__).resolve().parent
PYPROJECT = tomllib.loads((ROOT / "pyproject.toml").read_text(encoding="utf-8"))
PROJECT = PYPROJECT["project"]
NAME = str(PROJECT["name"])
VERSION = str(PROJECT["version"])
DIST_INFO = f"{NAME.replace('-', '_')}-{VERSION}.dist-info"
WHEEL_NAME = f"{NAME.replace('-', '_')}-{VERSION}-py3-none-any.whl"
@dataclass(frozen=True)
class WheelFile:
arcname: str
data: bytes
def get_requires_for_build_wheel(config_settings=None): # noqa: D401, ANN001
return []
def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None): # noqa: D401, ANN001
metadata_dir = Path(metadata_directory) / DIST_INFO
metadata_dir.mkdir(parents=True, exist_ok=True)
_write_metadata_files(metadata_dir)
return DIST_INFO
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): # noqa: D401, ANN001
wheel_path = Path(wheel_directory) / WHEEL_NAME
files = _collect_wheel_files()
record_lines: list[str] = []
with zipfile.ZipFile(wheel_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:
for wheel_file in files:
archive.writestr(wheel_file.arcname, wheel_file.data)
digest = base64.urlsafe_b64encode(hashlib.sha256(wheel_file.data).digest()).decode("ascii").rstrip("=")
record_lines.append(f"{wheel_file.arcname},sha256={digest},{len(wheel_file.data)}")
record_name = f"{DIST_INFO}/RECORD"
record_data = ("\n".join(record_lines + [f"{record_name},,"]) + "\n").encode("utf-8")
archive.writestr(record_name, record_data)
return wheel_path.name
def build_sdist(sdist_directory, config_settings=None): # noqa: D401, ANN001
sdist_path = Path(sdist_directory) / f"{NAME}-{VERSION}.tar.gz"
with tempfile.TemporaryDirectory() as tmpdir:
staging = Path(tmpdir) / f"{NAME}-{VERSION}"
staging.mkdir(parents=True, exist_ok=True)
for relative in ("autoweave", "apps", "docs", "tests"):
source = ROOT / relative
if source.exists():
_copy_tree(source, staging / relative)
for relative in ("pyproject.toml", "README.md", "build_backend.py", ".gitignore", ".dockerignore", "Dockerfile", "docker-compose.yml"):
source = ROOT / relative
if source.exists():
(staging / relative).write_bytes(source.read_bytes())
import tarfile
with tarfile.open(sdist_path, "w:gz") as archive:
archive.add(staging, arcname=f"{NAME}-{VERSION}")
return sdist_path.name
def _collect_wheel_files() -> list[WheelFile]:
files: list[WheelFile] = []
for package_root in ("autoweave", "apps"):
root = ROOT / package_root
if not root.exists():
continue
for path in sorted(root.rglob("*.py")):
files.append(WheelFile(arcname=str(path.relative_to(ROOT)), data=path.read_bytes()))
files.extend(
[
WheelFile(
arcname=f"{DIST_INFO}/METADATA",
data=_metadata_content().encode("utf-8"),
),
WheelFile(
arcname=f"{DIST_INFO}/WHEEL",
data=(
"Wheel-Version: 1.0\n"
"Generator: build_backend\n"
"Root-Is-Purelib: true\n"
"Tag: py3-none-any\n"
).encode("utf-8"),
),
WheelFile(
arcname=f"{DIST_INFO}/entry_points.txt",
data=b"[console_scripts]\nautoweave = apps.cli.main:main\n",
),
]
)
return files
def _metadata_content() -> str:
lines = [
"Metadata-Version: 2.3",
f"Name: {NAME}",
f"Version: {VERSION}",
f"Summary: {PROJECT.get('description', '')}",
]
requires_python = PROJECT.get("requires-python")
if requires_python:
lines.append(f"Requires-Python: {requires_python}")
for requirement in PROJECT.get("dependencies", []):
lines.append(f"Requires-Dist: {requirement}")
return "\n".join(lines) + "\n"
def _write_metadata_files(metadata_dir: Path) -> None:
(metadata_dir / "METADATA").write_text(_metadata_content(), encoding="utf-8")
(metadata_dir / "WHEEL").write_text(
"Wheel-Version: 1.0\nGenerator: build_backend\nRoot-Is-Purelib: true\nTag: py3-none-any\n",
encoding="utf-8",
)
(metadata_dir / "entry_points.txt").write_text(
"[console_scripts]\nautoweave = apps.cli.main:main\n",
encoding="utf-8",
)
def _copy_tree(source: Path, destination: Path) -> None:
destination.mkdir(parents=True, exist_ok=True)
for path in source.rglob("*"):
relative = path.relative_to(source)
target = destination / relative
if path.is_dir():
target.mkdir(parents=True, exist_ok=True)
continue
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(path.read_bytes())