metrics.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/python
  2. # Implementation of various source code metrics.
  3. # These are currently ad-hoc string operations and regexps.
  4. # We might want to use a proper static analysis library in the future, if we want to get more advanced metrics.
  5. import re
  6. def get_file_len(f):
  7. """Get file length of file"""
  8. for i, l in enumerate(f):
  9. pass
  10. return i + 1
  11. def get_include_count(f):
  12. """Get number of #include statements in the file"""
  13. include_count = 0
  14. for line in f:
  15. if re.match(r' *# *include', line):
  16. include_count += 1
  17. return include_count
  18. def get_function_lines(f):
  19. """
  20. Return iterator which iterates over functions and returns (function name, function lines)
  21. """
  22. # Skip lines with these terms since they confuse our regexp
  23. REGEXP_CONFUSE_TERMS = {"MOCK_IMPL", "ENABLE_GCC_WARNINGS", "ENABLE_GCC_WARNING", "DUMMY_TYPECHECK_INSTANCE",
  24. "DISABLE_GCC_WARNING", "DISABLE_GCC_WARNINGS"}
  25. in_function = False
  26. for lineno, line in enumerate(f):
  27. if any(x in line for x in REGEXP_CONFUSE_TERMS):
  28. continue
  29. if not in_function:
  30. # find the start of a function
  31. m = re.match(r'^([a-zA-Z_][a-zA-Z_0-9]*),?\(', line)
  32. if m:
  33. func_name = m.group(1)
  34. func_start = lineno
  35. in_function = True
  36. else:
  37. # Find the end of a function
  38. if line.startswith("}"):
  39. n_lines = lineno - func_start
  40. in_function = False
  41. yield (func_name, n_lines)