aboutsummaryrefslogtreecommitdiff
path: root/scripts/maint/practracker/problem.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/maint/practracker/problem.py')
-rw-r--r--scripts/maint/practracker/problem.py135
1 files changed, 111 insertions, 24 deletions
diff --git a/scripts/maint/practracker/problem.py b/scripts/maint/practracker/problem.py
index c82c5db572..bee5eeb903 100644
--- a/scripts/maint/practracker/problem.py
+++ b/scripts/maint/practracker/problem.py
@@ -7,12 +7,19 @@ problem is worse than a registered exception so that it only warns when things
get worse.
"""
+# Future imports for Python 2.7, mandatory in 3.0
+from __future__ import division
from __future__ import print_function
+from __future__ import unicode_literals
import os.path
import re
import sys
+STATUS_ERR = 2
+STATUS_WARN = 1
+STATUS_OK = 0
+
class ProblemVault(object):
"""
Singleton where we store the various new problems we
@@ -22,6 +29,9 @@ class ProblemVault(object):
def __init__(self, exception_fname=None):
# Exception dictionary: { problem.key() : Problem object }
self.exceptions = {}
+ # Exception dictionary: maps key to the problem it was used to
+ # suppress.
+ self.used_exception_for = {}
if exception_fname == None:
return
@@ -57,42 +67,93 @@ class ProblemVault(object):
def register_problem(self, problem):
"""
- Register this problem to the problem value. Return True if it was a new
- problem or it worsens an already existing problem.
+ Register this problem to the problem value. Return true if it was a new
+ problem or it worsens an already existing problem. A true
+ value may be STATUS_ERR to indicate a hard violation, or STATUS_WARN
+ to indicate a warning.
"""
# This is a new problem, print it
if problem.key() not in self.exceptions:
- print(problem)
- return True
+ return STATUS_ERR
# If it's an old problem, we don't warn if the situation got better
# (e.g. we went from 4k LoC to 3k LoC), but we do warn if the
# situation worsened (e.g. we went from 60 includes to 80).
- if problem.is_worse_than(self.exceptions[problem.key()]):
- print(problem)
- return True
+ status = problem.is_worse_than(self.exceptions[problem.key()])
+
+ # Remember that we used this exception, so that we can later
+ # determine whether the exception was overbroad.
+ self.used_exception_for[problem.key()] = problem
- return False
+ return status
-class Problem(object):
+ def list_overbroad_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)
+
+ 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 ProblemFilter(object):
+ def __init__(self):
+ self.thresholds = dict()
+
+ def addThreshold(self, item):
+ self.thresholds[(item.get_type(),item.get_file_type())] = item
+
+ def matches(self, item):
+ key = (item.get_type(), item.get_file_type())
+ filt = self.thresholds.get(key, None)
+ if filt is None:
+ return False
+ return item.is_worse_than(filt)
+
+ def filter(self, sequence):
+ for item in iter(sequence):
+ if self.matches(item):
+ yield item
+
+class Item(object):
"""
- A generic problem in our source code. See the subclasses below for the
- specific problems we are trying to tackle.
+ A generic measurement about some aspect of our source code. See
+ the subclasses below for the specific problems we are trying to tackle.
"""
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"""
+ """Return STATUS_ERR if this is a worse problem than other_problem.
+ Return STATUS_WARN if it is a little worse, but falls within the
+ warning threshold. Return STATUS_OK if this problem is not
+ at all worse than other_problem.
+ """
if self.metric_value > other_problem.metric_value:
- return True
- return False
+ return STATUS_ERR
+ elif self.metric_value > other_problem.warning_threshold:
+ return STATUS_WARN
+ else:
+ return STATUS_OK
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
+ # Item location is a filesystem path, so we need to normalize this
# across platforms otherwise same paths are not gonna match.
canonical_location = os.path.normcase(self.problem_location)
return "%s:%s" % (canonical_location, self.problem_type)
@@ -100,7 +161,16 @@ class Problem(object):
def __str__(self):
return "problem %s %s %s" % (self.problem_type, self.problem_location, self.metric_value)
-class FileSizeProblem(Problem):
+ def get_type(self):
+ return self.problem_type
+
+ def get_file_type(self):
+ if self.problem_location.endswith(".h"):
+ return "*.h"
+ else:
+ return "*.c"
+
+class FileSizeItem(Item):
"""
Denotes a problem with the size of a .c file.
@@ -108,9 +178,9 @@ class FileSizeProblem(Problem):
'metric_value' is the number of lines in the .c file.
"""
def __init__(self, problem_location, metric_value):
- super(FileSizeProblem, self).__init__("file-size", problem_location, metric_value)
+ super(FileSizeItem, self).__init__("file-size", problem_location, metric_value)
-class IncludeCountProblem(Problem):
+class IncludeCountItem(Item):
"""
Denotes a problem with the number of #includes in a .c file.
@@ -118,9 +188,9 @@ class IncludeCountProblem(Problem):
'metric_value' is the number of #includes in the .c file.
"""
def __init__(self, problem_location, metric_value):
- super(IncludeCountProblem, self).__init__("include-count", problem_location, metric_value)
+ super(IncludeCountItem, self).__init__("include-count", problem_location, metric_value)
-class FunctionSizeProblem(Problem):
+class FunctionSizeItem(Item):
"""
Denotes a problem with a size of a function in a .c file.
@@ -131,7 +201,22 @@ class FunctionSizeProblem(Problem):
The 'metric_value' is the size of the offending function in lines.
"""
def __init__(self, problem_location, metric_value):
- super(FunctionSizeProblem, self).__init__("function-size", problem_location, metric_value)
+ super(FunctionSizeItem, self).__init__("function-size", problem_location, metric_value)
+
+class DependencyViolationItem(Item):
+ """
+ Denotes a dependency violation in a .c or .h file. A dependency violation
+ occurs when a file includes a file from some module that is not listed
+ in its .may_include file.
+
+ The 'problem_location' is the file that contains the problem.
+
+ The 'metric_value' is the number of forbidden includes.
+ """
+ def __init__(self, problem_location, metric_value):
+ super(DependencyViolationItem, self).__init__("dependency-violation",
+ problem_location,
+ metric_value)
comment_re = re.compile(r'#.*$')
@@ -149,10 +234,12 @@ def get_old_problem_from_exception_str(exception_str):
raise ValueError("Misformatted line {!r}".format(orig_str))
if problem_type == "file-size":
- return FileSizeProblem(problem_location, metric_value)
+ return FileSizeItem(problem_location, metric_value)
elif problem_type == "include-count":
- return IncludeCountProblem(problem_location, metric_value)
+ return IncludeCountItem(problem_location, metric_value)
elif problem_type == "function-size":
- return FunctionSizeProblem(problem_location, metric_value)
+ return FunctionSizeItem(problem_location, metric_value)
+ elif problem_type == "dependency-violation":
+ return DependencyViolationItem(problem_location, metric_value)
else:
raise ValueError("Unknown exception type {!r}".format(orig_str))