Files
Open_Ultra_SAST/src/openultrasast/plane/workspaces.py
T

37 lines
1.8 KiB
Python
Raw Normal View History

"""Workspace manifests for the cases of an independent population file (ai-service-plane task 4).
``ousast plane workspaces <population.toml> [--out DIR]`` writes one ax ``Workspace`` per case pin (vulnerable,
fixed, benign base, benign tip) with the case's repository URL (checked out at ``<path>/repo``, depth 1) and the
pinned commit in the ``openultrasast.io/git-commits`` annotation (ax's ``GitRepo`` has no commit field), so a Run
binds a case at a pin by name. The layout of a case is that of ``benchmarks/independent/evaluate.py``'s ``pins()``.
"""
from __future__ import annotations
import tomllib
from pathlib import Path
import yaml
from .manifests import GIT_COMMITS_ANNOTATION
from .reconciler import _ax_name, _doc
__all__ = ["workspaces"]
def workspaces(population: Path, out: Path) -> list[Path]:
"""One Workspace manifest per case pin of a population file; returns the paths written."""
out.mkdir(parents=True, exist_ok=True)
written: list[Path] = []
for case in tomllib.loads(population.read_text(encoding="utf-8"))["case"]:
benign = case["benign"]
pins = {"vulnerable": case["vulnerable"], "fixed": case["fixed"], "benign-base": benign["base"], "benign-tip": benign["tip"]}
for label, commit in pins.items():
name = _ax_name(case["id"], label)
doc = _doc("Workspace", name, {"git": [{"name": "repo", "repo": case["repo"], "dir": "repo", "depth": 1}]})
doc["metadata"]["annotations"] = {GIT_COMMITS_ANNOTATION: f"repo={commit}"}
text = f"# generated by `ousast plane workspaces` from {population.name}\n" + yaml.safe_dump(doc, sort_keys=False)
(out / f"{name}.yaml").write_text(text, encoding="utf-8")
written.append(out / f"{name}.yaml")
return written