Intrepid/Prometeu/Runtime/pipeline/pr-master There was a failure building this commit
162 lines
7.4 KiB
Python
162 lines
7.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Audit exclusive source ownership and nonempty LLVM coverage domain evidence."""
|
|
import argparse
|
|
import json
|
|
import math
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
class AuditError(ValueError):
|
|
pass
|
|
|
|
|
|
def load_json(path):
|
|
try:
|
|
return json.loads(path.read_text())
|
|
except (OSError, ValueError) as error:
|
|
raise AuditError(f"cannot read {path}: {error}") from error
|
|
|
|
|
|
def matches(name, domain):
|
|
return any(name.startswith(p) for p in domain["paths"]) and not any(
|
|
name.startswith(p) for p in domain.get("exclude_paths", [])
|
|
)
|
|
|
|
|
|
def owners(name, domains):
|
|
return [key for key, domain in domains.items() if matches(name, domain)]
|
|
|
|
|
|
def source_name(filename, root):
|
|
"""Normalize absolute or remapped relative report paths into repository paths."""
|
|
if not isinstance(filename, str):
|
|
raise AuditError("report filename must be a string")
|
|
path = Path(filename)
|
|
if not path.is_absolute():
|
|
path = root / path
|
|
try:
|
|
name = path.resolve().relative_to(root).as_posix()
|
|
except ValueError:
|
|
return None
|
|
parts = Path(name).parts
|
|
return name if parts and parts[0] == "crates" and "src" in parts and name.endswith(".rs") else None
|
|
|
|
|
|
def audit(root, config_path, report_path=None):
|
|
root = root.resolve()
|
|
config = load_json(config_path)
|
|
if not isinstance(config, dict) or config.get("version") != 1:
|
|
raise AuditError("unsupported coverage configuration version (expected 1)")
|
|
domains = config.get("domains")
|
|
if not isinstance(domains, dict) or not domains:
|
|
raise AuditError("configuration must contain nonempty domains")
|
|
sources = sorted(p.relative_to(root).as_posix() for p in (root / "crates").glob("**/src/**/*.rs"))
|
|
if not sources:
|
|
raise AuditError("no crate src Rust files found")
|
|
for key, domain in domains.items():
|
|
if not key or not isinstance(domain, dict):
|
|
raise AuditError(f"invalid domain {key!r}")
|
|
baseline = domain.get("baseline_percent")
|
|
if isinstance(baseline, bool) or not isinstance(baseline, (float, int)) or not math.isfinite(baseline) or not 0 <= baseline <= 100:
|
|
raise AuditError(f"{key}: baseline_percent must be finite in [0,100]")
|
|
paths = domain.get("paths")
|
|
exclusions = domain.get("exclude_paths", [])
|
|
if not isinstance(paths, list) or not paths or not isinstance(exclusions, list):
|
|
raise AuditError(f"{key}: paths must be nonempty and exclude_paths must be a list")
|
|
for prefix in paths + exclusions:
|
|
if not isinstance(prefix, str) or not prefix.startswith("crates/") or ".." in Path(prefix).parts or not any(s.startswith(prefix) for s in sources):
|
|
raise AuditError(f"{key}: prefix does not match crate sources: {prefix!r}")
|
|
if any(not any(p.startswith(include) for include in paths) for p in exclusions):
|
|
raise AuditError(f"{key}: exclusions must be within inclusive paths")
|
|
source_owners = {}
|
|
for name in sources:
|
|
assigned = owners(name, domains)
|
|
if len(assigned) != 1:
|
|
raise AuditError(f"{name}: expected exactly one domain, got {assigned}")
|
|
source_owners[name] = assigned[0]
|
|
result = {
|
|
"source_files": len(sources),
|
|
"domains": {
|
|
key: {"source_files": sum(owner == key for owner in source_owners.values()),
|
|
"report_files": 0, "files": [], "lines_count": 0, "lines_covered": 0,
|
|
"functions": {"count": 0, "covered": 0},
|
|
"regions": {"count": 0, "covered": 0},
|
|
"baseline_percent": domain["baseline_percent"]}
|
|
for key, domain in domains.items()
|
|
},
|
|
}
|
|
if report_path is None:
|
|
return result
|
|
report = load_json(report_path)
|
|
if not isinstance(report, dict) or not isinstance(report.get("data"), list) or not report["data"]:
|
|
raise AuditError("coverage report must contain nonempty data")
|
|
seen = set()
|
|
for data in report["data"]:
|
|
if not isinstance(data, dict) or not isinstance(data.get("files"), list):
|
|
raise AuditError("coverage report data must contain files")
|
|
for record in data["files"]:
|
|
if not isinstance(record, dict):
|
|
raise AuditError("coverage report file must be an object")
|
|
name = source_name(record.get("filename"), root)
|
|
if name is None:
|
|
continue
|
|
if name not in source_owners:
|
|
raise AuditError(f"report source not present in repository: {name}")
|
|
if name in seen:
|
|
raise AuditError(f"duplicate source in coverage report: {name}")
|
|
seen.add(name)
|
|
try:
|
|
lines = record["summary"]["lines"]
|
|
count, covered = lines["count"], lines["covered"]
|
|
except (KeyError, TypeError) as error:
|
|
raise AuditError(f"{name}: missing line summary") from error
|
|
if any(isinstance(v, bool) or not isinstance(v, int) for v in (count, covered)) or not 0 <= covered <= count:
|
|
raise AuditError(f"{name}: invalid line counts")
|
|
domain = result["domains"][source_owners[name]]
|
|
domain["report_files"] += 1
|
|
domain["files"].append(name)
|
|
domain["lines_count"] += count
|
|
domain["lines_covered"] += covered
|
|
for metric in ("functions", "regions"):
|
|
item = record["summary"].get(metric, {"count": 0, "covered": 0})
|
|
if not isinstance(item, dict):
|
|
raise AuditError(f"{name}: invalid {metric} summary")
|
|
metric_count, metric_covered = item.get("count"), item.get("covered")
|
|
if any(isinstance(v, bool) or not isinstance(v, int) for v in (metric_count, metric_covered)) or not 0 <= metric_covered <= metric_count:
|
|
raise AuditError(f"{name}: invalid {metric} counts")
|
|
domain[metric]["count"] += metric_count
|
|
domain[metric]["covered"] += metric_covered
|
|
for key, domain in result["domains"].items():
|
|
if domain["lines_count"] <= 0:
|
|
raise AuditError(f"{key}: no instrumented lines in report")
|
|
domain["line_percent"] = (domain["lines_covered"] * 10000 // domain["lines_count"]) / 100
|
|
domain["gate_passed"] = domain["line_percent"] >= domain["baseline_percent"]
|
|
return result
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parent.parent)
|
|
parser.add_argument("--config", type=Path)
|
|
parser.add_argument("--report", type=Path)
|
|
parser.add_argument("--json", action="store_true")
|
|
parser.add_argument("--quiet", action="store_true")
|
|
args = parser.parse_args()
|
|
try:
|
|
result = audit(args.root, args.config or args.root / "files/config/runtime-edge-coverage-domains.json", args.report)
|
|
except AuditError as error:
|
|
print(f"Coverage domain audit failed: {error}", file=sys.stderr)
|
|
return 1
|
|
if args.json:
|
|
print(json.dumps(result, indent=2))
|
|
elif not args.quiet:
|
|
print(f"Coverage ownership: {result['source_files']} source files, {len(result['domains'])} exclusive domains")
|
|
if args.report:
|
|
print("Coverage evidence: all domains contain instrumented lines; no source orphans or duplicates")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|