This repository was archived by the owner on May 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_backend.py
More file actions
152 lines (123 loc) · 4.85 KB
/
Copy pathbuild_backend.py
File metadata and controls
152 lines (123 loc) · 4.85 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
"""Minimal local build backend for offline editable installs.
This backend keeps the repository self-contained and avoids any network fetches
for build requirements. It supports editable installs by publishing a wheel
that adds the `src/` directory to `sys.path` through a `.pth` file.
"""
from __future__ import annotations
import base64
import csv
import hashlib
import io
import os
import tomllib
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile
ROOT = Path(__file__).resolve().parent
SRC = ROOT / "src"
PROJECT = tomllib.loads((ROOT / "pyproject.toml").read_text(encoding="utf-8"))["project"]
NAME = PROJECT["name"]
VERSION = PROJECT["version"]
SUMMARY = PROJECT["description"]
DIST_INFO = f"{NAME}-{VERSION}.dist-info"
WHEEL_NAME = f"{NAME}-{VERSION}-py3-none-any.whl"
def _metadata() -> str:
lines = [
"Metadata-Version: 2.1",
f"Name: {NAME}",
f"Version: {VERSION}",
f"Summary: {SUMMARY}",
]
urls = PROJECT.get("urls", {})
homepage = urls.get("Homepage")
if homepage:
lines.append(f"Home-page: {homepage}")
return "\n".join(lines) + "\n"
def _package_files() -> dict[str, bytes]:
package_root = SRC / NAME
files: dict[str, bytes] = {}
for path in sorted(package_root.glob("**/*")):
if path.is_file():
rel = path.relative_to(SRC).as_posix()
files[rel] = path.read_bytes()
return files
def _editable_files() -> dict[str, bytes]:
entry_points = (
"[console_scripts]\n"
f"{NAME} = {NAME}.cli:main\n"
)
files: dict[str, bytes] = {
f"{NAME}.pth": (str(SRC) + os.linesep).encode(),
f"{DIST_INFO}/METADATA": _metadata().encode(),
f"{DIST_INFO}/WHEEL": (
"Wheel-Version: 1.0\n"
"Generator: intelligence.build_backend\n"
"Root-Is-Purelib: true\n"
"Tag: py3-none-any\n"
).encode(),
f"{DIST_INFO}/entry_points.txt": entry_points.encode(),
f"{DIST_INFO}/top_level.txt": f"{NAME}\n".encode(),
}
return files
def _wheel_files(editable: bool) -> dict[str, bytes]:
entry_points = (
"[console_scripts]\n"
f"{NAME} = {NAME}.cli:main\n"
)
files = _editable_files() if editable else _package_files()
files[f"{DIST_INFO}/METADATA"] = _metadata().encode()
files[f"{DIST_INFO}/WHEEL"] = (
"Wheel-Version: 1.0\n"
"Generator: intelligence.build_backend\n"
"Root-Is-Purelib: true\n"
"Tag: py3-none-any\n"
).encode()
files[f"{DIST_INFO}/entry_points.txt"] = entry_points.encode()
files[f"{DIST_INFO}/top_level.txt"] = f"{NAME}\n".encode()
return files
def _record_bytes(files: dict[str, bytes]) -> bytes:
rows: list[list[str]] = []
for path, data in sorted(files.items()):
digest = hashlib.sha256(data).digest()
b64 = base64.urlsafe_b64encode(digest).rstrip(b"=").decode()
rows.append([path, f"sha256={b64}", str(len(data))])
rows.append([f"{DIST_INFO}/RECORD", "", ""])
buffer = io.StringIO()
writer = csv.writer(buffer, lineterminator="\n")
writer.writerows(rows)
return buffer.getvalue().encode()
def _build_wheel(wheel_directory: str, editable: bool) -> str:
wheel_path = Path(wheel_directory) / WHEEL_NAME
files = _wheel_files(editable)
files[f"{DIST_INFO}/RECORD"] = _record_bytes(files)
wheel_path.parent.mkdir(parents=True, exist_ok=True)
with ZipFile(wheel_path, "w", compression=ZIP_DEFLATED) as zf:
for path, data in files.items():
zf.writestr(path, data)
return WHEEL_NAME
def get_requires_for_build_wheel(config_settings=None): # noqa: D401
return []
def get_requires_for_build_editable(config_settings=None): # noqa: D401
return []
def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):
dist_info = Path(metadata_directory) / DIST_INFO
dist_info.mkdir(parents=True, exist_ok=True)
(dist_info / "METADATA").write_text(_metadata(), encoding="utf-8")
(dist_info / "WHEEL").write_text(
"Wheel-Version: 1.0\n"
"Generator: intelligence.build_backend\n"
"Root-Is-Purelib: true\n"
"Tag: py3-none-any\n",
encoding="utf-8",
)
(dist_info / "entry_points.txt").write_text(
"[console_scripts]\nintelligence = intelligence.cli:main\n",
encoding="utf-8",
)
(dist_info / "top_level.txt").write_text(f"{NAME}\n", encoding="utf-8")
return DIST_INFO
def prepare_metadata_for_build_editable(metadata_directory, config_settings=None):
return prepare_metadata_for_build_wheel(metadata_directory, config_settings)
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
return _build_wheel(wheel_directory, editable=False)
def build_editable(wheel_directory, config_settings=None, metadata_directory=None):
return _build_wheel(wheel_directory, editable=True)