problem.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. from __future__ import print_function
  9. import os.path
  10. import re
  11. import sys
  12. STATUS_ERR = 2
  13. STATUS_WARN = 1
  14. STATUS_OK = 0
  15. class ProblemVault(object):
  16. """
  17. Singleton where we store the various new problems we
  18. found in the code, and also the old problems we read from the exception
  19. file.
  20. """
  21. def __init__(self, exception_fname=None):
  22. # Exception dictionary: { problem.key() : Problem object }
  23. self.exceptions = {}
  24. # Exception dictionary: maps key to the problem it was used to
  25. # suppress.
  26. self.used_exception_for = {}
  27. if exception_fname == None:
  28. return
  29. try:
  30. with open(exception_fname, 'r') as exception_f:
  31. self.register_exceptions(exception_f)
  32. except IOError:
  33. print("No exception file provided", file=sys.stderr)
  34. def register_exceptions(self, exception_file):
  35. # Register exceptions
  36. for lineno, line in enumerate(exception_file, 1):
  37. try:
  38. problem = get_old_problem_from_exception_str(line)
  39. except ValueError as v:
  40. print("Exception file line {} not recognized: {}"
  41. .format(lineno,v),
  42. file=sys.stderr)
  43. continue
  44. if problem is None:
  45. continue
  46. # Fail if we see dup exceptions. There is really no reason to have dup exceptions.
  47. if problem.key() in self.exceptions:
  48. print("Duplicate exceptions lines found in exception file:\n\t{}\n\t{}\nAborting...".format(problem, self.exceptions[problem.key()]),
  49. file=sys.stderr)
  50. sys.exit(1)
  51. self.exceptions[problem.key()] = problem
  52. #print "Registering exception: %s" % problem
  53. def register_problem(self, problem):
  54. """
  55. Register this problem to the problem value. Return true if it was a new
  56. problem or it worsens an already existing problem. A true
  57. value may be STATUS_ERR to indicate a hard violation, or STATUS_WARN
  58. to indicate a warning.
  59. """
  60. # This is a new problem, print it
  61. if problem.key() not in self.exceptions:
  62. return STATUS_ERR
  63. # If it's an old problem, we don't warn if the situation got better
  64. # (e.g. we went from 4k LoC to 3k LoC), but we do warn if the
  65. # situation worsened (e.g. we went from 60 includes to 80).
  66. status = problem.is_worse_than(self.exceptions[problem.key()])
  67. if status == STATUS_OK:
  68. self.used_exception_for[problem.key()] = problem
  69. return status
  70. def list_overbroad_exceptions(self):
  71. """Return an iterator of tuples containing (ex,prob) where ex is an
  72. exceptions in this vault that are stricter than it needs to be, and
  73. prob is the worst problem (if any) that it covered.
  74. """
  75. for k in self.exceptions:
  76. e = self.exceptions[k]
  77. p = self.used_exception_for.get(k)
  78. if p is None or e.is_worse_than(p):
  79. yield (e, p)
  80. def set_tolerances(self, fns):
  81. """Adjust the tolerances for the exceptions in this vault. Takes
  82. a map of problem type to a function that adjusts the permitted
  83. function to its new maximum value."""
  84. for k in self.exceptions:
  85. ex = self.exceptions[k]
  86. fn = fns.get(ex.problem_type)
  87. if fn is not None:
  88. ex.metric_value = fn(ex.metric_value)
  89. class ProblemFilter(object):
  90. def __init__(self):
  91. self.thresholds = dict()
  92. def addThreshold(self, item):
  93. self.thresholds[item.get_type()] = item
  94. def matches(self, item):
  95. filt = self.thresholds.get(item.get_type(), None)
  96. if filt is None:
  97. return False
  98. return item.is_worse_than(filt)
  99. def filter(self, sequence):
  100. for item in iter(sequence):
  101. if self.matches(item):
  102. yield item
  103. class Item(object):
  104. """
  105. A generic measurement about some aspect of our source code. See
  106. the subclasses below for the specific problems we are trying to tackle.
  107. """
  108. def __init__(self, problem_type, problem_location, metric_value):
  109. self.problem_location = problem_location
  110. self.metric_value = int(metric_value)
  111. self.warning_threshold = self.metric_value
  112. self.problem_type = problem_type
  113. def is_worse_than(self, other_problem):
  114. """Return STATUS_ERR if this is a worse problem than other_problem.
  115. Return STATUS_WARN if it is a little worse, but falls within the
  116. warning threshold. Return STATUS_OK if this problem is not
  117. at all worse than other_problem.
  118. """
  119. if self.metric_value > other_problem.metric_value:
  120. return STATUS_ERR
  121. elif self.metric_value > other_problem.warning_threshold:
  122. return STATUS_WARN
  123. else:
  124. return STATUS_OK
  125. def key(self):
  126. """Generate a unique key that describes this problem that can be used as a dictionary key"""
  127. # Item location is a filesystem path, so we need to normalize this
  128. # across platforms otherwise same paths are not gonna match.
  129. canonical_location = os.path.normcase(self.problem_location)
  130. return "%s:%s" % (canonical_location, self.problem_type)
  131. def __str__(self):
  132. return "problem %s %s %s" % (self.problem_type, self.problem_location, self.metric_value)
  133. def get_type(self):
  134. return self.problem_type
  135. class FileSizeItem(Item):
  136. """
  137. Denotes a problem with the size of a .c file.
  138. The 'problem_location' is the filesystem path of the .c file, and the
  139. 'metric_value' is the number of lines in the .c file.
  140. """
  141. def __init__(self, problem_location, metric_value):
  142. super(FileSizeItem, self).__init__("file-size", problem_location, metric_value)
  143. class IncludeCountItem(Item):
  144. """
  145. Denotes a problem with the number of #includes in a .c file.
  146. The 'problem_location' is the filesystem path of the .c file, and the
  147. 'metric_value' is the number of #includes in the .c file.
  148. """
  149. def __init__(self, problem_location, metric_value):
  150. super(IncludeCountItem, self).__init__("include-count", problem_location, metric_value)
  151. class FunctionSizeItem(Item):
  152. """
  153. Denotes a problem with a size of a function in a .c file.
  154. The 'problem_location' is "<path>:<function>()" where <path> is the
  155. filesystem path of the .c file and <function> is the name of the offending
  156. function.
  157. The 'metric_value' is the size of the offending function in lines.
  158. """
  159. def __init__(self, problem_location, metric_value):
  160. super(FunctionSizeItem, self).__init__("function-size", problem_location, metric_value)
  161. comment_re = re.compile(r'#.*$')
  162. def get_old_problem_from_exception_str(exception_str):
  163. orig_str = exception_str
  164. exception_str = comment_re.sub("", exception_str)
  165. fields = exception_str.split()
  166. if len(fields) == 0:
  167. # empty line or comment
  168. return None
  169. elif len(fields) == 4:
  170. # valid line
  171. _, problem_type, problem_location, metric_value = fields
  172. else:
  173. raise ValueError("Misformatted line {!r}".format(orig_str))
  174. if problem_type == "file-size":
  175. return FileSizeItem(problem_location, metric_value)
  176. elif problem_type == "include-count":
  177. return IncludeCountItem(problem_location, metric_value)
  178. elif problem_type == "function-size":
  179. return FunctionSizeItem(problem_location, metric_value)
  180. else:
  181. raise ValueError("Unknown exception type {!r}".format(orig_str))