1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# Copyright 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
This module defines classes used in matching changes based on job
configuration.
"""
import re
class AbstractChangeMatcher(object):
def __init__(self, regex):
self._regex = regex
self.regex = re.compile(regex)
def matches(self, change):
"""Return a boolean indication of whether change matches
implementation-specific criteria.
"""
raise NotImplementedError()
def copy(self):
return self.__class__(self._regex)
def __deepcopy__(self, memo):
return self.copy()
def __eq__(self, other):
return str(self) == str(other)
def __ne__(self, other):
return not self.__eq__(other)
def __str__(self):
return '{%s:%s}' % (self.__class__.__name__, self._regex)
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, self._regex)
class ProjectMatcher(AbstractChangeMatcher):
def matches(self, change):
return self.regex.match(str(change.project))
class BranchMatcher(AbstractChangeMatcher):
exactmatch = False
def matches(self, change):
if hasattr(change, 'branch'):
# an implied branch matcher must do a fullmatch to work correctly
if self.exactmatch:
if self._regex == change.branch:
return True
else:
if self.regex.match(change.branch):
return True
return False
if self.regex.match(change.ref):
return True
if hasattr(change, 'containing_branches'):
for branch in change.containing_branches:
if self.exactmatch:
if self._regex == branch:
return True
else:
if self.regex.fullmatch(branch):
return True
return False
def serialize(self):
return {
"implied": self.exactmatch,
"regex": self._regex,
}
@classmethod
def deserialize(cls, data):
o = cls.__new__(cls, data['regex'])
return o
class ImpliedBranchMatcher(BranchMatcher):
exactmatch = True
class FileMatcher(AbstractChangeMatcher):
pass
class AbstractMatcherCollection(AbstractChangeMatcher):
def __init__(self, matchers):
self.matchers = matchers
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return '{%s:%s}' % (self.__class__.__name__,
','.join([str(x) for x in self.matchers]))
def __repr__(self):
return '<%s>' % self.__class__.__name__
def copy(self):
return self.__class__(self.matchers[:])
class AbstractMatchFiles(AbstractMatcherCollection):
commit_regex = re.compile('^/COMMIT_MSG$')
@property
def regexes(self):
for matcher in self.matchers:
yield matcher.regex
class MatchAllFiles(AbstractMatchFiles):
def matches(self, change):
# NOTE(yoctozepto): make irrelevant files matcher match when
# there are no files to check - return False (NB: reversed)
if not (hasattr(change, 'files') and change.files):
return False
if len(change.files) == 1 and self.commit_regex.match(change.files[0]):
return False
for file_ in change.files:
matched_file = False
for regex in self.regexes:
if regex.match(file_):
matched_file = True
break
if self.commit_regex.match(file_):
matched_file = True
if not matched_file:
return False
return True
class MatchAnyFiles(AbstractMatchFiles):
def matches(self, change):
# NOTE(yoctozepto): make files matcher match when
# there are no files to check - return True
if not (hasattr(change, 'files') and change.files):
return True
if len(change.files) == 1 and self.commit_regex.match(change.files[0]):
return True
for file_ in change.files:
for regex in self.regexes:
if regex.match(file_):
return True
return False
class MatchAll(AbstractMatcherCollection):
def matches(self, change):
for matcher in self.matchers:
if not matcher.matches(change):
return False
return True
class MatchAny(AbstractMatcherCollection):
def matches(self, change):
for matcher in self.matchers:
if matcher.matches(change):
return True
return False
|