| 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 |
# Copyright (C) 2020-2022 Joseph Benden <[email protected]> |
| 7 |
|
| 8 |
""" |
| 9 |
Count the number of issues found in an Infer report. |
| 10 |
|
| 11 |
Usage: count-infer-bugs.py REPORT [MAX-ISSUES] |
| 12 |
Where: |
| 13 |
- REPORT is a file in JSON-format |
| 14 |
- MAX-ISSUES is as a positive integer indicating the maximum |
| 15 |
issues that should be permitted before returning failure |
| 16 |
to the shell. Default is non-limit. |
| 17 |
|
| 18 |
""" |
| 19 |
|
| 20 |
# pylint: disable=invalid-name |
| 21 |
# pylint: disable=missing-docstring |
| 22 |
|
| 23 |
import collections |
| 24 |
import os |
| 25 |
import json |
| 26 |
import sys |
| 27 |
|
| 28 |
def parse_issues(filename): |
| 29 |
""" |
| 30 |
Returns a dict of source filename keys having occurrence-count values |
| 31 |
|
| 32 |
""" |
| 33 |
cwd = os.getcwd() |
| 34 |
issues = collections.defaultdict(int) |
| 35 |
types = collections.defaultdict(int) |
| 36 |
with open(filename) as csvfile: |
| 37 |
reader = json.load(csvfile) |
| 38 |
for row in reader: |
| 39 |
bug_type = row['bug_type_hum'] or row['bug_type'] |
| 40 |
sourcefile = os.path.realpath(row['file']) |
| 41 |
# Skip non-file lines |
| 42 |
if not sourcefile.startswith('/'): |
| 43 |
continue |
| 44 |
sourcefile = os.path.relpath(sourcefile, cwd) |
| 45 |
issues[sourcefile] += 1 |
| 46 |
types[bug_type] += 1 |
| 47 |
return issues, types |
| 48 |
|
| 49 |
|
| 50 |
def main(argv): |
| 51 |
# assume success until proven otherwise |
| 52 |
rcode = 0 |
| 53 |
|
| 54 |
# Get the issues and the total tally |
| 55 |
issues, types = parse_issues(argv[1]) |
| 56 |
tally = sum(issues.values()) |
| 57 |
tally_types = sum(types.values()) |
| 58 |
|
| 59 |
if tally_types > 0: |
| 60 |
# find the longest entry |
| 61 |
longest_name = max(len(entry) for entry in types.keys()) |
| 62 |
# Print the category and their issue counts |
| 63 |
print("Counts sorted by bug category:\n") |
| 64 |
|
| 65 |
for entry in sorted(types, key=types.get, reverse=True): |
| 66 |
print(f' {entry:{longest_name}} : {types[entry]}') |
| 67 |
|
| 68 |
print("") |
| 69 |
|
| 70 |
if tally > 0: |
| 71 |
# find the longest source filename |
| 72 |
longest_name = max(len(sourcefile) for sourcefile in issues.keys()) |
| 73 |
# Print the source filenames and their issue counts |
| 74 |
print("Counts sorted by filename:\n") |
| 75 |
|
| 76 |
for sourcefile in sorted(issues, key=issues.get, reverse=True): |
| 77 |
print(f' {sourcefile:{longest_name}} : {issues[sourcefile]}') |
| 78 |
|
| 79 |
# Print the tally against the desired maximum |
| 80 |
if len(sys.argv) == 3: |
| 81 |
max_issues = int(sys.argv[2]) |
| 82 |
print(f'\nTotal: {tally} issues (out of {max_issues} allowed)') |
| 83 |
if tally > max_issues: |
| 84 |
rcode = 1 |
| 85 |
else: |
| 86 |
print(f'\nTotal: {tally} issues') |
| 87 |
|
| 88 |
return rcode |
| 89 |
|
| 90 |
if __name__ == "__main__": |
| 91 |
sys.exit(main(sys.argv)) |