|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Inspect the locally built SDK; no network access or publication.""" |
| 3 | + |
| 4 | +import argparse |
| 5 | +import hashlib |
| 6 | +import json |
| 7 | +from pathlib import Path |
| 8 | +import struct |
| 9 | +import xml.etree.ElementTree as ET |
| 10 | +import zipfile |
| 11 | + |
| 12 | + |
| 13 | +def require(condition, message): |
| 14 | + if not condition: |
| 15 | + raise ValueError(message) |
| 16 | + |
| 17 | + |
| 18 | +def check(root): |
| 19 | + pom = root / "pom.xml" |
| 20 | + xml = ET.parse(pom).getroot() |
| 21 | + ns = {"m": "http://maven.apache.org/POM/4.0.0"} |
| 22 | + artifact = xml.findtext("m:artifactId", namespaces=ns) |
| 23 | + version = xml.findtext("m:version", namespaces=ns) |
| 24 | + require(artifact == "qca-sdk" and version, "Unexpected SDK coordinates") |
| 25 | + target = root / "target" |
| 26 | + jars = [target / (artifact + "-" + version + suffix + ".jar") |
| 27 | + for suffix in ("", "-sources", "-javadoc")] |
| 28 | + for jar in jars: |
| 29 | + require(jar.is_file(), "Missing package: " + jar.name) |
| 30 | + with zipfile.ZipFile(jars[0]) as jar: |
| 31 | + names = jar.namelist() |
| 32 | + classes = [name for name in names if name.endswith(".class")] |
| 33 | + require(classes, "SDK JAR has no classes") |
| 34 | + require("com/qoder/cloudagents/ForwardClient.class" in names, "Missing Forward client") |
| 35 | + require(jar.read("META-INF/LICENSE") == (root / "LICENSE").read_bytes(), "License differs") |
| 36 | + properties = jar.read("com/qoder/cloudagents/version.properties").decode("utf-8") |
| 37 | + require("version=" + version in properties.splitlines(), "Embedded version differs from POM") |
| 38 | + for name in classes: |
| 39 | + require(name.startswith("com/qoder/cloudagents/") and "/examples/" not in name, |
| 40 | + "Bundled dependency or example class: " + name) |
| 41 | + data = jar.read(name) |
| 42 | + require(data[:4] == b"\xca\xfe\xba\xbe" and struct.unpack(">H", data[6:8])[0] == 52, |
| 43 | + "Class does not target Java 8: " + name) |
| 44 | + require(not any(name.startswith(("src/test/", "test-results/", "artifacts/")) |
| 45 | + or Path(name).name.startswith(".env") for name in names), |
| 46 | + "Unexpected local or test data in SDK JAR") |
| 47 | + with zipfile.ZipFile(jars[1]) as jar: |
| 48 | + require("com/qoder/cloudagents/ForwardClient.java" in jar.namelist(), "Missing source attachment") |
| 49 | + with zipfile.ZipFile(jars[2]) as jar: |
| 50 | + require("com/qoder/cloudagents/ForwardClient.html" in jar.namelist(), "Missing Javadoc attachment") |
| 51 | + require(not any(name.startswith("com/qoder/cloudagents/core/internal/") for name in jar.namelist()), |
| 52 | + "Internal implementation leaked into public Javadoc") |
| 53 | + files = jars + [pom] |
| 54 | + digests = {str(path.relative_to(root)): hashlib.sha256(path.read_bytes()).hexdigest() for path in files} |
| 55 | + report = {"artifact_id": artifact, "version": version, "class_count": len(classes), |
| 56 | + "class_major_version": 52, "sha256": digests} |
| 57 | + (target / "package-check.json").write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8") |
| 58 | + (target / "SHA256SUMS").write_text("".join(digest + " " + name + "\n" for name, digest in digests.items()), encoding="utf-8") |
| 59 | + print(json.dumps(report, indent=2)) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + parser = argparse.ArgumentParser(description=__doc__) |
| 64 | + parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[2]) |
| 65 | + try: |
| 66 | + check(parser.parse_args().root.resolve()) |
| 67 | + except (ValueError, KeyError, OSError, zipfile.BadZipFile, ET.ParseError) as error: |
| 68 | + raise SystemExit("Package check failed: " + str(error)) |
0 commit comments