Practracker: add a --list-overstrict option

This option lists every exception that is stricter than it needs to
be.

Part of 30752
This commit is contained in:
Nick Mathewson 2019-07-17 15:06:34 +02:00
parent 78768aafe1
commit a5e1fa3a03
2 changed files with 29 additions and 0 deletions

View File

@ -56,6 +56,7 @@ else:
def consider_file_size(fname, f): def consider_file_size(fname, f):
"""Consider file size issues for 'f' and return the number of new issues was found""" """Consider file size issues for 'f' and return the number of new issues was found"""
file_size = metrics.get_file_len(f) file_size = metrics.get_file_len(f)
if file_size > MAX_FILE_SIZE: if file_size > MAX_FILE_SIZE:
p = problem.FileSizeProblem(fname, file_size) p = problem.FileSizeProblem(fname, file_size)
if ProblemVault.register_problem(p): if ProblemVault.register_problem(p):
@ -164,6 +165,8 @@ def main(argv):
parser = argparse.ArgumentParser(prog=progname) parser = argparse.ArgumentParser(prog=progname)
parser.add_argument("--regen", action="store_true", parser.add_argument("--regen", action="store_true",
help="Regenerate the exceptions file") help="Regenerate the exceptions file")
parser.add_argument("--list-overstrict", action="store_true",
help="List over-strict exceptions")
parser.add_argument("--exceptions", parser.add_argument("--exceptions",
help="Override the location for the exceptions file") help="Override the location for the exceptions file")
parser.add_argument("topdir", default=".", nargs="?", parser.add_argument("topdir", default=".", nargs="?",
@ -213,6 +216,15 @@ See doc/HACKING/HelpfulTools.md for more information on using practracker.\
""".format(found_new_issues, exceptions_file) """.format(found_new_issues, exceptions_file)
print(new_issues_str) print(new_issues_str)
if args.list_overstrict:
def k_fn(tup):
return tup[0].key()
for (ex,p) in sorted(ProblemVault.list_overstrict_exceptions(), key=k_fn):
if p is None:
print(ex, "->", 0)
else:
print(ex, "->", p.metric_value)
sys.exit(found_new_issues) sys.exit(found_new_issues)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -22,6 +22,9 @@ class ProblemVault(object):
def __init__(self, exception_fname=None): def __init__(self, exception_fname=None):
# Exception dictionary: { problem.key() : Problem object } # Exception dictionary: { problem.key() : Problem object }
self.exceptions = {} self.exceptions = {}
# Exception dictionary: maps key to the problem it was used to
# suppress.
self.used_exception_for = {}
if exception_fname == None: if exception_fname == None:
return return
@ -71,9 +74,23 @@ class ProblemVault(object):
if problem.is_worse_than(self.exceptions[problem.key()]): if problem.is_worse_than(self.exceptions[problem.key()]):
print(problem) print(problem)
return True return True
else:
self.used_exception_for[problem.key()] = problem
return False return False
def list_overstrict_exceptions(self):
"""Return an iterator of tuples containing (ex,prob) where ex is an
exceptions in this vault that are stricter than it needs to be, and
prob is the worst problem (if any) that it covered.
"""
for k in self.exceptions:
e = self.exceptions[k]
p = self.used_exception_for.get(k)
if p is None or e.is_worse_than(p):
yield (e, p)
class Problem(object): class Problem(object):
""" """
A generic problem in our source code. See the subclasses below for the A generic problem in our source code. See the subclasses below for the