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: |
| 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 |
| 17 | |
| 18 | # |
| 19 | # Modules that must be included, and modules that need not be included |
| 20 | # (but are if they are found) |
| 21 | # |
| 22 | MAC_INCLUDE_MODULES=['site', 'exceptions'] |
| 23 | MAC_MAYMISS_MODULES=['posix', 'os2', 'nt', 'dos', 'dospath', 'nturl2path', 'pwd', 'sitecustomize'] |
| 24 | |
| 25 | # An exception: |
| 26 | Missing="macmodulefinder.Missing" |
| 27 | |
| 28 | class Module(modulefinder.Module): |
| 29 | |
| 30 | def gettype(self): |
| 31 | """Return type of module""" |
| 32 | if self.__path__: |
| 33 | return 'package' |
| 34 | if self.__code__: |
| 35 | return 'module' |
| 36 | if self.__file__: |
| 37 | return 'dynamic' |
| 38 | return 'builtin' |
| 39 | |
| 40 | class ModuleFinder(modulefinder.ModuleFinder): |
| 41 | |
| 42 | def add_module(self, fqname): |
| 43 | if self.modules.has_key(fqname): |
| 44 | return self.modules[fqname] |
| 45 | self.modules[fqname] = m = Module(fqname) |
| 46 | return m |
| 47 | |
| 48 | def process(program, modules=[], module_files = [], debug=0): |
| 49 | error = [] |
| 50 | # |
| 51 | # Add the standard modules needed for startup |
| 52 | # |
| 53 | modules = modules + MAC_INCLUDE_MODULES |
| 54 | # |
| 55 | # search the main source for directives |
| 56 | # |
| 57 | extra_modules, exclude_modules, extra_path = \ |
| 58 | directives.findfreezedirectives(program) |
| 59 | for m in extra_modules: |
| 60 | if os.sep in m: |
| 61 | # It is a file |
| 62 | module_files.append(m) |
| 63 | else: |
| 64 | modules.append(m) |
| 65 | path = extra_path + sys.path[:] |
| 66 | # |
| 67 | # Create the module finder and let it do its work |
| 68 | # |
| 69 | modfinder = ModuleFinder(path, |
| 70 | excludes=exclude_modules, debug=debug) |
| 71 | for m in modules: |
| 72 | modfinder.import_hook(m) |
| 73 | for m in module_files: |
| 74 | modfinder.load_file(m) |
| 75 | modfinder.run_script(program) |
| 76 | module_dict = modfinder.modules |
| 77 | # |
| 78 | # Tell the user about missing modules |
| 79 | # |
| 80 | maymiss = exclude_modules + MAC_MAYMISS_MODULES |
| 81 | for m in modfinder.badmodules.keys(): |
| 82 | if not m in maymiss: |
| 83 | if debug > 0: |
| 84 | print 'Missing', m |
| 85 | error.append(m) |
| 86 | # |
| 87 | # Warn the user about unused builtins |
| 88 | # |
| 89 | for m in sys.builtin_module_names: |
| 90 | if m in ('__main__', '__builtin__'): |
| 91 | pass |
| 92 | elif not module_dict.has_key(m): |
| 93 | if debug > 0: |
| 94 | print 'Unused', m |
| 95 | elif module_dict[m].gettype() != 'builtin': |
| 96 | # XXXX Can this happen? |
| 97 | if debug > 0: |
| 98 | print 'Conflict', m |
| 99 | if error: |
| 100 | raise Missing, error |
| 101 | return module_dict |