-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreports.py
More file actions
62 lines (49 loc) · 2.3 KB
/
Copy pathreports.py
File metadata and controls
62 lines (49 loc) · 2.3 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
"""Building scan reports and exporting them to JSON or CSV."""
import csv
import json
from pathlib import Path
from typing import Dict, List
from tool import ToolResult, ToolStatus
from utils import get_os_name, get_timestamp
def build_report(results: List[ToolResult]) -> Dict:
"""Assemble a full scan report dictionary from a list of ToolResult objects."""
return {
"timestamp": get_timestamp(),
"operating_system": get_os_name(),
"results": [result.to_dict() for result in results],
}
def summarize(results: List[ToolResult]) -> Dict[str, int]:
"""Return counts of installed vs. missing tools for a results list."""
installed = sum(1 for result in results if result.status != ToolStatus.NOT_INSTALLED)
missing = sum(1 for result in results if result.status == ToolStatus.NOT_INSTALLED)
return {"installed": installed, "missing": missing, "total": len(results)}
def print_scan_table(results: List[ToolResult]) -> None:
"""Print a clean, aligned table of scan results followed by a summary."""
print("=" * 60)
print("ENVIRONMENT SCAN")
print("=" * 16)
print()
print(f"{'Tool':<18}{'Status':<32}{'Version'}")
for result in results:
version_display = result.version if result.version else "-"
print(f"{result.name:<18}{result.status.value:<32}{version_display}")
print()
summary = summarize(results)
print("=" * 60)
print(f"Installed: {summary['installed']}")
print(f"Missing: {summary['missing']}")
print("=" * 60)
def export_to_json(report: Dict, output_path: Path) -> None:
"""Write a report dictionary to disk as formatted JSON."""
output_path.parent.mkdir(parents=True, exist_ok=True)
with output_path.open("w", encoding="utf-8") as file_handle:
json.dump(report, file_handle, indent=2)
def export_to_csv(report: Dict, output_path: Path) -> None:
"""Write a report's per-tool results to disk as a CSV file."""
output_path.parent.mkdir(parents=True, exist_ok=True)
fieldnames = ["tool", "installed", "status", "version", "path", "error"]
with output_path.open("w", newline="", encoding="utf-8") as file_handle:
writer = csv.DictWriter(file_handle, fieldnames=fieldnames)
writer.writeheader()
for row in report["results"]:
writer.writerow(row)