"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.5.1/buildbot/www/authz/endpointmatchers.py" (24 Nov 2019, 7096 Bytes) of package /linux/misc/buildbot-2.5.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "endpointmatchers.py" see the Fossies "Dox" file reference documentation.

    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 inspect
   17 
   18 from twisted.internet import defer
   19 
   20 from buildbot.data.exceptions import InvalidPathError
   21 from buildbot.util import bytes2unicode
   22 
   23 
   24 class EndpointMatcherBase:
   25 
   26     def __init__(self, role, defaultDeny=True):
   27         self.role = role
   28         self.defaultDeny = defaultDeny
   29         self.owner = None
   30 
   31     def setAuthz(self, authz):
   32         self.authz = authz
   33         self.master = authz.master
   34 
   35     def match(self, ep, action="get", options=None):
   36         if options is None:
   37             options = {}
   38         try:
   39             epobject, epdict = self.master.data.getEndpoint(ep)
   40             for klass in inspect.getmro(epobject.__class__):
   41                 m = getattr(
   42                     self, "match_" + klass.__name__ + "_" + action, None)
   43                 if m is not None:
   44                     return m(epobject, epdict, options)
   45                 m = getattr(self, "match_" + klass.__name__, None)
   46                 if m is not None:
   47                     return m(epobject, epdict, options)
   48         except InvalidPathError:
   49             return defer.succeed(None)
   50         return defer.succeed(None)
   51 
   52     def __repr__(self):
   53         # a repr for debugging. displays the class, and string attributes
   54         args = []
   55         for k, v in self.__dict__.items():
   56             if isinstance(v, str):
   57                 args.append("%s='%s'" % (k, v))
   58         return "%s(%s)" % (self.__class__.__name__, ", ".join(args))
   59 
   60 
   61 class Match:
   62 
   63     def __init__(self, master, build=None, buildrequest=None, buildset=None):
   64         self.master = master
   65         self.build = build
   66         self.buildrequest = buildrequest
   67         self.buildset = buildset
   68 
   69     def getOwner(self):
   70         if self.buildset:
   71             return self.getOwnerFromBuildset(self.buildset)
   72         elif self.buildrequest:
   73             return self.getOwnerFromBuildRequest(self.buildrequest)
   74         elif self.build:
   75             return self.getOwnerFromBuild(self.build)
   76         return defer.succeed(None)
   77 
   78     @defer.inlineCallbacks
   79     def getOwnerFromBuild(self, build):
   80         br = yield self.master.data.get(("buildrequests", build['buildrequestid']))
   81         owner = yield self.getOwnerFromBuildRequest(br)
   82         return owner
   83 
   84     @defer.inlineCallbacks
   85     def getOwnerFromBuildsetOrBuildRequest(self, buildsetorbuildrequest):
   86         props = yield self.master.data.get(("buildsets", buildsetorbuildrequest['buildsetid'], "properties"))
   87         if 'owner' in props:
   88             return props['owner'][0]
   89         return None
   90 
   91     getOwnerFromBuildRequest = getOwnerFromBuildsetOrBuildRequest
   92     getOwnerFromBuildSet = getOwnerFromBuildsetOrBuildRequest
   93 
   94 
   95 class AnyEndpointMatcher(EndpointMatcherBase):
   96 
   97     def match(self, ep, action="get", options=None):
   98         return defer.succeed(Match(self.master))
   99 
  100 
  101 class AnyControlEndpointMatcher(EndpointMatcherBase):
  102 
  103     def match(self, ep, action="", options=None):
  104         if bytes2unicode(action).lower() != "get":
  105             return defer.succeed(Match(self.master))
  106         return defer.succeed(None)
  107 
  108 
  109 class StopBuildEndpointMatcher(EndpointMatcherBase):
  110 
  111     def __init__(self, builder=None, **kwargs):
  112         self.builder = builder
  113         super().__init__(**kwargs)
  114 
  115     @defer.inlineCallbacks
  116     def matchFromBuilderId(self, builderid):
  117         if builderid is not None:
  118             builder = yield self.master.data.get(('builders', builderid))
  119             buildername = builder['name']
  120             return self.authz.match(buildername, self.builder)
  121         return False
  122 
  123     @defer.inlineCallbacks
  124     def match_BuildEndpoint_stop(self, epobject, epdict, options):
  125         build = yield epobject.get({}, epdict)
  126         if self.builder is None:
  127             # no filtering needed: we match!
  128             return Match(self.master, build=build)
  129         # if filtering needed, we need to get some more info
  130         ret = yield self.matchFromBuilderId(build['builderid'])
  131         if ret:
  132             return Match(self.master, build=build)
  133 
  134         return None
  135 
  136     @defer.inlineCallbacks
  137     def match_BuildRequestEndpoint_stop(self, epobject, epdict, options):
  138         buildrequest = yield epobject.get({}, epdict)
  139         if self.builder is None:
  140             # no filtering needed: we match!
  141             return Match(self.master, buildrequest=buildrequest)
  142         # if filtering needed, we need to get some more info
  143         ret = yield self.matchFromBuilderId(buildrequest['builderid'])
  144         if ret:
  145             return Match(self.master, buildrequest=buildrequest)
  146         return None
  147 
  148 
  149 class ForceBuildEndpointMatcher(EndpointMatcherBase):
  150 
  151     def __init__(self, builder=None, **kwargs):
  152         self.builder = builder
  153         super().__init__(**kwargs)
  154 
  155     @defer.inlineCallbacks
  156     def match_ForceSchedulerEndpoint_force(self, epobject, epdict, options):
  157         if self.builder is None:
  158             # no filtering needed: we match without querying!
  159             return Match(self.master)
  160         sched = yield epobject.findForceScheduler(epdict['schedulername'])
  161         if sched is not None:
  162             builderNames = options.get('builderNames')
  163             builderid = options.get('builderid')
  164             builderNames = yield sched.computeBuilderNames(builderNames, builderid)
  165             for buildername in builderNames:
  166                 if self.authz.match(buildername, self.builder):
  167                     return Match(self.master)
  168         return None
  169 
  170 
  171 class RebuildBuildEndpointMatcher(EndpointMatcherBase):
  172 
  173     def __init__(self, builder=None, **kwargs):
  174         self.builder = builder
  175         super().__init__(**kwargs)
  176 
  177     @defer.inlineCallbacks
  178     def match_BuildEndpoint_rebuild(self, epobject, epdict, options):
  179         build = yield epobject.get({}, epdict)
  180         return Match(self.master, build=build)
  181 
  182 
  183 class EnableSchedulerEndpointMatcher(EndpointMatcherBase):
  184 
  185     def match_SchedulerEndpoint_enable(self, epobject, epdict, options):
  186         return defer.succeed(Match(self.master))
  187 
  188 #####
  189 # not yet implemented
  190 
  191 
  192 class ViewBuildsEndpointMatcher(EndpointMatcherBase):
  193 
  194     def __init__(self, branch=None, project=None, builder=None, **kwargs):
  195         super().__init__(**kwargs)
  196         self.branch = branch
  197         self.project = project
  198         self.builder = builder
  199 
  200 
  201 class BranchEndpointMatcher(EndpointMatcherBase):
  202 
  203     def __init__(self, branch, **kwargs):
  204         self.branch = branch
  205         super().__init__(**kwargs)