[PATCH] Add support for external command handlers

Michael Ellerman michael+bazaar at ellerman.id.au
Wed May 4 06:23:50 BST 2005


Hi Martin,

This patch allows you to create a script, eg. 'do-cool-stuff' which you place
somewhere in your BZRPATH. Then when you type 'bzr do-cool-stuff' bzr will
find the script and call it.

Rusty held a shotgun to my head and made me implement this. I swear!

cheers


*** modified file 'bzrlib/commands.py'
--- bzrlib/commands.py 
+++ bzrlib/commands.py 
@@ -126,13 +126,27 @@
     
     cmd = cmd_aliases.get(cmd, cmd)
     
+    is_external = False
     try:
         cmd_handler = globals()['cmd_' + cmd.replace('-', '_')]
     except KeyError:
-        raise BzrError("unknown command %r" % cmd)
-
-    return cmd, cmd_handler
-
+        cmd_handler = _lookup_external_cmd_handler(cmd)
+        if not cmd_handler:
+            raise BzrError("unknown command %r" % cmd)
+        is_external = True
+
+    return cmd, cmd_handler, is_external
+
+
+def _lookup_external_cmd_handler(cmd):
+    bzrpath = os.environ.get('BZRPATH', '')
+
+    for dir in bzrpath.split(':'):
+        path = os.path.join(dir, cmd)
+        if os.path.isfile(path):
+            return lambda argv: os.spawnv(os.P_WAIT, path, [path] + argv)
+
+    return None
 
 
 def cmd_status(all=False):
@@ -770,13 +784,15 @@
         help_commands()
     else:
         # otherwise, maybe the name of a command?
-        topic, cmdfn = get_cmd_handler(topic)
+        topic, cmdfn, is_external = get_cmd_handler(topic)
 
         doc = getdoc(cmdfn)
-        if doc == None:
+        if doc: print doc
+        elif is_external:
+            # For external commands we just call them with --help.
+            cmdfn([topic, '--help'])
+        else:
             bailout("sorry, no detailed help yet for %r" % topic)
-
-        print doc
 
 
 def help_commands():
@@ -1035,7 +1051,10 @@
         log_error('  try "bzr help"')
         return 1
 
-    canonical_cmd, cmd_handler = get_cmd_handler(cmd)
+    canonical_cmd, cmd_handler, is_external = get_cmd_handler(cmd)
+
+    if is_external:
+        return cmd_handler(argv[1:])
 
     # global option
     if 'profile' in opts:






More information about the bazaar mailing list