Files
id_configurator/tests/dockerfiles/repos/build_test_jar.py
T

67 lines
2.3 KiB
Python

"""Build a minimal, offline, hand-rolled jar and its pom.xml, laid out as a
Maven-compatible repository, for integration-testing the `java` module of
id-configurator.
No network access and no build backend (maven/gradle) are used: the jar 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 hashlib
import io
import pathlib
import zipfile
GROUP = "org.interdiode"
GROUP_PATH = GROUP.replace(".", "/")
ARTIFACT = "idconfiguratortestpkg"
VERSION = "0.0.1"
OUT_DIR = pathlib.Path("/srv") / GROUP_PATH / ARTIFACT / VERSION
MANIFEST = "Manifest-Version: 1.0\n"
POM = f"""<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>{GROUP}</groupId>
<artifactId>{ARTIFACT}</artifactId>
<version>{VERSION}</version>
<packaging>jar</packaging>
<description>Test-only artifact served by the id-configurator integration tests</description>
</project>
"""
def build_jar() -> bytes:
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as archive:
archive.writestr("META-INF/MANIFEST.MF", MANIFEST)
archive.writestr(
"marker.txt",
"test-only artifact served by the id-configurator integration tests\n",
)
return buffer.getvalue()
def _write_with_checksums(path: pathlib.Path, content: bytes) -> None:
"""Write `content` to `path`, plus the .sha1/.md5 sidecar files most
build tools (Leiningen's aether resolver in particular) require to be
present, unlike Maven which merely warns when they are missing."""
path.write_bytes(content)
sha1_digest = hashlib.sha1(content).hexdigest() # nosec B324
(path.parent / f"{path.name}.sha1").write_text(sha1_digest, encoding="ascii")
md5_digest = hashlib.md5(content).hexdigest() # nosec B324
(path.parent / f"{path.name}.md5").write_text(md5_digest, encoding="ascii")
def main() -> None:
OUT_DIR.mkdir(parents=True, exist_ok=True)
_write_with_checksums(OUT_DIR / f"{ARTIFACT}-{VERSION}.jar", build_jar())
_write_with_checksums(OUT_DIR / f"{ARTIFACT}-{VERSION}.pom", POM.encode("utf-8"))
if __name__ == "__main__":
main()