Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
|
chanli
2017/01/26 21:31:06
Nit: 2017
Sharu Jiang
2017/01/27 03:20:42
Done.
| |
| 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. | |
| 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 | |
|
chanli
2017/01/26 21:31:06
how about else []? same for function_regexs. so in
Sharu Jiang
2017/01/27 03:20:42
Done.
| |
| 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 component 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 print frame.function, function_regex.pattern | |
|
chanli
2017/01/26 21:31:06
Nit:remove this
Sharu Jiang
2017/01/27 03:20:42
Done.
| |
| 42 return True | |
| 43 | |
| 44 if not self.host_directories: | |
| 45 return False | |
|
chanli
2017/01/26 21:31:06
This can be removed if you use for host_directory
Sharu Jiang
2017/01/27 03:20:42
Done.
| |
| 46 | |
| 47 for host_directory in self.host_directories: | |
| 48 if frame.dep_path.startswith(host_directory): | |
| 49 return True | |
| 50 | |
| 51 return False | |
| 52 | |
| 53 def MatchesTouchedFile(self, dep_path, touched_file): | |
| 54 """Returns true if this component matches file path.""" | |
| 55 # TODO(ymzhang): use component of new path as default. RENAME might | |
| 56 # need to return two (old path new path may have different components) | |
| 57 if touched_file.change_type == ChangeType.DELETE: | |
| 58 path = touched_file.old_path | |
| 59 else: | |
| 60 path = touched_file.new_path | |
| 61 | |
| 62 path = dep_path + path | |
| 63 for path_regex in self.path_regexs or []: | |
| 64 if path_regex.match(path): | |
| 65 return True | |
| 66 | |
| 67 for host_directory in self.host_directories or []: | |
| 68 if dep_path.startswith(host_directory): | |
| 69 return True | |
| 70 | |
| 71 return False | |
| 72 | |
| 73 def GetName(self, dep_path): | |
|
chanli
2017/01/26 21:31:07
Based on returned value, it's not just project's n
Sharu Jiang
2017/01/27 03:20:42
Right, should add more doc about that.
| |
| 74 """Returns the project name from a dep path.""" | |
| 75 if 'chromium' not in self.name: | |
| 76 return self.name | |
| 77 | |
| 78 if not dep_path: | |
| 79 return None | |
| 80 | |
| 81 for host_directory in self.host_directories or []: | |
| 82 if dep_path.startswith(host_directory): | |
| 83 path = dep_path[len(host_directory):] | |
| 84 if not path: | |
| 85 return self.name | |
| 86 | |
| 87 return '%s-%s' % (self.name, path.split('/')[0].lower()) | |
| 88 | |
| 89 # Unknown path, return the whole path as project name. | |
| 90 return '%s-%s' % (self.name, '_'.join(dep_path.split('/'))) | |
| OLD | NEW |