Practracker: add tolerances for exceptions

When an exception is present, we can now violate the limit by a little
bit and only produce a warning.  The strict flag overrides this
behavior.

I've given file sizes a 2% tolerances and function sizes/include
counts a 10% tolerance.

Part of 30752
This commit is contained in:
Nick Mathewson 2019-07-17 15:20:58 +02:00
parent a5e1fa3a03
commit 6303c9aa26
2 changed files with 30 additions and 0 deletions

View File

@ -36,6 +36,13 @@ MAX_FUNCTION_SIZE = 100 # lines
# Recommended number of #includes
MAX_INCLUDE_COUNT = 50
# Map from problem type to functions that adjust for tolerance
TOLERANCE_FNS = {
'include-count': lambda n: int(n*1.1),
'function-size': lambda n: int(n*1.1),
'file-size': lambda n: int(n*1.02)
}
#######################################################
# ProblemVault singleton
@ -169,6 +176,8 @@ def main(argv):
help="List over-strict exceptions")
parser.add_argument("--exceptions",
help="Override the location for the exceptions file")
parser.add_argument("--strict", action="store_true",
help="Make all warnings into errors")
parser.add_argument("topdir", default=".", nargs="?",
help="Top-level directory for the tor source")
args = parser.parse_args(argv[1:])
@ -196,6 +205,11 @@ def main(argv):
else:
ProblemVault = problem.ProblemVault(exceptions_file)
# 2.1) Adjust the exceptions so that we warn only about small problems,
# and produce errors on big ones.
if not (args.regen or args.list_overstrict or args.strict):
ProblemVault.set_tolerances(TOLERANCE_FNS)
# 3) Go through all the files and report problems if they are not exceptions
found_new_issues = consider_all_metrics(files_list)

View File

@ -90,6 +90,15 @@ class ProblemVault(object):
if p is None or e.is_worse_than(p):
yield (e, p)
def set_tolerances(self, fns):
"""Adjust the tolerances for the exceptions in this vault. Takes
a map of problem type to a function that adjusts the permitted
function to its new maximum value."""
for k in self.exceptions:
ex = self.exceptions[k]
fn = fns.get(ex.problem_type)
if fn is not None:
ex.metric_value = fn(ex.metric_value)
class Problem(object):
"""
@ -99,14 +108,21 @@ class Problem(object):
def __init__(self, problem_type, problem_location, metric_value):
self.problem_location = problem_location
self.metric_value = int(metric_value)
self.warning_threshold = self.metric_value
self.problem_type = problem_type
def is_worse_than(self, other_problem):
"""Return True if this is a worse problem than other_problem"""
if self.metric_value > other_problem.metric_value:
return True
elif self.metric_value > other_problem.warning_threshold:
self.warn()
return False
def warn(self):
"""Warn about this problem on stderr only."""
print("(warning) {}".format(self), file=sys.stderr)
def key(self):
"""Generate a unique key that describes this problem that can be used as a dictionary key"""
# Problem location is a filesystem path, so we need to normalize this