blob: ef3f221bca070d4414a3c4474834bb7fb1dc6981 [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#
Jack Jansenb15491e2000-09-14 13:36:06 +000022MAC_INCLUDE_MODULES=['site', 'macfsn']
Just van Rossumf59a89b1999-01-30 22:33:40 +000023MAC_MAYMISS_MODULES=['posix', 'os2', 'nt', 'ntpath', 'dos', 'dospath',
Jack Jansenb15491e2000-09-14 13:36:06 +000024 'win32api', 'ce', '_winreg',
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
Just van Rossum8ff52761999-11-04 10:28:00 +000051def 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 = []
Jack Jansen144fa671998-06-26 14:56:00 +000057 #
58 # Add the standard modules needed for startup
59 #
60 modules = modules + MAC_INCLUDE_MODULES
61 #
62 # search the main source for directives
63 #
Jack Jansenfb278a51999-06-04 15:56:33 +000064 extra_modules, exclude_modules, optional_modules, extra_path = \
Jack Jansen144fa671998-06-26 14:56:00 +000065 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)
Jack Jansen201f46d1998-08-18 12:21:48 +000072 # 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
Jack Jansen144fa671998-06-26 14:56:00 +000077 #
78 # Create the module finder and let it do its work
79 #
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
88 #
89 # Tell the user about missing modules
90 #
Jack Jansenfb278a51999-06-04 15:56:33 +000091 maymiss = exclude_modules + optional_modules + MAC_MAYMISS_MODULES
Jack Jansen144fa671998-06-26 14:56:00 +000092 for m in modfinder.badmodules.keys():
93 if not m in maymiss:
94 if debug > 0:
95 print 'Missing', m
Just van Rossum8ff52761999-11-04 10:28:00 +000096 missing.append(m)
Jack Jansen144fa671998-06-26 14:56:00 +000097 #
98 # Warn the user about unused builtins
99 #
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
Just van Rossum8ff52761999-11-04 10:28:00 +0000110 return module_dict, missing