Debian Codegraph @

build/count-pvs-bugs.py

raw
1 #!/usr/bin/python3
2
3 # SPDX-License-Identifier: GPL-2.0-or-later
4 #
5 # Copyright (C) 2020-2021 Kevin R. Croft <[email protected]>
6
7 """
8 Count the number of issues found in an PVS-Studio report.
9
10 Usage: count-pvs-issues.py REPORT [MAX-ISSUES]
11 Where:
12 - REPORT is a file in CSV-format
13 - MAX-ISSUES is as a positive integer indicating the maximum
14 issues that should be permitted before returning failure
15 to the shell. Default is non-limit.
16
17 """
18
19 # pylint: disable=invalid-name
20 # pylint: disable=missing-docstring
21
22 import collections
23 import csv
24 import os
25 import sys
26
27 def parse_issues(filename):
28 """
29 Returns a dict of source filename keys having occurrence-count values
30
31 """
32 cwd = os.getcwd()
33 issues = collections.defaultdict(int)
34 with open(filename) as csvfile:
35 reader = csv.DictReader(csvfile)
36 for row in reader:
37 # Skip non-file lines
38 if not row['FilePath'].startswith('/'):
39 continue
40 sourcefile = os.path.realpath(row['FilePath'])
41 # Skip non-file lines
42 if not sourcefile.startswith('/'):
43 continue
44 sourcefile = os.path.relpath(sourcefile, cwd)
45 issues[sourcefile] += 1
46 return issues
47
48
49 def main(argv):
50 # assume success until proven otherwise
51 rcode = 0
52
53 # Get the issues and the total tally
54 issues = parse_issues(argv[1])
55 tally = sum(issues.values())
56
57 if tally > 0:
58 # find the longest source filename
59 longest_name = max(len(sourcefile) for sourcefile in issues.keys())
60 # Print the source filenames and their issue counts
61 print("Sorted by issue count:\n")
62
63 for sourcefile in sorted(issues, key=issues.get, reverse=True):
64 print(f' {sourcefile:{longest_name}} : {issues[sourcefile]}')
65
66 # Print the tally against the desired maximum
67 if len(sys.argv) == 3:
68 max_issues = int(sys.argv[2])
69 print(f'\nTotal: {tally} issues (out of {max_issues} allowed)')
70 if tally > max_issues:
71 rcode = 1
72 else:
73 print(f'\nTotal: {tally} issues')
74
75 return rcode
76
77 if __name__ == "__main__":
78 sys.exit(main(sys.argv))