mercurial/packagescan.py
changeset 2325 c4ea7f927dab
parent 2323 c58a403aa830
parent 2322 685597676a13
child 2402 82cef38fea56
equal deleted inserted replaced
2324:7cbe8cd69d6b 2325:c4ea7f927dab
     8 # of the GNU General Public License, incorporated herein by reference.
     8 # of the GNU General Public License, incorporated herein by reference.
     9 import glob
     9 import glob
    10 import os
    10 import os
    11 import sys
    11 import sys
    12 import ihooks
    12 import ihooks
       
    13 import types
       
    14 import string
    13 
    15 
    14 # Install this module as fake demandload module
    16 # Install this module as fake demandload module
    15 sys.modules['mercurial.demandload'] = sys.modules[__name__]
    17 sys.modules['mercurial.demandload'] = sys.modules[__name__]
    16 
    18 
    17 # Requiredmodules contains the modules imported by demandload.
    19 # Requiredmodules contains the modules imported by demandload.
    18 # Please note that demandload can be invoked before the 
    20 # Please note that demandload can be invoked before the 
    19 # mercurial.packagescan.scan method is invoked in case a mercurial
    21 # mercurial.packagescan.scan method is invoked in case a mercurial
    20 # module is imported.
    22 # module is imported.
    21 requiredmodules = {} 
    23 requiredmodules = {} 
    22 def demandload(scope, modules):
    24 def demandload(scope, modules):
    23     """ fake demandload function that collects the required modules """
    25     """ fake demandload function that collects the required modules 
       
    26         foo            import foo
       
    27         foo bar        import foo, bar
       
    28         foo.bar        import foo.bar
       
    29         foo:bar        from foo import bar
       
    30         foo:bar,quux   from foo import bar, quux
       
    31         foo.bar:quux   from foo.bar import quux"""
       
    32 
    24     for m in modules.split():
    33     for m in modules.split():
    25         mod = None
    34         mod = None
    26         try:
    35         try:
    27             module, submodules = m.split(':')
    36             module, fromlist = m.split(':')
    28             submodules = submodules.split(',')
    37             fromlist = fromlist.split(',')
    29         except:
    38         except:
    30             module = m
    39             module = m
    31             submodules = []
    40             fromlist = []
    32         mod = __import__(module, scope, scope, submodules)
    41         mod = __import__(module, scope, scope, fromlist)
    33         scope[module] = mod
    42         if fromlist == []:
    34         requiredmodules[mod.__name__] = 1
    43             # mod is only the top package, but we need all packages
       
    44             comp = module.split('.')
       
    45             i = 1
       
    46             mn = comp[0]
       
    47             while True:
       
    48                 # mn and mod.__name__ might not be the same
       
    49                 scope[mn] = mod
       
    50                 requiredmodules[mod.__name__] = 1
       
    51                 if len(comp) == i: break
       
    52                 mod = getattr(mod,comp[i]) 
       
    53                 mn = string.join(comp[:i+1],'.')
       
    54                 i += 1
       
    55         else:
       
    56             # mod is the last package in the component list
       
    57             requiredmodules[mod.__name__] = 1
       
    58             for f in fromlist:
       
    59                 scope[f] = getattr(mod,f)
       
    60                 if type(scope[f]) == types.ModuleType:
       
    61                     requiredmodules[scope[f].__name__] = 1
    35 
    62 
    36 def scan(libpath,packagename):
    63 def scan(libpath,packagename):
    37     """ helper for finding all required modules of package <packagename> """
    64     """ helper for finding all required modules of package <packagename> """
    38     # Use the package in the build directory
    65     # Use the package in the build directory
    39     libpath = os.path.abspath(libpath)
    66     libpath = os.path.abspath(libpath)