blob: 8cd6a00ddd563f4d383374771e1a0e0621897aab [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',
Jack Jansen26d42df2001-03-20 21:55:07 +000026 'org.python.core',
27 'riscos', 'riscosenviron', 'riscospath'
28 ]
Jack Jansen144fa671998-06-26 14:56:00 +000029
30# An exception:
31Missing="macmodulefinder.Missing"
32
33class Module(modulefinder.Module):
34
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'
44
45class ModuleFinder(modulefinder.ModuleFinder):
46
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
52
Just van Rossum8ff52761999-11-04 10:28:00 +000053def 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 = []
Jack Jansen144fa671998-06-26 14:56:00 +000059 #
60 # Add the standard modules needed for startup
61 #
62 modules = modules + MAC_INCLUDE_MODULES
63 #
64 # search the main source for directives
65 #
Jack Jansenfb278a51999-06-04 15:56:33 +000066 extra_modules, exclude_modules, optional_modules, extra_path = \
Jack Jansen144fa671998-06-26 14:56:00 +000067 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)
Jack Jansen201f46d1998-08-18 12:21:48 +000074 # 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
Jack Jansen144fa671998-06-26 14:56:00 +000079 #
80 # Create the module finder and let it do its work
81 #
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
90 #
91 # Tell the user about missing modules
92 #
Jack Jansenfb278a51999-06-04 15:56:33 +000093 maymiss = exclude_modules + optional_modules + MAC_MAYMISS_MODULES
Jack Jansen144fa671998-06-26 14:56:00 +000094 for m in modfinder.badmodules.keys():
95 if not m in maymiss:
96 if debug > 0:
97 print 'Missing', m
Just van Rossum8ff52761999-11-04 10:28:00 +000098 missing.append(m)
Jack Jansen144fa671998-06-26 14:56:00 +000099 #
100 # Warn the user about unused builtins
101 #
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
Just van Rossum8ff52761999-11-04 10:28:00 +0000112 return module_dict, missing