Rev 2371: Factor out hook cleanups for the test suite and the core of hook logic, creating a SmartServerHooks. in file:///home/robertc/source/baz/hpss-hooks/

Robert Collins robertc at robertcollins.net
Sun Mar 25 03:03:58 BST 2007


At file:///home/robertc/source/baz/hpss-hooks/

------------------------------------------------------------
revno: 2371
revision-id: robertc at robertcollins.net-20070325020354-e4vanhd9lrlosir9
parent: pqm at pqm.ubuntu.com-20070321071219-55447700ec71371f
committer: Robert Collins <robertc at robertcollins.net>
branch nick: hpss-hooks
timestamp: Sun 2007-03-25 12:03:54 +1000
message:
  Factor out hook cleanups for the test suite and the core of hook logic, creating a SmartServerHooks.
added:
  bzrlib/hooks.py                hooks.py-20070325015548-ix4np2q0kd8452au-1
modified:
  bzrlib/branch.py               branch.py-20050309040759-e4baf4e0d046576e
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/test_selftest.py  test_selftest.py-20051202044319-c110a115d8c0456a
  bzrlib/transport/smart.py      ssh.py-20060608202016-c25gvf1ob7ypbus6-1
=== added file 'bzrlib/hooks.py'
--- a/bzrlib/hooks.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/hooks.py	2007-03-25 02:03:54 +0000
@@ -0,0 +1,40 @@
+# Copyright (C) 2007 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+
+"""Support for plugin hooking logic."""
+
+
+class Hooks(dict):
+    """A dictionary mapping hook name to a list of callables.
+    
+    e.g. ['FOO'] Is the list of items to be called when the
+    FOO hook is triggered.
+    """
+
+    def install_hook(self, hook_name, a_callable):
+        """Install a_callable in to the hook hook_name.
+
+        :param hook_name: A hook name. See the __init__ method of BranchHooks
+            for the complete list of hooks.
+        :param a_callable: The callable to be invoked when the hook triggers.
+            The exact signature will depend on the hook - see the __init__ 
+            method of BranchHooks for details on each hook.
+        """
+        try:
+            self[hook_name].append(a_callable)
+        except KeyError:
+            raise errors.UnknownHook(self.__class__.__name__, hook_name)

=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2007-03-13 01:00:34 +0000
+++ b/bzrlib/branch.py	2007-03-25 02:03:54 +0000
@@ -54,6 +54,7 @@
                            NotBranchError, UninitializableFormat,
                            UnlistableStore, UnlistableBranch,
                            )
+from bzrlib.hooks import Hooks
 from bzrlib.symbol_versioning import (deprecated_function,
                                       deprecated_method,
                                       DEPRECATED_PARAMETER,
@@ -884,7 +885,7 @@
             control_files.unlock()
 
 
-class BranchHooks(dict):
+class BranchHooks(Hooks):
     """A dictionary mapping hook name to a list of callables for branch hooks.
     
     e.g. ['set_rh'] Is the list of items to be called when the
@@ -897,7 +898,7 @@
         These are all empty initially, because by default nothing should get
         notified.
         """
-        dict.__init__(self)
+        Hooks.__init__(self)
         # Introduced in 0.15:
         # invoked whenever the revision history has been set
         # with set_revision_history. The api signature is
@@ -936,20 +937,6 @@
         # and an empty branch recieves new_revno of 0, new_revid of None.
         self['post_uncommit'] = []
 
-    def install_hook(self, hook_name, a_callable):
-        """Install a_callable in to the hook hook_name.
-
-        :param hook_name: A hook name. See the __init__ method of BranchHooks
-            for the complete list of hooks.
-        :param a_callable: The callable to be invoked when the hook triggers.
-            The exact signature will depend on the hook - see the __init__ 
-            method of BranchHooks for details on each hook.
-        """
-        try:
-            self[hook_name].append(a_callable)
-        except KeyError:
-            raise errors.UnknownHook('branch', hook_name)
-
 
 # install the default hooks into the Branch class.
 Branch.hooks = BranchHooks()

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-03-15 22:35:35 +0000
+++ b/bzrlib/tests/__init__.py	2007-03-25 02:03:54 +0000
@@ -680,7 +680,10 @@
         self._benchcalls = []
         self._benchtime = None
         # prevent hooks affecting tests
-        self._preserved_hooks = bzrlib.branch.Branch.hooks
+        self._preserved_hooks = {
+            bzrlib.branch.Branch:bzrlib.branch.Branch.hooks,
+            bzrlib.transport.smart.SmartTCPServer:bzrlib.transport.smart.SmartTCPServer.hooks,
+            }
         self.addCleanup(self._restoreHooks)
         # this list of hooks must be kept in sync with the defaults
         # in branch.py
@@ -968,7 +971,8 @@
             osutils.set_or_unset_env(name, value)
 
     def _restoreHooks(self):
-        bzrlib.branch.Branch.hooks = self._preserved_hooks
+        for klass, hooks in self._preserved_hooks.items():
+            setattr(klass, 'hooks', hooks)
 
     def tearDown(self):
         self._runCleanups()

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2007-03-12 20:55:23 +0000
+++ b/bzrlib/tests/test_selftest.py	2007-03-25 02:03:54 +0000
@@ -903,6 +903,8 @@
         """The bzrlib hooks should be sanitised by setUp."""
         self.assertEqual(bzrlib.branch.BranchHooks(),
             bzrlib.branch.Branch.hooks)
+        self.assertEqual(bzrlib.transport.smart.SmartServerHooks(),
+            bzrlib.transport.smart.SmartTCPServer.hooks)
 
     def test__gather_lsprof_in_benchmarks(self):
         """When _gather_lsprof_in_benchmarks is on, accumulate profile data.

=== modified file 'bzrlib/transport/smart.py'
--- a/bzrlib/transport/smart.py	2007-03-04 00:28:13 +0000
+++ b/bzrlib/transport/smart.py	2007-03-25 02:03:54 +0000
@@ -212,6 +212,7 @@
     urlutils,
     )
 from bzrlib.bundle.serializer import write_bundle
+from bzrlib.hooks import Hooks
 try:
     from bzrlib.transport import ssh
 except errors.ParamikoNotPresent:
@@ -887,6 +888,13 @@
         ## self._server_thread.join()
 
 
+class SmartServerHooks(Hooks):
+    """Hooks for the smart server."""
+
+
+SmartTCPServer.hooks = SmartServerHooks()
+
+
 class SmartTCPServer_for_testing(SmartTCPServer):
     """Server suitable for use by transport tests.
     



More information about the bazaar-commits mailing list