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