mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
95 lines
4.5 KiB
Python
95 lines
4.5 KiB
Python
"""Validate all release partitions before publishing combined coverage and JUnit."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from collections import Counter
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import warnings
|
|
from xml.etree import ElementTree as ET
|
|
|
|
from coverage import Coverage
|
|
from coverage.exceptions import CoverageException, CoverageWarning
|
|
|
|
from scripts.check_release_partitions import junit_nodes, validate
|
|
from scripts.release_source_identity import check_report_source, checkout_only_sources
|
|
|
|
|
|
def combine(*, report_root: Path, workspace_root: Path, prefix: Path, junit: Path, **validation) -> dict:
|
|
prefix, junit = prefix.resolve(), junit.resolve()
|
|
report_root, workspace_root = report_root.resolve(), workspace_root.resolve()
|
|
outputs = [Path(str(prefix) + suffix) for suffix in (".json", ".xml", ".accepted.json")]
|
|
for path in [*outputs, junit]:
|
|
path.unlink(missing_ok=True)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
accepted = validate(**validation)
|
|
check_report_source(report_root, accepted)
|
|
previous = Path.cwd()
|
|
try:
|
|
os.chdir(report_root)
|
|
with tempfile.TemporaryDirectory(prefix="release-combined-") as scratch:
|
|
stage = Path(scratch)
|
|
coverage = Coverage(data_file=str(stage / "coverage.data"), config_file=str(report_root / "pyproject.toml"))
|
|
coverage.set_option("run:branch", True)
|
|
checkout = Path(accepted["source_roots"][0]).parent
|
|
coverage.set_option("paths", {
|
|
"source": [str(report_root / "obliteratus"), *accepted["source_roots"]],
|
|
"checkout_scripts": [str(report_root / "scripts"), str(checkout / "scripts")],
|
|
})
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("error", CoverageWarning)
|
|
coverage.combine(data_paths=accepted["coverage"], strict=True, keep=True)
|
|
# Preserve the complete target denominator, including unexecuted modules.
|
|
coverage.get_data().touch_files([str(report_root / name) for name in accepted["source_files"]])
|
|
coverage.json_report(outfile=str(stage / "coverage.json"))
|
|
coverage.xml_report(outfile=str(stage / "coverage.xml"))
|
|
files = json.loads((stage / "coverage.json").read_text())["files"]
|
|
if set(files) != set(accepted["source_files"]):
|
|
raise ValueError("combined coverage differs from the bound source inventory")
|
|
for path in checkout_only_sources(report_root):
|
|
name = path.relative_to(report_root).as_posix()
|
|
if not files[name]["summary"]["covered_lines"]:
|
|
raise ValueError(f"no observed execution for checkout-only source: {name}")
|
|
merged = ET.Element("testsuites")
|
|
for path in accepted["junit"]:
|
|
root = ET.parse(path).getroot()
|
|
merged.extend([root] if root.tag == "testsuite" else list(root))
|
|
ET.ElementTree(merged).write(stage / "junit.xml", encoding="utf-8")
|
|
expected = accepted["nodes"]["core"] + accepted["nodes"]["gloo"]
|
|
if Counter(junit_nodes(stage / "junit.xml")) != Counter(expected):
|
|
raise ValueError("combined JUnit does not preserve the exact node inventory")
|
|
check_report_source(report_root, accepted)
|
|
outputs[0].write_bytes((stage / "coverage.json").read_bytes())
|
|
outputs[1].write_bytes((stage / "coverage.xml").read_bytes())
|
|
junit.write_bytes((stage / "junit.xml").read_bytes())
|
|
outputs[2].write_text(json.dumps(accepted, indent=2, sort_keys=True), encoding="utf-8")
|
|
except BaseException:
|
|
for path in [*outputs, junit]:
|
|
path.unlink(missing_ok=True)
|
|
raise
|
|
finally:
|
|
os.chdir(previous)
|
|
return accepted
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
for name in ("artifacts", "expected", "report-root", "workspace-root", "prefix", "junit"):
|
|
parser.add_argument("--" + name, type=Path, required=True)
|
|
for name in ("candidate-sha", "target-sha", "target", "python-version", "core-result", "gloo-result", "checkout-origin"):
|
|
parser.add_argument("--" + name, required=True)
|
|
args = parser.parse_args()
|
|
try:
|
|
combine(**vars(args))
|
|
except (OSError, ValueError, ET.ParseError, CoverageException, CoverageWarning) as exc:
|
|
print(f"Release evidence combination failed: {exc}")
|
|
return 1
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|