mercurial/packagescan.py
changeset 2322 685597676a13
parent 1841 7f12a63568ae
child 2325 c4ea7f927dab
equal deleted inserted replaced
2298:4be9a79b49b1 2322:685597676a13
     8 import glob
     8 import glob
     9 import os
     9 import os
    10 import sys
    10 import sys
    11 import demandload
    11 import demandload
    12 import ihooks
    12 import ihooks
       
    13 import types
       
    14 import string
    13 
    15 
    14 requiredmodules = {} # Will contain the modules imported by demandload
    16 requiredmodules = {} # Will contain the modules imported by demandload
    15 def demandload(scope, modules):
    17 def demandload(scope, modules):
    16     """ fake demandload function that collects the required modules """
    18     """ fake demandload function that collects the required modules 
       
    19         foo            import foo
       
    20         foo bar        import foo, bar
       
    21         foo.bar        import foo.bar
       
    22         foo:bar        from foo import bar
       
    23         foo:bar,quux   from foo import bar, quux
       
    24         foo.bar:quux   from foo.bar import quux"""
       
    25 
    17     for m in modules.split():
    26     for m in modules.split():
    18         mod = None
    27         mod = None
    19         try:
    28         try:
    20             module, submodules = m.split(':')
    29             module, fromlist = m.split(':')
    21             submodules = submodules.split(',')
    30             fromlist = fromlist.split(',')
    22         except:
    31         except:
    23             module = m
    32             module = m
    24             submodules = []
    33             fromlist = []
    25         mod = __import__(module, scope, scope, submodules)
    34         mod = __import__(module, scope, scope, fromlist)
    26         scope[module] = mod
    35         if fromlist == []:
    27         requiredmodules[mod.__name__] = 1
    36             # mod is only the top package, but we need all packages
       
    37             comp = module.split('.')
       
    38             i = 1
       
    39             mn = comp[0]
       
    40             while True:
       
    41                 # mn and mod.__name__ might not be the same
       
    42                 scope[mn] = mod
       
    43                 requiredmodules[mod.__name__] = 1
       
    44                 if len(comp) == i: break
       
    45                 mod = getattr(mod,comp[i]) 
       
    46                 mn = string.join(comp[:i+1],'.')
       
    47                 i += 1
       
    48         else:
       
    49             # mod is the last package in the component list
       
    50             requiredmodules[mod.__name__] = 1
       
    51             for f in fromlist:
       
    52                 scope[f] = getattr(mod,f)
       
    53                 if type(scope[f]) == types.ModuleType:
       
    54                     requiredmodules[scope[f].__name__] = 1
    28 
    55 
    29 def getmodules(libpath,packagename):
    56 def getmodules(libpath,packagename):
    30     """ helper for finding all required modules of package <packagename> """
    57     """ helper for finding all required modules of package <packagename> """
    31     # Use the package in the build directory
    58     # Use the package in the build directory
    32     libpath = os.path.abspath(libpath)
    59     libpath = os.path.abspath(libpath)