Files
id_configurator/tests/dockerfiles/repos/build_test_wheel.py
T
2026-09-04 19:20:41 +02:00

81 lines
2.5 KiB
Python

"""Build a minimal, offline, hand-rolled wheel and PEP 503 "simple" index for
integration-testing the `python` module of id-configurator.
No network access and no build backend (setuptools/build/pip) are used: the
wheel is a plain zip file assembled with the standard library only, so this
script can run in a minimal Python image with nothing installed.
"""
from __future__ import annotations
import base64
import hashlib
import pathlib
import zipfile
PACKAGE = "idconfiguratortestpkg"
VERSION = "0.0.1"
WHEEL_NAME = f"{PACKAGE}-{VERSION}-py3-none-any.whl"
OUT_DIR = pathlib.Path("/srv")
METADATA = f"""Metadata-Version: 2.1
Name: {PACKAGE}
Version: {VERSION}
Summary: Test-only package served by the id-configurator integration tests
"""
WHEEL_FILE = """Wheel-Version: 1.0
Generator: id-configurator-tests
Root-Is-Purelib: true
Tag: py3-none-any
"""
INIT_PY = '"""Test-only package used to verify the python module mirror."""\n'
def _record_line(path: str, content: bytes) -> str:
digest = base64.urlsafe_b64encode(hashlib.sha256(content).digest()).rstrip(b"=")
return f"{path},sha256={digest.decode('ascii')},{len(content)}"
def build_wheel(packages_dir: pathlib.Path) -> bytes:
dist_info = f"{PACKAGE}-{VERSION}.dist-info"
files = {
f"{PACKAGE}/__init__.py": INIT_PY.encode("utf-8"),
f"{dist_info}/METADATA": METADATA.encode("utf-8"),
f"{dist_info}/WHEEL": WHEEL_FILE.encode("utf-8"),
}
record_lines = [_record_line(name, content) for name, content in files.items()]
record_lines.append(f"{dist_info}/RECORD,,")
files[f"{dist_info}/RECORD"] = ("\n".join(record_lines) + "\n").encode("utf-8")
packages_dir.mkdir(parents=True, exist_ok=True)
wheel_path = packages_dir / WHEEL_NAME
with zipfile.ZipFile(wheel_path, "w", zipfile.ZIP_DEFLATED) as archive:
for name, content in files.items():
archive.writestr(name, content)
return wheel_path.read_bytes()
def write_simple_index(simple_dir: pathlib.Path, wheel_content: bytes) -> None:
package_dir = simple_dir / PACKAGE
package_dir.mkdir(parents=True, exist_ok=True)
digest = hashlib.sha256(wheel_content).hexdigest()
html = f"""<!DOCTYPE html>
<html>
<body>
<a href="../../packages/{WHEEL_NAME}#sha256={digest}">{WHEEL_NAME}</a>
</body>
</html>
"""
(package_dir / "index.html").write_text(html, encoding="utf-8")
def main() -> None:
wheel_content = build_wheel(OUT_DIR / "packages")
write_simple_index(OUT_DIR / "simple", wheel_content)
if __name__ == "__main__":
main()