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
17 self.violated_rule = violated_rule
19 # The set of rules containing self.violated_rule.
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
29 self.dependee_path = dependee_path
31 # List of DependencyViolation objects that apply to the dependee
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|.
52 raise NotImplementedError()
55 """Returns the results. May be overridden e.g. to process the
56 results that have been accumulated.
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.
70 def __init__(self, verbose):
72 self.verbose = verbose
74 def AddError(self, dependee_status):
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))
82 def FormatViolation(violation, verbose=False):
85 lines.append(' For %s' % violation.rules)
87 ' Illegal include: "%s"\n Because of %s' %
88 (violation.include_path, str(violation.violated_rule)))
89 return '\n'.join(lines)
94 def PrintResults(self):
95 for result in self.results:
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.
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():
122 class CountViolationsFormatter(ResultsFormatter):
123 """A results formatter that produces a number, the count of #include
124 statements that are in violation of the dependency rules.
126 Note that you normally want to instantiate DepsChecker with
127 ignore_temp_rules=True when you use this formatter.
133 def AddError(self, dependee_status):
134 self.count += len(dependee_status.violations)
136 def GetResults(self):
137 return '%d' % self.count
139 def PrintResults(self):