]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Practracker: add tolerances for exceptions
authorNick Mathewson <nickm@torproject.org>
Wed, 17 Jul 2019 13:20:58 +0000 (15:20 +0200)
committerNick Mathewson <nickm@torproject.org>
Thu, 18 Jul 2019 13:28:08 +0000 (09:28 -0400)
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

scripts/maint/practracker/practracker.py
scripts/maint/practracker/problem.py

index 1b6502fe5a20829bd548b471fe90c790cfb50ac4..75cd44d228c3238dbf4b3945a9f11c0bc565b5a0 100755 (executable)
@@ -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)
 
index 751ceb910556e9e89b91c55644c67905efdb24e2..89a8f12346e83ba700f69ed4a6193532fb1b87af 100644 (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