packagescan: handle demandload module naming changes.
authorVolker Kleinfeld <Volker.Kleinfeld@gmx.de>
Fri, 19 May 2006 08:51:58 -0700
changeset 2322 685597676a13
parent 2298 4be9a79b49b1
child 2325 c4ea7f927dab
packagescan: handle demandload module naming changes.
mercurial/packagescan.py
--- a/mercurial/packagescan.py	Wed May 17 13:21:36 2006 -0500
+++ b/mercurial/packagescan.py	Fri May 19 08:51:58 2006 -0700
@@ -10,21 +10,48 @@
 import sys
 import demandload
 import ihooks
+import types
+import string
 
 requiredmodules = {} # Will contain the modules imported by demandload
 def demandload(scope, modules):
-    """ fake demandload function that collects the required modules """
+    """ fake demandload function that collects the required modules 
+        foo            import foo
+        foo bar        import foo, bar
+        foo.bar        import foo.bar
+        foo:bar        from foo import bar
+        foo:bar,quux   from foo import bar, quux
+        foo.bar:quux   from foo.bar import quux"""
+
     for m in modules.split():
         mod = None
         try:
-            module, submodules = m.split(':')
-            submodules = submodules.split(',')
+            module, fromlist = m.split(':')
+            fromlist = fromlist.split(',')
         except:
             module = m
-            submodules = []
-        mod = __import__(module, scope, scope, submodules)
-        scope[module] = mod
-        requiredmodules[mod.__name__] = 1
+            fromlist = []
+        mod = __import__(module, scope, scope, fromlist)
+        if fromlist == []:
+            # mod is only the top package, but we need all packages
+            comp = module.split('.')
+            i = 1
+            mn = comp[0]
+            while True:
+                # mn and mod.__name__ might not be the same
+                scope[mn] = mod
+                requiredmodules[mod.__name__] = 1
+                if len(comp) == i: break
+                mod = getattr(mod,comp[i]) 
+                mn = string.join(comp[:i+1],'.')
+                i += 1
+        else:
+            # mod is the last package in the component list
+            requiredmodules[mod.__name__] = 1
+            for f in fromlist:
+                scope[f] = getattr(mod,f)
+                if type(scope[f]) == types.ModuleType:
+                    requiredmodules[scope[f].__name__] = 1
 
 def getmodules(libpath,packagename):
     """ helper for finding all required modules of package <packagename> """