Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 1 | """Find modules used by a script, using introspection.""" |
Thomas Heller | 919000e | 2002-11-25 20:21:59 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 3 | import dis |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 4 | import importlib._bootstrap_external |
Brett Cannon | 73b969e | 2012-12-22 19:34:21 -0500 | [diff] [blame] | 5 | import importlib.machinery |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 6 | import marshal |
| 7 | import os |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 8 | import sys |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 9 | import types |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 10 | import struct |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 11 | import warnings |
| 12 | with warnings.catch_warnings(): |
| 13 | warnings.simplefilter('ignore', PendingDeprecationWarning) |
| 14 | import imp |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 15 | |
Serhiy Storchaka | 02d9f5e | 2016-05-08 23:43:50 +0300 | [diff] [blame] | 16 | LOAD_CONST = dis.opmap['LOAD_CONST'] |
| 17 | IMPORT_NAME = dis.opmap['IMPORT_NAME'] |
| 18 | STORE_NAME = dis.opmap['STORE_NAME'] |
| 19 | STORE_GLOBAL = dis.opmap['STORE_GLOBAL'] |
| 20 | STORE_OPS = STORE_NAME, STORE_GLOBAL |
| 21 | EXTENDED_ARG = dis.EXTENDED_ARG |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | f1b5a0e | 1998-05-18 20:21:56 +0000 | [diff] [blame] | 23 | # Modulefinder does a good job at simulating Python's, but it can not |
| 24 | # handle __path__ modifications packages make at runtime. Therefore there |
| 25 | # is a mechanism whereby you can register extra paths in this map for a |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 26 | # package, and it will be honored. |
Guido van Rossum | f1b5a0e | 1998-05-18 20:21:56 +0000 | [diff] [blame] | 27 | |
| 28 | # Note this is a mapping is lists of paths. |
| 29 | packagePathMap = {} |
| 30 | |
| 31 | # A Public interface |
| 32 | def AddPackagePath(packagename, path): |
Éric Araujo | cee6bb5 | 2011-08-01 15:29:07 +0200 | [diff] [blame] | 33 | packagePathMap.setdefault(packagename, []).append(path) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 34 | |
Thomas Heller | c7aaf95 | 2002-11-14 18:45:11 +0000 | [diff] [blame] | 35 | replacePackageMap = {} |
| 36 | |
Martin v. Löwis | 2f48d89 | 2011-05-09 08:05:43 +0200 | [diff] [blame] | 37 | # This ReplacePackage mechanism allows modulefinder to work around |
| 38 | # situations in which a package injects itself under the name |
| 39 | # of another package into sys.modules at runtime by calling |
| 40 | # ReplacePackage("real_package_name", "faked_package_name") |
Thomas Heller | c7aaf95 | 2002-11-14 18:45:11 +0000 | [diff] [blame] | 41 | # before running ModuleFinder. |
| 42 | |
| 43 | def ReplacePackage(oldname, newname): |
| 44 | replacePackageMap[oldname] = newname |
| 45 | |
| 46 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 47 | class Module: |
| 48 | |
| 49 | def __init__(self, name, file=None, path=None): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 50 | self.__name__ = name |
| 51 | self.__file__ = file |
| 52 | self.__path__ = path |
| 53 | self.__code__ = None |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 54 | # The set of global names that are assigned to in the module. |
| 55 | # This includes those names imported through starimports of |
| 56 | # Python modules. |
| 57 | self.globalnames = {} |
| 58 | # The set of starimports this module did that could not be |
| 59 | # resolved, ie. a starimport from a non-Python module. |
| 60 | self.starimports = {} |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 61 | |
| 62 | def __repr__(self): |
Neil Schemenauer | 32d23c9 | 2004-02-15 16:43:20 +0000 | [diff] [blame] | 63 | s = "Module(%r" % (self.__name__,) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 64 | if self.__file__ is not None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 65 | s = s + ", %r" % (self.__file__,) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 66 | if self.__path__ is not None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 67 | s = s + ", %r" % (self.__path__,) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 68 | s = s + ")" |
| 69 | return s |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 71 | class ModuleFinder: |
| 72 | |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 73 | def __init__(self, path=None, debug=0, excludes=[], replace_paths=[]): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 74 | if path is None: |
| 75 | path = sys.path |
| 76 | self.path = path |
| 77 | self.modules = {} |
| 78 | self.badmodules = {} |
| 79 | self.debug = debug |
| 80 | self.indent = 0 |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 81 | self.excludes = excludes |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 82 | self.replace_paths = replace_paths |
| 83 | self.processed_paths = [] # Used in debugging only |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 84 | |
| 85 | def msg(self, level, str, *args): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 86 | if level <= self.debug: |
| 87 | for i in range(self.indent): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 88 | print(" ", end=' ') |
| 89 | print(str, end=' ') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 90 | for arg in args: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 91 | print(repr(arg), end=' ') |
| 92 | print() |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 93 | |
| 94 | def msgin(self, *args): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 95 | level = args[0] |
| 96 | if level <= self.debug: |
| 97 | self.indent = self.indent + 1 |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 98 | self.msg(*args) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 99 | |
| 100 | def msgout(self, *args): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 101 | level = args[0] |
| 102 | if level <= self.debug: |
| 103 | self.indent = self.indent - 1 |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 104 | self.msg(*args) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 105 | |
| 106 | def run_script(self, pathname): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 107 | self.msg(2, "run_script", pathname) |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 108 | with open(pathname) as fp: |
Brett Cannon | 028011f | 2010-10-30 00:26:48 +0000 | [diff] [blame] | 109 | stuff = ("", "r", imp.PY_SOURCE) |
| 110 | self.load_module('__main__', fp, pathname, stuff) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 111 | |
| 112 | def load_file(self, pathname): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 113 | dir, name = os.path.split(pathname) |
| 114 | name, ext = os.path.splitext(name) |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 115 | with open(pathname) as fp: |
Brett Cannon | 028011f | 2010-10-30 00:26:48 +0000 | [diff] [blame] | 116 | stuff = (ext, "r", imp.PY_SOURCE) |
| 117 | self.load_module(name, fp, pathname, stuff) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 119 | def import_hook(self, name, caller=None, fromlist=None, level=-1): |
| 120 | self.msg(3, "import_hook", name, caller, fromlist, level) |
| 121 | parent = self.determine_parent(caller, level=level) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 122 | q, tail = self.find_head_package(parent, name) |
| 123 | m = self.load_tail(q, tail) |
| 124 | if not fromlist: |
| 125 | return q |
| 126 | if m.__path__: |
| 127 | self.ensure_fromlist(m, fromlist) |
Thomas Heller | 318b7b9 | 2002-11-26 08:06:50 +0000 | [diff] [blame] | 128 | return None |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 130 | def determine_parent(self, caller, level=-1): |
| 131 | self.msgin(4, "determine_parent", caller, level) |
| 132 | if not caller or level == 0: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 133 | self.msgout(4, "determine_parent -> None") |
| 134 | return None |
| 135 | pname = caller.__name__ |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 136 | if level >= 1: # relative import |
| 137 | if caller.__path__: |
| 138 | level -= 1 |
| 139 | if level == 0: |
| 140 | parent = self.modules[pname] |
| 141 | assert parent is caller |
| 142 | self.msgout(4, "determine_parent ->", parent) |
| 143 | return parent |
| 144 | if pname.count(".") < level: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 145 | raise ImportError("relative importpath too deep") |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 146 | pname = ".".join(pname.split(".")[:-level]) |
| 147 | parent = self.modules[pname] |
| 148 | self.msgout(4, "determine_parent ->", parent) |
| 149 | return parent |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 150 | if caller.__path__: |
| 151 | parent = self.modules[pname] |
| 152 | assert caller is parent |
| 153 | self.msgout(4, "determine_parent ->", parent) |
| 154 | return parent |
| 155 | if '.' in pname: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 156 | i = pname.rfind('.') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 157 | pname = pname[:i] |
| 158 | parent = self.modules[pname] |
| 159 | assert parent.__name__ == pname |
| 160 | self.msgout(4, "determine_parent ->", parent) |
| 161 | return parent |
| 162 | self.msgout(4, "determine_parent -> None") |
| 163 | return None |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 164 | |
| 165 | def find_head_package(self, parent, name): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 166 | self.msgin(4, "find_head_package", parent, name) |
| 167 | if '.' in name: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 168 | i = name.find('.') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 169 | head = name[:i] |
| 170 | tail = name[i+1:] |
| 171 | else: |
| 172 | head = name |
| 173 | tail = "" |
| 174 | if parent: |
| 175 | qname = "%s.%s" % (parent.__name__, head) |
| 176 | else: |
| 177 | qname = head |
| 178 | q = self.import_module(head, qname, parent) |
| 179 | if q: |
| 180 | self.msgout(4, "find_head_package ->", (q, tail)) |
| 181 | return q, tail |
| 182 | if parent: |
| 183 | qname = head |
| 184 | parent = None |
| 185 | q = self.import_module(head, qname, parent) |
| 186 | if q: |
| 187 | self.msgout(4, "find_head_package ->", (q, tail)) |
| 188 | return q, tail |
| 189 | self.msgout(4, "raise ImportError: No module named", qname) |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 190 | raise ImportError("No module named " + qname) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 191 | |
| 192 | def load_tail(self, q, tail): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 193 | self.msgin(4, "load_tail", q, tail) |
| 194 | m = q |
| 195 | while tail: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 196 | i = tail.find('.') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 197 | if i < 0: i = len(tail) |
| 198 | head, tail = tail[:i], tail[i+1:] |
| 199 | mname = "%s.%s" % (m.__name__, head) |
| 200 | m = self.import_module(head, mname, m) |
| 201 | if not m: |
| 202 | self.msgout(4, "raise ImportError: No module named", mname) |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 203 | raise ImportError("No module named " + mname) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 204 | self.msgout(4, "load_tail ->", m) |
| 205 | return m |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 206 | |
| 207 | def ensure_fromlist(self, m, fromlist, recursive=0): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 208 | self.msg(4, "ensure_fromlist", m, fromlist, recursive) |
| 209 | for sub in fromlist: |
| 210 | if sub == "*": |
| 211 | if not recursive: |
| 212 | all = self.find_all_submodules(m) |
| 213 | if all: |
| 214 | self.ensure_fromlist(m, all, 1) |
| 215 | elif not hasattr(m, sub): |
| 216 | subname = "%s.%s" % (m.__name__, sub) |
| 217 | submod = self.import_module(sub, subname, m) |
| 218 | if not submod: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 219 | raise ImportError("No module named " + subname) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 220 | |
| 221 | def find_all_submodules(self, m): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 222 | if not m.__path__: |
| 223 | return |
| 224 | modules = {} |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 225 | # 'suffixes' used to be a list hardcoded to [".py", ".pyc"]. |
Thomas Heller | aaf1c8d | 2003-11-14 10:28:42 +0000 | [diff] [blame] | 226 | # But we must also collect Python extension modules - although |
| 227 | # we cannot separate normal dlls from Python extensions. |
| 228 | suffixes = [] |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 229 | suffixes += importlib.machinery.EXTENSION_SUFFIXES[:] |
| 230 | suffixes += importlib.machinery.SOURCE_SUFFIXES[:] |
| 231 | suffixes += importlib.machinery.BYTECODE_SUFFIXES[:] |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 232 | for dir in m.__path__: |
| 233 | try: |
| 234 | names = os.listdir(dir) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 235 | except OSError: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 236 | self.msg(2, "can't list directory", dir) |
| 237 | continue |
| 238 | for name in names: |
| 239 | mod = None |
| 240 | for suff in suffixes: |
| 241 | n = len(suff) |
| 242 | if name[-n:] == suff: |
| 243 | mod = name[:-n] |
| 244 | break |
| 245 | if mod and mod != "__init__": |
| 246 | modules[mod] = mod |
| 247 | return modules.keys() |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 248 | |
| 249 | def import_module(self, partname, fqname, parent): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 250 | self.msgin(3, "import_module", partname, fqname, parent) |
| 251 | try: |
| 252 | m = self.modules[fqname] |
| 253 | except KeyError: |
| 254 | pass |
| 255 | else: |
| 256 | self.msgout(3, "import_module ->", m) |
| 257 | return m |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 258 | if fqname in self.badmodules: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 259 | self.msgout(3, "import_module -> None") |
| 260 | return None |
Thomas Heller | 2e7c832 | 2004-05-11 15:10:59 +0000 | [diff] [blame] | 261 | if parent and parent.__path__ is None: |
| 262 | self.msgout(3, "import_module -> None") |
| 263 | return None |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 264 | try: |
| 265 | fp, pathname, stuff = self.find_module(partname, |
Just van Rossum | f0dfbaf | 2003-03-05 17:23:48 +0000 | [diff] [blame] | 266 | parent and parent.__path__, parent) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 267 | except ImportError: |
| 268 | self.msgout(3, "import_module ->", None) |
| 269 | return None |
| 270 | try: |
| 271 | m = self.load_module(fqname, fp, pathname, stuff) |
| 272 | finally: |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 273 | if fp: |
| 274 | fp.close() |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 275 | if parent: |
| 276 | setattr(parent, partname, m) |
| 277 | self.msgout(3, "import_module ->", m) |
| 278 | return m |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 280 | def load_module(self, fqname, fp, pathname, file_info): |
| 281 | suffix, mode, type = file_info |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 282 | self.msgin(2, "load_module", fqname, fp and "fp", pathname) |
| 283 | if type == imp.PKG_DIRECTORY: |
| 284 | m = self.load_package(fqname, pathname) |
| 285 | self.msgout(2, "load_module ->", m) |
| 286 | return m |
| 287 | if type == imp.PY_SOURCE: |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 288 | co = compile(fp.read()+'\n', pathname, 'exec') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 289 | elif type == imp.PY_COMPILED: |
Brett Cannon | 0f38478 | 2014-02-28 10:50:34 -0500 | [diff] [blame] | 290 | try: |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 291 | marshal_data = importlib._bootstrap_external._validate_bytecode_header(fp.read()) |
Brett Cannon | 0f38478 | 2014-02-28 10:50:34 -0500 | [diff] [blame] | 292 | except ImportError as exc: |
| 293 | self.msgout(2, "raise ImportError: " + str(exc), pathname) |
| 294 | raise |
| 295 | co = marshal.loads(marshal_data) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 296 | else: |
| 297 | co = None |
| 298 | m = self.add_module(fqname) |
Guido van Rossum | ab045f9 | 1998-03-06 19:55:10 +0000 | [diff] [blame] | 299 | m.__file__ = pathname |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 300 | if co: |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 301 | if self.replace_paths: |
| 302 | co = self.replace_paths_in_code(co) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 303 | m.__code__ = co |
Guido van Rossum | 3c51cf2 | 1998-03-05 05:15:07 +0000 | [diff] [blame] | 304 | self.scan_code(co, m) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 305 | self.msgout(2, "load_module ->", m) |
| 306 | return m |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 307 | |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 308 | def _add_badmodule(self, name, caller): |
| 309 | if name not in self.badmodules: |
| 310 | self.badmodules[name] = {} |
Benjamin Peterson | c0747cf | 2008-11-03 20:31:38 +0000 | [diff] [blame] | 311 | if caller: |
| 312 | self.badmodules[name][caller.__name__] = 1 |
| 313 | else: |
| 314 | self.badmodules[name]["-"] = 1 |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 316 | def _safe_import_hook(self, name, caller, fromlist, level=-1): |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 317 | # wrapper for self.import_hook() that won't raise ImportError |
| 318 | if name in self.badmodules: |
| 319 | self._add_badmodule(name, caller) |
| 320 | return |
| 321 | try: |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 322 | self.import_hook(name, caller, level=level) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 323 | except ImportError as msg: |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 324 | self.msg(2, "ImportError:", str(msg)) |
| 325 | self._add_badmodule(name, caller) |
| 326 | else: |
| 327 | if fromlist: |
| 328 | for sub in fromlist: |
| 329 | if sub in self.badmodules: |
| 330 | self._add_badmodule(sub, caller) |
| 331 | continue |
| 332 | try: |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 333 | self.import_hook(name, caller, [sub], level=level) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 334 | except ImportError as msg: |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 335 | self.msg(2, "ImportError:", str(msg)) |
| 336 | fullname = name + "." + sub |
| 337 | self._add_badmodule(fullname, caller) |
| 338 | |
Serhiy Storchaka | ec5d545 | 2016-05-11 22:19:49 +0300 | [diff] [blame] | 339 | def scan_opcodes_25(self, co, |
| 340 | unpack = struct.unpack): |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 341 | # Scan the code, and yield 'interesting' opcode combinations |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 342 | code = co.co_code |
| 343 | names = co.co_names |
| 344 | consts = co.co_consts |
Serhiy Storchaka | 02d9f5e | 2016-05-08 23:43:50 +0300 | [diff] [blame] | 345 | opargs = [(op, arg) for _, op, arg in dis._unpack_opargs(code) |
| 346 | if op != EXTENDED_ARG] |
| 347 | for i, (op, oparg) in enumerate(opargs): |
| 348 | if op in STORE_OPS: |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 349 | yield "store", (names[oparg],) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 350 | continue |
Serhiy Storchaka | 02d9f5e | 2016-05-08 23:43:50 +0300 | [diff] [blame] | 351 | if (op == IMPORT_NAME and i >= 2 |
| 352 | and opargs[i-1][0] == opargs[i-2][0] == LOAD_CONST): |
| 353 | level = consts[opargs[i-2][1]] |
| 354 | fromlist = consts[opargs[i-1][1]] |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 355 | if level == 0: # absolute import |
Serhiy Storchaka | 02d9f5e | 2016-05-08 23:43:50 +0300 | [diff] [blame] | 356 | yield "absolute_import", (fromlist, names[oparg]) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 357 | else: # relative import |
Serhiy Storchaka | 02d9f5e | 2016-05-08 23:43:50 +0300 | [diff] [blame] | 358 | yield "relative_import", (level, fromlist, names[oparg]) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 359 | continue |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | 3c51cf2 | 1998-03-05 05:15:07 +0000 | [diff] [blame] | 361 | def scan_code(self, co, m): |
| 362 | code = co.co_code |
Serhiy Storchaka | ec5d545 | 2016-05-11 22:19:49 +0300 | [diff] [blame] | 363 | scanner = self.scan_opcodes_25 |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 364 | for what, args in scanner(co): |
| 365 | if what == "store": |
| 366 | name, = args |
| 367 | m.globalnames[name] = 1 |
| 368 | elif what == "absolute_import": |
| 369 | fromlist, name = args |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 370 | have_star = 0 |
| 371 | if fromlist is not None: |
| 372 | if "*" in fromlist: |
| 373 | have_star = 1 |
| 374 | fromlist = [f for f in fromlist if f != "*"] |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 375 | self._safe_import_hook(name, m, fromlist, level=0) |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 376 | if have_star: |
| 377 | # We've encountered an "import *". If it is a Python module, |
| 378 | # the code has already been parsed and we can suck out the |
| 379 | # global names. |
| 380 | mm = None |
| 381 | if m.__path__: |
| 382 | # At this point we don't know whether 'name' is a |
| 383 | # submodule of 'm' or a global module. Let's just try |
| 384 | # the full name first. |
| 385 | mm = self.modules.get(m.__name__ + "." + name) |
| 386 | if mm is None: |
| 387 | mm = self.modules.get(name) |
| 388 | if mm is not None: |
| 389 | m.globalnames.update(mm.globalnames) |
| 390 | m.starimports.update(mm.starimports) |
| 391 | if mm.__code__ is None: |
| 392 | m.starimports[name] = 1 |
| 393 | else: |
| 394 | m.starimports[name] = 1 |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 395 | elif what == "relative_import": |
| 396 | level, fromlist, name = args |
| 397 | if name: |
| 398 | self._safe_import_hook(name, m, fromlist, level=level) |
| 399 | else: |
| 400 | parent = self.determine_parent(m, level=level) |
| 401 | self._safe_import_hook(parent.__name__, None, fromlist, level=0) |
| 402 | else: |
| 403 | # We don't expect anything else from the generator. |
| 404 | raise RuntimeError(what) |
| 405 | |
Guido van Rossum | 3c51cf2 | 1998-03-05 05:15:07 +0000 | [diff] [blame] | 406 | for c in co.co_consts: |
| 407 | if isinstance(c, type(co)): |
| 408 | self.scan_code(c, m) |
| 409 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 410 | def load_package(self, fqname, pathname): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 411 | self.msgin(2, "load_package", fqname, pathname) |
Thomas Heller | c7aaf95 | 2002-11-14 18:45:11 +0000 | [diff] [blame] | 412 | newname = replacePackageMap.get(fqname) |
| 413 | if newname: |
| 414 | fqname = newname |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 415 | m = self.add_module(fqname) |
| 416 | m.__file__ = pathname |
| 417 | m.__path__ = [pathname] |
Guido van Rossum | f1b5a0e | 1998-05-18 20:21:56 +0000 | [diff] [blame] | 418 | |
Guido van Rossum | e7e632a | 1998-09-14 16:02:28 +0000 | [diff] [blame] | 419 | # As per comment at top of file, simulate runtime __path__ additions. |
| 420 | m.__path__ = m.__path__ + packagePathMap.get(fqname, []) |
Guido van Rossum | f1b5a0e | 1998-05-18 20:21:56 +0000 | [diff] [blame] | 421 | |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 422 | fp, buf, stuff = self.find_module("__init__", m.__path__) |
Brett Cannon | 028011f | 2010-10-30 00:26:48 +0000 | [diff] [blame] | 423 | try: |
| 424 | self.load_module(fqname, fp, buf, stuff) |
| 425 | self.msgout(2, "load_package ->", m) |
| 426 | return m |
| 427 | finally: |
| 428 | if fp: |
| 429 | fp.close() |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 430 | |
| 431 | def add_module(self, fqname): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 432 | if fqname in self.modules: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 433 | return self.modules[fqname] |
| 434 | self.modules[fqname] = m = Module(fqname) |
| 435 | return m |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 436 | |
Just van Rossum | f0dfbaf | 2003-03-05 17:23:48 +0000 | [diff] [blame] | 437 | def find_module(self, name, path, parent=None): |
| 438 | if parent is not None: |
Thomas Heller | 2e7c832 | 2004-05-11 15:10:59 +0000 | [diff] [blame] | 439 | # assert path is not None |
Just van Rossum | f0dfbaf | 2003-03-05 17:23:48 +0000 | [diff] [blame] | 440 | fullname = parent.__name__+'.'+name |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 441 | else: |
| 442 | fullname = name |
| 443 | if fullname in self.excludes: |
| 444 | self.msgout(3, "find_module -> Excluded", fullname) |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 445 | raise ImportError(name) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 446 | |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 447 | if path is None: |
| 448 | if name in sys.builtin_module_names: |
| 449 | return (None, None, ("", "", imp.C_BUILTIN)) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 450 | |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 451 | path = self.path |
| 452 | return imp.find_module(name, path) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 453 | |
| 454 | def report(self): |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 455 | """Print a report to stdout, listing the found modules with their |
| 456 | paths, as well as modules that are missing, or seem to be missing. |
| 457 | """ |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 458 | print() |
| 459 | print(" %-25s %s" % ("Name", "File")) |
| 460 | print(" %-25s %s" % ("----", "----")) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 461 | # Print modules found |
Guido van Rossum | d59cde8 | 2007-06-12 00:25:08 +0000 | [diff] [blame] | 462 | keys = sorted(self.modules.keys()) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 463 | for key in keys: |
| 464 | m = self.modules[key] |
| 465 | if m.__path__: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 466 | print("P", end=' ') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 467 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 468 | print("m", end=' ') |
| 469 | print("%-25s" % key, m.__file__ or "") |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 470 | |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 471 | # Print missing modules |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 472 | missing, maybe = self.any_missing_maybe() |
| 473 | if missing: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 474 | print() |
| 475 | print("Missing modules:") |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 476 | for name in missing: |
Guido van Rossum | d59cde8 | 2007-06-12 00:25:08 +0000 | [diff] [blame] | 477 | mods = sorted(self.badmodules[name].keys()) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 478 | print("?", name, "imported from", ', '.join(mods)) |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 479 | # Print modules that may be missing, but then again, maybe not... |
| 480 | if maybe: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 481 | print() |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 482 | print("Submodules that appear to be missing, but could also be", end=' ') |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 483 | print("global names in the parent package:") |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 484 | for name in maybe: |
Guido van Rossum | d59cde8 | 2007-06-12 00:25:08 +0000 | [diff] [blame] | 485 | mods = sorted(self.badmodules[name].keys()) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 486 | print("?", name, "imported from", ', '.join(mods)) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 487 | |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 488 | def any_missing(self): |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 489 | """Return a list of modules that appear to be missing. Use |
| 490 | any_missing_maybe() if you want to know which modules are |
| 491 | certain to be missing, and which *may* be missing. |
| 492 | """ |
| 493 | missing, maybe = self.any_missing_maybe() |
| 494 | return missing + maybe |
| 495 | |
| 496 | def any_missing_maybe(self): |
| 497 | """Return two lists, one with modules that are certainly missing |
| 498 | and one with modules that *may* be missing. The latter names could |
| 499 | either be submodules *or* just global names in the package. |
| 500 | |
| 501 | The reason it can't always be determined is that it's impossible to |
| 502 | tell which names are imported when "from module import *" is done |
| 503 | with an extension module, short of actually importing it. |
| 504 | """ |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 505 | missing = [] |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 506 | maybe = [] |
| 507 | for name in self.badmodules: |
| 508 | if name in self.excludes: |
| 509 | continue |
| 510 | i = name.rfind(".") |
| 511 | if i < 0: |
| 512 | missing.append(name) |
| 513 | continue |
| 514 | subname = name[i+1:] |
| 515 | pkgname = name[:i] |
| 516 | pkg = self.modules.get(pkgname) |
| 517 | if pkg is not None: |
| 518 | if pkgname in self.badmodules[name]: |
| 519 | # The package tried to import this module itself and |
| 520 | # failed. It's definitely missing. |
| 521 | missing.append(name) |
| 522 | elif subname in pkg.globalnames: |
| 523 | # It's a global in the package: definitely not missing. |
| 524 | pass |
| 525 | elif pkg.starimports: |
| 526 | # It could be missing, but the package did an "import *" |
| 527 | # from a non-Python module, so we simply can't be sure. |
| 528 | maybe.append(name) |
| 529 | else: |
| 530 | # It's not a global in the package, the package didn't |
| 531 | # do funny star imports, it's very likely to be missing. |
| 532 | # The symbol could be inserted into the package from the |
| 533 | # outside, but since that's not good style we simply list |
| 534 | # it missing. |
| 535 | missing.append(name) |
| 536 | else: |
| 537 | missing.append(name) |
| 538 | missing.sort() |
| 539 | maybe.sort() |
| 540 | return missing, maybe |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 541 | |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 542 | def replace_paths_in_code(self, co): |
| 543 | new_filename = original_filename = os.path.normpath(co.co_filename) |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 544 | for f, r in self.replace_paths: |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 545 | if original_filename.startswith(f): |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 546 | new_filename = r + original_filename[len(f):] |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 547 | break |
| 548 | |
| 549 | if self.debug and original_filename not in self.processed_paths: |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 550 | if new_filename != original_filename: |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 551 | self.msgout(2, "co_filename %r changed to %r" \ |
| 552 | % (original_filename,new_filename,)) |
| 553 | else: |
| 554 | self.msgout(2, "co_filename %r remains unchanged" \ |
| 555 | % (original_filename,)) |
| 556 | self.processed_paths.append(original_filename) |
| 557 | |
| 558 | consts = list(co.co_consts) |
| 559 | for i in range(len(consts)): |
| 560 | if isinstance(consts[i], type(co)): |
| 561 | consts[i] = self.replace_paths_in_code(consts[i]) |
| 562 | |
Berker Peksag | 0a0d1da | 2014-07-07 14:58:12 +0300 | [diff] [blame] | 563 | return types.CodeType(co.co_argcount, co.co_kwonlyargcount, |
| 564 | co.co_nlocals, co.co_stacksize, co.co_flags, |
| 565 | co.co_code, tuple(consts), co.co_names, |
| 566 | co.co_varnames, new_filename, co.co_name, |
| 567 | co.co_firstlineno, co.co_lnotab, co.co_freevars, |
| 568 | co.co_cellvars) |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 569 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 570 | |
| 571 | def test(): |
| 572 | # Parse command line |
| 573 | import getopt |
| 574 | try: |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 575 | opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 576 | except getopt.error as msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 577 | print(msg) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 578 | return |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 579 | |
| 580 | # Process options |
| 581 | debug = 1 |
| 582 | domods = 0 |
| 583 | addpath = [] |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 584 | exclude = [] |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 585 | for o, a in opts: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 586 | if o == '-d': |
| 587 | debug = debug + 1 |
| 588 | if o == '-m': |
| 589 | domods = 1 |
| 590 | if o == '-p': |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 591 | addpath = addpath + a.split(os.pathsep) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 592 | if o == '-q': |
| 593 | debug = 0 |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 594 | if o == '-x': |
| 595 | exclude.append(a) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 596 | |
| 597 | # Provide default arguments |
| 598 | if not args: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 599 | script = "hello.py" |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 600 | else: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 601 | script = args[0] |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 602 | |
| 603 | # Set the path based on sys.path and the script directory |
| 604 | path = sys.path[:] |
| 605 | path[0] = os.path.dirname(script) |
| 606 | path = addpath + path |
| 607 | if debug > 1: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 608 | print("path:") |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 609 | for item in path: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 610 | print(" ", repr(item)) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 611 | |
| 612 | # Create the module finder and turn its crank |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 613 | mf = ModuleFinder(path, debug, exclude) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 614 | for arg in args[1:]: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 615 | if arg == '-m': |
| 616 | domods = 1 |
| 617 | continue |
| 618 | if domods: |
| 619 | if arg[-2:] == '.*': |
| 620 | mf.import_hook(arg[:-2], None, ["*"]) |
| 621 | else: |
| 622 | mf.import_hook(arg) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 623 | else: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 624 | mf.load_file(arg) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 625 | mf.run_script(script) |
| 626 | mf.report() |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 627 | return mf # for -i debugging |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 628 | |
| 629 | |
| 630 | if __name__ == '__main__': |
| 631 | try: |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 632 | mf = test() |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 633 | except KeyboardInterrupt: |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 634 | print("\n[interrupted]") |