-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostprocessors.py
More file actions
63 lines (56 loc) · 2.62 KB
/
Copy pathpostprocessors.py
File metadata and controls
63 lines (56 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# postprocessors.py
from __future__ import annotations
from pathlib import Path
from typing import Dict, Any, Tuple
import json, shutil
def _issues(report: Dict[str, Any]):
return report.get("issues") or report.get("findings") or []
def _sev_counts(report: Dict[str, Any]) -> Dict[str, int]:
sev = lambda i: str(i.get("severity", "")).upper()
return {
"CRITICAL": sum(sev(i) in ("CRIT", "CRITICAL") for i in _issues(report)),
"HIGH": sum(sev(i) == "HIGH" for i in _issues(report)),
"MEDIUM": sum(sev(i) == "MEDIUM" for i in _issues(report)),
"LOW": sum(sev(i) == "LOW" for i in _issues(report)),
}
def summarize(report: Dict[str, Any], model_path: Path) -> Tuple[bool, str]:
c = _sev_counts(report)
total = sum(c.values())
return True, f"Summary → CRIT:{c['CRITICAL']} HIGH:{c['HIGH']} MED:{c['MEDIUM']} LOW:{c['LOW']} (issues: {total})"
def quarantine_if_severe(report: Dict[str, Any], model_path: Path, threshold: str = "HIGH") -> Tuple[bool, str]:
order = {"LOW":0, "MEDIUM":1, "HIGH":2, "CRITICAL":3}
counts = _sev_counts(report)
worst = None
for level in ("CRITICAL","HIGH","MEDIUM","LOW"):
if counts[level] > 0:
worst = level
break
if worst and order[worst] >= order[threshold]:
qdir = model_path.parent / "quarantine"
qdir.mkdir(exist_ok=True)
dest = qdir / model_path.name
shutil.move(str(model_path), dest)
return True, f"🔒 Quarantined '{model_path.name}' → {dest}"
return True, "✅ No quarantine needed (below threshold)."
def export_issues_csv(report: Dict[str, Any], model_path: Path, out_path: Path = Path("issues.csv")) -> Tuple[bool, str]:
try:
import pandas as pd
df = pd.DataFrame(_issues(report))
df.to_csv(out_path, index=False)
return True, f"📄 Exported {len(df)} issues → {out_path}"
except Exception as e:
return False, f"CSV export failed: {e}"
def http_post(report: Dict[str, Any], model_path: Path, url: str = "http://localhost:5000/report") -> Tuple[bool, str]:
try:
import requests
except ImportError:
return False, "Install requests first: pip install requests"
r = requests.post(url, json=report, timeout=5)
return True, f"🌐 POST {url} → {r.status_code} {r.text[:120]}"
# Map nice names → callables with a common signature (report_dict, model_path) -> (ok, msg)
ACTIONS = {
"Summarize (on-screen)": summarize,
"Quarantine if ≥ HIGH": quarantine_if_severe,
"Export issues.csv": export_issues_csv,
"HTTP POST → http://localhost:5000/report": http_post,
}