tor/scripts/maint/practracker/metrics.py

51 lines
1.6 KiB
Python
Raw Normal View History

2019-02-27 14:14:19 +01:00
#!/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.
2019-02-27 14:14:19 +01:00
import re
def get_file_len(f):
2019-02-27 14:14:19 +01:00
"""Get file length of file"""
for i, l in enumerate(f):
pass
return i + 1
def get_include_count(f):
"""Get number of #include statements in the file"""
include_count = 0
for line in f:
if re.match(r' *# *include', line):
include_count += 1
return include_count
def get_function_lines(f):
2019-02-27 14:14:19 +01:00
"""
Return iterator which iterates over functions and returns (function name, function lines)
"""
# 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"]
2019-02-27 14:14:19 +01:00
in_function = False
for lineno, line in enumerate(f):
if any(x in line for x in REGEXP_CONFUSE_TERMS):
continue
2019-02-27 14:14:19 +01:00
if not in_function:
# find the start of a function
m = re.match(r'^([a-zA-Z_][a-zA-Z_0-9]*),?\(', line)
if m:
func_name = m.group(1)
func_start = lineno
in_function = True
2019-02-27 14:14:19 +01:00
else:
# Fund the end of a function
if line.startswith("}"):
n_lines = lineno - func_start
in_function = False
yield (func_name, n_lines)