Announcing bzr-tiplog

Brian de Alwis briandealwis at gmail.com
Tue Oct 25 00:09:40 UTC 2011


On 13-Oct-2011, at 11:36 PM, Martin Pool wrote:
> On 28 September 2011 00:29, Brian de Alwis <briandealwis at gmail.com> wrote:
>> On reflection, what I'd like is to have a command-level hooks like 'command_finished'.  This would also help with recording a rebase, as rebase will be recorded as a slew of commits.

So I had 30 minutes to kill and started in on this… and it was surprisingly easy (see diff below).  It this approach considered too gruesome?

> That does sound worthwhile, and perhaps also some hooks that tell you
> about the logical operations separate from the particular commands
> that cause them (which might be more sustainable.)


I'm not exactly sure what you mean by logical operations?  How would that be different from the existing branch, pull, push, commit, and branch-tip-change hooks?

Brian.

=== modified file 'bzrlib/commands.py'
--- bzrlib/commands.py	2011-09-29 15:50:58 +0000
+++ bzrlib/commands.py	2011-10-24 21:25:21 +0000
@@ -408,6 +408,7 @@
         """Construct an instance of this command."""
         # List of standard options directly supported
         self.supported_std_options = []
+        self._hook_run()
         self._setup_run()
 
     def add_cleanup(self, cleanup_func, *args, **kwargs):
@@ -682,6 +683,29 @@
                 display=('bytes' in debug.debug_flags))
             trace.set_verbosity_level(0)
 
+    def _hook_run(self):
+        """Wrap the defined run method on self with command execution hooks.
+
+        This is called by __init__ to make the Command be able to be run
+        by just calling run(), as it could be before hook executions were added.
+        """
+        class_run = self.run
+        def run(*args, **kwargs):
+            for hook in Command.hooks['pre_command']:
+                try:
+                    hook(self)
+                except: pass
+            raised = None
+            try:
+                return class_run(*args, **kwargs)
+            except Exception, e:
+                raised = e
+                raise e
+            finally:
+                for hook in Command.hooks['post_command']:
+                    hook(self, raised)
+        self.run = run
+
     def _setup_run(self):
         """Wrap the defined run method on self with a cleanup.
 
@@ -791,6 +815,14 @@
             " is safe to mutate - e.g. to remove a command. "
             "list_commands should return the updated set of command names.",
             (1, 17))
+        self.add_hook('pre_command',
+            "Called prior to executing a command. Called with the command "
+            "object.",
+            (2, 5))
+        self.add_hook('post_command',
+            "Called after executing a command. Called with the command "
+            "object and the raised exception (or None).",
+            (2, 5))
 
 Command.hooks = CommandHooks()
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2759 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/bazaar/attachments/20111024/d7b1100f/attachment.bin>


More information about the bazaar mailing list