Repository navigation
Expand file tree
/
Copy path_fedference_build_backend.py
More file actions
181 lines (160 loc) · 6.93 KB
/
Copy path_fedference_build_backend.py
File metadata and controls
181 lines (160 loc) · 6.93 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
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
"""PEP 517 backend wrapper for reproducible Active Fedference distributions.
Setuptools already honors ``SOURCE_DATE_EPOCH`` for wheel member timestamps,
but its source distributions retain checkout and build-time metadata. This
wrapper delegates all build semantics to the exactly pinned setuptools backend
and normalizes only archive metadata after a successful build. File contents
are never rewritten.
"""
from __future__ import annotations
import copy
import gzip
import os
import tarfile
import tempfile
import zipfile
from pathlib import Path
from typing import Any
from setuptools import build_meta as _setuptools
# Editable and metadata hooks retain setuptools semantics.
build_editable = _setuptools.build_editable
get_requires_for_build_editable = _setuptools.get_requires_for_build_editable
get_requires_for_build_sdist = _setuptools.get_requires_for_build_sdist
get_requires_for_build_wheel = _setuptools.get_requires_for_build_wheel
prepare_metadata_for_build_editable = _setuptools.prepare_metadata_for_build_editable
prepare_metadata_for_build_wheel = _setuptools.prepare_metadata_for_build_wheel
_ZIP_EPOCH_FLOOR = 315532800 # 1980-01-01T00:00:00Z, the ZIP format floor.
def _source_date_epoch(value: str | None = None) -> int | None:
"""Return a validated reproducible-build epoch, or ``None`` when unset."""
raw = os.environ.get("SOURCE_DATE_EPOCH") if value is None else value
if raw is None:
return None
normalized = raw.strip()
if not normalized or not normalized.isascii() or not normalized.isdecimal():
raise ValueError("SOURCE_DATE_EPOCH must be a nonnegative integer")
if len(normalized) > 20:
raise ValueError("SOURCE_DATE_EPOCH is outside the supported range")
epoch = int(normalized)
if epoch > 253402300799: # 9999-12-31T23:59:59Z
raise ValueError("SOURCE_DATE_EPOCH is outside the supported range")
return epoch
def _temporary_archive_path(archive: Path) -> Path:
"""Allocate a sibling temporary path for an atomic archive replacement."""
with tempfile.NamedTemporaryFile(
prefix=f".{archive.name}.",
suffix=".tmp",
dir=archive.parent,
delete=False,
) as handle:
return Path(handle.name)
def _normalize_sdist_archive(archive: Path, epoch: int) -> None:
"""Normalize tar, gzip, owner, and timestamp metadata in one sdist."""
temporary = _temporary_archive_path(archive)
try:
with tarfile.open(archive, mode="r:gz") as source:
members = source.getmembers()
with temporary.open("wb") as raw_output:
with gzip.GzipFile(
filename="",
mode="wb",
fileobj=raw_output,
compresslevel=9,
mtime=epoch,
) as compressed:
with tarfile.open(
fileobj=compressed,
mode="w|",
format=tarfile.PAX_FORMAT,
) as destination:
for member in sorted(members, key=lambda item: item.name):
normalized = copy.copy(member)
normalized.uid = 0
normalized.gid = 0
normalized.uname = ""
normalized.gname = ""
normalized.mtime = epoch
normalized.pax_headers = {}
if member.isfile():
extracted = source.extractfile(member)
if extracted is None:
raise ValueError(
f"sdist member could not be read: {member.name}"
)
with extracted:
destination.addfile(normalized, extracted)
else:
destination.addfile(normalized)
os.replace(temporary, archive)
finally:
temporary.unlink(missing_ok=True)
def _zip_datetime(epoch: int) -> tuple[int, int, int, int, int, int]:
"""Convert an epoch into a UTC ZIP timestamp at the format's lower bound."""
import time
bounded = max(epoch, _ZIP_EPOCH_FLOOR)
value = time.gmtime(bounded)
return (
value.tm_year,
value.tm_mon,
value.tm_mday,
value.tm_hour,
value.tm_min,
value.tm_sec,
)
def _normalize_wheel_archive(archive: Path, epoch: int) -> None:
"""Normalize member order and ZIP metadata without changing wheel payloads."""
temporary = _temporary_archive_path(archive)
try:
with zipfile.ZipFile(archive, mode="r") as source:
members = [(item, source.read(item.filename)) for item in source.infolist()]
with zipfile.ZipFile(
temporary,
mode="w",
compression=zipfile.ZIP_DEFLATED,
compresslevel=9,
) as destination:
for original, payload in sorted(members, key=lambda item: item[0].filename):
normalized = zipfile.ZipInfo(
filename=original.filename,
date_time=_zip_datetime(epoch),
)
normalized.compress_type = zipfile.ZIP_DEFLATED
normalized.create_system = original.create_system
normalized.create_version = original.create_version
normalized.extract_version = original.extract_version
normalized.external_attr = original.external_attr
normalized.internal_attr = original.internal_attr
normalized.flag_bits = original.flag_bits
normalized.comment = original.comment
destination.writestr(
normalized,
payload,
compress_type=zipfile.ZIP_DEFLATED,
compresslevel=9,
)
os.replace(temporary, archive)
finally:
temporary.unlink(missing_ok=True)
def build_sdist(
sdist_directory: str,
config_settings: dict[str, Any] | None = None,
) -> str:
"""Build with setuptools and normalize archive metadata when time is pinned."""
filename = _setuptools.build_sdist(sdist_directory, config_settings)
epoch = _source_date_epoch()
if epoch is not None:
_normalize_sdist_archive(Path(sdist_directory) / filename, epoch)
return filename
def build_wheel(
wheel_directory: str,
config_settings: dict[str, Any] | None = None,
metadata_directory: str | None = None,
) -> str:
"""Build with setuptools and normalize wheel archive metadata when pinned."""
filename = _setuptools.build_wheel(
wheel_directory,
config_settings,
metadata_directory,
)
epoch = _source_date_epoch()
if epoch is not None:
_normalize_wheel_archive(Path(wheel_directory) / filename, epoch)
return filename