Show More
@@ -1,86 +1,113 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 | # This module must be the first mercurial module imported in setup.py |
|
3 | # This module must be the first mercurial module imported in setup.py | |
4 | # |
|
4 | # | |
5 | # Copyright 2005 Volker Kleinfeld <Volker.Kleinfeld@gmx.de> |
|
5 | # Copyright 2005 Volker Kleinfeld <Volker.Kleinfeld@gmx.de> | |
6 | # |
|
6 | # | |
7 | # This software may be used and distributed according to the terms |
|
7 | # This software may be used and distributed according to the terms | |
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, |
|
36 | module, fromlist = m.split(':') | |
28 |
|
|
37 | fromlist = fromlist.split(',') | |
29 | except: |
|
38 | except: | |
30 | module = m |
|
39 | module = m | |
31 |
|
|
40 | fromlist = [] | |
32 |
mod = __import__(module, scope, scope, |
|
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) | |
40 | sys.path.insert(0,libpath) |
|
67 | sys.path.insert(0,libpath) | |
41 | packdir = os.path.join(libpath,packagename) |
|
68 | packdir = os.path.join(libpath,packagename) | |
42 | # A normal import would not find the package in |
|
69 | # A normal import would not find the package in | |
43 | # the build directory. ihook is used to force the import. |
|
70 | # the build directory. ihook is used to force the import. | |
44 | # After the package is imported the import scope for |
|
71 | # After the package is imported the import scope for | |
45 | # the following imports is settled. |
|
72 | # the following imports is settled. | |
46 | p = importfrom(packdir) |
|
73 | p = importfrom(packdir) | |
47 | globals()[packagename] = p |
|
74 | globals()[packagename] = p | |
48 | sys.modules[packagename] = p |
|
75 | sys.modules[packagename] = p | |
49 | # Fetch the python modules in the package |
|
76 | # Fetch the python modules in the package | |
50 | cwd = os.getcwd() |
|
77 | cwd = os.getcwd() | |
51 | os.chdir(packdir) |
|
78 | os.chdir(packdir) | |
52 | pymodulefiles = glob.glob('*.py') |
|
79 | pymodulefiles = glob.glob('*.py') | |
53 | extmodulefiles = glob.glob('*.pyd') |
|
80 | extmodulefiles = glob.glob('*.pyd') | |
54 | os.chdir(cwd) |
|
81 | os.chdir(cwd) | |
55 | # Import all python modules and by that run the fake demandload |
|
82 | # Import all python modules and by that run the fake demandload | |
56 | for m in pymodulefiles: |
|
83 | for m in pymodulefiles: | |
57 | if m == '__init__.py': continue |
|
84 | if m == '__init__.py': continue | |
58 | tmp = {} |
|
85 | tmp = {} | |
59 | mname,ext = os.path.splitext(m) |
|
86 | mname,ext = os.path.splitext(m) | |
60 | fullname = packagename+'.'+mname |
|
87 | fullname = packagename+'.'+mname | |
61 | __import__(fullname,tmp,tmp) |
|
88 | __import__(fullname,tmp,tmp) | |
62 | requiredmodules[fullname] = 1 |
|
89 | requiredmodules[fullname] = 1 | |
63 | # Import all extension modules and by that run the fake demandload |
|
90 | # Import all extension modules and by that run the fake demandload | |
64 | for m in extmodulefiles: |
|
91 | for m in extmodulefiles: | |
65 | tmp = {} |
|
92 | tmp = {} | |
66 | mname,ext = os.path.splitext(m) |
|
93 | mname,ext = os.path.splitext(m) | |
67 | fullname = packagename+'.'+mname |
|
94 | fullname = packagename+'.'+mname | |
68 | __import__(fullname,tmp,tmp) |
|
95 | __import__(fullname,tmp,tmp) | |
69 | requiredmodules[fullname] = 1 |
|
96 | requiredmodules[fullname] = 1 | |
70 |
|
97 | |||
71 | def getmodules(): |
|
98 | def getmodules(): | |
72 | return requiredmodules.keys() |
|
99 | return requiredmodules.keys() | |
73 |
|
100 | |||
74 | def importfrom(filename): |
|
101 | def importfrom(filename): | |
75 | """ |
|
102 | """ | |
76 | import module/package from a named file and returns the module. |
|
103 | import module/package from a named file and returns the module. | |
77 | It does not check on sys.modules or includes the module in the scope. |
|
104 | It does not check on sys.modules or includes the module in the scope. | |
78 | """ |
|
105 | """ | |
79 | loader = ihooks.BasicModuleLoader() |
|
106 | loader = ihooks.BasicModuleLoader() | |
80 | path, file = os.path.split(filename) |
|
107 | path, file = os.path.split(filename) | |
81 | name, ext = os.path.splitext(file) |
|
108 | name, ext = os.path.splitext(file) | |
82 | m = loader.find_module_in_dir(name, path) |
|
109 | m = loader.find_module_in_dir(name, path) | |
83 | if not m: |
|
110 | if not m: | |
84 | raise ImportError, name |
|
111 | raise ImportError, name | |
85 | m = loader.load_module(name, m) |
|
112 | m = loader.load_module(name, m) | |
86 | return m |
|
113 | return m |
General Comments 0
You need to be logged in to leave comments.
Login now