#!/usr/bin/env python3 """Checks a staged release (build/staging-repository) file by file before anything is uploaded. check-staging.py signed|unsigned ... For every module: the jar, sources and javadoc jars, POM and Gradle module file, each with its .md5/.sha1/.sha256/.sha512 checksum (and .asc when signed), and every dependency in the POM and the module file carries a version. The last check is the publishing trap the design names: the modules declare most dependencies without a version and let a BOM fill it, and a published file written from the declarations would leave consumers nothing to resolve. For the SBOM artifact (messaging-library): its POM and the CycloneDX JSON. """ import json import sys import xml.etree.ElementTree as ET from pathlib import Path CHECKSUMS = (".md5", ".sha1", ".sha256", ".sha512") POM = "{http://maven.apache.org/POM/4.0.0}" def expect(path: Path, signed: bool, problems: list[str]) -> None: if not path.is_file(): problems.append(f"missing {path.name}") return for suffix in CHECKSUMS + ((".asc",) if signed else ()): if not path.with_name(path.name + suffix).is_file(): problems.append(f"missing {path.name}{suffix}") def unversioned_pom(pom: Path) -> list[str]: root = ET.parse(pom).getroot() out = [] for dependency in root.iter(f"{POM}dependency"): version = dependency.findtext(f"{POM}version") if not version or not version.strip(): out.append(f"{dependency.findtext(f'{POM}groupId')}:{dependency.findtext(f'{POM}artifactId')}") return out def unversioned_module(module: Path) -> list[str]: data = json.loads(module.read_text(encoding="utf-8")) out = [] for variant in data.get("variants", []): for dependency in variant.get("dependencies", []): version = dependency.get("version") or {} if not any(version.get(k) for k in ("requires", "strictly", "prefers")): out.append(f"{variant['name']}: {dependency['group']}:{dependency['module']}") return out def main() -> int: if len(sys.argv) < 6: print(__doc__, file=sys.stderr) return 2 repository, group, version, signing = Path(sys.argv[1]), sys.argv[2], sys.argv[3], sys.argv[4] signed = signing == "signed" failed = False for artifact in sys.argv[5:] + ["messaging-library"]: directory = repository / group.replace(".", "/") / artifact / version base = directory / f"{artifact}-{version}" problems: list[str] = [] if artifact == "messaging-library": expect(base.with_name(base.name + ".pom"), signed, problems) expect(base.with_name(base.name + "-cyclonedx.json"), signed, problems) else: for suffix in (".jar", "-sources.jar", "-javadoc.jar", ".pom", ".module"): expect(base.with_name(base.name + suffix), signed, problems) pom, module = base.with_name(base.name + ".pom"), base.with_name(base.name + ".module") if pom.is_file(): problems += [f"POM dependency without a version: {d}" for d in unversioned_pom(pom)] if module.is_file(): problems += [f"module dependency without a version: {d}" for d in unversioned_module(module)] if problems: failed = True print(f"{group}:{artifact}:{version}", file=sys.stderr) for problem in problems: print(f" - {problem}", file=sys.stderr) else: print(f"staged: {group}:{artifact}:{version}{' (signed)' if signed else ''}") return 1 if failed else 0 if __name__ == "__main__": sys.exit(main())