mercurial/packagescan.py
changeset 3673 eb0b4a2d70a9
parent 2982 799811087044
equal deleted inserted replaced
3672:e8730b5b8a32 3673:eb0b4a2d70a9
    53                 if not as_:
    53                 if not as_:
    54                     as_ = mn
    54                     as_ = mn
    55                 scope[as_] = mod
    55                 scope[as_] = mod
    56                 requiredmodules[mod.__name__] = 1
    56                 requiredmodules[mod.__name__] = 1
    57                 if len(comp) == i: break
    57                 if len(comp) == i: break
    58                 mod = getattr(mod,comp[i])
    58                 mod = getattr(mod, comp[i])
    59                 mn = string.join(comp[:i+1],'.')
    59                 mn = string.join(comp[:i+1],'.')
    60                 i += 1
    60                 i += 1
    61         else:
    61         else:
    62             # mod is the last package in the component list
    62             # mod is the last package in the component list
    63             requiredmodules[mod.__name__] = 1
    63             requiredmodules[mod.__name__] = 1
    64             for f in fromlist:
    64             for f in fromlist:
    65                 scope[f] = getattr(mod,f)
    65                 scope[f] = getattr(mod, f)
    66                 if type(scope[f]) == types.ModuleType:
    66                 if type(scope[f]) == types.ModuleType:
    67                     requiredmodules[scope[f].__name__] = 1
    67                     requiredmodules[scope[f].__name__] = 1
    68 
    68 
    69 class SkipPackage(Exception):
    69 class SkipPackage(Exception):
    70     def __init__(self, reason):
    70     def __init__(self, reason):
    71         self.reason = reason
    71         self.reason = reason
    72 
    72 
    73 scan_in_progress = False
    73 scan_in_progress = False
    74 
    74 
    75 def scan(libpath,packagename):
    75 def scan(libpath, packagename):
    76     """ helper for finding all required modules of package <packagename> """
    76     """ helper for finding all required modules of package <packagename> """
    77     global scan_in_progress
    77     global scan_in_progress
    78     scan_in_progress = True
    78     scan_in_progress = True
    79     # Use the package in the build directory
    79     # Use the package in the build directory
    80     libpath = os.path.abspath(libpath)
    80     libpath = os.path.abspath(libpath)
    81     sys.path.insert(0,libpath)
    81     sys.path.insert(0, libpath)
    82     packdir = os.path.join(libpath,packagename.replace('.', '/'))
    82     packdir = os.path.join(libpath, packagename.replace('.', '/'))
    83     # A normal import would not find the package in
    83     # A normal import would not find the package in
    84     # the build directory. ihook is used to force the import.
    84     # the build directory. ihook is used to force the import.
    85     # After the package is imported the import scope for
    85     # After the package is imported the import scope for
    86     # the following imports is settled.
    86     # the following imports is settled.
    87     p = importfrom(packdir)
    87     p = importfrom(packdir)
    95     os.chdir(cwd)
    95     os.chdir(cwd)
    96     # Import all python modules and by that run the fake demandload
    96     # Import all python modules and by that run the fake demandload
    97     for m in pymodulefiles:
    97     for m in pymodulefiles:
    98         if m == '__init__.py': continue
    98         if m == '__init__.py': continue
    99         tmp = {}
    99         tmp = {}
   100         mname,ext = os.path.splitext(m)
   100         mname, ext = os.path.splitext(m)
   101         fullname = packagename+'.'+mname
   101         fullname = packagename+'.'+mname
   102         try:
   102         try:
   103             __import__(fullname,tmp,tmp)
   103             __import__(fullname, tmp, tmp)
   104         except SkipPackage, inst:
   104         except SkipPackage, inst:
   105             print >> sys.stderr, 'skipping %s: %s' % (fullname, inst.reason)
   105             print >> sys.stderr, 'skipping %s: %s' % (fullname, inst.reason)
   106             continue
   106             continue
   107         requiredmodules[fullname] = 1
   107         requiredmodules[fullname] = 1
   108     # Import all extension modules and by that run the fake demandload
   108     # Import all extension modules and by that run the fake demandload
   109     for m in extmodulefiles:
   109     for m in extmodulefiles:
   110         tmp = {}
   110         tmp = {}
   111         mname,ext = os.path.splitext(m)
   111         mname, ext = os.path.splitext(m)
   112         fullname = packagename+'.'+mname
   112         fullname = packagename+'.'+mname
   113         __import__(fullname,tmp,tmp)
   113         __import__(fullname, tmp, tmp)
   114         requiredmodules[fullname] = 1
   114         requiredmodules[fullname] = 1
   115 
   115 
   116 def getmodules():
   116 def getmodules():
   117     return requiredmodules.keys()
   117     return requiredmodules.keys()
   118 
   118