problem.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """
  2. In this file we define a ProblemVault class where we store all the
  3. exceptions and all the problems we find with the code.
  4. The ProblemVault is capable of registering problems and also figuring out if a
  5. problem is worse than a registered exception so that it only warns when things
  6. get worse.
  7. """
  8. import os.path
  9. class ProblemVault(object):
  10. """
  11. Singleton where we store the various new problems we
  12. found in the code, and also the old problems we read from the exception
  13. file.
  14. """
  15. def __init__(self, exception_fname):
  16. # Exception dictionary: { problem.key() : Problem object }
  17. self.exceptions = {}
  18. try:
  19. with open(exception_fname, 'r') as exception_f:
  20. self.register_exceptions(exception_f)
  21. except IOError:
  22. print("No exception file provided")
  23. def register_exceptions(self, exception_file):
  24. # Register exceptions
  25. for line in exception_file:
  26. problem = get_old_problem_from_exception_str(line)
  27. if problem is None:
  28. continue
  29. self.exceptions[problem.key()] = problem
  30. #print "Registering exception: %s" % problem
  31. def register_problem(self, problem):
  32. """
  33. Register this problem to the problem value. Return True if it was a new
  34. problem or it worsens an already existing problem.
  35. """
  36. # This is a new problem, print it
  37. if problem.key() not in self.exceptions:
  38. print(problem)
  39. return True
  40. # If it's an old problem, we don't warn if the situation got better
  41. # (e.g. we went from 4k LoC to 3k LoC), but we do warn if the
  42. # situation worsened (e.g. we went from 60 includes to 80).
  43. if problem.is_worse_than(self.exceptions[problem.key()]):
  44. print(problem)
  45. return True
  46. return False
  47. class Problem(object):
  48. """
  49. A generic problem in our source code. See the subclasses below for the
  50. specific problems we are trying to tackle.
  51. """
  52. def __init__(self, problem_type, problem_location, metric_value):
  53. self.problem_location = problem_location
  54. self.metric_value = int(metric_value)
  55. self.problem_type = problem_type
  56. def is_worse_than(self, other_problem):
  57. """Return True if this is a worse problem than other_problem"""
  58. if self.metric_value > other_problem.metric_value:
  59. return True
  60. return False
  61. def key(self):
  62. """Generate a unique key that describes this problem that can be used as a dictionary key"""
  63. # Problem location is a filesystem path, so we need to normalize this
  64. # across platforms otherwise same paths are not gonna match.
  65. canonical_location = os.path.normcase(self.problem_location)
  66. return "%s:%s" % (canonical_location, self.problem_type)
  67. def __str__(self):
  68. return "problem %s %s %s" % (self.problem_type, self.problem_location, self.metric_value)
  69. class FileSizeProblem(Problem):
  70. """
  71. Denotes a problem with the size of a .c file.
  72. The 'problem_location' is the filesystem path of the .c file, and the
  73. 'metric_value' is the number of lines in the .c file.
  74. """
  75. def __init__(self, problem_location, metric_value):
  76. super(FileSizeProblem, self).__init__("file-size", problem_location, metric_value)
  77. class IncludeCountProblem(Problem):
  78. """
  79. Denotes a problem with the number of #includes in a .c file.
  80. The 'problem_location' is the filesystem path of the .c file, and the
  81. 'metric_value' is the number of #includes in the .c file.
  82. """
  83. def __init__(self, problem_location, metric_value):
  84. super(IncludeCountProblem, self).__init__("include-count", problem_location, metric_value)
  85. class FunctionSizeProblem(Problem):
  86. """
  87. Denotes a problem with a size of a function in a .c file.
  88. The 'problem_location' is "<path>:<function>()" where <path> is the
  89. filesystem path of the .c file and <function> is the name of the offending
  90. function.
  91. The 'metric_value' is the size of the offending function in lines.
  92. """
  93. def __init__(self, problem_location, metric_value):
  94. super(FunctionSizeProblem, self).__init__("function-size", problem_location, metric_value)
  95. def get_old_problem_from_exception_str(exception_str):
  96. try:
  97. _, problem_type, problem_location, metric_value = exception_str.split(" ")
  98. except ValueError:
  99. return None
  100. if problem_type == "file-size":
  101. return FileSizeProblem(problem_location, metric_value)
  102. elif problem_type == "include-count":
  103. return IncludeCountProblem(problem_location, metric_value)
  104. elif problem_type == "function-size":
  105. return FunctionSizeProblem(problem_location, metric_value)
  106. else:
  107. # print("Unknown exception line '{}'".format(exception_str))
  108. return None