[PATCH 1 of 2] extensions: load all extensions first, call setup functions thereafter

Simon Heimberg simohe at besonet.ch
Wed Aug 26 16:02:21 CDT 2009


# HG changeset patch
# User Simon Heimberg <simohe at besonet.ch>
# Date 1250376074 -7200
# Node ID 51ca46023b3844b11ee304555ac6f23b7581d67c
# Parent  45c108d14fa1af9e3048dc85bc497b80c2f17431
extensions: load all extensions first, call setup functions thereafter

new order for extension calls:
- load all extensions
- uisetup(ui) for all extensions
- extsetup() for all extensions
- add commands to commandtable for all extensions
- reposetup(ui, repo) for all extensions

diff -r 45c108d14fa1 -r 51ca46023b38 mercurial/dispatch.py
--- a/mercurial/dispatch.py	Son Aug 16 00:41:12 2009 +0200
+++ b/mercurial/dispatch.py	Son Aug 16 00:41:14 2009 +0200
@@ -320,7 +320,6 @@
               result = ret)
     return ret
 
-_loaded = set()
 def _dispatch(ui, args):
     # read --config before doing anything else
     # (e.g. to change trust settings for reading .hg/hgrc)
@@ -350,25 +349,6 @@
         lui.readconfig(os.path.join(path, ".hg", "hgrc"))
 
     extensions.loadall(lui)
-    for name, module in extensions.extensions():
-        if name in _loaded:
-            continue
-
-        # setup extensions
-        # TODO this should be generalized to scheme, where extensions can
-        #      redepend on other extensions.  then we should toposort them, and
-        #      do initialization in correct order
-        extsetup = getattr(module, 'extsetup', None)
-        if extsetup:
-            extsetup()
-
-        cmdtable = getattr(module, 'cmdtable', {})
-        overrides = [cmd for cmd in cmdtable if cmd in commands.table]
-        if overrides:
-            ui.warn(_("extension '%s' overrides commands: %s\n")
-                    % (name, " ".join(overrides)))
-        commands.table.update(cmdtable)
-        _loaded.add(name)
 
     addaliases(lui, commands.table)
 
diff -r 45c108d14fa1 -r 51ca46023b38 mercurial/extensions.py
--- a/mercurial/extensions.py	Son Aug 16 00:41:12 2009 +0200
+++ b/mercurial/extensions.py	Son Aug 16 00:41:14 2009 +0200
@@ -39,7 +39,7 @@
     else:
         return imp.load_source(module_name, path)
 
-def load(ui, name, path):
+def load(ui, name, path, nosetup=False):
     if name.startswith('hgext.') or name.startswith('hgext/'):
         shortname = name[6:]
     else:
@@ -66,18 +66,21 @@
     _extensions[shortname] = mod
     _order.append(shortname)
 
-    uisetup = getattr(mod, 'uisetup', None)
-    if uisetup:
-        uisetup(ui)
+    if nosetup:
+        return
+    setup(ui, [shortname])
 
 def loadall(ui):
+    '''
+    load all configured extensions and execute setup
+    '''
     result = ui.configitems("extensions")
     for (name, path) in result:
         if path:
             if path[0] == '!':
                 continue
         try:
-            load(ui, name, path)
+            load(ui, name, path, True)
         except KeyboardInterrupt:
             raise
         except Exception, inst:
@@ -89,6 +92,40 @@
                         % (name, inst))
             if ui.traceback():
                 return 1
+    setup(ui, _order)
+
+_loaded = set()
+def setup(ui, modules):
+    '''
+    Execute uisetup and extsetup for all given modules and update the
+    commandtable. Each task is done for all extensions before the next task.
+    If modules is none, setup is executed for all loaded uninitialized
+    modules.
+    '''
+    import commands # import here for breaking import cycle
+
+    unloaded = []
+    for name in modules:
+        if name in _loaded:
+            continue
+        mod = _extensions[name]
+        unloaded.append((name, mod))
+        _loaded.add(name)
+        uisetup = getattr(mod, 'uisetup', None)
+        if uisetup:
+            uisetup(ui)
+    for name, mod in unloaded:
+        extsetup = getattr(mod, 'extsetup', None)
+        if extsetup:
+            extsetup()
+    for name, mod in unloaded:
+        cmdtable = getattr(mod, 'cmdtable', {})
+        overrides = [cmd for cmd in cmdtable if cmd in commands.table]
+        if overrides:
+            ui.warn(_("extension '%s' overrides commands: %s\n")
+                    % (name, " ".join(overrides)))
+        commands.table.update(cmdtable)
+    return unloaded
 
 def wrapcommand(table, command, wrapper):
     aliases, entry = cmdutil.findcmd(command, table)


More information about the Mercurial-devel mailing list