| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 import collections | 5 import collections |
| 6 import copy | 6 import copy |
| 7 | 7 |
| 8 | 8 |
| 9 class FailureSignal(object): | 9 class FailureSignal(object): |
| 10 """Represents the signals extracted from a failed step or test.""" | 10 """Represents the signals extracted from a failed step or test.""" |
| 11 | 11 |
| 12 def __init__(self): | 12 def __init__(self): |
| 13 self.files = collections.defaultdict(list) | 13 self.files = collections.defaultdict(list) |
| 14 self.keywords = collections.defaultdict(int) | 14 self.keywords = collections.defaultdict(int) |
| 15 self.failed_targets = [] | 15 self.failed_targets = [] # A list of dict. |
| 16 self.failed_output_nodes = [] # A list of string. |
| 16 | 17 |
| 17 def AddFile(self, file_path, line_number=None): | 18 def AddFile(self, file_path, line_number=None): |
| 18 line_numbers = self.files[file_path] | 19 line_numbers = self.files[file_path] |
| 19 if line_number: | 20 if line_number: |
| 20 line_number = int(line_number) | 21 line_number = int(line_number) |
| 21 if line_number not in line_numbers: | 22 if line_number not in line_numbers: |
| 22 line_numbers.append(line_number) | 23 line_numbers.append(line_number) |
| 23 | 24 |
| 24 def AddKeyword(self, keyword): | 25 def AddKeyword(self, keyword): |
| 25 keyword = keyword.strip() | 26 keyword = keyword.strip() |
| (...skipping 10 matching lines...) Expand all Loading... |
| 36 for file_path, line_numbers in other_signal['files'].iteritems(): | 37 for file_path, line_numbers in other_signal['files'].iteritems(): |
| 37 if file_path in self.files: | 38 if file_path in self.files: |
| 38 self.files[file_path].extend( | 39 self.files[file_path].extend( |
| 39 x for x in line_numbers if x not in self.files[file_path]) | 40 x for x in line_numbers if x not in self.files[file_path]) |
| 40 else: | 41 else: |
| 41 self.files[file_path] = line_numbers[:] | 42 self.files[file_path] = line_numbers[:] |
| 42 | 43 |
| 43 new_failed_targets = other_signal.get('failed_targets', []) | 44 new_failed_targets = other_signal.get('failed_targets', []) |
| 44 for target in new_failed_targets: | 45 for target in new_failed_targets: |
| 45 self.AddTarget(target) | 46 self.AddTarget(target) |
| 47 self.failed_output_nodes = list( |
| 48 set(self.failed_output_nodes + |
| 49 other_signal.get('failed_output_nodes', []))) |
| 46 | 50 |
| 47 def ToDict(self): | 51 def ToDict(self): |
| 48 if self.failed_targets: | 52 json_dict = { |
| 49 return { | |
| 50 'files': self.files, | |
| 51 'keywords': self.keywords, | |
| 52 'failed_targets': self.failed_targets | |
| 53 } | |
| 54 | |
| 55 return { | |
| 56 'files': self.files, | 53 'files': self.files, |
| 57 'keywords': self.keywords, | 54 'keywords': self.keywords, |
| 58 } | 55 } |
| 56 if self.failed_targets: |
| 57 json_dict['failed_targets'] = self.failed_targets |
| 58 if self.failed_output_nodes: |
| 59 json_dict['failed_output_nodes'] = self.failed_output_nodes |
| 60 return json_dict |
| 59 | 61 |
| 60 @staticmethod | 62 @staticmethod |
| 61 def FromDict(data): | 63 def FromDict(data): |
| 62 signal = FailureSignal() | 64 signal = FailureSignal() |
| 63 signal.files.update(copy.deepcopy(data.get('files', {}))) | 65 signal.files.update(copy.deepcopy(data.get('files', {}))) |
| 64 signal.keywords.update(data.get('keywords', {})) | 66 signal.keywords.update(data.get('keywords', {})) |
| 65 signal.failed_targets = data.get('failed_targets', []) | 67 signal.failed_targets = data.get('failed_targets', []) |
| 68 signal.failed_output_nodes = data.get('failed_output_nodes', []) |
| 66 return signal | 69 return signal |
| 67 | 70 |
| 68 def PrettyPrint(self): # pragma: no cover | 71 def PrettyPrint(self): # pragma: no cover |
| 69 if self.files: | 72 if self.files: |
| 70 print 'Files:' | 73 print 'Files:' |
| 71 for file_path, line_numbers in self.files.iteritems(): | 74 for file_path, line_numbers in self.files.iteritems(): |
| 72 print ' %s : %s' % (file_path, ','.join(map(str, line_numbers))) | 75 print ' %s : %s' % (file_path, ','.join(map(str, line_numbers))) |
| 73 if self.keywords: | 76 if self.keywords: |
| 74 print 'Keywords:' | 77 print 'Keywords:' |
| 75 for keyword, count in self.keywords.iteritems(): | 78 for keyword, count in self.keywords.iteritems(): |
| 76 print ' %s (%d)' % (keyword, count) | 79 print ' %s (%d)' % (keyword, count) |
| 77 if self.failed_targets: | 80 if self.failed_targets: |
| 78 print 'Failed Targets:' | 81 print 'Failed Targets:' |
| 79 for source_target in self.failed_targets: | 82 for source_target in self.failed_targets: |
| 80 target = source_target.get('target') | 83 target = source_target.get('target') |
| 81 source = source_target.get('source') | 84 source = source_target.get('source') |
| 82 if target: | 85 if target: |
| 83 print ' Target: %s' % target | 86 print ' Target: %s' % target |
| 84 if source: | 87 if source: |
| 85 print ' Source: %s' % source | 88 print ' Source: %s' % source |
| 89 if self.failed_output_nodes: |
| 90 print ' Failed output nodes: %s' % ','.join(self.failed_output_nodes) |
| OLD | NEW |