| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 """Results object and results formatters for checkdeps tool.""" | 6 """Results object and results formatters for checkdeps tool.""" |
| 7 | 7 |
| 8 | 8 |
| 9 import json |
| 10 |
| 11 |
| 9 class DependencyViolation(object): | 12 class DependencyViolation(object): |
| 10 """A single dependency violation.""" | 13 """A single dependency violation.""" |
| 11 | 14 |
| 12 def __init__(self, include_path, violated_rule, rules): | 15 def __init__(self, include_path, violated_rule, rules): |
| 13 # The include or import path that is in violation of a rule. | 16 # The include or import path that is in violation of a rule. |
| 14 self.include_path = include_path | 17 self.include_path = include_path |
| 15 | 18 |
| 16 # The violated rule. | 19 # The violated rule. |
| 17 self.violated_rule = violated_rule | 20 self.violated_rule = violated_rule |
| 18 | 21 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 def GetResults(self): | 94 def GetResults(self): |
| 92 return self.results | 95 return self.results |
| 93 | 96 |
| 94 def PrintResults(self): | 97 def PrintResults(self): |
| 95 for result in self.results: | 98 for result in self.results: |
| 96 print result | 99 print result |
| 97 if self.results: | 100 if self.results: |
| 98 print '\nFAILED\n' | 101 print '\nFAILED\n' |
| 99 | 102 |
| 100 | 103 |
| 104 class JSONResultsFormatter(ResultsFormatter): |
| 105 """A results formatter that outputs results to a file as JSON.""" |
| 106 |
| 107 def __init__(self, output_path, wrapped_formatter=None): |
| 108 self.output_path = output_path |
| 109 self.wrapped_formatter = wrapped_formatter |
| 110 |
| 111 self.results = [] |
| 112 |
| 113 def AddError(self, dependee_status): |
| 114 self.results.append({ |
| 115 'dependee_path': dependee_status.dependee_path, |
| 116 'violations': [{ |
| 117 'include_path': violation.include_path, |
| 118 'violated_rule': violation.violated_rule.AsDependencyTuple(), |
| 119 } for violation in dependee_status.violations] |
| 120 }) |
| 121 |
| 122 if self.wrapped_formatter: |
| 123 self.wrapped_formatter.AddError(dependee_status) |
| 124 |
| 125 def GetResults(self): |
| 126 with open(self.output_path, 'w') as f: |
| 127 f.write(json.dumps(self.results)) |
| 128 |
| 129 |
| 130 def PrintResults(self): |
| 131 if self.wrapped_formatter: |
| 132 self.wrapped_formatter.PrintResults() |
| 133 return |
| 134 |
| 135 print self.results |
| 136 |
| 137 |
| 101 class TemporaryRulesFormatter(ResultsFormatter): | 138 class TemporaryRulesFormatter(ResultsFormatter): |
| 102 """A results formatter that produces a single line per nonconforming | 139 """A results formatter that produces a single line per nonconforming |
| 103 include. The combined output is suitable for directly pasting into a | 140 include. The combined output is suitable for directly pasting into a |
| 104 DEPS file as a list of temporary-allow rules. | 141 DEPS file as a list of temporary-allow rules. |
| 105 """ | 142 """ |
| 106 | 143 |
| 107 def __init__(self): | 144 def __init__(self): |
| 108 self.violations = set() | 145 self.violations = set() |
| 109 | 146 |
| 110 def AddError(self, dependee_status): | 147 def AddError(self, dependee_status): |
| (...skipping 20 matching lines...) Expand all Loading... |
| 131 self.count = 0 | 168 self.count = 0 |
| 132 | 169 |
| 133 def AddError(self, dependee_status): | 170 def AddError(self, dependee_status): |
| 134 self.count += len(dependee_status.violations) | 171 self.count += len(dependee_status.violations) |
| 135 | 172 |
| 136 def GetResults(self): | 173 def GetResults(self): |
| 137 return '%d' % self.count | 174 return '%d' % self.count |
| 138 | 175 |
| 139 def PrintResults(self): | 176 def PrintResults(self): |
| 140 print self.count | 177 print self.count |
| OLD | NEW |