Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from collections import namedtuple | 5 from collections import namedtuple |
| 6 import os | |
| 6 import re | 7 import re |
| 7 | 8 |
| 9 from libs.gitiles.diff import ChangeType | |
| 10 | |
| 8 | 11 |
| 9 # TODO(http://crbug.com/659346): write the coverage tests. | 12 # TODO(http://crbug.com/659346): write the coverage tests. |
| 10 class Component(namedtuple('Component', | 13 class Component(namedtuple('Component', |
| 11 ['component_name', 'path_regex', 'function_regex'])): # pragma: no cover | 14 ['component_name', 'path_regex', 'function_regex'])): # pragma: no cover |
| 12 """A representation of a "component" in Chromium. | 15 """A representation of a "component" in Chromium. |
| 13 | 16 |
| 14 For example: 'Blink>DOM' or 'Blink>HTML'. Notably, a component knows | 17 For example: 'Blink>DOM' or 'Blink>HTML'. Notably, a component knows |
| 15 how to identify itself. Hence, given a stack frame or change list | 18 how to identify itself. Hence, given a stack frame or change list |
| 16 or whatever, we ask the Component whether it matches that frame, | 19 or whatever, we ask the Component whether it matches that frame, |
| 17 CL, etc.""" | 20 CL, etc.""" |
| 18 __slots__ = () | 21 __slots__ = () |
| 19 | 22 |
| 20 def __new__(cls, component_name, path_regex, function_regex=None): | 23 def __new__(cls, component_name, path_regex, function_regex=None): |
| 21 return super(cls, Component).__new__(cls, | 24 return super(cls, Component).__new__(cls, |
| 22 component_name, | 25 component_name, |
| 23 re.compile(path_regex), | 26 re.compile(path_regex), |
| 24 re.compile(function_regex) if function_regex else None) | 27 re.compile(function_regex) if function_regex else None) |
| 25 | 28 |
| 26 | |
| 27 def MatchesStackFrame(self, frame): | 29 def MatchesStackFrame(self, frame): |
| 28 """Returns true if this component matches the frame.""" | 30 """Returns true if this component matches the frame.""" |
| 29 if not self.path_regex.match(frame.dep_path + frame.file_path): | 31 if not self.path_regex.match(frame.dep_path + frame.file_path): |
| 30 return False | 32 return False |
| 31 | 33 |
| 32 # We interpret function_regex=None to mean the regex that matches | 34 # We interpret function_regex=None to mean the regex that matches |
| 33 # everything. | 35 # everything. |
| 34 if not self.function_regex: | 36 if not self.function_regex: |
| 35 return True | 37 return True |
| 36 return self.function_regex.match(frame.function) | 38 return self.function_regex.match(frame.function) |
| 37 | 39 |
| 38 def MatchesFile(self, file_path): | 40 def MatchesTouchedFile(self, dep_path, touched_file): |
| 39 """Returns true if this component matches file path.""" | 41 """Returns true if the touched file belongs to this component.""" |
| 40 return self.path_regex.match(file_path) | 42 # TODO(crbug.com/685884): use component of new path as default. RENAME might |
| 43 # need to return two (old path new path may have different components) | |
| 44 if touched_file.change_type == ChangeType.DELETE: | |
| 45 path = touched_file.old_path | |
| 46 else: | |
| 47 path = touched_file.new_path | |
| 48 | |
| 49 if self.path_regex.match(os.path.join(dep_path, path)): | |
|
lijeffrey
2017/01/27 15:42:20
how about just return path.regex.match(...)
Sharu Jiang
2017/01/27 18:39:04
Done.
| |
| 50 return True | |
| 51 | |
| 52 return False | |
| OLD | NEW |