Files
OBLITERATUS/scripts/release_source_identity.py

100 lines
4.5 KiB
Python

"""Bind coverage origins to the exact tested source, lock, and toolchain."""
from __future__ import annotations
import hashlib
import importlib.metadata
import json
from pathlib import Path
import sys
from scripts.release_bytecode_policy import bytecode_identity, reject_project_bytecode
IDENTITY_FIELDS = ("checkout_tree_digest", "project_digest", "lock_digest", "coverage_version", "python_full_version", "source_files", "bytecode_policy")
CHECKOUT_ONLY_SOURCES = ("scripts/observe_rank_local_profile.py",)
def digest(path: Path) -> str:
return hashlib.sha256(path.read_bytes()).hexdigest()
def checkout_only_sources(root: Path) -> list[Path]:
"""Require the recognized standalone sources declared in this revision."""
risk_map = root / "ci/test-risk-map.json"
if not risk_map.is_file():
return []
declarations = json.loads(risk_map.read_text(encoding="utf-8"))["modules"]
declared = {module["path"] for module in declarations}
sources = []
for name in CHECKOUT_ONLY_SOURCES:
if name not in declared:
continue
path = root / name
if path.is_symlink() or path.parent.is_symlink():
raise ValueError(f"invalid checkout-only source origin: {name}")
if not path.is_file():
raise ValueError(f"missing declared checkout-only source: {name}")
sources.append(path)
return sources
def source_files(root: Path) -> dict[str, str]:
return {path.relative_to(root).as_posix(): digest(path)
for path in [*sorted((root / "obliteratus").rglob("*.py")), *checkout_only_sources(root)]}
def source_identity(root: Path) -> dict:
reject_project_bytecode(root / "obliteratus")
sources = source_files(root)
if not any(name.startswith("obliteratus/") for name in sources):
raise ValueError("target has no project source inventory")
installed = Path(importlib.metadata.distribution("obliteratus").locate_file("obliteratus")).resolve()
reject_project_bytecode(installed)
for name, expected in sources.items():
if name in CHECKOUT_ONLY_SOURCES:
continue
path = installed / Path(name).relative_to("obliteratus")
if not path.is_file() or digest(path) != expected:
raise ValueError(f"installed package differs from target source: {name}")
return {
"project_digest": digest(root / "pyproject.toml"), "lock_digest": digest(root / "uv.lock"),
"coverage_version": importlib.metadata.version("coverage"), "python_full_version": sys.version.split()[0],
"source_files": sources, "source_roots": [str(root / "obliteratus"), str(installed)],
}
def check_report_source(root: Path, identity: dict) -> None:
reject_project_bytecode(root / "obliteratus")
if (source_files(root) != identity["source_files"]
or digest(root / "pyproject.toml") != identity["project_digest"]
or digest(root / "uv.lock") != identity["lock_digest"]
or importlib.metadata.version("coverage") != identity["coverage_version"]
or sys.version.split()[0] != identity["python_full_version"]):
raise ValueError("report source, lock/config, coverage tool, or Python identity differs from measured target")
if bytecode_identity()["bytecode_policy"] != identity["bytecode_policy"]:
raise ValueError("report bytecode startup policy differs from measured target")
def check_origins(data, identity: dict, *, unexecuted: bool = False) -> None:
expected_context = "release:" + identity["target_sha"]
contexts = data.measured_contexts()
if contexts != {expected_context} and not (unexecuted and contexts <= {"", expected_context}):
raise ValueError("coverage context does not identify the exact target revision")
checkout = Path(identity["source_roots"][0]).parent
for name in data.measured_files():
path = Path(name)
if not path.is_absolute() or str(path) != name or ".." in path.parts:
raise ValueError("noncanonical coverage origin")
relative = None
for origin in identity["source_roots"]:
if path.is_relative_to(origin):
relative = "obliteratus/" + path.relative_to(origin).as_posix()
break
if relative is None and path.is_relative_to(checkout):
checkout_relative = path.relative_to(checkout).as_posix()
if checkout_relative in CHECKOUT_ONLY_SOURCES:
relative = checkout_relative
if relative not in identity["source_files"]:
raise ValueError(f"coverage has unknown or cross-target source origin: {name}")