| OLD | NEW |
| 1 # Copyright 2012 The Chromium Authors. All rights reserved. | 1 # Copyright 2012 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 """Base classes to represent dependency rules, used by checkdeps.py""" | 5 """Base classes to represent dependency rules, used by checkdeps.py""" |
| 6 | 6 |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import re | 9 import re |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 self._source = source | 28 self._source = source |
| 29 | 29 |
| 30 def __str__(self): | 30 def __str__(self): |
| 31 return '"%s%s" from %s.' % (self.allow, self._dir, self._source) | 31 return '"%s%s" from %s.' % (self.allow, self._dir, self._source) |
| 32 | 32 |
| 33 def AsDependencyTuple(self): | 33 def AsDependencyTuple(self): |
| 34 """Returns a tuple (allow, dependent dir, dependee dir) for this rule, | 34 """Returns a tuple (allow, dependent dir, dependee dir) for this rule, |
| 35 which is fully self-sufficient to answer the question whether the dependent | 35 which is fully self-sufficient to answer the question whether the dependent |
| 36 is allowed to depend on the dependee, without knowing the external | 36 is allowed to depend on the dependee, without knowing the external |
| 37 context.""" | 37 context.""" |
| 38 return (self.allow, self._dependent_dir or '.', self._dir or '.') | 38 return self.allow, self._dependent_dir or '.', self._dir or '.' |
| 39 | 39 |
| 40 def ParentOrMatch(self, other): | 40 def ParentOrMatch(self, other): |
| 41 """Returns true if the input string is an exact match or is a parent | 41 """Returns true if the input string is an exact match or is a parent |
| 42 of the current rule. For example, the input "foo" would match "foo/bar".""" | 42 of the current rule. For example, the input "foo" would match "foo/bar".""" |
| 43 return self._dir == other or self._dir.startswith(other + '/') | 43 return self._dir == other or self._dir.startswith(other + '/') |
| 44 | 44 |
| 45 def ChildOrMatch(self, other): | 45 def ChildOrMatch(self, other): |
| 46 """Returns true if the input string would be covered by this rule. For | 46 """Returns true if the input string would be covered by this rule. For |
| 47 example, the input "foo/bar" would match the rule "foo".""" | 47 example, the input "foo/bar" would match the rule "foo".""" |
| 48 return self._dir == other or other.startswith(self._dir + '/') | 48 return self._dir == other or other.startswith(self._dir + '/') |
| (...skipping 18 matching lines...) Expand all Loading... |
| 67 """ | 67 """ |
| 68 if not rule_string: | 68 if not rule_string: |
| 69 raise Exception('The rule string "%s" is empty\nin %s' % | 69 raise Exception('The rule string "%s" is empty\nin %s' % |
| 70 (rule_string, source)) | 70 (rule_string, source)) |
| 71 | 71 |
| 72 if not rule_string[0] in [Rule.ALLOW, Rule.DISALLOW, Rule.TEMP_ALLOW]: | 72 if not rule_string[0] in [Rule.ALLOW, Rule.DISALLOW, Rule.TEMP_ALLOW]: |
| 73 raise Exception( | 73 raise Exception( |
| 74 'The rule string "%s" does not begin with a "+", "-" or "!".' % | 74 'The rule string "%s" does not begin with a "+", "-" or "!".' % |
| 75 rule_string) | 75 rule_string) |
| 76 | 76 |
| 77 return (rule_string[0], rule_string[1:]) | 77 return rule_string[0], rule_string[1:] |
| 78 | 78 |
| 79 | 79 |
| 80 class Rules(object): | 80 class Rules(object): |
| 81 """Sets of rules for files in a directory. | 81 """Sets of rules for files in a directory. |
| 82 | 82 |
| 83 By default, rules are added to the set of rules applicable to all | 83 By default, rules are added to the set of rules applicable to all |
| 84 dependee files in the directory. Rules may also be added that apply | 84 dependee files in the directory. Rules may also be added that apply |
| 85 only to dependee files whose filename (last component of their path) | 85 only to dependee files whose filename (last component of their path) |
| 86 matches a given regular expression; hence there is one additional | 86 matches a given regular expression; hence there is one additional |
| 87 set of rules per unique regular expression. | 87 set of rules per unique regular expression. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 Args: | 134 Args: |
| 135 rule_string: The include_rule string read from the DEPS file to apply. | 135 rule_string: The include_rule string read from the DEPS file to apply. |
| 136 source: A string representing the location of that string (filename, etc.) | 136 source: A string representing the location of that string (filename, etc.) |
| 137 so that we can give meaningful errors. | 137 so that we can give meaningful errors. |
| 138 dependent_dir: The directory to which this rule applies. | 138 dependent_dir: The directory to which this rule applies. |
| 139 dependee_regexp: The rule will only be applied to dependee files | 139 dependee_regexp: The rule will only be applied to dependee files |
| 140 whose filename (last component of their path) | 140 whose filename (last component of their path) |
| 141 matches the expression. None to match all | 141 matches the expression. None to match all |
| 142 dependee files. | 142 dependee files. |
| 143 """ | 143 """ |
| 144 (rule_type, rule_dir) = ParseRuleString(rule_string, source) | 144 rule_type, rule_dir = ParseRuleString(rule_string, source) |
| 145 | 145 |
| 146 if not dependee_regexp: | 146 if not dependee_regexp: |
| 147 rules_to_update = self._general_rules | 147 rules_to_update = self._general_rules |
| 148 else: | 148 else: |
| 149 if dependee_regexp in self._specific_rules: | 149 if dependee_regexp in self._specific_rules: |
| 150 rules_to_update = self._specific_rules[dependee_regexp] | 150 rules_to_update = self._specific_rules[dependee_regexp] |
| 151 else: | 151 else: |
| 152 rules_to_update = [] | 152 rules_to_update = [] |
| 153 | 153 |
| 154 # Remove any existing rules or sub-rules that apply. For example, if we're | 154 # Remove any existing rules or sub-rules that apply. For example, if we're |
| (...skipping 14 matching lines...) Expand all Loading... |
| 169 dependee_filename = os.path.basename(dependee_path) | 169 dependee_filename = os.path.basename(dependee_path) |
| 170 for regexp, specific_rules in self._specific_rules.iteritems(): | 170 for regexp, specific_rules in self._specific_rules.iteritems(): |
| 171 if re.match(regexp, dependee_filename): | 171 if re.match(regexp, dependee_filename): |
| 172 for rule in specific_rules: | 172 for rule in specific_rules: |
| 173 if rule.ChildOrMatch(include_path): | 173 if rule.ChildOrMatch(include_path): |
| 174 return rule | 174 return rule |
| 175 for rule in self._general_rules: | 175 for rule in self._general_rules: |
| 176 if rule.ChildOrMatch(include_path): | 176 if rule.ChildOrMatch(include_path): |
| 177 return rule | 177 return rule |
| 178 return MessageRule('no rule applying.') | 178 return MessageRule('no rule applying.') |
| OLD | NEW |