refactor: move campaign and probe modes to probes

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 15:47:20 +01:00
1 parent fd1c2e1c16
commit 2123bd8317
7 files changed
+206 -178

No files matched your search

+6 -111
View File
@@ -61,8 +61,6 @@ job runs it on a schedule.
from __future__ import annotations
import argparse
import json
import signal
import sys
from collections.abc import Sequence
from pathlib import Path
@@ -76,27 +74,18 @@ if sys.version_info < (3, 9):
sys.path.insert(0, str(Path(__file__).resolve().parent))
import yaml
from unattended import worktree
from core import hostguard
from probes.profileedit import ProfileEditError
from core.paths import REPO_ROOT
from probes.campaign import Campaign
from probes.campaignmodes import run_mode
from probes.content import ROMFETCH_ENV
from probes.gate import changed_profiles, gate
from probes.host import CONTENT_FILE, Paths, Settings, host_lock, parse_budget
from probes.reports import REVIEWED, open_findings, pass_report, render_status, review_finding, status_report
EXIT_REFUSED = 3
from probes.host import CONTENT_FILE, parse_budget
from probes.reports import REVIEWED
# --------------------------------------------------------------------------
# Command line
# --------------------------------------------------------------------------
def _parser() -> argparse.ArgumentParser:
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(allow_abbrev=False, description=__doc__.split("\n\n")[0],
formatter_class=argparse.RawDescriptionHelpFormatter)
sub = parser.add_subparsers(dest="command", required=True)
@@ -147,103 +136,9 @@ def _parser() -> argparse.ArgumentParser:
return parser
def _review(parser: argparse.ArgumentParser, args: argparse.Namespace, paths: Paths) -> int:
updates: dict[str, object] = {}
for item in args.set:
name, sep, value = item.partition("=")
if not sep or not name:
parser.error(f"--set {item!r}: FIELD=YAML")
updates[name] = yaml.safe_load(value)
try:
line = review_finding(paths.root, args.key, args.status, args.reason, updates,
args.index)
except (ValueError, ProfileEditError) as exc:
print(f"campaign: {exc}", file=sys.stderr)
return 1
print(f"{line['status']} {line['key']} [{line['id']}]")
return 0
def _findings(args: argparse.Namespace, paths: Paths) -> int:
lines = open_findings(paths.root, args.all)
if args.json:
print(json.dumps(lines, indent=2))
for line in [] if args.json else lines:
print(f"{line['status']:8} {line['key']}: profile says {json.dumps(line['profile_says'])}, "
f"run shows {json.dumps(line['run_shows'])}. {line['statement']} "
f"[{', '.join(line['evidence'])}]")
return 0
def _settings(args: argparse.Namespace) -> Settings:
if args.command == "run":
return Settings(budget=args.budget, max_load_wait=args.max_load_wait,
min_free_gb=args.min_free_gb, cache_cap_gb=args.cache_cap_gb,
romfetch=args.romfetch, network=not args.offline,
allow_unsandboxed=args.allow_unsandboxed, bench=args.bench)
if args.command == "references":
return Settings(romfetch=args.romfetch, network=not args.offline,
allow_unsandboxed=args.allow_unsandboxed)
return Settings()
def _references(campaign: Campaign, args: argparse.Namespace, only: set[str]) -> int:
try:
with host_lock(campaign.lock_path):
listed = campaign.references(only, args.system, Path(args.png_dir))
except hostguard.LockBusy as exc:
print(f"campaign: deferred, {exc}", file=sys.stderr)
return 75
print(json.dumps(listed, indent=2))
return 0
def main(argv: Sequence[str] | None = None) -> int:
parser = _parser()
args = parser.parse_args(argv)
paths = Paths.default()
if args.command == "gate":
return gate(paths.root, changed_profiles(paths.root))
if args.command == "review":
return _review(parser, args, paths)
if args.command == "findings":
return _findings(args, paths)
settings = _settings(args)
if args.command in ("run", "references"):
paths.content_file = Path(args.content_file)
campaign = Campaign(paths, settings)
if args.command == "status":
report = status_report(campaign)
print(json.dumps(report, indent=2) if args.json else render_status(report))
return 0
only = {item.strip() for item in args.only.split(",") if item.strip()}
if args.command == "references":
return _references(campaign, args, only)
if args.dry_run:
campaign.run(only, args.system, dry_run=True)
return 0
return _run_pass(campaign, args, only, paths)
def _run_pass(campaign: Campaign, args: argparse.Namespace, only: set[str], paths: Paths) -> int:
campaign.dirty_at_start = set(worktree.changed_files(paths.root)) if args.commit else set()
def stop(signum: int, frame: object) -> None:
# SIGTERM ends the pass like Ctrl-C: state saved, pass line written.
raise KeyboardInterrupt
signal.signal(signal.SIGTERM, stop)
try:
with host_lock(campaign.lock_path):
current = campaign.run(only, args.system, commit_every=args.commit_every,
commit=args.commit)
except hostguard.LockBusy as exc:
print(f"campaign: deferred, {exc}", file=sys.stderr)
return 75
print(json.dumps(pass_report(campaign, current)))
if current.result == "refused":
return EXIT_REFUSED
return 75 if current.result == "paused" else 0
parser = build_parser()
return run_mode(parser, parser.parse_args(argv))
if __name__ == "__main__":