Show More
@@ -1,74 +1,80 b'' | |||
|
1 | 1 | # packagescan.py - Helper module for identifing used modules. |
|
2 | 2 | # Used for the py2exe distutil. |
|
3 | 3 | # |
|
4 | 4 | # Copyright 2005 Volker Kleinfeld <Volker.Kleinfeld@gmx.de> |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms |
|
7 | 7 | # of the GNU General Public License, incorporated herein by reference. |
|
8 | 8 | import glob |
|
9 | 9 | import os |
|
10 | 10 | import sys |
|
11 | 11 | import demandload |
|
12 | 12 | import ihooks |
|
13 | 13 | |
|
14 | 14 | requiredmodules = {} # Will contain the modules imported by demandload |
|
15 | 15 | def demandload(scope, modules): |
|
16 | 16 | """ fake demandload function that collects the required modules """ |
|
17 | 17 | for m in modules.split(): |
|
18 | 18 | mod = None |
|
19 | mod = __import__(m,scope,scope) | |
|
20 | scope[m] = mod | |
|
19 | try: | |
|
20 | module, submodules = m.split(':') | |
|
21 | submodules = submodules.split(',') | |
|
22 | except: | |
|
23 | module = m | |
|
24 | submodules = [] | |
|
25 | mod = __import__(module, scope, scope, submodules) | |
|
26 | scope[module] = mod | |
|
21 | 27 | requiredmodules[mod.__name__] = 1 |
|
22 | 28 | |
|
23 | 29 | def getmodules(libpath,packagename): |
|
24 | 30 | """ helper for finding all required modules of package <packagename> """ |
|
25 | 31 | # Use the package in the build directory |
|
26 | 32 | libpath = os.path.abspath(libpath) |
|
27 | 33 | sys.path.insert(0,libpath) |
|
28 | 34 | packdir = os.path.join(libpath,packagename) |
|
29 | 35 | # A normal import would not find the package in |
|
30 | 36 | # the build directory. ihook is used to force the import. |
|
31 | 37 | # After the package is imported the import scope for |
|
32 | 38 | # the following imports is settled. |
|
33 | 39 | p = importfrom(packdir) |
|
34 | 40 | globals()[packagename] = p |
|
35 | 41 | sys.modules[packagename] = p |
|
36 | 42 | # Fetch the python modules in the package |
|
37 | 43 | cwd = os.getcwd() |
|
38 | 44 | os.chdir(packdir) |
|
39 | 45 | pymodulefiles = glob.glob('*.py') |
|
40 | 46 | extmodulefiles = glob.glob('*.pyd') |
|
41 | 47 | os.chdir(cwd) |
|
42 | 48 | # Install a fake demandload module |
|
43 | 49 | sys.modules['mercurial.demandload'] = sys.modules['mercurial.packagescan'] |
|
44 | 50 | # Import all python modules and by that run the fake demandload |
|
45 | 51 | for m in pymodulefiles: |
|
46 | 52 | if m == '__init__.py': continue |
|
47 | 53 | tmp = {} |
|
48 | 54 | mname,ext = os.path.splitext(m) |
|
49 | 55 | fullname = packagename+'.'+mname |
|
50 | 56 | __import__(fullname,tmp,tmp) |
|
51 | 57 | requiredmodules[fullname] = 1 |
|
52 | 58 | # Import all extension modules and by that run the fake demandload |
|
53 | 59 | for m in extmodulefiles: |
|
54 | 60 | tmp = {} |
|
55 | 61 | mname,ext = os.path.splitext(m) |
|
56 | 62 | fullname = packagename+'.'+mname |
|
57 | 63 | __import__(fullname,tmp,tmp) |
|
58 | 64 | requiredmodules[fullname] = 1 |
|
59 | 65 | includes = requiredmodules.keys() |
|
60 | 66 | return includes |
|
61 | 67 | |
|
62 | 68 | def importfrom(filename): |
|
63 | 69 | """ |
|
64 | 70 | import module/package from a named file and returns the module. |
|
65 | 71 | It does not check on sys.modules or includes the module in the scope. |
|
66 | 72 | """ |
|
67 | 73 | loader = ihooks.BasicModuleLoader() |
|
68 | 74 | path, file = os.path.split(filename) |
|
69 | 75 | name, ext = os.path.splitext(file) |
|
70 | 76 | m = loader.find_module_in_dir(name, path) |
|
71 | 77 | if not m: |
|
72 | 78 | raise ImportError, name |
|
73 | 79 | m = loader.load_module(name, m) |
|
74 | 80 | return m |
General Comments 0
You need to be logged in to leave comments.
Login now