Chromium Code Reviews| Index: appengine/findit/crash/project_classifier.py |
| diff --git a/appengine/findit/crash/project_classifier.py b/appengine/findit/crash/project_classifier.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..84f156dd6d7a419671639d63afbeaae75c74e943 |
| --- /dev/null |
| +++ b/appengine/findit/crash/project_classifier.py |
| @@ -0,0 +1,105 @@ |
| +# Copyright 2016 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 copy |
| + |
| +from crash import classifier |
| +from crash.type_enums import CallStackLanguageType |
| +from model.crash.crash_config import CrashConfig |
| + |
| + |
| +class ProjectClassifier(classifier.Classifier): |
| + """Determines the project of a crash - (project_name, project_path). |
| + |
| + For example: ('chromium', 'src/'), ('skia', 'src/skia/'), ...etc. |
| + """ |
| + |
| + def __init__(self): |
| + super(ProjectClassifier, self).__init__() |
| + |
| + def _GetCompiledConfig(config): |
| + config = copy.copy(config) |
| + config['top_n'] = int(config['top_n']) |
| + return config |
| + |
| + self.config = _GetCompiledConfig(CrashConfig.Get().project_classifier) |
| + |
| + def _GetProjectFromDepPath(self, dep_path): |
| + """Returns the component name of a path.""" |
|
Martin Barbella
2016/05/13 23:05:07
Update this (if needed once other comments are add
Sharu Jiang
2016/05/17 19:27:54
Done.
|
| + if not dep_path: |
| + return '', '' |
|
Martin Barbella
2016/05/13 23:05:07
I'm a bit confused by why we need to return dep_pa
Sharu Jiang
2016/05/17 19:27:54
I just want to offer more information before, but
|
| + |
| + if dep_path == 'src/': |
| + return 'chromium', dep_path |
| + |
| + for host_directory in self.config['host_directories']: |
| + if dep_path.startswith(host_directory): |
| + path = dep_path[len(host_directory):] |
| + return 'chromium-%s' % path.split('/')[0].lower(), dep_path |
| + |
| + # Unknown path, return the whole path as component name. |
| + return 'chromium-%s' % '_'.join(dep_path.split('/')), dep_path |
| + |
| + def GetClassFromStackFrame(self, frame): |
| + """Returns a tuple (project_name, project_path) of a StackFrame.""" |
| + for marker, name in self.config[ |
| + 'function_marker_to_project_name'].iteritems(): |
| + if frame.function.startswith(marker): |
| + return name, '' |
| + |
| + for marker, name in self.config[ |
| + 'file_path_marker_to_project_name'].iteritems(): |
| + if marker in frame.file_path or marker in frame.raw_file_path: |
| + return name, '' |
| + |
| + return self._GetProjectFromDepPath(frame.dep_path) |
| + |
| + def GetClassFromResult(self, result): |
| + """Returns (project_name, project_path) of a Result.""" |
| + if result.file_to_stack_infos: |
| + # A file in culprit result should always have its stack_info, namely a |
| + # list of (frame, callstack_priority) pairs. |
| + frame, _ = result.file_to_stack_infos.values()[0][0] |
| + return self.GetClassFromStackFrame(frame) |
| + |
| + return '', '' |
| + |
| + def Classify(self, results, crash_stack): |
| + """Classify project of a crash. |
| + |
| + Args: |
| + results (list of Result): culprit results. |
| + crash_stack (CallStack): the callstack that caused the crash. |
| + |
| + Returns: |
| + A tuple, project of the crash - (project_name, project_path). |
| + """ |
| + def _GetRankFunction(language_type): |
| + if language_type == CallStackLanguageType.JAVA: |
| + def _RankFunctionForJava(class_occurrences_info): |
| + project_name, _ = class_occurrences_info.name |
| + return (len(class_occurrences_info.occurrences), |
| + 0 if 'chromium' in project_name else self.config[ |
| + 'non_chromium_project_rank_priority'][project_name]) |
| + |
| + return _RankFunctionForJava |
| + |
| + return classifier.DefaultRankFunction |
| + |
| + # Set the max_classes to 1, so the returned projects only has one element. |
| + projects = self._Classify( |
| + results, crash_stack, |
| + self.config['top_n'], 1, |
| + rank_function=_GetRankFunction(crash_stack.language_type)) |
| + |
| + def _Format(project_name, project_path): |
| + if project_path: |
| + return '%s (%s)' % (project_name, project_path) |
| + |
| + return project_name |
| + |
| + if projects: |
| + return _Format(*projects[0]) |
| + |
| + return '' |