Add callbacks for first two levels of subscription notifications.
authorTim Potter <tpot@hp.com>
Fri, 28 Mar 2008 06:53:24 +0000 (17:53 +1100)
committerTim Potter <tpot@hp.com>
Fri, 28 Mar 2008 06:53:24 +0000 (17:53 +1100)
MODE_BUILDERS receives builderAdded, builderRemoved events.

MODE_BUILDS receives above plus builderChangedState, buildStarted,
buildFinished.

bbmodel.py

index 6b64831..36be1ca 100644 (file)
@@ -5,8 +5,41 @@ from twisted.spread import pb
 from twisted.cred import credentials
 from twisted.python import log
 
+#
+# Subscription modes
+#
+
+# Receive builderAdded, builderRemoved events
+
+MODE_BUILDERS = 'builders'
+
+# Receive above plus builderChangedState, buildStarted, buildFinished
+
+MODE_BUILDS = 'builds'
+
+# Receive above plus buildETAUpdate, stepStarted, stepFinished
+
+MODE_STEPS = 'steps'
+
+# Receive above plus stepETAUpdate, logStarted, logFinished
+
+MODE_LOGS = 'logs'
+
+# Receive above plus logChunk
+
+MOD_FULL = 'full'
+
+#
+# BuildBot state model
+#
+
 class BBModel(pb.Referenceable):
     
+    def __init__(self, updateInterval = 5):
+
+        self.builders = {}
+        self.updateInterval = updateInterval
+
     def connect(self, host, port, username, password):
 
         cf = pb.PBClientFactory()
@@ -29,6 +62,89 @@ class BBModel(pb.Referenceable):
         self.remote = ref
         self.remote.notifyOnDisconnect(self.disconnected)
 
+        self.remote.callRemote(
+            'subscribe', MODE_BUILDS, self.updateInterval, self)
+
     def disconnected(self, ref):
         
         log.msg('disconnected')
+
+    #
+    # Remote callbacks for PB client subscriptions
+    #
+
+    # Subscription mode >= MODE_BUILDERS
+
+    def remote_builderAdded(self, buildername, builder):
+        """Called by the PB server when a builder has been added to the
+        buildbot.  The buildername is the name of the build as a
+        string, and the builder is a remote reference to the builder."""
+
+        log.msg('added builder "%s"' % buildername)
+
+        # Add builder to our model
+
+        self.builders[buildername] = {}
+
+        self.builders[buildername]['builder'] = builder
+        self.builders[buildername]['state'] = None
+        self.builders[buildername]['builds'] = {}
+        self.builders[buildername]['results'] = {}
+
+    def remote_builderRemoved(self, buildername):
+        """Called by the PB server when a builder has been removed from the
+        buildbot."""
+
+        log.msg('removed builder "%s"' % buildername)
+
+        # Remove builder from our model
+
+        del(self.builders[buildername])
+
+    # Subscription mode >= builds
+
+    def remote_buildStarted(self, buildername, build):
+        """Called by the PB server when a builder has started a build. The
+        buildername parameter is the name of the build and the build
+        parameter is a remote reference to the build."""
+
+        # Add build to model
+
+        buildnum = build.callRemote('getNumber')
+
+        def updateModel(num):
+            log.msg('build %s started for "%s"' % (num, buildername))
+            self.builders[buildername]['builds'][num] = build
+
+        buildnum.addCallback(updateModel)
+
+    def remote_builderChangedState(self, buildername, state, eta):
+        """Called by the PB server when a builder has changed state.  The
+        buildername parameter is the name of the build, state is a
+        description of the state of the build, and eta is the
+        estimated time until the completion of the build."""
+
+        log.msg('builder "%s" changed state from "%s" to "%s"' % 
+                (buildername, self.builders[buildername]['state'], state))
+
+        # Reflect state change in model
+
+        self.builders[buildername]['state'] = state
+
+    def remote_buildFinished(self, buildername, build, results):
+        """Called by the PB server when a build has finished."""
+
+        # Remove from active builds and add results
+
+        buildnum = build.callRemote('getNumber')
+
+        def updateModel(num):
+
+            log.msg('build %d finished on "%s"' % (num, buildername))
+
+            if self.buidlers[buildername]['builds'].has_key(num):
+                del(self.builders[buildername]['builds'][num])
+
+            self.builders[buildername]['results'][num] = results
+
+        buildnum.addCallback(updateModel)