metrics.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/python
  2. import re
  3. def file_len(f):
  4. """Get file length of file"""
  5. for i, l in enumerate(f):
  6. pass
  7. return i + 1
  8. def get_include_count(f):
  9. """Get number of #include statements in the file"""
  10. include_count = 0
  11. for line in f:
  12. if re.match(r' *# *include', line):
  13. include_count += 1
  14. return include_count
  15. def function_lines(f):
  16. """
  17. Return iterator which iterates over functions and returns (function name, function lines)
  18. """
  19. # XXX Buggy! Doesn't work with MOCK_IMPL and ENABLE_GCC_WARNINGS
  20. in_function = False
  21. for lineno, line in enumerate(f):
  22. if not in_function:
  23. # find the start of a function
  24. m = re.match(r'^([a-zA-Z_][a-zA-Z_0-9]*),?\(', line)
  25. if m:
  26. func_name = m.group(1)
  27. func_start = lineno
  28. in_function = True
  29. else:
  30. # Fund the end of a function
  31. if line.startswith("}"):
  32. n_lines = lineno - func_start
  33. in_function = False
  34. yield (func_name, n_lines)