practracker.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/python
  2. """
  3. Tor code best-practices tracker
  4. Go through the various .c files and collect metrics about them. If the metrics
  5. violate some of our best practices and they are not found in the optional
  6. exceptions file ("./exceptions.txt"), then log a violation about them.
  7. The exceptions file is meant to be initialized with the current state of the
  8. source code as follows: ./practracker.py > ./exceptions.txt
  9. We currently do metrics about file size, function size and number of includes.
  10. TODO:
  11. - How is this tool supposed to be used? How should the exception file work?
  12. How should the UI work? Does it need special exit codes?
  13. - Fix the function_length function so that practracker_tests.py passes.
  14. """
  15. import os, sys
  16. import metrics
  17. import util
  18. # We don't want to run metrics for unittests, automatically-generated C files,
  19. # external libraries or git leftovers.
  20. EXCLUDE_SOURCE_DIRS = ["/src/test/", "/src/trunnel/", "/src/ext/", "/.git/"]
  21. # Where the Tor source code is
  22. TOR_TOPDIR = "../../../"
  23. # An optional exceptions_file
  24. EXCEPTIONS_FILE = "./exceptions.txt"
  25. # Recommended file size
  26. MAX_FILE_SIZE = 3000 # lines
  27. # Recommended function size
  28. MAX_FUNCTION_SIZE = 100 # lines
  29. # Recommended number of #includes
  30. MAX_INCLUDE_COUNT = 50
  31. #######################################################
  32. def print_violation_if_not_exception(violation_str, exceptions_str):
  33. # Check if this violation is already in the optional exceptions file
  34. if exceptions_str and violation_str in exceptions_str:
  35. return
  36. print violation_str
  37. #######################################################
  38. def consider_file_size(fname, f, exceptions_str):
  39. file_size = metrics.file_len(f)
  40. if file_size > MAX_FILE_SIZE:
  41. violation_str = "violation file-size %s %d" % (fname, file_size)
  42. print_violation_if_not_exception(violation_str, exceptions_str)
  43. def consider_includes(fname, f, exceptions_str):
  44. include_count = metrics.get_include_count(f)
  45. if include_count > MAX_INCLUDE_COUNT:
  46. violation_str = "violation include-count %s %d" % (fname, include_count)
  47. print_violation_if_not_exception(violation_str, exceptions_str)
  48. def consider_function_size(fname, f, exceptions_str):
  49. for name, lines in metrics.function_lines(f):
  50. # Don't worry about functions within our limits
  51. if lines <= MAX_FUNCTION_SIZE:
  52. continue
  53. # That's a big function! Issue a violation!
  54. canonical_function_name = "%s:%s()" % (fname,name)
  55. violation_str = "violation function-size %s %s" % (lines, canonical_function_name)
  56. print_violation_if_not_exception(violation_str, exceptions_str)
  57. #######################################################
  58. def consider_all_metrics(files_list, exceptions_str):
  59. """Consider metrics for all files"""
  60. for fname in files_list:
  61. with open(fname, 'r') as f:
  62. consider_metrics_for_file(fname, f, exceptions_str)
  63. def consider_metrics_for_file(fname, f, exceptions_str):
  64. """
  65. Get metrics for file with filename 'fname' and file descriptor 'f'.
  66. """
  67. # Get file length
  68. consider_file_size(fname, f, exceptions_str)
  69. # Consider number of #includes
  70. f.seek(0)
  71. consider_includes(fname, f, exceptions_str)
  72. # Get function length
  73. f.seek(0)
  74. consider_function_size(fname, f, exceptions_str)
  75. def main():
  76. # 1) Get all the .c files we care about
  77. files_list = util.get_tor_c_files(TOR_TOPDIR, EXCLUDE_SOURCE_DIRS)
  78. # 2) Read an optional exceptions file so that we don't warn about the past
  79. exceptions_str = None
  80. try:
  81. with open(EXCEPTIONS_FILE, 'r') as exception_f:
  82. exceptions_str = exception_f.read()
  83. except IOError:
  84. print "No exception file provided"
  85. # 3) Go through all the files and report violations if they are not exceptions
  86. consider_all_metrics(files_list, exceptions_str)
  87. if __name__ == '__main__':
  88. main()