Chromium Code Reviews| Index: tools/findit/component_dictionary.py |
| diff --git a/tools/findit/component_dictionary.py b/tools/findit/component_dictionary.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2515007887a01a0bda52727dbd5e321956e7560a |
| --- /dev/null |
| +++ b/tools/findit/component_dictionary.py |
| @@ -0,0 +1,116 @@ |
| +# 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. |
| + |
| +import os |
| + |
| + |
| +class FileDictionary(object): |
| + """Maps file in a stacktrace to its crash information. |
| + |
| + It maps file to another dictionary, which maps the file's path to crashed |
| + lines, stack frame indices and crashed functions. |
| + """ |
| + |
| + def __init__(self): |
| + """Initializes the file dictionary.""" |
| + self.file_dic = {} |
|
aarya
2014/08/07 15:38:47
s/dic/dict everywhere.
jeun
2014/08/07 18:43:19
Done.
|
| + |
| + def AddFile(self, file_name, file_path, crashed_line_number, |
| + stack_frame_index, function): |
| + """Adds file and its crash information to the map. |
| + |
| + Args: |
| + file_name: The name of the crashed file. |
| + file_path: The path of the crashed file. |
| + crashed_line_number: The crashed line of the file. |
| + stack_frame_index: The file's position in the callstack. |
| + function: The name of the crashed function. |
| + """ |
| + # Populate the dictionary if this file/path has not been added before. |
| + if file_name not in self.file_dic: |
| + self.file_dic[file_name] = {} |
|
aarya
2014/08/07 15:38:46
I don't understand why you use file_name as key. f
jeun
2014/08/07 18:43:19
It is easier and faster to check if a crashing fil
|
| + |
| + if file_path not in self.file_dic[file_name]: |
|
aarya
2014/08/07 15:38:47
Why all these seperate ifs. All this initializatio
jeun
2014/08/07 18:43:19
Done.
|
| + self.file_dic[file_name][file_path] = {} |
| + |
| + if 'lines' not in self.file_dic[file_name][file_path]: |
| + self.file_dic[file_name][file_path]['lines'] = [] |
|
aarya
2014/08/07 15:38:46
s/lines/stack_frames
jeun
2014/08/07 18:43:19
changed to line_numbers (the first name was a bit
|
| + |
| + if 'stack_frame_index' not in self.file_dic[file_name][file_path]: |
| + self.file_dic[file_name][file_path]['stack_frame_index'] = [] |
|
aarya
2014/08/07 15:38:46
s/stack_frame_index/stack_frame_indices. it is not
jeun
2014/08/07 18:43:19
Done.
|
| + |
| + if 'function' not in self.file_dic[file_name][file_path]: |
| + self.file_dic[file_name][file_path]['function'] = [] |
| + |
| + # Add the crashed line, frame index and function name. |
| + self.file_dic[file_name][file_path]['lines'].append(crashed_line_number) |
| + self.file_dic[file_name][file_path]['stack_frame_index'].append( |
| + stack_frame_index) |
| + self.file_dic[file_name][file_path]['function'].append(function) |
| + |
| + def GetPathDic(self, file_name): |
| + """Returns file's path and crash information.""" |
| + return self.file_dic[file_name] |
| + |
| + def GetCrashedLines(self, file_path): |
| + """Returns crashed lines given a file name and path.""" |
| + file_name = os.path.basename(file_path) |
| + return self.file_dic[file_name][file_path]['lines'] |
| + |
| + def GetCrashStackFrameindex(self, file_path): |
| + """Returns stack frame indices given a file name and path.""" |
| + file_name = os.path.basename(file_path) |
| + return self.file_dic[file_name][file_path]['stack_frame_index'] |
| + |
| + def GetCrashFunction(self, file_path): |
| + """Returns list of crashed functions given a file name and path.""" |
| + file_name = os.path.basename(file_path) |
| + return self.file_dic[file_name][file_path]['function'] |
| + |
| + def __iter__(self): |
| + return iter(self.file_dic) |
| + |
| + |
| +class ComponentDictionary(object): |
| + """Represents a file dictionary. |
| + |
| + It maps each component (blink, chrome, etc) to a file dictionary. |
| + """ |
| + |
| + def __init__(self, components): |
| + """Initializes the dictionary with given components.""" |
| + self.component_dic = {} |
|
aarya
2014/08/07 15:38:47
s/component_dic/component_dict
jeun
2014/08/07 18:43:19
Done.
|
| + |
| + # Create file dictionary for all the components. |
| + for component in components: |
| + self.component_dic[component] = FileDictionary() |
| + |
| + def __iter__(self): |
| + return iter(self.component_dic) |
| + |
| + def GetFileDic(self, component): |
|
aarya
2014/08/07 15:38:47
s/Dic(/Dict( everywhere please
jeun
2014/08/07 18:43:19
Done.
|
| + """Returns a file dictionary for a given component.""" |
| + return self.component_dic[component] |
| + |
| + def GenerateFileDic(self, stack_frame_list): |
| + """Generates file dictionary, given an instance of StackFrame list.""" |
| + # Iterate through the list of stackframe objects. |
| + for stack_frame in stack_frame_list: |
| + # If the component of this line is not in the list of components to |
| + # look for, ignore this line. |
| + component = stack_frame.component |
| + if component not in self.component_dic: |
| + continue |
| + |
| + # Get values of the variables |
| + file_name = stack_frame.file_name |
| + file_path = stack_frame.file_path |
| + crashed_line_number = stack_frame.crashed_line_number |
| + stack_frame_index = stack_frame.index |
| + function = stack_frame.function |
| + |
| + # Add the file to this component's dictionary of files. |
| + file_dic = self.component_dic[component] |
| + file_dic.AddFile(file_name, file_path, crashed_line_number, |
| + stack_frame_index, function) |