|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | +from dataclasses import dataclass |
| 9 | +from pathlib import Path |
| 10 | +from typing import Sequence |
| 11 | + |
| 12 | + |
| 13 | +@dataclass(frozen=True) |
| 14 | +class ExampleArtifactSet: |
| 15 | + name: str |
| 16 | + base_args: tuple[str, ...] |
| 17 | + outputs: tuple[tuple[str, str], ...] |
| 18 | + expected_exit_codes: tuple[int, ...] = (0,) |
| 19 | + |
| 20 | + |
| 21 | +ARTIFACT_SETS: tuple[ExampleArtifactSet, ...] = ( |
| 22 | + ExampleArtifactSet( |
| 23 | + name="cyclonedx report, summary, and markdown", |
| 24 | + base_args=( |
| 25 | + "--before", |
| 26 | + "examples/cdx_before.json", |
| 27 | + "--after", |
| 28 | + "examples/cdx_after.json", |
| 29 | + "--format", |
| 30 | + "auto", |
| 31 | + ), |
| 32 | + outputs=( |
| 33 | + ("--out-json", "sample-report.json"), |
| 34 | + ("--summary-json", "sample-summary.json"), |
| 35 | + ("--out-md", "sample-report.md"), |
| 36 | + ), |
| 37 | + ), |
| 38 | + ExampleArtifactSet( |
| 39 | + name="warn-only policy report", |
| 40 | + base_args=( |
| 41 | + "--before", |
| 42 | + "examples/cdx_before.json", |
| 43 | + "--after", |
| 44 | + "examples/cdx_after.json", |
| 45 | + "--policy", |
| 46 | + "examples/policy-minimal.yml", |
| 47 | + ), |
| 48 | + outputs=( |
| 49 | + ("--out-json", "sample-policy-warn-report.json"), |
| 50 | + ("--out-md", "sample-policy-warn-report.md"), |
| 51 | + ), |
| 52 | + ), |
| 53 | + ExampleArtifactSet( |
| 54 | + name="blocking policy report and sidecar", |
| 55 | + base_args=( |
| 56 | + "--before", |
| 57 | + "examples/cdx_before.json", |
| 58 | + "--after", |
| 59 | + "examples/cdx_after.json", |
| 60 | + "--policy", |
| 61 | + "examples/policy-strict.yml", |
| 62 | + ), |
| 63 | + outputs=( |
| 64 | + ("--out-json", "sample-policy-fail-report.json"), |
| 65 | + ("--policy-json", "sample-policy.json"), |
| 66 | + ("--out-md", "sample-policy-fail-report.md"), |
| 67 | + ), |
| 68 | + expected_exit_codes=(1,), |
| 69 | + ), |
| 70 | + ExampleArtifactSet( |
| 71 | + name="requirements report", |
| 72 | + base_args=( |
| 73 | + "--before", |
| 74 | + "examples/requirements_before.txt", |
| 75 | + "--after", |
| 76 | + "examples/requirements_after.txt", |
| 77 | + "--format", |
| 78 | + "auto", |
| 79 | + ), |
| 80 | + outputs=( |
| 81 | + ("--out-json", "sample-requirements-report.json"), |
| 82 | + ("--out-md", "sample-requirements-report.md"), |
| 83 | + ), |
| 84 | + ), |
| 85 | +) |
| 86 | + |
| 87 | + |
| 88 | +def main(argv: Sequence[str] | None = None) -> int: |
| 89 | + parser = argparse.ArgumentParser( |
| 90 | + description="Regenerate checked-in no-network example report artifacts.", |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "--check", |
| 94 | + action="store_true", |
| 95 | + help="Generate artifacts into a temporary directory and fail if checked-in examples are stale.", |
| 96 | + ) |
| 97 | + args = parser.parse_args(argv) |
| 98 | + |
| 99 | + project_root = Path(__file__).resolve().parents[1] |
| 100 | + if args.check: |
| 101 | + with tempfile.TemporaryDirectory(prefix="sbom-diff-risk-examples-") as temp_dir: |
| 102 | + return _check_artifacts(project_root, Path(temp_dir)) |
| 103 | + return _write_artifacts(project_root, project_root / "examples") |
| 104 | + |
| 105 | + |
| 106 | +def _write_artifacts(project_root: Path, output_root: Path) -> int: |
| 107 | + for artifact_set in ARTIFACT_SETS: |
| 108 | + _run_artifact_set(project_root, output_root, artifact_set) |
| 109 | + print(f"generated: {artifact_set.name}") |
| 110 | + return 0 |
| 111 | + |
| 112 | + |
| 113 | +def _check_artifacts(project_root: Path, output_root: Path) -> int: |
| 114 | + _write_artifacts(project_root, output_root) |
| 115 | + |
| 116 | + examples_dir = project_root / "examples" |
| 117 | + stale_files: list[str] = [] |
| 118 | + for artifact_set in ARTIFACT_SETS: |
| 119 | + for _, output_name in artifact_set.outputs: |
| 120 | + expected = (examples_dir / output_name).read_text(encoding="utf-8") |
| 121 | + generated = (output_root / output_name).read_text(encoding="utf-8") |
| 122 | + if generated != expected: |
| 123 | + stale_files.append(output_name) |
| 124 | + |
| 125 | + if stale_files: |
| 126 | + print("stale example artifacts detected:", file=sys.stderr) |
| 127 | + for name in stale_files: |
| 128 | + print(f" {name}", file=sys.stderr) |
| 129 | + print("run scripts/regenerate-example-artifacts.py and commit the updated files.", file=sys.stderr) |
| 130 | + return 1 |
| 131 | + |
| 132 | + print("all checked example artifacts are up to date") |
| 133 | + return 0 |
| 134 | + |
| 135 | + |
| 136 | +def _run_artifact_set(project_root: Path, output_root: Path, artifact_set: ExampleArtifactSet) -> None: |
| 137 | + output_root.mkdir(parents=True, exist_ok=True) |
| 138 | + command = [sys.executable, "-m", "sbom_diff_risk.cli", "compare", *artifact_set.base_args] |
| 139 | + for flag, output_name in artifact_set.outputs: |
| 140 | + command.extend([flag, str(output_root / output_name)]) |
| 141 | + |
| 142 | + env = dict(os.environ) |
| 143 | + src_path = str(project_root / "src") |
| 144 | + env["PYTHONPATH"] = src_path if not env.get("PYTHONPATH") else f"{src_path}{os.pathsep}{env['PYTHONPATH']}" |
| 145 | + |
| 146 | + result = subprocess.run( |
| 147 | + command, |
| 148 | + cwd=project_root, |
| 149 | + text=True, |
| 150 | + capture_output=True, |
| 151 | + env=env, |
| 152 | + ) |
| 153 | + if result.returncode not in artifact_set.expected_exit_codes: |
| 154 | + detail = result.stderr.strip() or result.stdout.strip() |
| 155 | + raise RuntimeError( |
| 156 | + f"{artifact_set.name} exited with {result.returncode}; " |
| 157 | + f"expected {artifact_set.expected_exit_codes}: {detail}" |
| 158 | + ) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + raise SystemExit(main()) |
0 commit comments