Package buildbot :: Package changes :: Module filter
[frames] | no frames]

Source Code for Module buildbot.changes.filter

  1  # This file is part of Buildbot.  Buildbot is free software: you can 
  2  # redistribute it and/or modify it under the terms of the GNU General Public 
  3  # License as published by the Free Software Foundation, version 2. 
  4  # 
  5  # This program is distributed in the hope that it will be useful, but WITHOUT 
  6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  8  # details. 
  9  # 
 10  # You should have received a copy of the GNU General Public License along with 
 11  # this program; if not, write to the Free Software Foundation, Inc., 51 
 12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 13  # 
 14  # Copyright Buildbot Team Members 
 15   
 16  import re, types 
 17   
 18  from buildbot.util import ComparableMixin, NotABranch 
19 20 -class ChangeFilter(ComparableMixin):
21 22 # NOTE: If users use a filter_fn, we have no way to determine whether it has 23 # changed at reconfig, so the scheduler will always be restarted. That's as 24 # good as Python can do. 25 compare_attrs = ('filter_fn', 'checks') 26
27 - def __init__(self, 28 # gets a Change object, returns boolean 29 filter_fn=None, 30 # change attribute comparisons: exact match to PROJECT, member of 31 # list PROJECTS, regular expression match to PROJECT_RE, or 32 # PROJECT_FN returns True when called with the project; repository, 33 # branch, and so on are similar. Note that the regular expressions 34 # are anchored to the first character of the string. For convenience, 35 # a list can also be specified to the singular option (e.g,. PROJETS 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): # branch needs to be handled specially 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
65 - def filter_change(self, change):
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
78 - def __repr__(self):
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
93 - def fromSchedulerConstructorArgs(change_filter=None, 94 branch=NotABranch, categories=None):
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 # use a change_filter, if given one 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 # build a change filter from the deprecated category and branch args 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