Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1 | # Copyright 2015 Red Hat, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | """ |
| 16 | This module defines classes used in matching changes based on job |
| 17 | configuration. |
| 18 | """ |
| 19 | |
| 20 | import re |
| 21 | |
| 22 | |
| 23 | class AbstractChangeMatcher(object): |
| 24 | |
| 25 | def __init__(self, regex): |
| 26 | self._regex = regex |
| 27 | self.regex = re.compile(regex) |
| 28 | |
| 29 | def matches(self, change): |
| 30 | """Return a boolean indication of whether change matches |
| 31 | implementation-specific criteria. |
| 32 | """ |
| 33 | raise NotImplementedError() |
| 34 | |
| 35 | def copy(self): |
| 36 | return self.__class__(self._regex) |
| 37 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 38 | def __deepcopy__(self, memo): |
| 39 | return self.copy() |
| 40 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 41 | def __eq__(self, other): |
| 42 | return str(self) == str(other) |
| 43 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 44 | def __ne__(self, other): |
| 45 | return not self.__eq__(other) |
| 46 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 47 | def __str__(self): |
| 48 | return '{%s:%s}' % (self.__class__.__name__, self._regex) |
| 49 | |
| 50 | def __repr__(self): |
| 51 | return '<%s %s>' % (self.__class__.__name__, self._regex) |
| 52 | |
| 53 | |
| 54 | class ProjectMatcher(AbstractChangeMatcher): |
| 55 | |
| 56 | def matches(self, change): |
| 57 | return self.regex.match(str(change.project)) |
| 58 | |
| 59 | |
| 60 | class BranchMatcher(AbstractChangeMatcher): |
| 61 | |
| 62 | def matches(self, change): |
David Shrewsbury | f6dc176 | 2017-10-02 13:34:37 -0400 | [diff] [blame] | 63 | if hasattr(change, 'branch'): |
| 64 | if self.regex.match(change.branch): |
| 65 | return True |
| 66 | return False |
| 67 | if self.regex.match(change.ref): |
| 68 | return True |
| 69 | return False |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 70 | |
| 71 | |
James E. Blair | 1edfd97 | 2017-12-01 15:54:24 -0800 | [diff] [blame] | 72 | class ImpliedBranchMatcher(AbstractChangeMatcher): |
| 73 | """ |
| 74 | A branch matcher that only considers branch refs, and always |
| 75 | succeeds on other types (e.g., tags). |
| 76 | """ |
| 77 | |
| 78 | def matches(self, change): |
| 79 | if hasattr(change, 'branch'): |
| 80 | if self.regex.match(change.branch): |
| 81 | return True |
| 82 | return False |
| 83 | return True |
| 84 | |
| 85 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 86 | class FileMatcher(AbstractChangeMatcher): |
| 87 | |
| 88 | def matches(self, change): |
| 89 | if not hasattr(change, 'files'): |
| 90 | return False |
| 91 | for file_ in change.files: |
| 92 | if self.regex.match(file_): |
| 93 | return True |
| 94 | return False |
| 95 | |
| 96 | |
| 97 | class AbstractMatcherCollection(AbstractChangeMatcher): |
| 98 | |
| 99 | def __init__(self, matchers): |
| 100 | self.matchers = matchers |
| 101 | |
| 102 | def __eq__(self, other): |
| 103 | return str(self) == str(other) |
| 104 | |
| 105 | def __str__(self): |
| 106 | return '{%s:%s}' % (self.__class__.__name__, |
| 107 | ','.join([str(x) for x in self.matchers])) |
| 108 | |
| 109 | def __repr__(self): |
| 110 | return '<%s>' % self.__class__.__name__ |
| 111 | |
| 112 | def copy(self): |
| 113 | return self.__class__(self.matchers[:]) |
| 114 | |
| 115 | |
| 116 | class MatchAllFiles(AbstractMatcherCollection): |
| 117 | |
| 118 | commit_regex = re.compile('^/COMMIT_MSG$') |
| 119 | |
| 120 | @property |
| 121 | def regexes(self): |
| 122 | for matcher in self.matchers: |
| 123 | yield matcher.regex |
| 124 | yield self.commit_regex |
| 125 | |
| 126 | def matches(self, change): |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 127 | if not (hasattr(change, 'files') and change.files): |
| 128 | return False |
| 129 | if len(change.files) == 1 and self.commit_regex.match(change.files[0]): |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 130 | return False |
| 131 | for file_ in change.files: |
| 132 | matched_file = False |
| 133 | for regex in self.regexes: |
| 134 | if regex.match(file_): |
| 135 | matched_file = True |
| 136 | break |
| 137 | if not matched_file: |
| 138 | return False |
| 139 | return True |
| 140 | |
| 141 | |
| 142 | class MatchAll(AbstractMatcherCollection): |
| 143 | |
| 144 | def matches(self, change): |
| 145 | for matcher in self.matchers: |
| 146 | if not matcher.matches(change): |
| 147 | return False |
| 148 | return True |
| 149 | |
| 150 | |
| 151 | class MatchAny(AbstractMatcherCollection): |
| 152 | |
| 153 | def matches(self, change): |
| 154 | for matcher in self.matchers: |
| 155 | if matcher.matches(change): |
| 156 | return True |
| 157 | return False |