blob: 5d2c06d319db306f9fdaaca7b1a103d04644f10d [file] [log] [blame]
Guido van Rossum75dc4961998-03-05 03:42:00 +00001"""Find modules used by a script, using introspection."""
Thomas Heller919000e2002-11-25 20:21:59 +00002
Guido van Rossum75dc4961998-03-05 03:42:00 +00003import dis
Brett Cannon0f384782014-02-28 10:50:34 -05004import importlib._bootstrap
Brett Cannon73b969e2012-12-22 19:34:21 -05005import importlib.machinery
Guido van Rossum75dc4961998-03-05 03:42:00 +00006import marshal
7import os
Guido van Rossum75dc4961998-03-05 03:42:00 +00008import sys
Christian Heimes45f9af32007-11-27 21:50:00 +00009import types
Guido van Rossumfc2a0a82006-10-27 23:06:01 +000010import struct
Brett Cannone4f41de2013-06-16 13:13:40 -040011import warnings
12with warnings.catch_warnings():
13 warnings.simplefilter('ignore', PendingDeprecationWarning)
14 import imp
Guido van Rossum75dc4961998-03-05 03:42:00 +000015
Brett Cannon40430012007-10-22 20:24:51 +000016# XXX Clean up once str8's cstor matches bytes.
Guido van Rossum98297ee2007-11-06 21:34:58 +000017LOAD_CONST = bytes([dis.opname.index('LOAD_CONST')])
18IMPORT_NAME = bytes([dis.opname.index('IMPORT_NAME')])
19STORE_NAME = bytes([dis.opname.index('STORE_NAME')])
20STORE_GLOBAL = bytes([dis.opname.index('STORE_GLOBAL')])
Just van Rossume29310a2002-12-31 16:33:00 +000021STORE_OPS = [STORE_NAME, STORE_GLOBAL]
Guido van Rossum98297ee2007-11-06 21:34:58 +000022HAVE_ARGUMENT = bytes([dis.HAVE_ARGUMENT])
Guido van Rossum75dc4961998-03-05 03:42:00 +000023
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +000024# 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 Wouters7e474022000-07-16 12:04:32 +000027# package, and it will be honored.
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +000028
29# Note this is a mapping is lists of paths.
30packagePathMap = {}
31
32# A Public interface
33def AddPackagePath(packagename, path):
Éric Araujocee6bb52011-08-01 15:29:07 +020034 packagePathMap.setdefault(packagename, []).append(path)
Guido van Rossum75dc4961998-03-05 03:42:00 +000035
Thomas Hellerc7aaf952002-11-14 18:45:11 +000036replacePackageMap = {}
37
Martin v. Löwis2f48d892011-05-09 08:05:43 +020038# This ReplacePackage mechanism allows modulefinder to work around
39# situations in which a package injects itself under the name
40# of another package into sys.modules at runtime by calling
41# ReplacePackage("real_package_name", "faked_package_name")
Thomas Hellerc7aaf952002-11-14 18:45:11 +000042# before running ModuleFinder.
43
44def ReplacePackage(oldname, newname):
45 replacePackageMap[oldname] = newname
46
47
Guido van Rossum75dc4961998-03-05 03:42:00 +000048class Module:
49
50 def __init__(self, name, file=None, path=None):
Guido van Rossum912a14c1998-03-05 04:56:37 +000051 self.__name__ = name
52 self.__file__ = file
53 self.__path__ = path
54 self.__code__ = None
Just van Rossume29310a2002-12-31 16:33:00 +000055 # The set of global names that are assigned to in the module.
56 # This includes those names imported through starimports of
57 # Python modules.
58 self.globalnames = {}
59 # The set of starimports this module did that could not be
60 # resolved, ie. a starimport from a non-Python module.
61 self.starimports = {}
Guido van Rossum75dc4961998-03-05 03:42:00 +000062
63 def __repr__(self):
Neil Schemenauer32d23c92004-02-15 16:43:20 +000064 s = "Module(%r" % (self.__name__,)
Guido van Rossum912a14c1998-03-05 04:56:37 +000065 if self.__file__ is not None:
Walter Dörwald70a6b492004-02-12 17:35:32 +000066 s = s + ", %r" % (self.__file__,)
Guido van Rossum912a14c1998-03-05 04:56:37 +000067 if self.__path__ is not None:
Walter Dörwald70a6b492004-02-12 17:35:32 +000068 s = s + ", %r" % (self.__path__,)
Guido van Rossum912a14c1998-03-05 04:56:37 +000069 s = s + ")"
70 return s
Guido van Rossum75dc4961998-03-05 03:42:00 +000071
Guido van Rossum75dc4961998-03-05 03:42:00 +000072class ModuleFinder:
73
Just van Rossume29310a2002-12-31 16:33:00 +000074 def __init__(self, path=None, debug=0, excludes=[], replace_paths=[]):
Guido van Rossum912a14c1998-03-05 04:56:37 +000075 if path is None:
76 path = sys.path
77 self.path = path
78 self.modules = {}
79 self.badmodules = {}
80 self.debug = debug
81 self.indent = 0
Guido van Rossum78fc3631998-03-20 17:37:24 +000082 self.excludes = excludes
Guido van Rossum6b767ac2001-03-20 20:43:34 +000083 self.replace_paths = replace_paths
84 self.processed_paths = [] # Used in debugging only
Guido van Rossum75dc4961998-03-05 03:42:00 +000085
86 def msg(self, level, str, *args):
Guido van Rossum912a14c1998-03-05 04:56:37 +000087 if level <= self.debug:
88 for i in range(self.indent):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000089 print(" ", end=' ')
90 print(str, end=' ')
Guido van Rossum912a14c1998-03-05 04:56:37 +000091 for arg in args:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000092 print(repr(arg), end=' ')
93 print()
Guido van Rossum75dc4961998-03-05 03:42:00 +000094
95 def msgin(self, *args):
Guido van Rossum912a14c1998-03-05 04:56:37 +000096 level = args[0]
97 if level <= self.debug:
98 self.indent = self.indent + 1
Guido van Rossum68468eb2003-02-27 20:14:51 +000099 self.msg(*args)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000100
101 def msgout(self, *args):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000102 level = args[0]
103 if level <= self.debug:
104 self.indent = self.indent - 1
Guido van Rossum68468eb2003-02-27 20:14:51 +0000105 self.msg(*args)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000106
107 def run_script(self, pathname):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000108 self.msg(2, "run_script", pathname)
Éric Araujo1e3a68d2011-07-28 23:35:29 +0200109 with open(pathname) as fp:
Brett Cannon028011f2010-10-30 00:26:48 +0000110 stuff = ("", "r", imp.PY_SOURCE)
111 self.load_module('__main__', fp, pathname, stuff)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000112
113 def load_file(self, pathname):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000114 dir, name = os.path.split(pathname)
115 name, ext = os.path.splitext(name)
Éric Araujo1e3a68d2011-07-28 23:35:29 +0200116 with open(pathname) as fp:
Brett Cannon028011f2010-10-30 00:26:48 +0000117 stuff = (ext, "r", imp.PY_SOURCE)
118 self.load_module(name, fp, pathname, stuff)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000119
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000120 def import_hook(self, name, caller=None, fromlist=None, level=-1):
121 self.msg(3, "import_hook", name, caller, fromlist, level)
122 parent = self.determine_parent(caller, level=level)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000123 q, tail = self.find_head_package(parent, name)
124 m = self.load_tail(q, tail)
125 if not fromlist:
126 return q
127 if m.__path__:
128 self.ensure_fromlist(m, fromlist)
Thomas Heller318b7b92002-11-26 08:06:50 +0000129 return None
Guido van Rossum75dc4961998-03-05 03:42:00 +0000130
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000131 def determine_parent(self, caller, level=-1):
132 self.msgin(4, "determine_parent", caller, level)
133 if not caller or level == 0:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000134 self.msgout(4, "determine_parent -> None")
135 return None
136 pname = caller.__name__
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000137 if level >= 1: # relative import
138 if caller.__path__:
139 level -= 1
140 if level == 0:
141 parent = self.modules[pname]
142 assert parent is caller
143 self.msgout(4, "determine_parent ->", parent)
144 return parent
145 if pname.count(".") < level:
Collin Winterce36ad82007-08-30 01:19:48 +0000146 raise ImportError("relative importpath too deep")
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000147 pname = ".".join(pname.split(".")[:-level])
148 parent = self.modules[pname]
149 self.msgout(4, "determine_parent ->", parent)
150 return parent
Guido van Rossum912a14c1998-03-05 04:56:37 +0000151 if caller.__path__:
152 parent = self.modules[pname]
153 assert caller is parent
154 self.msgout(4, "determine_parent ->", parent)
155 return parent
156 if '.' in pname:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000157 i = pname.rfind('.')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000158 pname = pname[:i]
159 parent = self.modules[pname]
160 assert parent.__name__ == pname
161 self.msgout(4, "determine_parent ->", parent)
162 return parent
163 self.msgout(4, "determine_parent -> None")
164 return None
Guido van Rossum75dc4961998-03-05 03:42:00 +0000165
166 def find_head_package(self, parent, name):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000167 self.msgin(4, "find_head_package", parent, name)
168 if '.' in name:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000169 i = name.find('.')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000170 head = name[:i]
171 tail = name[i+1:]
172 else:
173 head = name
174 tail = ""
175 if parent:
176 qname = "%s.%s" % (parent.__name__, head)
177 else:
178 qname = head
179 q = self.import_module(head, qname, parent)
180 if q:
181 self.msgout(4, "find_head_package ->", (q, tail))
182 return q, tail
183 if parent:
184 qname = head
185 parent = None
186 q = self.import_module(head, qname, parent)
187 if q:
188 self.msgout(4, "find_head_package ->", (q, tail))
189 return q, tail
190 self.msgout(4, "raise ImportError: No module named", qname)
Collin Winterce36ad82007-08-30 01:19:48 +0000191 raise ImportError("No module named " + qname)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000192
193 def load_tail(self, q, tail):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000194 self.msgin(4, "load_tail", q, tail)
195 m = q
196 while tail:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000197 i = tail.find('.')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000198 if i < 0: i = len(tail)
199 head, tail = tail[:i], tail[i+1:]
200 mname = "%s.%s" % (m.__name__, head)
201 m = self.import_module(head, mname, m)
202 if not m:
203 self.msgout(4, "raise ImportError: No module named", mname)
Collin Winterce36ad82007-08-30 01:19:48 +0000204 raise ImportError("No module named " + mname)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000205 self.msgout(4, "load_tail ->", m)
206 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000207
208 def ensure_fromlist(self, m, fromlist, recursive=0):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000209 self.msg(4, "ensure_fromlist", m, fromlist, recursive)
210 for sub in fromlist:
211 if sub == "*":
212 if not recursive:
213 all = self.find_all_submodules(m)
214 if all:
215 self.ensure_fromlist(m, all, 1)
216 elif not hasattr(m, sub):
217 subname = "%s.%s" % (m.__name__, sub)
218 submod = self.import_module(sub, subname, m)
219 if not submod:
Collin Winterce36ad82007-08-30 01:19:48 +0000220 raise ImportError("No module named " + subname)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000221
222 def find_all_submodules(self, m):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000223 if not m.__path__:
224 return
225 modules = {}
Brett Cannonf299abd2015-04-13 14:21:02 -0400226 # 'suffixes' used to be a list hardcoded to [".py", ".pyc"].
Thomas Helleraaf1c8d2003-11-14 10:28:42 +0000227 # But we must also collect Python extension modules - although
228 # we cannot separate normal dlls from Python extensions.
229 suffixes = []
Brett Cannoncb66eb02012-05-11 12:58:42 -0400230 suffixes += importlib.machinery.EXTENSION_SUFFIXES[:]
231 suffixes += importlib.machinery.SOURCE_SUFFIXES[:]
232 suffixes += importlib.machinery.BYTECODE_SUFFIXES[:]
Guido van Rossum912a14c1998-03-05 04:56:37 +0000233 for dir in m.__path__:
234 try:
235 names = os.listdir(dir)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +0200236 except OSError:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000237 self.msg(2, "can't list directory", dir)
238 continue
239 for name in names:
240 mod = None
241 for suff in suffixes:
242 n = len(suff)
243 if name[-n:] == suff:
244 mod = name[:-n]
245 break
246 if mod and mod != "__init__":
247 modules[mod] = mod
248 return modules.keys()
Guido van Rossum75dc4961998-03-05 03:42:00 +0000249
250 def import_module(self, partname, fqname, parent):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000251 self.msgin(3, "import_module", partname, fqname, parent)
252 try:
253 m = self.modules[fqname]
254 except KeyError:
255 pass
256 else:
257 self.msgout(3, "import_module ->", m)
258 return m
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000259 if fqname in self.badmodules:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000260 self.msgout(3, "import_module -> None")
261 return None
Thomas Heller2e7c8322004-05-11 15:10:59 +0000262 if parent and parent.__path__ is None:
263 self.msgout(3, "import_module -> None")
264 return None
Guido van Rossum912a14c1998-03-05 04:56:37 +0000265 try:
266 fp, pathname, stuff = self.find_module(partname,
Just van Rossumf0dfbaf2003-03-05 17:23:48 +0000267 parent and parent.__path__, parent)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000268 except ImportError:
269 self.msgout(3, "import_module ->", None)
270 return None
271 try:
272 m = self.load_module(fqname, fp, pathname, stuff)
273 finally:
Éric Araujo1e3a68d2011-07-28 23:35:29 +0200274 if fp:
275 fp.close()
Guido van Rossum912a14c1998-03-05 04:56:37 +0000276 if parent:
277 setattr(parent, partname, m)
278 self.msgout(3, "import_module ->", m)
279 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000280
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000281 def load_module(self, fqname, fp, pathname, file_info):
282 suffix, mode, type = file_info
Guido van Rossum912a14c1998-03-05 04:56:37 +0000283 self.msgin(2, "load_module", fqname, fp and "fp", pathname)
284 if type == imp.PKG_DIRECTORY:
285 m = self.load_package(fqname, pathname)
286 self.msgout(2, "load_module ->", m)
287 return m
288 if type == imp.PY_SOURCE:
Guido van Rossum78fc3631998-03-20 17:37:24 +0000289 co = compile(fp.read()+'\n', pathname, 'exec')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000290 elif type == imp.PY_COMPILED:
Brett Cannon0f384782014-02-28 10:50:34 -0500291 try:
292 marshal_data = importlib._bootstrap._validate_bytecode_header(fp.read())
293 except ImportError as exc:
294 self.msgout(2, "raise ImportError: " + str(exc), pathname)
295 raise
296 co = marshal.loads(marshal_data)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000297 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] = {}
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000312 if caller:
313 self.badmodules[name][caller.__name__] = 1
314 else:
315 self.badmodules[name]["-"] = 1
Just van Rossume29310a2002-12-31 16:33:00 +0000316
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000317 def _safe_import_hook(self, name, caller, fromlist, level=-1):
Just van Rossume29310a2002-12-31 16:33:00 +0000318 # wrapper for self.import_hook() that won't raise ImportError
319 if name in self.badmodules:
320 self._add_badmodule(name, caller)
321 return
322 try:
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000323 self.import_hook(name, caller, level=level)
Guido van Rossumb940e112007-01-10 16:19:56 +0000324 except ImportError as msg:
Just van Rossume29310a2002-12-31 16:33:00 +0000325 self.msg(2, "ImportError:", str(msg))
326 self._add_badmodule(name, caller)
327 else:
328 if fromlist:
329 for sub in fromlist:
330 if sub in self.badmodules:
331 self._add_badmodule(sub, caller)
332 continue
333 try:
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000334 self.import_hook(name, caller, [sub], level=level)
Guido van Rossumb940e112007-01-10 16:19:56 +0000335 except ImportError as msg:
Just van Rossume29310a2002-12-31 16:33:00 +0000336 self.msg(2, "ImportError:", str(msg))
337 fullname = name + "." + sub
338 self._add_badmodule(fullname, caller)
339
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000340 def scan_opcodes_25(self, co,
341 unpack = struct.unpack):
342 # Scan the code, and yield 'interesting' opcode combinations
343 # Python 2.5 version (has absolute and relative imports)
344 code = co.co_code
345 names = co.co_names
346 consts = co.co_consts
347 LOAD_LOAD_AND_IMPORT = LOAD_CONST + LOAD_CONST + IMPORT_NAME
348 while code:
Guido van Rossum98297ee2007-11-06 21:34:58 +0000349 c = bytes([code[0]])
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000350 if c in STORE_OPS:
351 oparg, = unpack('<H', code[1:3])
352 yield "store", (names[oparg],)
353 code = code[3:]
354 continue
355 if code[:9:3] == LOAD_LOAD_AND_IMPORT:
356 oparg_1, oparg_2, oparg_3 = unpack('<xHxHxH', code[:9])
357 level = consts[oparg_1]
358 if level == 0: # absolute import
359 yield "absolute_import", (consts[oparg_2], names[oparg_3])
360 else: # relative import
361 yield "relative_import", (level, consts[oparg_2], names[oparg_3])
362 code = code[9:]
363 continue
364 if c >= HAVE_ARGUMENT:
365 code = code[3:]
366 else:
367 code = code[1:]
368
Guido van Rossum3c51cf21998-03-05 05:15:07 +0000369 def scan_code(self, co, m):
370 code = co.co_code
Serhiy Storchaka7e527052014-01-20 21:29:31 +0200371 scanner = self.scan_opcodes_25
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000372 for what, args in scanner(co):
373 if what == "store":
374 name, = args
375 m.globalnames[name] = 1
376 elif what == "absolute_import":
377 fromlist, name = args
Just van Rossume29310a2002-12-31 16:33:00 +0000378 have_star = 0
379 if fromlist is not None:
380 if "*" in fromlist:
381 have_star = 1
382 fromlist = [f for f in fromlist if f != "*"]
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000383 self._safe_import_hook(name, m, fromlist, level=0)
Just van Rossume29310a2002-12-31 16:33:00 +0000384 if have_star:
385 # We've encountered an "import *". If it is a Python module,
386 # the code has already been parsed and we can suck out the
387 # global names.
388 mm = None
389 if m.__path__:
390 # At this point we don't know whether 'name' is a
391 # submodule of 'm' or a global module. Let's just try
392 # the full name first.
393 mm = self.modules.get(m.__name__ + "." + name)
394 if mm is None:
395 mm = self.modules.get(name)
396 if mm is not None:
397 m.globalnames.update(mm.globalnames)
398 m.starimports.update(mm.starimports)
399 if mm.__code__ is None:
400 m.starimports[name] = 1
401 else:
402 m.starimports[name] = 1
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000403 elif what == "relative_import":
404 level, fromlist, name = args
405 if name:
406 self._safe_import_hook(name, m, fromlist, level=level)
407 else:
408 parent = self.determine_parent(m, level=level)
409 self._safe_import_hook(parent.__name__, None, fromlist, level=0)
410 else:
411 # We don't expect anything else from the generator.
412 raise RuntimeError(what)
413
Guido van Rossum3c51cf21998-03-05 05:15:07 +0000414 for c in co.co_consts:
415 if isinstance(c, type(co)):
416 self.scan_code(c, m)
417
Guido van Rossum75dc4961998-03-05 03:42:00 +0000418 def load_package(self, fqname, pathname):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000419 self.msgin(2, "load_package", fqname, pathname)
Thomas Hellerc7aaf952002-11-14 18:45:11 +0000420 newname = replacePackageMap.get(fqname)
421 if newname:
422 fqname = newname
Guido van Rossum912a14c1998-03-05 04:56:37 +0000423 m = self.add_module(fqname)
424 m.__file__ = pathname
425 m.__path__ = [pathname]
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +0000426
Guido van Rossume7e632a1998-09-14 16:02:28 +0000427 # As per comment at top of file, simulate runtime __path__ additions.
428 m.__path__ = m.__path__ + packagePathMap.get(fqname, [])
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +0000429
Guido van Rossum912a14c1998-03-05 04:56:37 +0000430 fp, buf, stuff = self.find_module("__init__", m.__path__)
Brett Cannon028011f2010-10-30 00:26:48 +0000431 try:
432 self.load_module(fqname, fp, buf, stuff)
433 self.msgout(2, "load_package ->", m)
434 return m
435 finally:
436 if fp:
437 fp.close()
Guido van Rossum75dc4961998-03-05 03:42:00 +0000438
439 def add_module(self, fqname):
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000440 if fqname in self.modules:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000441 return self.modules[fqname]
442 self.modules[fqname] = m = Module(fqname)
443 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000444
Just van Rossumf0dfbaf2003-03-05 17:23:48 +0000445 def find_module(self, name, path, parent=None):
446 if parent is not None:
Thomas Heller2e7c8322004-05-11 15:10:59 +0000447 # assert path is not None
Just van Rossumf0dfbaf2003-03-05 17:23:48 +0000448 fullname = parent.__name__+'.'+name
Guido van Rossum03f7f082001-10-18 19:15:32 +0000449 else:
450 fullname = name
451 if fullname in self.excludes:
452 self.msgout(3, "find_module -> Excluded", fullname)
Collin Winterce36ad82007-08-30 01:19:48 +0000453 raise ImportError(name)
Guido van Rossum78fc3631998-03-20 17:37:24 +0000454
Guido van Rossum912a14c1998-03-05 04:56:37 +0000455 if path is None:
456 if name in sys.builtin_module_names:
457 return (None, None, ("", "", imp.C_BUILTIN))
Guido van Rossum78fc3631998-03-20 17:37:24 +0000458
Guido van Rossum912a14c1998-03-05 04:56:37 +0000459 path = self.path
460 return imp.find_module(name, path)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000461
462 def report(self):
Just van Rossume29310a2002-12-31 16:33:00 +0000463 """Print a report to stdout, listing the found modules with their
464 paths, as well as modules that are missing, or seem to be missing.
465 """
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000466 print()
467 print(" %-25s %s" % ("Name", "File"))
468 print(" %-25s %s" % ("----", "----"))
Guido van Rossum912a14c1998-03-05 04:56:37 +0000469 # Print modules found
Guido van Rossumd59cde82007-06-12 00:25:08 +0000470 keys = sorted(self.modules.keys())
Guido van Rossum912a14c1998-03-05 04:56:37 +0000471 for key in keys:
472 m = self.modules[key]
473 if m.__path__:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000474 print("P", end=' ')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000475 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000476 print("m", end=' ')
477 print("%-25s" % key, m.__file__ or "")
Guido van Rossum75dc4961998-03-05 03:42:00 +0000478
Guido van Rossum912a14c1998-03-05 04:56:37 +0000479 # Print missing modules
Just van Rossume29310a2002-12-31 16:33:00 +0000480 missing, maybe = self.any_missing_maybe()
481 if missing:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000482 print()
483 print("Missing modules:")
Just van Rossume29310a2002-12-31 16:33:00 +0000484 for name in missing:
Guido van Rossumd59cde82007-06-12 00:25:08 +0000485 mods = sorted(self.badmodules[name].keys())
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000486 print("?", name, "imported from", ', '.join(mods))
Just van Rossume29310a2002-12-31 16:33:00 +0000487 # Print modules that may be missing, but then again, maybe not...
488 if maybe:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000489 print()
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300490 print("Submodules that appear to be missing, but could also be", end=' ')
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000491 print("global names in the parent package:")
Just van Rossume29310a2002-12-31 16:33:00 +0000492 for name in maybe:
Guido van Rossumd59cde82007-06-12 00:25:08 +0000493 mods = sorted(self.badmodules[name].keys())
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000494 print("?", name, "imported from", ', '.join(mods))
Guido van Rossum75dc4961998-03-05 03:42:00 +0000495
Guido van Rossum03f7f082001-10-18 19:15:32 +0000496 def any_missing(self):
Just van Rossume29310a2002-12-31 16:33:00 +0000497 """Return a list of modules that appear to be missing. Use
498 any_missing_maybe() if you want to know which modules are
499 certain to be missing, and which *may* be missing.
500 """
501 missing, maybe = self.any_missing_maybe()
502 return missing + maybe
503
504 def any_missing_maybe(self):
505 """Return two lists, one with modules that are certainly missing
506 and one with modules that *may* be missing. The latter names could
507 either be submodules *or* just global names in the package.
508
509 The reason it can't always be determined is that it's impossible to
510 tell which names are imported when "from module import *" is done
511 with an extension module, short of actually importing it.
512 """
Guido van Rossum03f7f082001-10-18 19:15:32 +0000513 missing = []
Just van Rossume29310a2002-12-31 16:33:00 +0000514 maybe = []
515 for name in self.badmodules:
516 if name in self.excludes:
517 continue
518 i = name.rfind(".")
519 if i < 0:
520 missing.append(name)
521 continue
522 subname = name[i+1:]
523 pkgname = name[:i]
524 pkg = self.modules.get(pkgname)
525 if pkg is not None:
526 if pkgname in self.badmodules[name]:
527 # The package tried to import this module itself and
528 # failed. It's definitely missing.
529 missing.append(name)
530 elif subname in pkg.globalnames:
531 # It's a global in the package: definitely not missing.
532 pass
533 elif pkg.starimports:
534 # It could be missing, but the package did an "import *"
535 # from a non-Python module, so we simply can't be sure.
536 maybe.append(name)
537 else:
538 # It's not a global in the package, the package didn't
539 # do funny star imports, it's very likely to be missing.
540 # The symbol could be inserted into the package from the
541 # outside, but since that's not good style we simply list
542 # it missing.
543 missing.append(name)
544 else:
545 missing.append(name)
546 missing.sort()
547 maybe.sort()
548 return missing, maybe
Guido van Rossum03f7f082001-10-18 19:15:32 +0000549
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000550 def replace_paths_in_code(self, co):
551 new_filename = original_filename = os.path.normpath(co.co_filename)
Just van Rossume29310a2002-12-31 16:33:00 +0000552 for f, r in self.replace_paths:
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000553 if original_filename.startswith(f):
Just van Rossume29310a2002-12-31 16:33:00 +0000554 new_filename = r + original_filename[len(f):]
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000555 break
556
557 if self.debug and original_filename not in self.processed_paths:
Just van Rossume29310a2002-12-31 16:33:00 +0000558 if new_filename != original_filename:
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000559 self.msgout(2, "co_filename %r changed to %r" \
560 % (original_filename,new_filename,))
561 else:
562 self.msgout(2, "co_filename %r remains unchanged" \
563 % (original_filename,))
564 self.processed_paths.append(original_filename)
565
566 consts = list(co.co_consts)
567 for i in range(len(consts)):
568 if isinstance(consts[i], type(co)):
569 consts[i] = self.replace_paths_in_code(consts[i])
570
Berker Peksag0a0d1da2014-07-07 14:58:12 +0300571 return types.CodeType(co.co_argcount, co.co_kwonlyargcount,
572 co.co_nlocals, co.co_stacksize, co.co_flags,
573 co.co_code, tuple(consts), co.co_names,
574 co.co_varnames, new_filename, co.co_name,
575 co.co_firstlineno, co.co_lnotab, co.co_freevars,
576 co.co_cellvars)
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000577
Guido van Rossum75dc4961998-03-05 03:42:00 +0000578
579def test():
580 # Parse command line
581 import getopt
582 try:
Guido van Rossumbaf06031998-08-25 14:06:55 +0000583 opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:")
Guido van Rossumb940e112007-01-10 16:19:56 +0000584 except getopt.error as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000585 print(msg)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000586 return
Guido van Rossum75dc4961998-03-05 03:42:00 +0000587
588 # Process options
589 debug = 1
590 domods = 0
591 addpath = []
Guido van Rossumbaf06031998-08-25 14:06:55 +0000592 exclude = []
Guido van Rossum75dc4961998-03-05 03:42:00 +0000593 for o, a in opts:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000594 if o == '-d':
595 debug = debug + 1
596 if o == '-m':
597 domods = 1
598 if o == '-p':
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000599 addpath = addpath + a.split(os.pathsep)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000600 if o == '-q':
601 debug = 0
Guido van Rossumbaf06031998-08-25 14:06:55 +0000602 if o == '-x':
603 exclude.append(a)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000604
605 # Provide default arguments
606 if not args:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000607 script = "hello.py"
Guido van Rossum75dc4961998-03-05 03:42:00 +0000608 else:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000609 script = args[0]
Guido van Rossum75dc4961998-03-05 03:42:00 +0000610
611 # Set the path based on sys.path and the script directory
612 path = sys.path[:]
613 path[0] = os.path.dirname(script)
614 path = addpath + path
615 if debug > 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000616 print("path:")
Guido van Rossum912a14c1998-03-05 04:56:37 +0000617 for item in path:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000618 print(" ", repr(item))
Guido van Rossum75dc4961998-03-05 03:42:00 +0000619
620 # Create the module finder and turn its crank
Guido van Rossumbaf06031998-08-25 14:06:55 +0000621 mf = ModuleFinder(path, debug, exclude)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000622 for arg in args[1:]:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000623 if arg == '-m':
624 domods = 1
625 continue
626 if domods:
627 if arg[-2:] == '.*':
628 mf.import_hook(arg[:-2], None, ["*"])
629 else:
630 mf.import_hook(arg)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000631 else:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000632 mf.load_file(arg)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000633 mf.run_script(script)
634 mf.report()
Just van Rossume29310a2002-12-31 16:33:00 +0000635 return mf # for -i debugging
Guido van Rossum75dc4961998-03-05 03:42:00 +0000636
637
638if __name__ == '__main__':
639 try:
Just van Rossume29310a2002-12-31 16:33:00 +0000640 mf = test()
Guido van Rossum75dc4961998-03-05 03:42:00 +0000641 except KeyboardInterrupt:
Éric Araujo1e3a68d2011-07-28 23:35:29 +0200642 print("\n[interrupted]")