feat(auditing): add --output-dir flag to audit_engine.py

This commit is contained in:
2026-08-14 16:13:11 +00:00
parent 2e1cb49cba
commit ba57991c0f
+38 -23
View File
@@ -37,7 +37,7 @@ SEVERITY_ENHANCEMENT = "enhancement" # 🟩
def _safe_get(data, *keys, default=None): def _safe_get(data, *keys, default=None):
"""Nested dict access without KeyError.""" """Nested dict access without KeyError. Last positional arg is default if not a key."""
if not keys: if not keys:
return default return default
cur = data cur = data
@@ -779,13 +779,23 @@ def to_markdown(findings, summary, contract):
def main(): def main():
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: audit_engine.py <multi_surface_json> [--json] [--md]") print("Usage: audit_engine.py <multi_surface_json> [--json] [--md] [--output-dir DIR]")
sys.exit(1) sys.exit(1)
filepath = sys.argv[1] filepath = sys.argv[1]
output_json = "--json" in sys.argv output_json = "--json" in sys.argv
output_md = "--md" in sys.argv output_md = "--md" in sys.argv
# --output-dir: write results to directory instead of stdout
output_dir = None
i = 2
while i < len(sys.argv):
if sys.argv[i] == "--output-dir" and i + 1 < len(sys.argv):
output_dir = sys.argv[i + 1]
i += 2
continue
i += 1
if not os.path.exists(filepath): if not os.path.exists(filepath):
print(f"Error: {filepath} not found") print(f"Error: {filepath} not found")
sys.exit(1) sys.exit(1)
@@ -796,28 +806,33 @@ def main():
findings = analyze(contract) findings = analyze(contract)
summary = summarize(findings, contract) summary = summarize(findings, contract)
if output_md: result = {
md = to_markdown(findings, summary, contract) "audit_engine": "v1",
print(md) "timestamp": datetime.now(timezone.utc).isoformat(),
elif output_json: "business": _safe_get(contract, "name"),
result = { "summary": summary,
"audit_engine": "v1", "findings": findings,
"timestamp": datetime.now(timezone.utc).isoformat(), }
"business": _safe_get(contract, "name"),
"summary": summary, if output_dir:
"findings": findings, os.makedirs(output_dir, exist_ok=True)
} json_path = os.path.join(output_dir, "findings.json")
print(json.dumps(result, indent=2)) with open(json_path, "w") as f:
json.dump(result, f, indent=2)
print(f"Findings written to {json_path}", file=sys.stderr)
if output_md:
md = to_markdown(findings, summary, contract)
md_path = os.path.join(output_dir, "findings.md")
with open(md_path, "w") as f:
f.write(md)
print(f"Report written to {md_path}", file=sys.stderr)
else: else:
# Default: JSON if output_md:
result = { md = to_markdown(findings, summary, contract)
"audit_engine": "v1", print(md)
"timestamp": datetime.now(timezone.utc).isoformat(), else:
"business": _safe_get(contract, "name"), print(json.dumps(result, indent=2))
"summary": summary,
"findings": findings,
}
print(json.dumps(result, indent=2))
if __name__ == "__main__": if __name__ == "__main__":