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