Skip to content

Commit cea5ce7

Browse files
committed
Add guideline recategorization report generation
1 parent 4461b2d commit cea5ce7

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

scripts/reports/analysis_report.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import diagnostics
22
import deviations
3+
import guideline_recategorizations
34
from pathlib import Path
45
import sys
56
import utils
@@ -53,6 +54,8 @@
5354
deviations.generate_deviations_report(
5455
database_path, repo_root, output_directory)
5556

57+
guideline_recategorizations.generate_guideline_recategorizations_report(database_path, repo_root, output_directory)
58+
5659
# Load the SARIF file and generate a results summary
5760
sarif_results_summary = utils.CodingStandardsResultSummary(
5861
sarif_results_file_path)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
from contextlib import redirect_stdout
2+
from pathlib import Path
3+
from codeqlvalidation import CodeQLValidationSummary
4+
from error import failure
5+
import sys
6+
7+
script_path = Path(__file__)
8+
# Add the shared modules to the path so we can import them.
9+
sys.path.append(str(script_path.parent.parent / 'shared'))
10+
from codeql import CodeQLError
11+
12+
13+
if __name__ == '__main__':
14+
failure("Error: this Python module does not support standalone execution!")
15+
16+
17+
class GuidelineRecategorizationsSummary:
18+
def __init__(self, database_path, repo_root):
19+
if isinstance(database_path, str):
20+
database_path = Path(database_path)
21+
if isinstance(repo_root, str):
22+
repo_root = Path(repo_root)
23+
24+
self.database_path = database_path
25+
try:
26+
self.codeql_summary = CodeQLValidationSummary()
27+
except CodeQLError as err:
28+
failure("Error: Could not initialize CodeQL", err)
29+
30+
guideline_recategorizations_path = repo_root.joinpath(
31+
'cpp', 'common', 'src', 'codingstandards', 'cpp', 'guideline_recategorizations')
32+
33+
queries = ['ListGuidelineRecategorizations.ql', 'InvalidGuidelineRecategorizations.ql']
34+
35+
query_paths = map(guideline_recategorizations_path.joinpath, queries)
36+
37+
try:
38+
# Cleanup database cache to prevent potential cache issue
39+
self.codeql_summary.codeql.cleanup(database_path, mode="brutal")
40+
# Get a list of guideline recategorizations
41+
print("Running the guideline recategorizations queries...")
42+
self.codeql_summary.codeql.run_queries(
43+
database_path, *query_paths, search_path=str(repo_root), no_rerun=True)
44+
45+
print("Decoding guideline recategorizations queries results")
46+
47+
for query in queries:
48+
if query.startswith("List"):
49+
decoded_results = self.codeql_summary.codeql.decode_results(
50+
database_path, guideline_recategorizations_path.joinpath(query), no_titles=True)
51+
self.guideline_recategorizations = decoded_results
52+
elif query.startswith("Invalid"):
53+
decoded_results = self.codeql_summary.codeql.decode_results(
54+
database_path, guideline_recategorizations_path.joinpath(query), entities='url,string', no_titles=True)
55+
self.invalid_guideline_recategorizations = decoded_results
56+
else:
57+
failure(
58+
f"Error: Don't know how to decode query results for {query}")
59+
except CodeQLError as err:
60+
failure("Error: Failed to run guideline recategorizations queries", err)
61+
62+
63+
def generate_guideline_recategorizations_report(database_path, repo_root, output_directory):
64+
"""Print a "guideline recategorizations report"."""
65+
66+
guideline_recategorizations_summary = GuidelineRecategorizationsSummary(database_path, repo_root)
67+
guideline_recategorizations_report_path = output_directory.joinpath(
68+
"guideline_recategorizations_report.md")
69+
try:
70+
guideline_recategorizations_report_file = open(
71+
guideline_recategorizations_report_path, "w")
72+
except PermissionError:
73+
failure(
74+
f"Error: No permission to write to the output file located at '{guideline_recategorizations_report_path}'")
75+
else:
76+
with guideline_recategorizations_report_file:
77+
# Print to report file, rather than stdout
78+
with redirect_stdout(guideline_recategorizations_report_file):
79+
print("# Guideline recategorizations report")
80+
print()
81+
print("## Overview")
82+
print()
83+
print(
84+
f" - Report generated with {'supported' if guideline_recategorizations_summary.codeql_summary.supported_cli else 'unsupported'} CodeQL version {guideline_recategorizations_summary.codeql_summary.codeql.version}")
85+
print(
86+
f" - Database path: {str(guideline_recategorizations_summary.database_path.resolve())}")
87+
number_of_guideline_recategorizations = len(
88+
guideline_recategorizations_summary.guideline_recategorizations)
89+
number_of_invalid_guideline_recategorizations = len(
90+
guideline_recategorizations_summary.invalid_guideline_recategorizations)
91+
print(
92+
f" - { number_of_guideline_recategorizations } applicable guideline recategorizations and {number_of_invalid_guideline_recategorizations} invalid guideline recategorizations found in the database")
93+
print()
94+
print("## Guideline recategorizations")
95+
print()
96+
print(
97+
"| Rule ID | Category | Recategorized category")
98+
print(
99+
"| --- | --- | --- |")
100+
for guideline_recategorization in guideline_recategorizations_summary.guideline_recategorizations:
101+
rule_id = guideline_recategorization[0]
102+
category = guideline_recategorization[1]
103+
recategorized_category = guideline_recategorization[2]
104+
print(
105+
f"| { rule_id } | { category } | { recategorized_category } | ")
106+
print()
107+
print("## Invalid guideline recategorizations")
108+
print("| Path | Reason |")
109+
print("| --- | --- |")
110+
for invalid_guideline_recategorization in guideline_recategorizations_summary.invalid_guideline_recategorizations:
111+
location = invalid_guideline_recategorization[1].split(':', 2)[2]
112+
path, reason = map(
113+
str.strip, invalid_guideline_recategorization[2].split(':'))
114+
print(f"| {path}:{location} | {reason} |")

0 commit comments

Comments
 (0)