Bump version to 0.9.1.
[python/dscho.git] / Mac / Tools / macfreeze / macmodulefinder.py
blob166942dad60bdb0d8856be4ae3b697eb012ae421
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', 'exceptions', 'macfsn']
23 MAC_MAYMISS_MODULES=['posix', 'os2', 'nt', 'ntpath', 'dos', 'dospath',
24 'win32api', 'ce',
25 'nturl2path', 'pwd', 'sitecustomize',
26 'org.python.core']
28 # An exception:
29 Missing="macmodulefinder.Missing"
31 class Module(modulefinder.Module):
33 def gettype(self):
34 """Return type of module"""
35 if self.__path__:
36 return 'package'
37 if self.__code__:
38 return 'module'
39 if self.__file__:
40 return 'dynamic'
41 return 'builtin'
43 class ModuleFinder(modulefinder.ModuleFinder):
45 def add_module(self, fqname):
46 if self.modules.has_key(fqname):
47 return self.modules[fqname]
48 self.modules[fqname] = m = Module(fqname)
49 return m
51 def process(program, modules=None, module_files=None, debug=0):
52 if modules is None:
53 modules = []
54 if module_files is None:
55 module_files = []
56 missing = []
58 # Add the standard modules needed for startup
60 modules = modules + MAC_INCLUDE_MODULES
62 # search the main source for directives
64 extra_modules, exclude_modules, optional_modules, extra_path = \
65 directives.findfreezedirectives(program)
66 for m in extra_modules:
67 if os.sep in m:
68 # It is a file
69 module_files.append(m)
70 else:
71 modules.append(m)
72 # collect all modules of the program
73 path = sys.path[:]
74 dir = os.path.dirname(program)
75 path[0] = dir # "current dir"
76 path = extra_path + path
78 # Create the module finder and let it do its work
80 modfinder = ModuleFinder(path,
81 excludes=exclude_modules, debug=debug)
82 for m in modules:
83 modfinder.import_hook(m)
84 for m in module_files:
85 modfinder.load_file(m)
86 modfinder.run_script(program)
87 module_dict = modfinder.modules
89 # Tell the user about missing modules
91 maymiss = exclude_modules + optional_modules + MAC_MAYMISS_MODULES
92 for m in modfinder.badmodules.keys():
93 if not m in maymiss:
94 if debug > 0:
95 print 'Missing', m
96 missing.append(m)
98 # Warn the user about unused builtins
100 for m in sys.builtin_module_names:
101 if m in ('__main__', '__builtin__'):
102 pass
103 elif not module_dict.has_key(m):
104 if debug > 0:
105 print 'Unused', m
106 elif module_dict[m].gettype() != 'builtin':
107 # XXXX Can this happen?
108 if debug > 0:
109 print 'Conflict', m
110 return module_dict, missing