Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 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. | |
| 4 | |
| 5 _INFINITY = 1000 | |
| 6 | |
| 7 | |
| 8 class Result(object): | |
| 9 | |
| 10 def __init__(self, changelog, dep_path, component, | |
| 11 confidence=None, reason=None): | |
| 12 self.changelog = changelog | |
| 13 self.dep_path = dep_path | |
| 14 self.component = component | |
| 15 self.confidence = confidence | |
| 16 self.reason = reason | |
| 17 | |
| 18 self.file_to_stack_infos = {} | |
| 19 | |
| 20 def ToDict(self): | |
| 21 return { | |
| 22 'url': self.changelog.commit_url, | |
| 23 'revision': self.changelog.revision, | |
| 24 'dep_path': self.dep_path, | |
| 25 'component': self.component, | |
| 26 'author': self.changelog.author_email, | |
| 27 'time': self.changelog.author_time, | |
| 28 'reason': self.reason, | |
| 29 'confidence': self.confidence, | |
| 30 } | |
| 31 | |
| 32 def ToString(self): | |
| 33 if not self.file_to_stack_infos: | |
| 34 return '' | |
| 35 | |
| 36 lines = [] | |
| 37 for file_path, stack_infos in self.file_to_stack_infos.iteritems(): | |
| 38 line_parts = [] | |
| 39 for frame, _ in stack_infos: | |
| 40 line_parts.append('%s (#%d)' % (frame.function, frame.index)) | |
| 41 | |
| 42 lines.append('Changed file %s which crashed in %s' % ( | |
| 43 file_path, ', '.join(line_parts))) | |
| 44 | |
| 45 return '\n'.join(lines) | |
| 46 | |
| 47 def __str__(self): | |
| 48 return self.ToString() | |
| 49 | |
| 50 def __repr__(self): | |
| 51 return self.ToString() | |
|
Martin Barbella
2016/04/18 21:01:18
This doesn't seem like what would usually be expec
Sharu Jiang
2016/04/19 20:38:38
This is more for a debug purpose, but yeah, this m
| |
| 52 | |
| 53 | |
| 54 class MatchResult(Result): | |
| 55 | |
| 56 def __init__(self, changelog, dep_path, component, | |
| 57 confidence=None, reason=None): | |
| 58 super(MatchResult, self).__init__( | |
| 59 changelog, dep_path, component, confidence, reason) | |
| 60 | |
| 61 self.min_distance = _INFINITY | |
| 62 | |
| 63 def Update(self, file_path, stack_infos, blame): | |
| 64 """Updates match result once a file_path is found both shown in | |
| 65 stacktrace and touched by the revision of this result. | |
| 66 | |
| 67 Inserts the file path and its stack infos, and updates the min distance | |
| 68 if less distance is found between touched lines of this result and | |
| 69 crashed lines in the file path. | |
| 70 | |
| 71 Args: | |
| 72 file_path (str): File path of the crashed file. | |
| 73 stack_infos (list): List of (StackFrame, callstack priority) tuples, | |
| 74 represents frames of this file and the callstack priorities of those | |
| 75 frames. | |
| 76 blame (Blame): Blame oject of this file. | |
| 77 """ | |
| 78 self.file_to_stack_infos[file_path] = stack_infos | |
| 79 | |
| 80 for region in blame: | |
| 81 if region.revision != self.changelog.revision: | |
| 82 continue | |
| 83 | |
| 84 region_lines = range(region.start, region.start + region.count) | |
| 85 | |
| 86 for frame, _ in stack_infos: | |
| 87 self.min_distance = min(self.min_distance, self._DistanceOfTwoRegions( | |
| 88 frame.crashed_line_numbers, region_lines)) | |
| 89 | |
| 90 def _DistanceOfTwoRegions(self, region1, region2): | |
| 91 | |
| 92 if set(region1).intersection(set(region2)): | |
| 93 return 0 | |
| 94 | |
| 95 if region1[-1] < region2[0]: | |
|
Martin Barbella
2016/04/18 21:01:18
I suspect there's a more compact way to express th
Sharu Jiang
2016/04/19 20:38:38
Since it's too long to fit in one line, I just kee
| |
| 96 return region2[0] - region1[-1] | |
| 97 | |
| 98 return region1[0] - region2[-1] | |
| 99 | |
| 100 | |
| 101 class MatchResults(dict): | |
| 102 | |
| 103 def __init__(self, ignore_cls=None): | |
| 104 super(MatchResults, self).__init__() | |
| 105 self.ignore_cls = ignore_cls | |
| 106 | |
| 107 def GenerateMatchResults(self, file_path, dep_path, | |
| 108 stack_infos, changelogs, blame): | |
| 109 """Generates match results based on newly found file path, its stack_infos, | |
| 110 and all the changelogs that touched this file in the dep in regression | |
| 111 ranges, those reverted changelogs should be ignored. | |
| 112 | |
| 113 Args: | |
| 114 file_path (str): File path of the crashed file. | |
| 115 dep_path (str): Path of the dependency of the file. | |
| 116 stack_infos (list): List of (StackFrame, callstack priority) tuples, | |
| 117 represents frames of this file and the callstack priorities of those | |
| 118 frames. | |
| 119 changelogs (list): List of Changelog objects in the dep in regression | |
| 120 range which touched the file. | |
| 121 blame (Blame): Blame of the file. | |
| 122 """ | |
| 123 | |
| 124 for changelog in changelogs: | |
| 125 if self.ignore_cls and changelog.revision in self.ignore_cls: | |
| 126 continue | |
| 127 | |
| 128 if changelog.revision not in self: | |
| 129 # TODO(katesonia): Enable component classifier later. Get it from | |
| 130 # file_path and dep_path. | |
| 131 component = '' | |
| 132 self[changelog.revision] = MatchResult(changelog, dep_path, component) | |
| 133 | |
| 134 match_result = self[changelog.revision] | |
| 135 | |
| 136 match_result.Update(file_path, stack_infos, blame) | |
| OLD | NEW |