practracker.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/python3
  2. """
  3. Best-practices tracker for Tor source code.
  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, then log a problem about them.
  7. We currently do metrics about file size, function size and number of includes.
  8. practracker.py should be run with its second argument pointing to the Tor
  9. top-level source directory like this:
  10. $ python3 ./scripts/maint/practracker/practracker.py .
  11. The exceptions file is meant to be initialized once with the current state of
  12. the source code and then get saved in the repository for ever after:
  13. $ python3 ./scripts/maint/practracker/practracker.py . > ./scripts/maint/practracker/exceptions.txt
  14. """
  15. from __future__ import print_function
  16. import os, sys
  17. import metrics
  18. import util
  19. import problem
  20. # The filename of the exceptions file (it should be placed in the practracker directory)
  21. EXCEPTIONS_FNAME = "./exceptions.txt"
  22. # Recommended file size
  23. MAX_FILE_SIZE = 3000 # lines
  24. # Recommended function size
  25. MAX_FUNCTION_SIZE = 100 # lines
  26. # Recommended number of #includes
  27. MAX_INCLUDE_COUNT = 50
  28. #######################################################
  29. # ProblemVault singleton
  30. ProblemVault = None
  31. # The Tor source code topdir
  32. TOR_TOPDIR = None
  33. #######################################################
  34. if sys.version_info[0] <= 2:
  35. def open_file(fname):
  36. return open(fname, 'r')
  37. else:
  38. def open_file(fname):
  39. return open(fname, 'r', encoding='utf-8')
  40. def consider_file_size(fname, f):
  41. """Consider file size issues for 'f' and return True if a new issue was found"""
  42. file_size = metrics.get_file_len(f)
  43. if file_size > MAX_FILE_SIZE:
  44. p = problem.FileSizeProblem(fname, file_size)
  45. return ProblemVault.register_problem(p)
  46. return False
  47. def consider_includes(fname, f):
  48. """Consider #include issues for 'f' and return True if a new issue was found"""
  49. include_count = metrics.get_include_count(f)
  50. if include_count > MAX_INCLUDE_COUNT:
  51. p = problem.IncludeCountProblem(fname, include_count)
  52. return ProblemVault.register_problem(p)
  53. return False
  54. def consider_function_size(fname, f):
  55. """Consider the function sizes for 'f' and return True if a new issue was found"""
  56. found_new_issues = False
  57. for name, lines in metrics.get_function_lines(f):
  58. # Don't worry about functions within our limits
  59. if lines <= MAX_FUNCTION_SIZE:
  60. continue
  61. # That's a big function! Issue a problem!
  62. canonical_function_name = "%s:%s()" % (fname, name)
  63. p = problem.FunctionSizeProblem(canonical_function_name, lines)
  64. found_new_issues |= ProblemVault.register_problem(p)
  65. return found_new_issues
  66. #######################################################
  67. def consider_all_metrics(files_list):
  68. """Consider metrics for all files, and return True if new issues were found"""
  69. found_new_issues = False
  70. for fname in files_list:
  71. with open_file(fname) as f:
  72. found_new_issues |= consider_metrics_for_file(fname, f)
  73. return found_new_issues
  74. def consider_metrics_for_file(fname, f):
  75. """
  76. Consider the various metrics for file with filename 'fname' and file descriptor 'f'.
  77. Return True if we found new issues.
  78. """
  79. # Strip the useless part of the path
  80. if fname.startswith(TOR_TOPDIR):
  81. fname = fname[len(TOR_TOPDIR):]
  82. found_new_issues = False
  83. # Get file length
  84. found_new_issues |= consider_file_size(fname, f)
  85. # Consider number of #includes
  86. f.seek(0)
  87. found_new_issues |= consider_includes(fname, f)
  88. # Get function length
  89. f.seek(0)
  90. found_new_issues |= consider_function_size(fname, f)
  91. return found_new_issues
  92. def main():
  93. if (len(sys.argv) != 2):
  94. print("Usage:\n\t$ practracker.py <tor topdir>\n\t(e.g. $ practracker.py ~/tor/)")
  95. return
  96. global TOR_TOPDIR
  97. TOR_TOPDIR = sys.argv[1]
  98. exceptions_file = os.path.join(TOR_TOPDIR, "scripts/maint/practracker", EXCEPTIONS_FNAME)
  99. # 1) Get all the .c files we care about
  100. files_list = util.get_tor_c_files(TOR_TOPDIR)
  101. # 2) Initialize problem vault and load an optional exceptions file so that
  102. # we don't warn about the past
  103. global ProblemVault
  104. ProblemVault = problem.ProblemVault(exceptions_file)
  105. # 3) Go through all the files and report problems if they are not exceptions
  106. found_new_issues = consider_all_metrics(files_list)
  107. # If new issues were found, try to give out some advice to the developer on how to resolve it.
  108. if (found_new_issues):
  109. new_issues_str = """\
  110. FAILURE: practracker found new problems in the code: see warnings above.
  111. Please fix the problems if you can, and update the exceptions file
  112. ({}) if you can't.
  113. See doc/HACKING/HelpfulTools.md for more information on using practracker.\
  114. """.format(exceptions_file)
  115. print(new_issues_str)
  116. sys.exit(found_new_issues)
  117. if __name__ == '__main__':
  118. main()