Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2069)

Unified Diff: tools/findit/blame.py

Issue 421223003: [Findit] Plain objects to represent the returned result from running the algorithm, (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed code review / added git support. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/findit/matchset.py » ('j') | tools/findit/matchset.py » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/findit/blame.py
diff --git a/tools/findit/blame.py b/tools/findit/blame.py
new file mode 100644
index 0000000000000000000000000000000000000000..8d17dcb5cf5ae0ffb44301383a5e3d047ec3cd88
--- /dev/null
+++ b/tools/findit/blame.py
@@ -0,0 +1,211 @@
+# Copyright (c) 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+from threading import Lock, Thread
+
+import crash_utils
+import gitparser
+import svnparser
+import utils
+
+
+class Blame(object):
+ """Represents a blame object.
+
+ The object contains blame information for one line of stack, and this
+ information is shown when there are no CLs that change the crashing files.
+ Attributes:
+ content: The content of the line to find the blame for.
+ component_name: The name of the component this line is in.
+ stack_frame_index: The stack frame index of this file.
+ file_name: The name of the file.
+ line_number: The line that caused a crash.
+ author: The author of this line on the latest revision.
+ crash_revision: The revision that caused the crash.
+ revision: The latest revision of this line before the crash revision.
+ url: The url of the change for the revision.
+ regression: The regression range of the component, if it exists.
+
+ """
+
+ def __init__(self, content, component_name, stack_frame_index, file_name,
+ line_number, author, crash_revision, revision, url,
+ range_start, range_end):
+ # Set all the variables from the arguments.
+ self.content = content
+ self.component_name = component_name
+ self.stack_frame_index = stack_frame_index
+ self.file = file_name
+ self.line_number = line_number
+ self.author = author
+ self.revision = revision
+ self.url = url
+ self.distance = crash_utils.INFINITY
+ revision = int(revision)
+
+ # Calculate the distance, where it measures how far the last revision is
+ # from the regression range.
+ if range_start and range_end:
+ self.distance = min(abs(revision - range_start),
+ abs(revision - range_end))
+
+ # If the regression is in SVN but it does not have regression info, check
+ # how far the last revision is from crash revision.
+ elif not utils.IsGitHash(crash_revision):
+ self.distance = abs(int(crash_revision) - revision)
+
+
+class BlameList(object):
+ """Represents a list of blame objects.
+
+ Thread-safe.
+ """
+
+ def __init__(self):
+ self.blame_list = []
+ self.blame_list_lock = Lock()
+
+ def __getitem__(self, index):
+ return self.blame_list[index]
+
+ def AddBlame(self, blame):
+ """Adds blame object to the set."""
+ with self.blame_list_lock:
+ self.blame_list.append(blame)
+
+ def sort(self, key=None):
+ return self.blame_list.sort(key=key)
+
+ def FindBlame(self, callstack, crash_revision_dict, regression_dict,
+ url_map, n=10):
stgao 2014/08/12 19:12:18 Maybe rename "n" to "top_n_frames"?
jeun 2014/08/12 20:21:05 Done.
+ """Given a stack within a stacktrace, retrieves blame information.
+
+ Only either first 'n' or the length of stack, whichever is shorter,
+ results are returned. The default value of 'n' is 10.
+
+ Args:
+ callstack: The list of stack frames.
+ crash_revision_dict: A dictionary that maps component to its crash
+ revision.
+ regression_dict: A dictionary that maps component to its revision
+ range.
+ url_map: A map from repository type to urls.
+ n: A number of stack frames to show the blame result for.
+ """
+ # Only return blame information for first 'n' frames.
+ stack_frames = callstack.GetTopNFrames(n)
+
+ threads = []
+ # Iterate through frames in stack.
+ for stack_frame in stack_frames:
+ # If the component this line is from does not have a crash revision,
+ # It is not possible to get blame information so ignore this line.
+ component_path = stack_frame.component_path
+ if component_path not in crash_revision_dict:
+ continue
+
+ crash_revision = crash_revision_dict[component_path]['revision']
+ range_start = None
+ range_end = None
+ is_svn = not utils.IsGitHash(crash_revision)
+
+ # If the revision is in SVN, and if regression information is available,
+ # get it. Not for Git because we cannot calculate the distance.
+ if is_svn:
+ repository_parser = svnparser.SVNParser(url_map['svn'])
+
+ if regression_dict and component_path in regression_dict:
+ component_object = regression_dict[component_path]
+ range_start = int(component_object['old_revision'])
+ range_end = int(component_object['new_revision'])
+ else:
+ repository_parser = gitparser.GitParser(regression_dict,
+ url_map['git'])
+
+ # Generate blame entry, one thread for one entry.
+ blame_thread = Thread(
+ target=self.__GenerateBlameEntry,
+ args=[repository_parser, stack_frame, crash_revision,
+ range_start, range_end])
+ threads.append(blame_thread)
+ blame_thread.start()
+
+ # Join the results before returning.
+ for blame_thread in threads:
+ blame_thread.join()
+
+ def __GenerateBlameEntry(self, repository_parser, stack_frame,
+ crash_revision, range_start, range_end):
+ """Generates blame list from the arguments."""
+ stack_frame_index = stack_frame.index
+ component_path = stack_frame.component_path
+ component_name = stack_frame.component_name
+ file_name = stack_frame.file_name
+ file_path = stack_frame.file_path
+ line = stack_frame.crashed_line_number
+
+ # Parse blame information.
+ parsed_blame_info = repository_parser.ParseBlameInfo(
+ component_path, file_path, line, crash_revision)
+
+ # If it fails to retrieve information, do not do anything.
+ if not parsed_blame_info:
+ return
+
+ # Create blame object from the parsed info and add it to the list.
+ (content, revision, author, url) = parsed_blame_info
+ blame = Blame(content, component_name, stack_frame_index, file_name, line,
+ author, crash_revision, revision, url,
+ range_start, range_end)
+ self.AddBlame(blame)
+
+ def PrettifyBlame(self):
+ """Returns a string representation of blame results.
+
+ Returns:
+ A string representation of blame_list.
+ """
+ blame_list = self.blame_list
+ return_string = []
+ return_string.append('Below are the blame information of the stacktrace.\n')
+ return_string.append('[\n')
+
+ # If blame info is not available, return the message.
+ if not blame_list:
+ return_string.append('No Blame Information Available.\n')
+ return ''.join(return_string)
+
+ # Sort the blame list by its distance, and its position in stack.
+ blame_list.sort(key=lambda blame: (blame.distance,
+ blame.stack_frame_index))
+
+ for blame in blame_list:
+ # If regression information is available, do some filtering.
+ if blame.range_start and blame.range_end:
+
+ # Discards results that are too far from the regression range.
+ # For example, if regression is 10000:11000, it is very not
+ # likely that a commit from revision 1000 would have caused a crash.
+ if (blame.distance > blame.range_start / 4) and (
+ blame.distance > blame.range_end / 4):
+ continue
+
+ return_string.append(' {\n')
+ return_string.append(
+ ' suspected_cl: %s\n' % crash_utils.AddHyperlink(
+ blame.revision,
+ blame.url))
+ return_string.append(' component: %s\n' % blame.component_name)
+ return_string.append(' owner: %s\n' % blame.author)
+ reason = (
+ 'The CL changes line %s of file %s from stack %d.' %
+ (blame.line_number, blame.file, blame.stack_frame_index))
+ return_string.append(' reason: %s\n' % reason)
+ if blame.content:
+ return_string.append(' content: %s\n' % blame.content)
+ return_string.append(' }\n')
+
+ return_string.append(']\n')
+ return_string.append('-------------------------------------------\n')
+ return ''.join(return_string)
« no previous file with comments | « no previous file | tools/findit/matchset.py » ('j') | tools/findit/matchset.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698