blob: d5328bccd54240ef5d0b9628c13a4d1f71a6a8f8 [file] [log] [blame]
Guido van Rossum75dc4961998-03-05 03:42:00 +00001"""Find modules used by a script, using introspection."""
Just van Rossuma920a882003-07-18 15:31:40 +00002# This module should be kept compatible with Python 2.2, see PEP 291.
Thomas Heller919000e2002-11-25 20:21:59 +00003
Guido van Rossumfc2a0a82006-10-27 23:06:01 +00004from __future__ import generators
Guido van Rossum75dc4961998-03-05 03:42:00 +00005import dis
6import imp
7import marshal
8import os
Guido van Rossum75dc4961998-03-05 03:42:00 +00009import sys
Guido van Rossum6b767ac2001-03-20 20:43:34 +000010import new
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000011import struct
Guido van Rossum75dc4961998-03-05 03:42:00 +000012
Just van Rossum5d0bd1e2002-11-26 09:53:16 +000013if hasattr(sys.__stdout__, "newlines"):
14 READ_MODE = "U" # universal line endings
15else:
16 # remain compatible with Python < 2.3
17 READ_MODE = "r"
18
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000019LOAD_CONST = chr(dis.opname.index('LOAD_CONST'))
20IMPORT_NAME = chr(dis.opname.index('IMPORT_NAME'))
21STORE_NAME = chr(dis.opname.index('STORE_NAME'))
22STORE_GLOBAL = chr(dis.opname.index('STORE_GLOBAL'))
Just van Rossume29310a2002-12-31 16:33:00 +000023STORE_OPS = [STORE_NAME, STORE_GLOBAL]
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000024HAVE_ARGUMENT = chr(dis.HAVE_ARGUMENT)
Guido van Rossum75dc4961998-03-05 03:42:00 +000025
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +000026# Modulefinder does a good job at simulating Python's, but it can not
27# handle __path__ modifications packages make at runtime. Therefore there
28# is a mechanism whereby you can register extra paths in this map for a
Thomas Wouters7e474022000-07-16 12:04:32 +000029# package, and it will be honored.
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +000030
31# Note this is a mapping is lists of paths.
32packagePathMap = {}
33
34# A Public interface
35def AddPackagePath(packagename, path):
Guido van Rossume7e632a1998-09-14 16:02:28 +000036 paths = packagePathMap.get(packagename, [])
37 paths.append(path)
38 packagePathMap[packagename] = paths
Guido van Rossum75dc4961998-03-05 03:42:00 +000039
Thomas Hellerc7aaf952002-11-14 18:45:11 +000040replacePackageMap = {}
41
42# This ReplacePackage mechanism allows modulefinder to work around the
43# way the _xmlplus package injects itself under the name "xml" into
44# sys.modules at runtime by calling ReplacePackage("_xmlplus", "xml")
45# before running ModuleFinder.
46
47def ReplacePackage(oldname, newname):
48 replacePackageMap[oldname] = newname
49
50
Guido van Rossum75dc4961998-03-05 03:42:00 +000051class Module:
52
53 def __init__(self, name, file=None, path=None):
Guido van Rossum912a14c1998-03-05 04:56:37 +000054 self.__name__ = name
55 self.__file__ = file
56 self.__path__ = path
57 self.__code__ = None
Just van Rossume29310a2002-12-31 16:33:00 +000058 # The set of global names that are assigned to in the module.
59 # This includes those names imported through starimports of
60 # Python modules.
61 self.globalnames = {}
62 # The set of starimports this module did that could not be
63 # resolved, ie. a starimport from a non-Python module.
64 self.starimports = {}
Guido van Rossum75dc4961998-03-05 03:42:00 +000065
66 def __repr__(self):
Neil Schemenauer32d23c92004-02-15 16:43:20 +000067 s = "Module(%r" % (self.__name__,)
Guido van Rossum912a14c1998-03-05 04:56:37 +000068 if self.__file__ is not None:
Walter Dörwald70a6b492004-02-12 17:35:32 +000069 s = s + ", %r" % (self.__file__,)
Guido van Rossum912a14c1998-03-05 04:56:37 +000070 if self.__path__ is not None:
Walter Dörwald70a6b492004-02-12 17:35:32 +000071 s = s + ", %r" % (self.__path__,)
Guido van Rossum912a14c1998-03-05 04:56:37 +000072 s = s + ")"
73 return s
Guido van Rossum75dc4961998-03-05 03:42:00 +000074
Guido van Rossum75dc4961998-03-05 03:42:00 +000075class ModuleFinder:
76
Just van Rossume29310a2002-12-31 16:33:00 +000077 def __init__(self, path=None, debug=0, excludes=[], replace_paths=[]):
Guido van Rossum912a14c1998-03-05 04:56:37 +000078 if path is None:
79 path = sys.path
80 self.path = path
81 self.modules = {}
82 self.badmodules = {}
83 self.debug = debug
84 self.indent = 0
Guido van Rossum78fc3631998-03-20 17:37:24 +000085 self.excludes = excludes
Guido van Rossum6b767ac2001-03-20 20:43:34 +000086 self.replace_paths = replace_paths
87 self.processed_paths = [] # Used in debugging only
Guido van Rossum75dc4961998-03-05 03:42:00 +000088
89 def msg(self, level, str, *args):
Guido van Rossum912a14c1998-03-05 04:56:37 +000090 if level <= self.debug:
91 for i in range(self.indent):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000092 print(" ", end=' ')
93 print(str, end=' ')
Guido van Rossum912a14c1998-03-05 04:56:37 +000094 for arg in args:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000095 print(repr(arg), end=' ')
96 print()
Guido van Rossum75dc4961998-03-05 03:42:00 +000097
98 def msgin(self, *args):
Guido van Rossum912a14c1998-03-05 04:56:37 +000099 level = args[0]
100 if level <= self.debug:
101 self.indent = self.indent + 1
Guido van Rossum68468eb2003-02-27 20:14:51 +0000102 self.msg(*args)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000103
104 def msgout(self, *args):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000105 level = args[0]
106 if level <= self.debug:
107 self.indent = self.indent - 1
Guido van Rossum68468eb2003-02-27 20:14:51 +0000108 self.msg(*args)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000109
110 def run_script(self, pathname):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000111 self.msg(2, "run_script", pathname)
Just van Rossum5d0bd1e2002-11-26 09:53:16 +0000112 fp = open(pathname, READ_MODE)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000113 stuff = ("", "r", imp.PY_SOURCE)
114 self.load_module('__main__', fp, pathname, stuff)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000115
116 def load_file(self, pathname):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000117 dir, name = os.path.split(pathname)
118 name, ext = os.path.splitext(name)
Just van Rossum5d0bd1e2002-11-26 09:53:16 +0000119 fp = open(pathname, READ_MODE)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000120 stuff = (ext, "r", imp.PY_SOURCE)
121 self.load_module(name, fp, pathname, stuff)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000122
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000123 def import_hook(self, name, caller=None, fromlist=None, level=-1):
124 self.msg(3, "import_hook", name, caller, fromlist, level)
125 parent = self.determine_parent(caller, level=level)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000126 q, tail = self.find_head_package(parent, name)
127 m = self.load_tail(q, tail)
128 if not fromlist:
129 return q
130 if m.__path__:
131 self.ensure_fromlist(m, fromlist)
Thomas Heller318b7b92002-11-26 08:06:50 +0000132 return None
Guido van Rossum75dc4961998-03-05 03:42:00 +0000133
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000134 def determine_parent(self, caller, level=-1):
135 self.msgin(4, "determine_parent", caller, level)
136 if not caller or level == 0:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000137 self.msgout(4, "determine_parent -> None")
138 return None
139 pname = caller.__name__
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000140 if level >= 1: # relative import
141 if caller.__path__:
142 level -= 1
143 if level == 0:
144 parent = self.modules[pname]
145 assert parent is caller
146 self.msgout(4, "determine_parent ->", parent)
147 return parent
148 if pname.count(".") < level:
149 raise ImportError, "relative importpath too deep"
150 pname = ".".join(pname.split(".")[:-level])
151 parent = self.modules[pname]
152 self.msgout(4, "determine_parent ->", parent)
153 return parent
Guido van Rossum912a14c1998-03-05 04:56:37 +0000154 if caller.__path__:
155 parent = self.modules[pname]
156 assert caller is parent
157 self.msgout(4, "determine_parent ->", parent)
158 return parent
159 if '.' in pname:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000160 i = pname.rfind('.')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000161 pname = pname[:i]
162 parent = self.modules[pname]
163 assert parent.__name__ == pname
164 self.msgout(4, "determine_parent ->", parent)
165 return parent
166 self.msgout(4, "determine_parent -> None")
167 return None
Guido van Rossum75dc4961998-03-05 03:42:00 +0000168
169 def find_head_package(self, parent, name):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000170 self.msgin(4, "find_head_package", parent, name)
171 if '.' in name:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000172 i = name.find('.')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000173 head = name[:i]
174 tail = name[i+1:]
175 else:
176 head = name
177 tail = ""
178 if parent:
179 qname = "%s.%s" % (parent.__name__, head)
180 else:
181 qname = head
182 q = self.import_module(head, qname, parent)
183 if q:
184 self.msgout(4, "find_head_package ->", (q, tail))
185 return q, tail
186 if parent:
187 qname = head
188 parent = None
189 q = self.import_module(head, qname, parent)
190 if q:
191 self.msgout(4, "find_head_package ->", (q, tail))
192 return q, tail
193 self.msgout(4, "raise ImportError: No module named", qname)
194 raise ImportError, "No module named " + qname
Guido van Rossum75dc4961998-03-05 03:42:00 +0000195
196 def load_tail(self, q, tail):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000197 self.msgin(4, "load_tail", q, tail)
198 m = q
199 while tail:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000200 i = tail.find('.')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000201 if i < 0: i = len(tail)
202 head, tail = tail[:i], tail[i+1:]
203 mname = "%s.%s" % (m.__name__, head)
204 m = self.import_module(head, mname, m)
205 if not m:
206 self.msgout(4, "raise ImportError: No module named", mname)
207 raise ImportError, "No module named " + mname
208 self.msgout(4, "load_tail ->", m)
209 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000210
211 def ensure_fromlist(self, m, fromlist, recursive=0):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000212 self.msg(4, "ensure_fromlist", m, fromlist, recursive)
213 for sub in fromlist:
214 if sub == "*":
215 if not recursive:
216 all = self.find_all_submodules(m)
217 if all:
218 self.ensure_fromlist(m, all, 1)
219 elif not hasattr(m, sub):
220 subname = "%s.%s" % (m.__name__, sub)
221 submod = self.import_module(sub, subname, m)
222 if not submod:
223 raise ImportError, "No module named " + subname
Guido van Rossum75dc4961998-03-05 03:42:00 +0000224
225 def find_all_submodules(self, m):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000226 if not m.__path__:
227 return
228 modules = {}
Thomas Helleraaf1c8d2003-11-14 10:28:42 +0000229 # 'suffixes' used to be a list hardcoded to [".py", ".pyc", ".pyo"].
230 # But we must also collect Python extension modules - although
231 # we cannot separate normal dlls from Python extensions.
232 suffixes = []
233 for triple in imp.get_suffixes():
234 suffixes.append(triple[0])
Guido van Rossum912a14c1998-03-05 04:56:37 +0000235 for dir in m.__path__:
236 try:
237 names = os.listdir(dir)
238 except os.error:
239 self.msg(2, "can't list directory", dir)
240 continue
241 for name in names:
242 mod = None
243 for suff in suffixes:
244 n = len(suff)
245 if name[-n:] == suff:
246 mod = name[:-n]
247 break
248 if mod and mod != "__init__":
249 modules[mod] = mod
250 return modules.keys()
Guido van Rossum75dc4961998-03-05 03:42:00 +0000251
252 def import_module(self, partname, fqname, parent):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000253 self.msgin(3, "import_module", partname, fqname, parent)
254 try:
255 m = self.modules[fqname]
256 except KeyError:
257 pass
258 else:
259 self.msgout(3, "import_module ->", m)
260 return m
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000261 if fqname in self.badmodules:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000262 self.msgout(3, "import_module -> None")
263 return None
Thomas Heller2e7c8322004-05-11 15:10:59 +0000264 if parent and parent.__path__ is None:
265 self.msgout(3, "import_module -> None")
266 return None
Guido van Rossum912a14c1998-03-05 04:56:37 +0000267 try:
268 fp, pathname, stuff = self.find_module(partname,
Just van Rossumf0dfbaf2003-03-05 17:23:48 +0000269 parent and parent.__path__, parent)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000270 except ImportError:
271 self.msgout(3, "import_module ->", None)
272 return None
273 try:
274 m = self.load_module(fqname, fp, pathname, stuff)
275 finally:
276 if fp: fp.close()
277 if parent:
278 setattr(parent, partname, m)
279 self.msgout(3, "import_module ->", m)
280 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000281
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000282 def load_module(self, fqname, fp, pathname, file_info):
283 suffix, mode, type = file_info
Guido van Rossum912a14c1998-03-05 04:56:37 +0000284 self.msgin(2, "load_module", fqname, fp and "fp", pathname)
285 if type == imp.PKG_DIRECTORY:
286 m = self.load_package(fqname, pathname)
287 self.msgout(2, "load_module ->", m)
288 return m
289 if type == imp.PY_SOURCE:
Guido van Rossum78fc3631998-03-20 17:37:24 +0000290 co = compile(fp.read()+'\n', pathname, 'exec')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000291 elif type == imp.PY_COMPILED:
292 if fp.read(4) != imp.get_magic():
293 self.msgout(2, "raise ImportError: Bad magic number", pathname)
Guido van Rossumce33eb32000-05-02 13:49:13 +0000294 raise ImportError, "Bad magic number in %s" % pathname
Guido van Rossum912a14c1998-03-05 04:56:37 +0000295 fp.read(4)
296 co = marshal.load(fp)
297 else:
298 co = None
299 m = self.add_module(fqname)
Guido van Rossumab045f91998-03-06 19:55:10 +0000300 m.__file__ = pathname
Guido van Rossum912a14c1998-03-05 04:56:37 +0000301 if co:
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000302 if self.replace_paths:
303 co = self.replace_paths_in_code(co)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000304 m.__code__ = co
Guido van Rossum3c51cf21998-03-05 05:15:07 +0000305 self.scan_code(co, m)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000306 self.msgout(2, "load_module ->", m)
307 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000308
Just van Rossume29310a2002-12-31 16:33:00 +0000309 def _add_badmodule(self, name, caller):
310 if name not in self.badmodules:
311 self.badmodules[name] = {}
312 self.badmodules[name][caller.__name__] = 1
313
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000314 def _safe_import_hook(self, name, caller, fromlist, level=-1):
Just van Rossume29310a2002-12-31 16:33:00 +0000315 # wrapper for self.import_hook() that won't raise ImportError
316 if name in self.badmodules:
317 self._add_badmodule(name, caller)
318 return
319 try:
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000320 self.import_hook(name, caller, level=level)
Guido van Rossumb940e112007-01-10 16:19:56 +0000321 except ImportError as msg:
Just van Rossume29310a2002-12-31 16:33:00 +0000322 self.msg(2, "ImportError:", str(msg))
323 self._add_badmodule(name, caller)
324 else:
325 if fromlist:
326 for sub in fromlist:
327 if sub in self.badmodules:
328 self._add_badmodule(sub, caller)
329 continue
330 try:
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000331 self.import_hook(name, caller, [sub], level=level)
Guido van Rossumb940e112007-01-10 16:19:56 +0000332 except ImportError as msg:
Just van Rossume29310a2002-12-31 16:33:00 +0000333 self.msg(2, "ImportError:", str(msg))
334 fullname = name + "." + sub
335 self._add_badmodule(fullname, caller)
336
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000337 def scan_opcodes(self, co,
338 unpack = struct.unpack):
339 # Scan the code, and yield 'interesting' opcode combinations
340 # Version for Python 2.4 and older
341 code = co.co_code
342 names = co.co_names
343 consts = co.co_consts
344 while code:
345 c = code[0]
346 if c in STORE_OPS:
347 oparg, = unpack('<H', code[1:3])
348 yield "store", (names[oparg],)
349 code = code[3:]
350 continue
351 if c == LOAD_CONST and code[3] == IMPORT_NAME:
352 oparg_1, oparg_2 = unpack('<xHxH', code[:6])
353 yield "import", (consts[oparg_1], names[oparg_2])
354 code = code[6:]
355 continue
356 if c >= HAVE_ARGUMENT:
357 code = code[3:]
358 else:
359 code = code[1:]
360
361 def scan_opcodes_25(self, co,
362 unpack = struct.unpack):
363 # Scan the code, and yield 'interesting' opcode combinations
364 # Python 2.5 version (has absolute and relative imports)
365 code = co.co_code
366 names = co.co_names
367 consts = co.co_consts
368 LOAD_LOAD_AND_IMPORT = LOAD_CONST + LOAD_CONST + IMPORT_NAME
369 while code:
370 c = code[0]
371 if c in STORE_OPS:
372 oparg, = unpack('<H', code[1:3])
373 yield "store", (names[oparg],)
374 code = code[3:]
375 continue
376 if code[:9:3] == LOAD_LOAD_AND_IMPORT:
377 oparg_1, oparg_2, oparg_3 = unpack('<xHxHxH', code[:9])
378 level = consts[oparg_1]
379 if level == 0: # absolute import
380 yield "absolute_import", (consts[oparg_2], names[oparg_3])
381 else: # relative import
382 yield "relative_import", (level, consts[oparg_2], names[oparg_3])
383 code = code[9:]
384 continue
385 if c >= HAVE_ARGUMENT:
386 code = code[3:]
387 else:
388 code = code[1:]
389
Guido van Rossum3c51cf21998-03-05 05:15:07 +0000390 def scan_code(self, co, m):
391 code = co.co_code
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000392 if sys.version_info >= (2, 5):
393 scanner = self.scan_opcodes_25
394 else:
395 scanner = self.scan_opcodes
396 for what, args in scanner(co):
397 if what == "store":
398 name, = args
399 m.globalnames[name] = 1
400 elif what == "absolute_import":
401 fromlist, name = args
Just van Rossume29310a2002-12-31 16:33:00 +0000402 have_star = 0
403 if fromlist is not None:
404 if "*" in fromlist:
405 have_star = 1
406 fromlist = [f for f in fromlist if f != "*"]
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000407 self._safe_import_hook(name, m, fromlist, level=0)
Just van Rossume29310a2002-12-31 16:33:00 +0000408 if have_star:
409 # We've encountered an "import *". If it is a Python module,
410 # the code has already been parsed and we can suck out the
411 # global names.
412 mm = None
413 if m.__path__:
414 # At this point we don't know whether 'name' is a
415 # submodule of 'm' or a global module. Let's just try
416 # the full name first.
417 mm = self.modules.get(m.__name__ + "." + name)
418 if mm is None:
419 mm = self.modules.get(name)
420 if mm is not None:
421 m.globalnames.update(mm.globalnames)
422 m.starimports.update(mm.starimports)
423 if mm.__code__ is None:
424 m.starimports[name] = 1
425 else:
426 m.starimports[name] = 1
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000427 elif what == "relative_import":
428 level, fromlist, name = args
429 if name:
430 self._safe_import_hook(name, m, fromlist, level=level)
431 else:
432 parent = self.determine_parent(m, level=level)
433 self._safe_import_hook(parent.__name__, None, fromlist, level=0)
434 else:
435 # We don't expect anything else from the generator.
436 raise RuntimeError(what)
437
Guido van Rossum3c51cf21998-03-05 05:15:07 +0000438 for c in co.co_consts:
439 if isinstance(c, type(co)):
440 self.scan_code(c, m)
441
Guido van Rossum75dc4961998-03-05 03:42:00 +0000442 def load_package(self, fqname, pathname):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000443 self.msgin(2, "load_package", fqname, pathname)
Thomas Hellerc7aaf952002-11-14 18:45:11 +0000444 newname = replacePackageMap.get(fqname)
445 if newname:
446 fqname = newname
Guido van Rossum912a14c1998-03-05 04:56:37 +0000447 m = self.add_module(fqname)
448 m.__file__ = pathname
449 m.__path__ = [pathname]
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +0000450
Guido van Rossume7e632a1998-09-14 16:02:28 +0000451 # As per comment at top of file, simulate runtime __path__ additions.
452 m.__path__ = m.__path__ + packagePathMap.get(fqname, [])
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +0000453
Guido van Rossum912a14c1998-03-05 04:56:37 +0000454 fp, buf, stuff = self.find_module("__init__", m.__path__)
455 self.load_module(fqname, fp, buf, stuff)
456 self.msgout(2, "load_package ->", m)
457 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000458
459 def add_module(self, fqname):
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000460 if fqname in self.modules:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000461 return self.modules[fqname]
462 self.modules[fqname] = m = Module(fqname)
463 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000464
Just van Rossumf0dfbaf2003-03-05 17:23:48 +0000465 def find_module(self, name, path, parent=None):
466 if parent is not None:
Thomas Heller2e7c8322004-05-11 15:10:59 +0000467 # assert path is not None
Just van Rossumf0dfbaf2003-03-05 17:23:48 +0000468 fullname = parent.__name__+'.'+name
Guido van Rossum03f7f082001-10-18 19:15:32 +0000469 else:
470 fullname = name
471 if fullname in self.excludes:
472 self.msgout(3, "find_module -> Excluded", fullname)
Guido van Rossum78fc3631998-03-20 17:37:24 +0000473 raise ImportError, name
474
Guido van Rossum912a14c1998-03-05 04:56:37 +0000475 if path is None:
476 if name in sys.builtin_module_names:
477 return (None, None, ("", "", imp.C_BUILTIN))
Guido van Rossum78fc3631998-03-20 17:37:24 +0000478
Guido van Rossum912a14c1998-03-05 04:56:37 +0000479 path = self.path
480 return imp.find_module(name, path)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000481
482 def report(self):
Just van Rossume29310a2002-12-31 16:33:00 +0000483 """Print a report to stdout, listing the found modules with their
484 paths, as well as modules that are missing, or seem to be missing.
485 """
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000486 print()
487 print(" %-25s %s" % ("Name", "File"))
488 print(" %-25s %s" % ("----", "----"))
Guido van Rossum912a14c1998-03-05 04:56:37 +0000489 # Print modules found
Guido van Rossumd59cde82007-06-12 00:25:08 +0000490 keys = sorted(self.modules.keys())
Guido van Rossum912a14c1998-03-05 04:56:37 +0000491 for key in keys:
492 m = self.modules[key]
493 if m.__path__:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000494 print("P", end=' ')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000495 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000496 print("m", end=' ')
497 print("%-25s" % key, m.__file__ or "")
Guido van Rossum75dc4961998-03-05 03:42:00 +0000498
Guido van Rossum912a14c1998-03-05 04:56:37 +0000499 # Print missing modules
Just van Rossume29310a2002-12-31 16:33:00 +0000500 missing, maybe = self.any_missing_maybe()
501 if missing:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000502 print()
503 print("Missing modules:")
Just van Rossume29310a2002-12-31 16:33:00 +0000504 for name in missing:
Guido van Rossumd59cde82007-06-12 00:25:08 +0000505 mods = sorted(self.badmodules[name].keys())
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000506 print("?", name, "imported from", ', '.join(mods))
Just van Rossume29310a2002-12-31 16:33:00 +0000507 # Print modules that may be missing, but then again, maybe not...
508 if maybe:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000509 print()
510 print("Submodules thay appear to be missing, but could also be", end=' ')
511 print("global names in the parent package:")
Just van Rossume29310a2002-12-31 16:33:00 +0000512 for name in maybe:
Guido van Rossumd59cde82007-06-12 00:25:08 +0000513 mods = sorted(self.badmodules[name].keys())
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000514 print("?", name, "imported from", ', '.join(mods))
Guido van Rossum75dc4961998-03-05 03:42:00 +0000515
Guido van Rossum03f7f082001-10-18 19:15:32 +0000516 def any_missing(self):
Just van Rossume29310a2002-12-31 16:33:00 +0000517 """Return a list of modules that appear to be missing. Use
518 any_missing_maybe() if you want to know which modules are
519 certain to be missing, and which *may* be missing.
520 """
521 missing, maybe = self.any_missing_maybe()
522 return missing + maybe
523
524 def any_missing_maybe(self):
525 """Return two lists, one with modules that are certainly missing
526 and one with modules that *may* be missing. The latter names could
527 either be submodules *or* just global names in the package.
528
529 The reason it can't always be determined is that it's impossible to
530 tell which names are imported when "from module import *" is done
531 with an extension module, short of actually importing it.
532 """
Guido van Rossum03f7f082001-10-18 19:15:32 +0000533 missing = []
Just van Rossume29310a2002-12-31 16:33:00 +0000534 maybe = []
535 for name in self.badmodules:
536 if name in self.excludes:
537 continue
538 i = name.rfind(".")
539 if i < 0:
540 missing.append(name)
541 continue
542 subname = name[i+1:]
543 pkgname = name[:i]
544 pkg = self.modules.get(pkgname)
545 if pkg is not None:
546 if pkgname in self.badmodules[name]:
547 # The package tried to import this module itself and
548 # failed. It's definitely missing.
549 missing.append(name)
550 elif subname in pkg.globalnames:
551 # It's a global in the package: definitely not missing.
552 pass
553 elif pkg.starimports:
554 # It could be missing, but the package did an "import *"
555 # from a non-Python module, so we simply can't be sure.
556 maybe.append(name)
557 else:
558 # It's not a global in the package, the package didn't
559 # do funny star imports, it's very likely to be missing.
560 # The symbol could be inserted into the package from the
561 # outside, but since that's not good style we simply list
562 # it missing.
563 missing.append(name)
564 else:
565 missing.append(name)
566 missing.sort()
567 maybe.sort()
568 return missing, maybe
Guido van Rossum03f7f082001-10-18 19:15:32 +0000569
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000570 def replace_paths_in_code(self, co):
571 new_filename = original_filename = os.path.normpath(co.co_filename)
Just van Rossume29310a2002-12-31 16:33:00 +0000572 for f, r in self.replace_paths:
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000573 if original_filename.startswith(f):
Just van Rossume29310a2002-12-31 16:33:00 +0000574 new_filename = r + original_filename[len(f):]
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000575 break
576
577 if self.debug and original_filename not in self.processed_paths:
Just van Rossume29310a2002-12-31 16:33:00 +0000578 if new_filename != original_filename:
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000579 self.msgout(2, "co_filename %r changed to %r" \
580 % (original_filename,new_filename,))
581 else:
582 self.msgout(2, "co_filename %r remains unchanged" \
583 % (original_filename,))
584 self.processed_paths.append(original_filename)
585
586 consts = list(co.co_consts)
587 for i in range(len(consts)):
588 if isinstance(consts[i], type(co)):
589 consts[i] = self.replace_paths_in_code(consts[i])
590
Tim Peters2c60f7a2003-01-29 03:49:43 +0000591 return new.code(co.co_argcount, co.co_nlocals, co.co_stacksize,
592 co.co_flags, co.co_code, tuple(consts), co.co_names,
593 co.co_varnames, new_filename, co.co_name,
Neal Norwitz82c72312002-11-12 23:09:12 +0000594 co.co_firstlineno, co.co_lnotab,
595 co.co_freevars, co.co_cellvars)
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000596
Guido van Rossum75dc4961998-03-05 03:42:00 +0000597
598def test():
599 # Parse command line
600 import getopt
601 try:
Guido van Rossumbaf06031998-08-25 14:06:55 +0000602 opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:")
Guido van Rossumb940e112007-01-10 16:19:56 +0000603 except getopt.error as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000604 print(msg)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000605 return
Guido van Rossum75dc4961998-03-05 03:42:00 +0000606
607 # Process options
608 debug = 1
609 domods = 0
610 addpath = []
Guido van Rossumbaf06031998-08-25 14:06:55 +0000611 exclude = []
Guido van Rossum75dc4961998-03-05 03:42:00 +0000612 for o, a in opts:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000613 if o == '-d':
614 debug = debug + 1
615 if o == '-m':
616 domods = 1
617 if o == '-p':
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000618 addpath = addpath + a.split(os.pathsep)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000619 if o == '-q':
620 debug = 0
Guido van Rossumbaf06031998-08-25 14:06:55 +0000621 if o == '-x':
622 exclude.append(a)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000623
624 # Provide default arguments
625 if not args:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000626 script = "hello.py"
Guido van Rossum75dc4961998-03-05 03:42:00 +0000627 else:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000628 script = args[0]
Guido van Rossum75dc4961998-03-05 03:42:00 +0000629
630 # Set the path based on sys.path and the script directory
631 path = sys.path[:]
632 path[0] = os.path.dirname(script)
633 path = addpath + path
634 if debug > 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000635 print("path:")
Guido van Rossum912a14c1998-03-05 04:56:37 +0000636 for item in path:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000637 print(" ", repr(item))
Guido van Rossum75dc4961998-03-05 03:42:00 +0000638
639 # Create the module finder and turn its crank
Guido van Rossumbaf06031998-08-25 14:06:55 +0000640 mf = ModuleFinder(path, debug, exclude)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000641 for arg in args[1:]:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000642 if arg == '-m':
643 domods = 1
644 continue
645 if domods:
646 if arg[-2:] == '.*':
647 mf.import_hook(arg[:-2], None, ["*"])
648 else:
649 mf.import_hook(arg)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000650 else:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000651 mf.load_file(arg)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000652 mf.run_script(script)
653 mf.report()
Just van Rossume29310a2002-12-31 16:33:00 +0000654 return mf # for -i debugging
Guido van Rossum75dc4961998-03-05 03:42:00 +0000655
656
657if __name__ == '__main__':
658 try:
Just van Rossume29310a2002-12-31 16:33:00 +0000659 mf = test()
Guido van Rossum75dc4961998-03-05 03:42:00 +0000660 except KeyboardInterrupt:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000661 print("\n[interrupt]")