WebKit merge 129140:129167
[chromium-blink-merge.git] / tools / checkdeps / results.py
blob5a50e0ca8c11917e960ab16a2ce4c6731e95d005
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
6 """Results object and results formatters for checkdeps tool."""
9 class DependencyViolation(object):
10 """A single dependency violation."""
12 def __init__(self, include_path, violated_rule, rules):
13 # The include or import path that is in violation of a rule.
14 self.include_path = include_path
16 # The violated rule.
17 self.violated_rule = violated_rule
19 # The set of rules containing self.violated_rule.
20 self.rules = rules
23 class DependeeStatus(object):
24 """Results object for a dependee file."""
26 def __init__(self, dependee_path):
27 # Path of the file whose nonconforming dependencies are listed in
28 # self.violations.
29 self.dependee_path = dependee_path
31 # List of DependencyViolation objects that apply to the dependee
32 # file. May be empty.
33 self.violations = []
35 def AddViolation(self, violation):
36 """Adds a violation."""
37 self.violations.append(violation)
39 def HasViolations(self):
40 """Returns True if this dependee is violating one or more rules."""
41 return not not self.violations
44 class ResultsFormatter(object):
45 """Base class for results formatters."""
47 def AddError(self, dependee_status):
48 """Add a formatted result to |self.results| for |dependee_status|,
49 which is guaranteed to return True for
50 |dependee_status.HasViolations|.
51 """
52 raise NotImplementedError()
54 def GetResults(self):
55 """Returns the results. May be overridden e.g. to process the
56 results that have been accumulated.
57 """
58 raise NotImplementedError()
60 def PrintResults(self):
61 """Prints the results to stdout."""
62 raise NotImplementedError()
65 class NormalResultsFormatter(ResultsFormatter):
66 """A results formatting object that produces the classical,
67 detailed, human-readable output of the checkdeps tool.
68 """
70 def __init__(self, verbose):
71 self.results = []
72 self.verbose = verbose
74 def AddError(self, dependee_status):
75 lines = []
76 lines.append('\nERROR in %s' % dependee_status.dependee_path)
77 for violation in dependee_status.violations:
78 lines.append(self.FormatViolation(violation, self.verbose))
79 self.results.append('\n'.join(lines))
81 @staticmethod
82 def FormatViolation(violation, verbose=False):
83 lines = []
84 if verbose:
85 lines.append(' For %s' % violation.rules)
86 lines.append(
87 ' Illegal include: "%s"\n Because of %s' %
88 (violation.include_path, str(violation.violated_rule)))
89 return '\n'.join(lines)
91 def GetResults(self):
92 return self.results
94 def PrintResults(self):
95 for result in self.results:
96 print result
97 if self.results:
98 print '\nFAILED\n'
101 class TemporaryRulesFormatter(ResultsFormatter):
102 """A results formatter that produces a single line per nonconforming
103 include. The combined output is suitable for directly pasting into a
104 DEPS file as a list of temporary-allow rules.
107 def __init__(self):
108 self.violations = set()
110 def AddError(self, dependee_status):
111 for violation in dependee_status.violations:
112 self.violations.add(violation.include_path)
114 def GetResults(self):
115 return [' "!%s",' % path for path in sorted(self.violations)]
117 def PrintResults(self):
118 for result in self.GetResults():
119 print result