"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.3.1/buildbot/data/connector.py" (23 May 2019, 5244 Bytes) of package /linux/misc/buildbot-2.3.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 "connector.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.1.0_vs_2.2.0.

    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 from twisted.python import reflect
   20 
   21 from buildbot.data import base
   22 from buildbot.data import exceptions
   23 from buildbot.data import resultspec
   24 from buildbot.util import pathmatch
   25 from buildbot.util import service
   26 
   27 
   28 class Updates:
   29     # empty container object; see _scanModule, below
   30     pass
   31 
   32 
   33 class RTypes:
   34     # empty container object; see _scanModule, below
   35     pass
   36 
   37 
   38 class DataConnector(service.AsyncService):
   39 
   40     submodules = [
   41         'buildbot.data.builders',
   42         'buildbot.data.builds',
   43         'buildbot.data.buildrequests',
   44         'buildbot.data.workers',
   45         'buildbot.data.steps',
   46         'buildbot.data.logs',
   47         'buildbot.data.logchunks',
   48         'buildbot.data.buildsets',
   49         'buildbot.data.changes',
   50         'buildbot.data.changesources',
   51         'buildbot.data.masters',
   52         'buildbot.data.sourcestamps',
   53         'buildbot.data.schedulers',
   54         'buildbot.data.forceschedulers',
   55         'buildbot.data.root',
   56         'buildbot.data.properties',
   57     ]
   58     name = "data"
   59 
   60     def __init__(self):
   61 
   62         self.matcher = pathmatch.Matcher()
   63         self.rootLinks = []  # links from the root of the API
   64 
   65     @defer.inlineCallbacks
   66     def setServiceParent(self, parent):
   67         yield super().setServiceParent(parent)
   68         self._setup()
   69 
   70     def _scanModule(self, mod, _noSetattr=False):
   71         for sym in dir(mod):
   72             obj = getattr(mod, sym)
   73             if inspect.isclass(obj) and issubclass(obj, base.ResourceType):
   74                 rtype = obj(self.master)
   75                 setattr(self.rtypes, rtype.name, rtype)
   76 
   77                 # put its update methods into our 'updates' attribute
   78                 for name in dir(rtype):
   79                     o = getattr(rtype, name)
   80                     if hasattr(o, 'isUpdateMethod'):
   81                         setattr(self.updates, name, o)
   82 
   83                 # load its endpoints
   84                 for ep in rtype.getEndpoints():
   85                     # don't use inherited values for these parameters
   86                     clsdict = ep.__class__.__dict__
   87                     pathPatterns = clsdict.get('pathPatterns', '')
   88                     pathPatterns = pathPatterns.split()
   89                     pathPatterns = [tuple(pp.split('/')[1:])
   90                                     for pp in pathPatterns]
   91                     for pp in pathPatterns:
   92                         # special-case the root
   93                         if pp == ('',):
   94                             pp = ()
   95                         self.matcher[pp] = ep
   96                     rootLinkName = clsdict.get('rootLinkName')
   97                     if rootLinkName:
   98                         self.rootLinks.append({'name': rootLinkName})
   99 
  100     def _setup(self):
  101         self.updates = Updates()
  102         self.rtypes = RTypes()
  103         for moduleName in self.submodules:
  104             module = reflect.namedModule(moduleName)
  105             self._scanModule(module)
  106 
  107     def getEndpoint(self, path):
  108         try:
  109             return self.matcher[path]
  110         except KeyError:
  111             raise exceptions.InvalidPathError(
  112                 "Invalid path: " + "/".join([str(p) for p in path]))
  113 
  114     def getResourceType(self, name):
  115         return getattr(self.rtypes, name)
  116 
  117     @defer.inlineCallbacks
  118     def get(self, path, filters=None, fields=None, order=None,
  119             limit=None, offset=None):
  120         resultSpec = resultspec.ResultSpec(filters=filters, fields=fields,
  121                                            order=order, limit=limit, offset=offset)
  122         endpoint, kwargs = self.getEndpoint(path)
  123         rv = yield endpoint.get(resultSpec, kwargs)
  124         if resultSpec:
  125             rv = resultSpec.apply(rv)
  126         return rv
  127 
  128     def control(self, action, args, path):
  129         endpoint, kwargs = self.getEndpoint(path)
  130         return endpoint.control(action, args, kwargs)
  131 
  132     def produceEvent(self, rtype, msg, event):
  133         # warning, this is temporary api, until all code is migrated to data
  134         # api
  135         rsrc = self.getResourceType(rtype)
  136         return rsrc.produceEvent(msg, event)
  137 
  138     def allEndpoints(self):
  139         """return the full spec of the connector as a list of dicts
  140         """
  141         paths = []
  142         for k, v in sorted(self.matcher.iterPatterns()):
  143             paths.append(dict(path="/".join(k),
  144                               plural=str(v.rtype.plural),
  145                               type=str(v.rtype.entityType.name),
  146                               type_spec=v.rtype.entityType.getSpec()))
  147         return paths