Jack Jansen | 144fa67 | 1998-06-26 14:56:00 +0000 | [diff] [blame] | 1 | """macmodulefinder - Find modules used in a script. Only slightly |
| 2 | mac-specific, really.""" |
| 3 | |
| 4 | import sys |
| 5 | import os |
| 6 | |
| 7 | import directives |
| 8 | |
| 9 | try: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 10 | # This will work if we are frozen ourselves |
| 11 | import modulefinder |
Jack Jansen | 144fa67 | 1998-06-26 14:56:00 +0000 | [diff] [blame] | 12 | except ImportError: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 13 | # And this will work otherwise |
| 14 | _FREEZEDIR=os.path.join(sys.prefix, ':Tools:freeze') |
| 15 | sys.path.insert(0, _FREEZEDIR) |
| 16 | import modulefinder |
Jack Jansen | 144fa67 | 1998-06-26 14:56:00 +0000 | [diff] [blame] | 17 | |
| 18 | # |
| 19 | # Modules that must be included, and modules that need not be included |
| 20 | # (but are if they are found) |
| 21 | # |
Jack Jansen | 9431e48 | 2002-12-26 21:17:42 +0000 | [diff] [blame] | 22 | MAC_INCLUDE_MODULES=['site'] |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 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' |
| 28 | ] |
Jack Jansen | 144fa67 | 1998-06-26 14:56:00 +0000 | [diff] [blame] | 29 | |
| 30 | # An exception: |
| 31 | Missing="macmodulefinder.Missing" |
| 32 | |
| 33 | class Module(modulefinder.Module): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 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' |
Jack Jansen | 144fa67 | 1998-06-26 14:56:00 +0000 | [diff] [blame] | 44 | |
| 45 | class 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 |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 52 | |
Just van Rossum | 8ff5276 | 1999-11-04 10:28:00 +0000 | [diff] [blame] | 53 | def process(program, modules=None, module_files=None, debug=0): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 54 | if modules is None: |
| 55 | modules = [] |
| 56 | if module_files is None: |
| 57 | module_files = [] |
| 58 | missing = [] |
| 59 | # |
| 60 | # Add the standard modules needed for startup |
| 61 | # |
| 62 | modules = modules + MAC_INCLUDE_MODULES |
| 63 | # |
| 64 | # search the main source for directives |
| 65 | # |
| 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) |
Jack Jansen | 201f46d | 1998-08-18 12:21:48 +0000 | [diff] [blame] | 74 | # collect all modules of the program |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 75 | path = sys.path[:] |
| 76 | dir = os.path.dirname(program) |
| 77 | path[0] = dir # "current dir" |
| 78 | path = extra_path + path |
| 79 | # |
| 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 | # |
| 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) |
| 99 | # |
| 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 |
| 112 | return module_dict, missing |