forked from reingart/pyfpdf
-
Notifications
You must be signed in to change notification settings - Fork 227
/
checker_commons.py
59 lines (53 loc) · 2.38 KB
/
checker_commons.py
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
import json, sys
from collections import defaultdict
def aggregate(pdf_filepath, report, aggregated_report_filepath):
agg_report = {
"failures": defaultdict(list),
"errors": defaultdict(list),
}
try:
with open(aggregated_report_filepath) as agg_file:
prev_agg_report = json.load(agg_file)
agg_report["failures"].update(prev_agg_report["failures"])
agg_report["errors"].update(prev_agg_report["errors"])
except FileNotFoundError:
print("Initializing a new JSON file for the aggregated report")
if "version" in report:
agg_report["version"] = report.pop("version")
if "failure" in report:
failure = report["failure"]
agg_report["failures"][failure].append(pdf_filepath)
else:
for error in report.get("errors", []):
agg_report["errors"][error].append(pdf_filepath)
with open(aggregated_report_filepath, "w") as agg_file:
json.dump(agg_report, agg_file)
def print_aggregated_report(
aggregated_report_filepath, checks_details_url, ignore_whitelist_filepath
):
with open(aggregated_report_filepath) as agg_file:
agg_report = json.load(agg_file)
if "version" in agg_report:
print(agg_report["version"])
print("Documentation on the checks:", checks_details_url)
print("# AGGREGATED REPORT #")
if agg_report["failures"]:
print("Failures:")
for failure, pdf_filepaths in agg_report["failures"].items():
print(f"- {failure} ({len(pdf_filepaths)}): {', '.join(pdf_filepaths)}")
print("Errors:")
sort_key = lambda error: -len(error[1])
for error, pdf_filepaths in sorted(agg_report["errors"].items(), key=sort_key):
print(f"- {error} ({len(pdf_filepaths)}): {', '.join(pdf_filepaths)}")
fail_on_unexpected_check_failure(agg_report, ignore_whitelist_filepath)
def fail_on_unexpected_check_failure(agg_report, ignore_whitelist_filepath):
"exit(1) if there is any non-passing & non-whitelisted error remaining"
with open(ignore_whitelist_filepath) as ignore_file:
ignore = json.load(ignore_file)
errors = set(agg_report["errors"].keys()) - set(ignore["errors"].keys())
if agg_report["failures"] or errors:
print(
"Non-whitelisted issues found:",
", ".join(sorted(agg_report["failures"].keys()) + sorted(errors)),
)
sys.exit(1)