[tor-commits] [tor/master] Improve metrics implementation.

nickm at torproject.org nickm at torproject.org
Wed Mar 13 13:30:09 UTC 2019


commit 2a722e2841c9b1631852affd313cc3d0403da7b9
Author: George Kadianakis <desnacked at riseup.net>
Date:   Wed Feb 27 19:29:27 2019 +0200

    Improve metrics implementation.
    
    No longer messes up with MOCK_IMPL. Also update the tests to show that. We are
    still being innacurate on the line count in some cases, but that doesnt matter
    so much.
---
 scripts/maint/practracker/metrics.py           | 17 ++++++++++++++---
 scripts/maint/practracker/practracker_tests.py |  4 ++++
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/scripts/maint/practracker/metrics.py b/scripts/maint/practracker/metrics.py
index 525a05840..c7d2091d0 100644
--- a/scripts/maint/practracker/metrics.py
+++ b/scripts/maint/practracker/metrics.py
@@ -1,8 +1,12 @@
 #!/usr/bin/python
 
+# Implementation of various source code metrics.
+# These are currently ad-hoc string operations and regexps.
+# We might want to use a proper static analysis library in the future, if we want to get more advanced metrics.
+
 import re
 
-def file_len(f):
+def get_file_len(f):
     """Get file length of file"""
     for i, l in enumerate(f):
         pass
@@ -16,14 +20,20 @@ def get_include_count(f):
             include_count += 1
     return include_count
 
-def function_lines(f):
+def get_function_lines(f):
     """
     Return iterator which iterates over functions and returns (function name, function lines)
     """
 
-    # XXX Buggy! Doesn't work with MOCK_IMPL and ENABLE_GCC_WARNINGS
+    # Skip lines with these terms since they confuse our regexp
+    REGEXP_CONFUSE_TERMS = ["MOCK_IMPL", "ENABLE_GCC_WARNINGS", "ENABLE_GCC_WARNING", "DUMMY_TYPECHECK_INSTANCE",
+                            "DISABLE_GCC_WARNING", "DISABLE_GCC_WARNINGS"]
+
     in_function = False
     for lineno, line in enumerate(f):
+        if any(x in line for x in REGEXP_CONFUSE_TERMS):
+            continue
+
         if not in_function:
             # find the start of a function
             m = re.match(r'^([a-zA-Z_][a-zA-Z_0-9]*),?\(', line)
@@ -31,6 +41,7 @@ def function_lines(f):
                 func_name = m.group(1)
                 func_start = lineno
                 in_function = True
+
         else:
             # Fund the end of a function
             if line.startswith("}"):
diff --git a/scripts/maint/practracker/practracker_tests.py b/scripts/maint/practracker/practracker_tests.py
index f3d225207..cdbab2908 100755
--- a/scripts/maint/practracker/practracker_tests.py
+++ b/scripts/maint/practracker/practracker_tests.py
@@ -40,6 +40,10 @@ class TestFunctionLength(unittest.TestCase):
         # All functions should have length 2
         for name, lines in metrics.function_lines(funcs):
             self.assertEqual(name, "fun")
+
+        funcs.seek(0)
+
+        for name, lines in metrics.function_lines(funcs):
             self.assertEqual(lines, 2)
 
 if __name__ == '__main__':





More information about the tor-commits mailing list