This commit was manufactured by cvs2svn to create tag 'r221'.
[python/dscho.git] / Mac / Tools / macfreeze / macmodulefinder.py
blob8cd6a00ddd563f4d383374771e1a0e0621897aab
1 """macmodulefinder - Find modules used in a script. Only slightly
2 mac-specific, really."""
4 import sys
5 import os
7 import directives
9 try:
10 # This will work if we are frozen ourselves
11 import modulefinder
12 except ImportError:
13 # And this will work otherwise
14 _FREEZEDIR=os.path.join(sys.prefix, ':Tools:freeze')
15 sys.path.insert(0, _FREEZEDIR)
16 import modulefinder
19 # Modules that must be included, and modules that need not be included
20 # (but are if they are found)
22 MAC_INCLUDE_MODULES=['site', 'macfsn']
23 MAC_MAYMISS_MODULES=['posix', 'os2', 'nt', 'ntpath', 'dos', 'dospath',
24 'win32api', 'ce', '_winreg',
25 'nturl2path', 'pwd', 'sitecustomize',
26 'org.python.core',
27 'riscos', 'riscosenviron', 'riscospath'
30 # An exception:
31 Missing="macmodulefinder.Missing"
33 class Module(modulefinder.Module):
35 def gettype(self):
36 """Return type of module"""
37 if self.__path__:
38 return 'package'
39 if self.__code__:
40 return 'module'
41 if self.__file__:
42 return 'dynamic'
43 return 'builtin'
45 class ModuleFinder(modulefinder.ModuleFinder):
47 def add_module(self, fqname):
48 if self.modules.has_key(fqname):
49 return self.modules[fqname]
50 self.modules[fqname] = m = Module(fqname)
51 return m
53 def process(program, modules=None, module_files=None, debug=0):
54 if modules is None:
55 modules = []
56 if module_files is None:
57 module_files = []
58 missing = []
60 # Add the standard modules needed for startup
62 modules = modules + MAC_INCLUDE_MODULES
64 # search the main source for directives
66 extra_modules, exclude_modules, optional_modules, extra_path = \
67 directives.findfreezedirectives(program)
68 for m in extra_modules:
69 if os.sep in m:
70 # It is a file
71 module_files.append(m)
72 else:
73 modules.append(m)
74 # collect all modules of the program
75 path = sys.path[:]
76 dir = os.path.dirname(program)
77 path[0] = dir # "current dir"
78 path = extra_path + path
80 # Create the module finder and let it do its work
82 modfinder = ModuleFinder(path,
83 excludes=exclude_modules, debug=debug)
84 for m in modules:
85 modfinder.import_hook(m)
86 for m in module_files:
87 modfinder.load_file(m)
88 modfinder.run_script(program)
89 module_dict = modfinder.modules
91 # Tell the user about missing modules
93 maymiss = exclude_modules + optional_modules + MAC_MAYMISS_MODULES
94 for m in modfinder.badmodules.keys():
95 if not m in maymiss:
96 if debug > 0:
97 print 'Missing', m
98 missing.append(m)
100 # Warn the user about unused builtins
102 for m in sys.builtin_module_names:
103 if m in ('__main__', '__builtin__'):
104 pass
105 elif not module_dict.has_key(m):
106 if debug > 0:
107 print 'Unused', m
108 elif module_dict[m].gettype() != 'builtin':
109 # XXXX Can this happen?
110 if debug > 0:
111 print 'Conflict', m
112 return module_dict, missing