blob: 0af04258a1a3a08fe819a761382dc13520fca720 [file] [log] [blame]
Jack Jansen144fa671998-06-26 14:56:00 +00001"""macmodulefinder - Find modules used in a script. Only slightly
2mac-specific, really."""
3
4import sys
5import os
6
7import directives
8
9try:
10 # This will work if we are frozen ourselves
11 import modulefinder
12except 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
17
18#
19# Modules that must be included, and modules that need not be included
20# (but are if they are found)
21#
22MAC_INCLUDE_MODULES=['site', 'exceptions']
Just van Rossumf59a89b1999-01-30 22:33:40 +000023MAC_MAYMISS_MODULES=['posix', 'os2', 'nt', 'ntpath', 'dos', 'dospath',
24 'win32api',
Just van Rossumd9f3c941999-04-24 16:51:22 +000025 'nturl2path', 'pwd', 'sitecustomize',
26 'org.python.core']
Jack Jansen144fa671998-06-26 14:56:00 +000027
28# An exception:
29Missing="macmodulefinder.Missing"
30
31class Module(modulefinder.Module):
32
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'
42
43class ModuleFinder(modulefinder.ModuleFinder):
44
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
50
51def process(program, modules=[], module_files = [], debug=0):
52 error = []
53 #
54 # Add the standard modules needed for startup
55 #
56 modules = modules + MAC_INCLUDE_MODULES
57 #
58 # search the main source for directives
59 #
60 extra_modules, exclude_modules, extra_path = \
61 directives.findfreezedirectives(program)
62 for m in extra_modules:
63 if os.sep in m:
64 # It is a file
65 module_files.append(m)
66 else:
67 modules.append(m)
Jack Jansen201f46d1998-08-18 12:21:48 +000068 # collect all modules of the program
69 path = sys.path[:]
70 dir = os.path.dirname(program)
71 path[0] = dir # "current dir"
72 path = extra_path + path
Jack Jansen144fa671998-06-26 14:56:00 +000073 #
74 # Create the module finder and let it do its work
75 #
76 modfinder = ModuleFinder(path,
77 excludes=exclude_modules, debug=debug)
78 for m in modules:
79 modfinder.import_hook(m)
80 for m in module_files:
81 modfinder.load_file(m)
82 modfinder.run_script(program)
83 module_dict = modfinder.modules
84 #
85 # Tell the user about missing modules
86 #
87 maymiss = exclude_modules + MAC_MAYMISS_MODULES
88 for m in modfinder.badmodules.keys():
89 if not m in maymiss:
90 if debug > 0:
91 print 'Missing', m
92 error.append(m)
93 #
94 # Warn the user about unused builtins
95 #
96 for m in sys.builtin_module_names:
97 if m in ('__main__', '__builtin__'):
98 pass
99 elif not module_dict.has_key(m):
100 if debug > 0:
101 print 'Unused', m
102 elif module_dict[m].gettype() != 'builtin':
103 # XXXX Can this happen?
104 if debug > 0:
105 print 'Conflict', m
106 if error:
107 raise Missing, error
108 return module_dict