-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.py
More file actions
74 lines (58 loc) · 2.69 KB
/
Copy pathhistory.py
File metadata and controls
74 lines (58 loc) · 2.69 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
64
65
66
67
68
69
70
71
72
73
74
"""Saving, loading, and comparing DevCheck scan history."""
import json
from pathlib import Path
from typing import Dict, List, Optional
def save_scan(report: Dict, history_path: Path) -> None:
"""Persist a scan report to the history file, overwriting any previous one."""
history_path.parent.mkdir(parents=True, exist_ok=True)
with history_path.open("w", encoding="utf-8") as file_handle:
json.dump(report, file_handle, indent=2)
def load_scan(history_path: Path) -> Optional[Dict]:
"""Load a previously saved scan report, if one exists and is readable."""
if not history_path.exists():
return None
try:
with history_path.open("r", encoding="utf-8") as file_handle:
return json.load(file_handle)
except (json.JSONDecodeError, OSError):
return None
def compare_scans(previous: Dict, current: Dict) -> List[Dict[str, str]]:
"""Compare two scan reports and return a list of human-readable changes."""
changes: List[Dict[str, str]] = []
previous_lookup = {item["tool"]: item for item in previous.get("results", [])}
current_lookup = {item["tool"]: item for item in current.get("results", [])}
all_tool_names = sorted(set(previous_lookup) | set(current_lookup))
for tool_name in all_tool_names:
old_entry = previous_lookup.get(tool_name)
new_entry = current_lookup.get(tool_name)
if old_entry is None and new_entry is not None:
changes.append({"tool": tool_name, "change": f"New tool tracked: {new_entry['status']}"})
continue
if new_entry is None and old_entry is not None:
changes.append({"tool": tool_name, "change": "Tool no longer tracked"})
continue
if old_entry is None or new_entry is None:
continue
if old_entry["status"] != new_entry["status"]:
changes.append(
{
"tool": tool_name,
"change": f"Status changed: {old_entry['status']} -> {new_entry['status']}",
}
)
elif old_entry["version"] != new_entry["version"]:
changes.append(
{
"tool": tool_name,
"change": f"Version changed: {old_entry['version']} -> {new_entry['version']}",
}
)
return changes
def print_comparison(changes: List[Dict[str, str]]) -> None:
"""Print a comparison result list produced by compare_scans()."""
if not changes:
print("No differences found between the previous scan and the current scan.")
return
print(f"{'Tool':<18}{'Change'}")
for change in changes:
print(f"{change['tool']:<18}{change['change']}")