problem.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. class ProblemVault(object):
  9. """
  10. Singleton where we store the various new problems we
  11. found in the code, and also the old problems we read from the exception
  12. file.
  13. """
  14. def __init__(self, exception_file):
  15. # Exception dictionary: { problem.key() : Problem object }
  16. self.exceptions = {}
  17. if exception_file:
  18. self.register_exceptions(exception_file)
  19. def register_exceptions(self, exception_file):
  20. # Register exceptions
  21. for line in exception_file:
  22. problem = get_old_problem_from_exception_str(line)
  23. if problem is None:
  24. continue
  25. # XXX this might overwrite problems with the same key (e.g. MOCK_IMPL)
  26. self.exceptions[problem.key()] = problem
  27. #print "Registering exception: %s" % problem
  28. def register_problem(self, problem):
  29. # This is a new problem, print it
  30. if problem.key() not in self.exceptions:
  31. print problem
  32. return
  33. # If it's an old problem, we don't warn if the situation got better
  34. # (e.g. we went from 4k LoC to 3k LoC), but we do warn if the
  35. # situation worsened (e.g. we went from 60 includes to 80).
  36. if problem.is_worse_than(self.exceptions[problem.key()]):
  37. print problem
  38. return
  39. # else:
  40. # print "OK %s better than %s" % (problem, self.exceptions[problem.key()])
  41. class Problem(object):
  42. def __init__(self, problem_type, problem_location, metric_value):
  43. self.problem_location = problem_location
  44. self.metric_value = int(metric_value)
  45. self.problem_type = problem_type
  46. def is_worse_than(self, other_problem):
  47. """Return True if this is a worse problem than other_problem"""
  48. if self.metric_value > other_problem.metric_value:
  49. return True
  50. return False
  51. def key(self):
  52. """Generate a unique key that describes this problem that can be used as a dictionary key"""
  53. return "%s:%s" % (self.problem_location, self.problem_type)
  54. def __str__(self):
  55. return "problem %s %s %s" % (self.problem_type, self.problem_location, self.metric_value)
  56. class FileSizeProblem(Problem):
  57. def __init__(self, problem_location, metric_value):
  58. super(FileSizeProblem, self).__init__("file-size", problem_location, metric_value)
  59. class IncludeCountProblem(Problem):
  60. def __init__(self, problem_location, metric_value):
  61. super(IncludeCountProblem, self).__init__("include-count", problem_location, metric_value)
  62. class FunctionSizeProblem(Problem):
  63. def __init__(self, problem_location, metric_value):
  64. super(FunctionSizeProblem, self).__init__("function-size", problem_location, metric_value)
  65. def get_old_problem_from_exception_str(exception_str):
  66. try:
  67. _, problem_type, problem_location, metric_value = exception_str.split(" ")
  68. except ValueError:
  69. return None
  70. if problem_type == "file-size":
  71. return FileSizeProblem(problem_location, metric_value)
  72. elif problem_type == "include-count":
  73. return IncludeCountProblem(problem_location, metric_value)
  74. elif problem_type == "function-size":
  75. return FunctionSizeProblem(problem_location, metric_value)
  76. else:
  77. print "Unknown exception line %s" % exception_str
  78. return None