OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Base classes to represent dependency rules, used by checkdeps.py""" | |
7 | |
8 # Specifies a single rule for an include, which can be either allow or disallow. | |
M-A Ruel
2012/07/26 13:06:03
Why not a class docstring?
Jói
2012/07/26 13:32:13
Done.
| |
9 class Rule(object): | |
10 | |
11 # These are the prefixes used to indicate each type of rule. These | |
12 # are also used as values for self.allow to indicate which type of | |
13 # rule this is. | |
14 ALLOW = "+" | |
15 DISALLOW = "-" | |
16 TEMP_ALLOW = "!" | |
17 | |
18 def __init__(self, allow, directory, source): | |
19 self.allow = allow | |
20 self._dir = directory | |
21 self._source = source | |
22 | |
23 def __str__(self): | |
24 return '"%s%s" from %s.' % (self.allow, self._dir, self._source) | |
25 | |
26 def ParentOrMatch(self, other): | |
27 """Returns true if the input string is an exact match or is a parent | |
28 of the current rule. For example, the input "foo" would match "foo/bar".""" | |
29 return self._dir == other or self._dir.startswith(other + "/") | |
30 | |
31 def ChildOrMatch(self, other): | |
32 """Returns true if the input string would be covered by this rule. For | |
33 example, the input "foo/bar" would match the rule "foo".""" | |
34 return self._dir == other or other.startswith(self._dir + "/") | |
35 | |
36 | |
37 def ParseRuleString(rule_string, source): | |
38 """Returns a tuple of a boolean indicating whether the directory is an allow | |
39 rule, and a string holding the directory name. | |
40 """ | |
41 if len(rule_string) < 1: | |
M-A Ruel
2012/07/26 13:06:03
if not rule_string: ?
Jói
2012/07/26 13:32:13
Done.
| |
42 raise Exception('The rule string "%s" is too short\nin %s' % | |
M-A Ruel
2012/07/26 13:06:03
is empty
Jói
2012/07/26 13:32:13
Done.
| |
43 (rule_string, source)) | |
44 | |
45 if not rule_string[0] in [Rule.ALLOW, Rule.DISALLOW, Rule.TEMP_ALLOW]: | |
46 raise Exception( | |
47 'The rule string "%s" does not begin with a "+", "-" or "!".' % | |
48 rule_string) | |
49 | |
50 return (rule_string[0], rule_string[1:]) | |
51 | |
52 | |
53 class Rules: | |
M-A Ruel
2012/07/26 13:06:03
class Rules(object):
Jói
2012/07/26 13:32:13
Done.
| |
54 def __init__(self): | |
55 """Initializes the current rules with an empty rule list.""" | |
56 self._rules = [] | |
57 | |
58 def __str__(self): | |
59 ret = "Rules = [\n" | |
M-A Ruel
2012/07/26 13:06:03
return 'Rules = [\n%s]' % ''.join(' %s\n' % x for
Jói
2012/07/26 13:32:13
Done.
| |
60 ret += "\n".join([" %s" % x for x in self._rules]) | |
61 ret += "]\n" | |
62 return ret | |
63 | |
64 def AddRule(self, rule_string, source): | |
65 """Adds a rule for the given rule string. | |
66 | |
67 Args: | |
68 rule_string: The include_rule string read from the DEPS file to apply. | |
69 source: A string representing the location of that string (filename, etc.) | |
70 so that we can give meaningful errors. | |
71 """ | |
72 (add_rule, rule_dir) = ParseRuleString(rule_string, source) | |
73 # Remove any existing rules or sub-rules that apply. For example, if we're | |
74 # passed "foo", we should remove "foo", "foo/bar", but not "foobar". | |
75 self._rules = [x for x in self._rules if not x.ParentOrMatch(rule_dir)] | |
76 self._rules.insert(0, Rule(add_rule, rule_dir, source)) | |
77 | |
78 def DirAllowed(self, allowed_dir): | |
79 """Returns a tuple (success, message), where success indicates if the given | |
80 directory is allowed given the current set of rules, and the message tells | |
81 why if the comparison failed.""" | |
82 for rule in self._rules: | |
83 if rule.ChildOrMatch(allowed_dir): | |
84 # This rule applies. | |
85 why_failed = "" | |
86 if rule.allow != Rule.ALLOW: | |
87 why_failed = rule.__str__() | |
88 return (rule.allow, why_failed) | |
89 # No rules apply, fail. | |
90 return (Rule.DISALLOW, "no rule applying") | |
OLD | NEW |