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..1633a45e6ddfbe1d51b9c5facd08e857fe46521b |
| --- /dev/null |
| +++ b/appengine/findit/crash/project_classifier.py |
| @@ -0,0 +1,108 @@ |
| +# 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__() |
| + self.project_classifier_config = CrashConfig.Get().project_classifier |
| + self.project_classifier_config['host_directories'].sort( |
| + cmp=self._CompareHosts) |
|
stgao
2016/05/24 22:28:48
This could be simplified by just comparing the len
Sharu Jiang
2016/05/24 23:30:24
I think only when a host is contained by another,
stgao
2016/05/24 23:35:36
For those that are not contained by another, does
Sharu Jiang
2016/05/25 00:08:07
Done.
|
| + |
| + def _CompareHosts(self, host1, host2): |
| + """Compares 2 host directories. |
| + |
| + If a host is contained in another host, the shorter the length the smaller, |
| + otherwise, use the alphabetic order. |
| + For example, 'src/tools' < 'src', 'src/chrome/ < src/media'. |
| + """ |
| + if host1.startswith(host2) or host2.startswith(host1): |
| + return -cmp(len(host1), len(host2)) |
| + |
| + return cmp(host1, host2) |
| + |
| + def _GetProjectFromDepPath(self, dep_path): |
| + """Returns the project name from a dep path.""" |
| + if not dep_path: |
| + return '' |
| + |
| + if dep_path == 'src/': |
| + return 'chromium' |
| + |
| + for host_directory in self.project_classifier_config['host_directories']: |
| + if dep_path.startswith(host_directory): |
| + path = dep_path[len(host_directory):] |
| + return 'chromium-%s' % path.split('/')[0].lower() |
| + |
| + # Unknown path, return the whole path as project name. |
| + return 'chromium-%s' % '_'.join(dep_path.split('/')) |
| + |
| + def GetClassFromStackFrame(self, frame): |
| + """Returns a tuple (project_name, project_path) of a StackFrame.""" |
| + for marker, name in self.project_classifier_config[ |
| + 'function_marker_to_project_name'].iteritems(): |
| + if frame.function.startswith(marker): |
| + return name |
| + |
| + for marker, name in self.project_classifier_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.project_classifier_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.project_classifier_config['top_n'], 1, |
| + rank_function=_GetRankFunction(crash_stack.language_type)) |
| + |
| + if projects: |
| + return projects[0] |
| + |
| + return '' |