view mercurial/ @ 2322:685597676a13

packagescan: handle demandload module naming changes.
author Volker Kleinfeld <>
date Fri, 19 May 2006 08:51:58 -0700
parents 7f12a63568ae
children c4ea7f927dab
line wrap: on
line source

# - Helper module for identifing used modules.
# Used for the py2exe distutil.
# Copyright 2005 Volker Kleinfeld <>
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.
import glob
import os
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 
        foo            import foo
        foo bar        import foo, bar        import
        foo:bar        from foo import bar
        foo:bar,quux   from foo import bar, quux   from import quux"""

    for m in modules.split():
        mod = None
            module, fromlist = m.split(':')
            fromlist = fromlist.split(',')
            module = m
            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
            # 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> """
    # Use the package in the build directory
    libpath = os.path.abspath(libpath)
    packdir = os.path.join(libpath,packagename)
    # A normal import would not find the package in
    # the build directory. ihook is used to force the import.
    # After the package is imported the import scope for
    # the following imports is settled.
    p = importfrom(packdir)
    globals()[packagename] = p
    sys.modules[packagename] = p
    # Fetch the python modules in the package
    cwd = os.getcwd()
    pymodulefiles = glob.glob('*.py')
    extmodulefiles = glob.glob('*.pyd')
    # Install a fake demandload module
    sys.modules['mercurial.demandload'] = sys.modules['mercurial.packagescan']
    # Import all python modules and by that run the fake demandload
    for m in pymodulefiles:
        if m == '': continue
        tmp = {}
        mname,ext = os.path.splitext(m)
        fullname = packagename+'.'+mname
        requiredmodules[fullname] = 1
    # Import all extension modules and by that run the fake demandload
    for m in extmodulefiles:
        tmp = {}
        mname,ext = os.path.splitext(m)
        fullname = packagename+'.'+mname
        requiredmodules[fullname] = 1
    includes = requiredmodules.keys()
    return includes

def importfrom(filename):
    import module/package from a named file and returns the module.
    It does not check on sys.modules or includes the module in the scope.
    loader = ihooks.BasicModuleLoader()
    path, file = os.path.split(filename)
    name, ext  = os.path.splitext(file)
    m = loader.find_module_in_dir(name, path)
    if not m:
        raise ImportError, name
    m = loader.load_module(name, m)
    return m