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 from collections import namedtuple | |
| 6 import re | |
| 7 | |
| 8 from libs.gitiles.diff import ChangeType | |
| 9 | |
| 10 | |
| 11 # TODO(http://crbug.com/659346): write the coverage tests. | |
|
Martin Barbella
2017/01/26 23:59:48
Is there anything still blocking it? Would be nice
Sharu Jiang
2017/01/27 03:20:42
Forgot to add it...
Done
| |
| 12 class Project(namedtuple('Project', | |
| 13 ['name', 'path_regexs', 'function_regexs', | |
| 14 'host_directories'])): # pragma: no cover | |
| 15 """A representation of a "project". | |
| 16 | |
| 17 For example: 'android_os', 'clank' or 'chromium'. Notably, a project knows | |
| 18 how to identify itself. Hence, given a stack frame, file path or dependency | |
| 19 path or whatever, we ask the ``Project`` whether it matches that frame, | |
| 20 CL, etc.""" | |
| 21 __slots__ = () | |
| 22 | |
| 23 def __new__(cls, name, path_regexs=None, | |
| 24 function_regexs=None, host_directories=None): | |
| 25 path_regexs = [re.compile(path_regex) for path_regex in | |
| 26 path_regexs] if path_regexs else None | |
| 27 function_regexs = [re.compile(function_regex) for function_regex in | |
| 28 function_regexs] if function_regexs else None | |
| 29 | |
| 30 return super(cls, Project).__new__( | |
| 31 cls, name, path_regexs, function_regexs, host_directories) | |
| 32 | |
| 33 def MatchesStackFrame(self, frame): | |
| 34 """Returns true if this project matches the frame.""" | |
| 35 for path_regex in self.path_regexs or []: | |
| 36 if path_regex.match(frame.raw_file_path): | |
| 37 return True | |
| 38 | |
| 39 for function_regex in self.function_regexs or []: | |
| 40 if function_regex.match(frame.function): | |
| 41 return True | |
| 42 | |
| 43 if not self.host_directories: | |
| 44 return False | |
| 45 | |
| 46 for host_directory in self.host_directories: | |
| 47 if frame.dep_path.startswith(host_directory): | |
| 48 return True | |
| 49 | |
| 50 return False | |
| 51 | |
| 52 def MatchesTouchedFile(self, dep_path, touched_file): | |
| 53 """Returns true if this project matches file path.""" | |
| 54 if touched_file.change_type == ChangeType.DELETE: | |
| 55 path = touched_file.old_path | |
| 56 else: | |
| 57 path = touched_file.new_path | |
| 58 | |
| 59 path = dep_path + path | |
| 60 for path_regex in self.path_regexs or []: | |
| 61 if path_regex.match(path): | |
| 62 return True | |
| 63 | |
| 64 for host_directory in self.host_directories or []: | |
| 65 if dep_path.startswith(host_directory): | |
| 66 return True | |
| 67 | |
| 68 return False | |
| 69 | |
| 70 def GetName(self, dep_path): | |
| 71 """Returns the project name from a dep path.""" | |
| 72 if 'chromium' not in self.name: | |
| 73 return self.name | |
| 74 | |
| 75 if not dep_path: | |
| 76 return None | |
| 77 | |
| 78 for host_directory in self.host_directories or []: | |
| 79 if dep_path.startswith(host_directory): | |
| 80 path = dep_path[len(host_directory):] | |
| 81 if not path: | |
| 82 return self.name | |
| 83 | |
| 84 return '%s-%s' % (self.name, path.split('/')[0].lower()) | |
| 85 | |
| 86 # Unknown path, return the whole path as project name. | |
| 87 return '%s-%s' % (self.name, '_'.join(dep_path.split('/'))) | |
| OLD | NEW |