Rev 22: Hook into the bzr smart server to determine served urls dynamically. in file:///home/robertc/source/baz/plugins/dbus/trunk/

Robert Collins robertc at robertcollins.net
Mon Mar 26 03:27:28 BST 2007


At file:///home/robertc/source/baz/plugins/dbus/trunk/

------------------------------------------------------------
revno: 22
revision-id: robertc at robertcollins.net-20070326022727-xatcyboet3na2p9d
parent: robertc at robertcollins.net-20070325223808-t3hben50faont9u5
committer: Robert Collins <robertc at robertcollins.net>
branch nick: trunk
timestamp: Mon 2007-03-26 12:27:27 +1000
message:
  Hook into the bzr smart server to determine served urls dynamically.
added:
  NEWS                           news-20070326022658-njw6zrfi8qxwriwq-1
modified:
  TODO                           todo-20070206032729-2b5teqiwctqrfgei-3
  activity.py                    activity.py-20070206034725-q208d0jtkshwu0fx-1
  hook.py                        hook.py-20070206032729-2b5teqiwctqrfgei-5
  tests/test_hook.py             test_hook.py-20070206032729-2b5teqiwctqrfgei-8
=== added file 'NEWS'
--- a/NEWS	1970-01-01 00:00:00 +0000
+++ b/NEWS	2007-03-26 02:27:27 +0000
@@ -0,0 +1,6 @@
+IN DEVELOPMENT
+
+  INTERNALS:
+
+    * With 0.16, hook into the smart server to detect served URL's.
+      (Robert Collins)

=== modified file 'TODO'
--- a/TODO	2007-03-25 22:20:24 +0000
+++ b/TODO	2007-03-26 02:27:27 +0000
@@ -14,6 +14,4 @@
    may be inherently flawed).
  * have the announce_revision method read bazaar.conf to pickup statically
    configured servers.
- * have the smart server start and stop methods add servers.
  * have announce_revision perform mapping of provided urls against all servers.
- * add dbus methods to announce server start and stop.

=== modified file 'activity.py'
--- a/activity.py	2007-03-25 22:38:08 +0000
+++ b/activity.py	2007-03-26 02:27:27 +0000
@@ -47,6 +47,10 @@
         else:
             self.bus = bus
 
+    def add_url_map(self, source_prefix, target_prefix):
+        """Helper to invoke add_url_map on the dbus Broadcast service."""
+        self._call_on_broadcast('add_url_map', source_prefix, target_prefix)
+
     def advertise_branch(self, branch):
         """Advertise branch to dbus.
 
@@ -67,6 +71,13 @@
         The recommended API is advertise_branch, announce_revision, while 
         public is not stable or supported. Use at your own warranty.
         """
+        if revision in (None, NULL_REVISION):
+            revision = '' # avoid sending None or NULL_REVISION (its internal
+                          # only) on the wire.
+        self._call_on_broadcast('announce_revision', revision, url)
+
+    def _call_on_broadcast(self, method_name, *args):
+        """Thunk method through to the dbus Broadcast service."""
         try:
             dbus_object = self.bus.get_object(Broadcast.DBUS_NAME,
                 Broadcast.DBUS_PATH)
@@ -95,16 +106,16 @@
             """
             mainloop.quit()
             raise error
-        if revision in (None, NULL_REVISION):
-            revision = '' # avoid sending None or NULL_REVISION (its internal
-                          # only) on the wire.
-        dbus_iface.announce_revision(revision, url,
-            reply_handler=handle_reply,
-            error_handler=handle_error)
+        method = getattr(dbus_iface, method_name)
+        method(reply_handler=handle_reply, error_handler=handle_error, *args)
         # iterate enough to emit the signal, in case we are being called from a
         # sync process.
         mainloop.run()
 
+    def remove_url_map(self, source_prefix, target_prefix):
+        """Helper to invoke remove_url_map on the dbus Broadcast service."""
+        self._call_on_broadcast('remove_url_map', source_prefix, target_prefix)
+
     def serve_broadcast(self, when_ready=None):
         """Run a 'Broadcast' server.
 

=== modified file 'hook.py'
--- a/hook.py	2007-03-25 12:42:46 +0000
+++ b/hook.py	2007-03-26 02:27:27 +0000
@@ -42,8 +42,10 @@
 
 
 def on_server_start(local_url, public_url):
-    pass
+    """Add the servers local and public urls to the session Broadcaster."""
+    activity.Activity().add_url_map(local_url, public_url)
 
 
 def on_server_stop(local_url, public_url):
-    pass
+    """The server has shutdown, so remove the servers local and public urls."""
+    activity.Activity().remove_url_map(local_url, public_url)

=== modified file 'tests/test_hook.py'
--- a/tests/test_hook.py	2007-03-25 21:54:25 +0000
+++ b/tests/test_hook.py	2007-03-26 02:27:27 +0000
@@ -28,10 +28,17 @@
 class TestHooksAreSet(TestCaseWithDBus):
 
     def test_set_rh_installed(self):
-        """Loading the plugin should have installed a set_rh branch hook."""
+        """Loading the plugin should have installed its hooks."""
         # check by looking in self._preserved hooks.
+        # the set_rh Branch hook to detect branch changes.
         self.assertTrue(hook.on_set_rh in
             self._preserved_hooks[Branch]['set_rh'])
+        # the server_started and server_stopped smart server hooks
+        # to detect url maps for servers.
+        self.assertTrue(hook.on_server_start in
+            self._preserved_hooks[SmartTCPServer]['server_started'])
+        self.assertTrue(hook.on_server_stop in
+            self._preserved_hooks[SmartTCPServer]['server_stopped'])
 
     def test_install_hooks(self):
         """dbus.hook.install_hooks() should install hooks."""
@@ -62,3 +69,45 @@
         finally:
             activity.Activity = original_class
         self.assertEqual([('advertise_branch', 'branch')], calls)
+
+    def test_on_server_start_hook(self):
+        """The on_server_start hook should add a URL mapping for the server."""
+        # change the global b.p.dbus.activity.Activity to instrument
+        # on_server_start.
+        calls = []
+        class SampleActivity(object):
+
+            def add_url_map(self, source_prefix, target_prefix):
+                calls.append(('add_url_map', source_prefix, target_prefix))
+
+        # prevent api skew: check we can use the API SampleActivity presents.
+        activity.Activity(bus=self.bus).add_url_map('foo/', 'bar/')
+        # now test the hook
+        original_class = activity.Activity
+        try:
+            activity.Activity = SampleActivity
+            hook.on_server_start('source', 'target')
+        finally:
+            activity.Activity = original_class
+        self.assertEqual([('add_url_map', 'source', 'target')], calls)
+
+    def test_on_server_stop_hook(self):
+        """The on_server_stop hook should add a URL mapping for the server."""
+        # change the global b.p.dbus.activity.Activity to instrument
+        # on_server_stop.
+        calls = []
+        class SampleActivity(object):
+
+            def remove_url_map(self, source_prefix, target_prefix):
+                calls.append(('remove_url_map', source_prefix, target_prefix))
+
+        # prevent api skew: check we can use the API SampleActivity presents.
+        activity.Activity(bus=self.bus).remove_url_map('foo/', 'bar/')
+        # now test the hook
+        original_class = activity.Activity
+        try:
+            activity.Activity = SampleActivity
+            hook.on_server_stop('source', 'target')
+        finally:
+            activity.Activity = original_class
+        self.assertEqual([('remove_url_map', 'source', 'target')], calls)



More information about the bazaar-commits mailing list