1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import re, types
17
18 from buildbot.util import ComparableMixin, NotABranch
21
22
23
24
25 compare_attrs = ('filter_fn', 'checks')
26
27 - def __init__(self,
28
29 filter_fn=None,
30
31
32
33
34
35
36 project=None, project_re=None, project_fn=None,
37 repository=None, repository_re=None, repository_fn=None,
38 branch=NotABranch, branch_re=None, branch_fn=None,
39 category=None, category_re=None, category_fn=None,
40 codebase=None, codebase_re=None, codebase_fn=None):
41 def mklist(x):
42 if x is not None and type(x) is not types.ListType:
43 return [ x ]
44 return x
45 def mklist_br(x):
46 if x is NotABranch:
47 return None
48 if type(x) is not types.ListType:
49 return [ x ]
50 return x
51 def mkre(r):
52 if r is not None and not hasattr(r, 'match'):
53 r = re.compile(r)
54 return r
55
56 self.filter_fn = filter_fn
57 self.checks = [
58 (mklist(project), mkre(project_re), project_fn, "project"),
59 (mklist(repository), mkre(repository_re), repository_fn, "repository"),
60 (mklist_br(branch), mkre(branch_re), branch_fn, "branch"),
61 (mklist(category), mkre(category_re), category_fn, "category"),
62 (mklist(codebase), mkre(codebase_re), codebase_fn, "codebase"),
63 ]
64
66 if self.filter_fn is not None and not self.filter_fn(change):
67 return False
68 for (filt_list, filt_re, filt_fn, chg_attr) in self.checks:
69 chg_val = getattr(change, chg_attr, '')
70 if filt_list is not None and chg_val not in filt_list:
71 return False
72 if filt_re is not None and (chg_val is None or not filt_re.match(chg_val)):
73 return False
74 if filt_fn is not None and not filt_fn(chg_val):
75 return False
76 return True
77
79 checks = []
80 for (filt_list, filt_re, filt_fn, chg_attr) in self.checks:
81 if filt_list is not None and len(filt_list) == 1:
82 checks.append('%s == %s' % (chg_attr, filt_list[0]))
83 elif filt_list is not None:
84 checks.append('%s in %r' % (chg_attr, filt_list))
85 if filt_re is not None :
86 checks.append('%s ~/%s/' % (chg_attr, filt_re))
87 if filt_fn is not None :
88 checks.append('%s(%s)' % (filt_fn.__name__, chg_attr))
89
90 return "<%s on %s>" % (self.__class__.__name__, ' and '.join(checks))
91
92 @staticmethod
95
96 """
97 Static method to create a filter based on constructor args
98 change_filter, branch, and categories; use default values @code{None},
99 @code{NotABranch}, and @code{None}, respectively. These arguments are
100 interpreted as documented for the
101 L{buildbot.schedulers.basic.Scheduler} class.
102
103 @returns: L{ChangeFilter} instance or None for not filtering
104 """
105
106
107 if change_filter:
108 if (branch is not NotABranch or categories is not None):
109 raise RuntimeError("cannot specify both change_filter and "
110 "branch or categories")
111 return change_filter
112 elif branch is not NotABranch or categories:
113
114 cfargs = {}
115 if branch is not NotABranch: cfargs['branch'] = branch
116 if categories: cfargs['category'] = categories
117 return ChangeFilter(**cfargs)
118 else:
119 return None
120