audit-labs/audit-tools
A collection of scripts, queries, and other goodies you can use in an audit.
clone: git clone https://gitbay.org/audit-labs/audit-tools.git
v1.0.0: applications/aws/reporters/csv_reporter.py · raw
1"""CSV reporter: writes one CSV file per data section into an output directory."""
2
3import csv
4import os
5
6
7def write(output_dir, filename, rows):
8 """
9 Write a list of dicts to a CSV file in output_dir.
10 Skips writing if rows is empty, but logs the skip.
11 """
12 if not rows:
13 print(f" {filename}: no data, skipping")
14 return
15
16 os.makedirs(output_dir, exist_ok=True)
17 path = os.path.join(output_dir, filename)
18
19 with open(path, "w", newline="", encoding="utf-8") as f:
20 writer = csv.DictWriter(f, fieldnames=rows[0].keys())
21 writer.writeheader()
22 writer.writerows(rows)
23
24 print(f" {filename}: {len(rows)} rows -> {path}")
25
26
27def write_summary(output_dir, subject, sections):
28 """
29 Write a plain-text summary file listing section names and row counts.
30 sections: list of (label, row_count) tuples
31 """
32 os.makedirs(output_dir, exist_ok=True)
33 path = os.path.join(output_dir, "summary.txt")
34 lines = [
35 "AWS Audit Package",
36 f"Profile: {subject}",
37 "",
38 "Section Rows",
39 f"{'─' * 40}",
40 ]
41 for label, count in sections:
42 lines.append(f"{label:<35}{count}")
43
44 with open(path, "w", encoding="utf-8") as f:
45 f.write("\n".join(lines) + "\n")
46
47 print(f" summary.txt -> {path}")