blob: e277ca72f8ef290fa0e409af190d90af52faad3b [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
Eric Snow32439d62015-05-02 19:15:18 -06004import importlib._bootstrap_external
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
Brett Cannone4f41de2013-06-16 13:13:40 -040010import warnings
11with warnings.catch_warnings():
Brett Cannon213b4052015-10-30 14:41:06 -070012 warnings.simplefilter('ignore', DeprecationWarning)
Brett Cannone4f41de2013-06-16 13:13:40 -040013 import imp
Guido van Rossum75dc4961998-03-05 03:42:00 +000014
Serhiy Storchaka02d9f5e2016-05-08 23:43:50 +030015LOAD_CONST = dis.opmap['LOAD_CONST']
16IMPORT_NAME = dis.opmap['IMPORT_NAME']
17STORE_NAME = dis.opmap['STORE_NAME']
18STORE_GLOBAL = dis.opmap['STORE_GLOBAL']
Victor Stinner7e509782016-04-12 18:17:06 +020019STORE_OPS = STORE_NAME, STORE_GLOBAL
Serhiy Storchaka02d9f5e2016-05-08 23:43:50 +030020EXTENDED_ARG = dis.EXTENDED_ARG
Guido van Rossum75dc4961998-03-05 03:42:00 +000021
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +000022# Modulefinder does a good job at simulating Python's, but it can not
23# handle __path__ modifications packages make at runtime. Therefore there
24# is a mechanism whereby you can register extra paths in this map for a
Thomas Wouters7e474022000-07-16 12:04:32 +000025# package, and it will be honored.
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +000026
27# Note this is a mapping is lists of paths.
28packagePathMap = {}
29
30# A Public interface
31def AddPackagePath(packagename, path):
Éric Araujocee6bb52011-08-01 15:29:07 +020032 packagePathMap.setdefault(packagename, []).append(path)
Guido van Rossum75dc4961998-03-05 03:42:00 +000033
Thomas Hellerc7aaf952002-11-14 18:45:11 +000034replacePackageMap = {}
35
Martin v. Löwis2f48d892011-05-09 08:05:43 +020036# This ReplacePackage mechanism allows modulefinder to work around
37# situations in which a package injects itself under the name
38# of another package into sys.modules at runtime by calling
39# ReplacePackage("real_package_name", "faked_package_name")
Thomas Hellerc7aaf952002-11-14 18:45:11 +000040# before running ModuleFinder.
41
42def ReplacePackage(oldname, newname):
43 replacePackageMap[oldname] = newname
44
45
Guido van Rossum75dc4961998-03-05 03:42:00 +000046class Module:
47
48 def __init__(self, name, file=None, path=None):
Guido van Rossum912a14c1998-03-05 04:56:37 +000049 self.__name__ = name
50 self.__file__ = file
51 self.__path__ = path
52 self.__code__ = None
Just van Rossume29310a2002-12-31 16:33:00 +000053 # The set of global names that are assigned to in the module.
54 # This includes those names imported through starimports of
55 # Python modules.
56 self.globalnames = {}
57 # The set of starimports this module did that could not be
58 # resolved, ie. a starimport from a non-Python module.
59 self.starimports = {}
Guido van Rossum75dc4961998-03-05 03:42:00 +000060
61 def __repr__(self):
Neil Schemenauer32d23c92004-02-15 16:43:20 +000062 s = "Module(%r" % (self.__name__,)
Guido van Rossum912a14c1998-03-05 04:56:37 +000063 if self.__file__ is not None:
Walter Dörwald70a6b492004-02-12 17:35:32 +000064 s = s + ", %r" % (self.__file__,)
Guido van Rossum912a14c1998-03-05 04:56:37 +000065 if self.__path__ is not None:
Walter Dörwald70a6b492004-02-12 17:35:32 +000066 s = s + ", %r" % (self.__path__,)
Guido van Rossum912a14c1998-03-05 04:56:37 +000067 s = s + ")"
68 return s
Guido van Rossum75dc4961998-03-05 03:42:00 +000069
Guido van Rossum75dc4961998-03-05 03:42:00 +000070class ModuleFinder:
71
Just van Rossume29310a2002-12-31 16:33:00 +000072 def __init__(self, path=None, debug=0, excludes=[], replace_paths=[]):
Guido van Rossum912a14c1998-03-05 04:56:37 +000073 if path is None:
74 path = sys.path
75 self.path = path
76 self.modules = {}
77 self.badmodules = {}
78 self.debug = debug
79 self.indent = 0
Guido van Rossum78fc3631998-03-20 17:37:24 +000080 self.excludes = excludes
Guido van Rossum6b767ac2001-03-20 20:43:34 +000081 self.replace_paths = replace_paths
82 self.processed_paths = [] # Used in debugging only
Guido van Rossum75dc4961998-03-05 03:42:00 +000083
84 def msg(self, level, str, *args):
Guido van Rossum912a14c1998-03-05 04:56:37 +000085 if level <= self.debug:
86 for i in range(self.indent):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000087 print(" ", end=' ')
88 print(str, end=' ')
Guido van Rossum912a14c1998-03-05 04:56:37 +000089 for arg in args:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000090 print(repr(arg), end=' ')
91 print()
Guido van Rossum75dc4961998-03-05 03:42:00 +000092
93 def msgin(self, *args):
Guido van Rossum912a14c1998-03-05 04:56:37 +000094 level = args[0]
95 if level <= self.debug:
96 self.indent = self.indent + 1
Guido van Rossum68468eb2003-02-27 20:14:51 +000097 self.msg(*args)
Guido van Rossum75dc4961998-03-05 03:42:00 +000098
99 def msgout(self, *args):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000100 level = args[0]
101 if level <= self.debug:
102 self.indent = self.indent - 1
Guido van Rossum68468eb2003-02-27 20:14:51 +0000103 self.msg(*args)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000104
105 def run_script(self, pathname):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000106 self.msg(2, "run_script", pathname)
Éric Araujo1e3a68d2011-07-28 23:35:29 +0200107 with open(pathname) as fp:
Brett Cannon028011f2010-10-30 00:26:48 +0000108 stuff = ("", "r", imp.PY_SOURCE)
109 self.load_module('__main__', fp, pathname, stuff)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000110
111 def load_file(self, pathname):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000112 dir, name = os.path.split(pathname)
113 name, ext = os.path.splitext(name)
Éric Araujo1e3a68d2011-07-28 23:35:29 +0200114 with open(pathname) as fp:
Brett Cannon028011f2010-10-30 00:26:48 +0000115 stuff = (ext, "r", imp.PY_SOURCE)
116 self.load_module(name, fp, pathname, stuff)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000117
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000118 def import_hook(self, name, caller=None, fromlist=None, level=-1):
119 self.msg(3, "import_hook", name, caller, fromlist, level)
120 parent = self.determine_parent(caller, level=level)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000121 q, tail = self.find_head_package(parent, name)
122 m = self.load_tail(q, tail)
123 if not fromlist:
124 return q
125 if m.__path__:
126 self.ensure_fromlist(m, fromlist)
Thomas Heller318b7b92002-11-26 08:06:50 +0000127 return None
Guido van Rossum75dc4961998-03-05 03:42:00 +0000128
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000129 def determine_parent(self, caller, level=-1):
130 self.msgin(4, "determine_parent", caller, level)
131 if not caller or level == 0:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000132 self.msgout(4, "determine_parent -> None")
133 return None
134 pname = caller.__name__
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000135 if level >= 1: # relative import
136 if caller.__path__:
137 level -= 1
138 if level == 0:
139 parent = self.modules[pname]
140 assert parent is caller
141 self.msgout(4, "determine_parent ->", parent)
142 return parent
143 if pname.count(".") < level:
Collin Winterce36ad82007-08-30 01:19:48 +0000144 raise ImportError("relative importpath too deep")
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000145 pname = ".".join(pname.split(".")[:-level])
146 parent = self.modules[pname]
147 self.msgout(4, "determine_parent ->", parent)
148 return parent
Guido van Rossum912a14c1998-03-05 04:56:37 +0000149 if caller.__path__:
150 parent = self.modules[pname]
151 assert caller is parent
152 self.msgout(4, "determine_parent ->", parent)
153 return parent
154 if '.' in pname:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000155 i = pname.rfind('.')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000156 pname = pname[:i]
157 parent = self.modules[pname]
158 assert parent.__name__ == pname
159 self.msgout(4, "determine_parent ->", parent)
160 return parent
161 self.msgout(4, "determine_parent -> None")
162 return None
Guido van Rossum75dc4961998-03-05 03:42:00 +0000163
164 def find_head_package(self, parent, name):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000165 self.msgin(4, "find_head_package", parent, name)
166 if '.' in name:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000167 i = name.find('.')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000168 head = name[:i]
169 tail = name[i+1:]
170 else:
171 head = name
172 tail = ""
173 if parent:
174 qname = "%s.%s" % (parent.__name__, head)
175 else:
176 qname = head
177 q = self.import_module(head, qname, parent)
178 if q:
179 self.msgout(4, "find_head_package ->", (q, tail))
180 return q, tail
181 if parent:
182 qname = head
183 parent = None
184 q = self.import_module(head, qname, parent)
185 if q:
186 self.msgout(4, "find_head_package ->", (q, tail))
187 return q, tail
188 self.msgout(4, "raise ImportError: No module named", qname)
Collin Winterce36ad82007-08-30 01:19:48 +0000189 raise ImportError("No module named " + qname)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000190
191 def load_tail(self, q, tail):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000192 self.msgin(4, "load_tail", q, tail)
193 m = q
194 while tail:
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000195 i = tail.find('.')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000196 if i < 0: i = len(tail)
197 head, tail = tail[:i], tail[i+1:]
198 mname = "%s.%s" % (m.__name__, head)
199 m = self.import_module(head, mname, m)
200 if not m:
201 self.msgout(4, "raise ImportError: No module named", mname)
Collin Winterce36ad82007-08-30 01:19:48 +0000202 raise ImportError("No module named " + mname)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000203 self.msgout(4, "load_tail ->", m)
204 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000205
206 def ensure_fromlist(self, m, fromlist, recursive=0):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000207 self.msg(4, "ensure_fromlist", m, fromlist, recursive)
208 for sub in fromlist:
209 if sub == "*":
210 if not recursive:
211 all = self.find_all_submodules(m)
212 if all:
213 self.ensure_fromlist(m, all, 1)
214 elif not hasattr(m, sub):
215 subname = "%s.%s" % (m.__name__, sub)
216 submod = self.import_module(sub, subname, m)
217 if not submod:
Collin Winterce36ad82007-08-30 01:19:48 +0000218 raise ImportError("No module named " + subname)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000219
220 def find_all_submodules(self, m):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000221 if not m.__path__:
222 return
223 modules = {}
Brett Cannonf299abd2015-04-13 14:21:02 -0400224 # 'suffixes' used to be a list hardcoded to [".py", ".pyc"].
Thomas Helleraaf1c8d2003-11-14 10:28:42 +0000225 # But we must also collect Python extension modules - although
226 # we cannot separate normal dlls from Python extensions.
227 suffixes = []
Brett Cannoncb66eb02012-05-11 12:58:42 -0400228 suffixes += importlib.machinery.EXTENSION_SUFFIXES[:]
229 suffixes += importlib.machinery.SOURCE_SUFFIXES[:]
230 suffixes += importlib.machinery.BYTECODE_SUFFIXES[:]
Guido van Rossum912a14c1998-03-05 04:56:37 +0000231 for dir in m.__path__:
232 try:
233 names = os.listdir(dir)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +0200234 except OSError:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000235 self.msg(2, "can't list directory", dir)
236 continue
237 for name in names:
238 mod = None
239 for suff in suffixes:
240 n = len(suff)
241 if name[-n:] == suff:
242 mod = name[:-n]
243 break
244 if mod and mod != "__init__":
245 modules[mod] = mod
246 return modules.keys()
Guido van Rossum75dc4961998-03-05 03:42:00 +0000247
248 def import_module(self, partname, fqname, parent):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000249 self.msgin(3, "import_module", partname, fqname, parent)
250 try:
251 m = self.modules[fqname]
252 except KeyError:
253 pass
254 else:
255 self.msgout(3, "import_module ->", m)
256 return m
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000257 if fqname in self.badmodules:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000258 self.msgout(3, "import_module -> None")
259 return None
Thomas Heller2e7c8322004-05-11 15:10:59 +0000260 if parent and parent.__path__ is None:
261 self.msgout(3, "import_module -> None")
262 return None
Guido van Rossum912a14c1998-03-05 04:56:37 +0000263 try:
264 fp, pathname, stuff = self.find_module(partname,
Just van Rossumf0dfbaf2003-03-05 17:23:48 +0000265 parent and parent.__path__, parent)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000266 except ImportError:
267 self.msgout(3, "import_module ->", None)
268 return None
269 try:
270 m = self.load_module(fqname, fp, pathname, stuff)
271 finally:
Éric Araujo1e3a68d2011-07-28 23:35:29 +0200272 if fp:
273 fp.close()
Guido van Rossum912a14c1998-03-05 04:56:37 +0000274 if parent:
275 setattr(parent, partname, m)
276 self.msgout(3, "import_module ->", m)
277 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000278
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000279 def load_module(self, fqname, fp, pathname, file_info):
280 suffix, mode, type = file_info
Guido van Rossum912a14c1998-03-05 04:56:37 +0000281 self.msgin(2, "load_module", fqname, fp and "fp", pathname)
282 if type == imp.PKG_DIRECTORY:
283 m = self.load_package(fqname, pathname)
284 self.msgout(2, "load_module ->", m)
285 return m
286 if type == imp.PY_SOURCE:
Guido van Rossum78fc3631998-03-20 17:37:24 +0000287 co = compile(fp.read()+'\n', pathname, 'exec')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000288 elif type == imp.PY_COMPILED:
Brett Cannon0f384782014-02-28 10:50:34 -0500289 try:
Eric Snow32439d62015-05-02 19:15:18 -0600290 marshal_data = importlib._bootstrap_external._validate_bytecode_header(fp.read())
Brett Cannon0f384782014-02-28 10:50:34 -0500291 except ImportError as exc:
292 self.msgout(2, "raise ImportError: " + str(exc), pathname)
293 raise
294 co = marshal.loads(marshal_data)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000295 else:
296 co = None
297 m = self.add_module(fqname)
Guido van Rossumab045f91998-03-06 19:55:10 +0000298 m.__file__ = pathname
Guido van Rossum912a14c1998-03-05 04:56:37 +0000299 if co:
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000300 if self.replace_paths:
301 co = self.replace_paths_in_code(co)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000302 m.__code__ = co
Guido van Rossum3c51cf21998-03-05 05:15:07 +0000303 self.scan_code(co, m)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000304 self.msgout(2, "load_module ->", m)
305 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000306
Just van Rossume29310a2002-12-31 16:33:00 +0000307 def _add_badmodule(self, name, caller):
308 if name not in self.badmodules:
309 self.badmodules[name] = {}
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000310 if caller:
311 self.badmodules[name][caller.__name__] = 1
312 else:
313 self.badmodules[name]["-"] = 1
Just van Rossume29310a2002-12-31 16:33:00 +0000314
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000315 def _safe_import_hook(self, name, caller, fromlist, level=-1):
Just van Rossume29310a2002-12-31 16:33:00 +0000316 # wrapper for self.import_hook() that won't raise ImportError
317 if name in self.badmodules:
318 self._add_badmodule(name, caller)
319 return
320 try:
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000321 self.import_hook(name, caller, level=level)
Guido van Rossumb940e112007-01-10 16:19:56 +0000322 except ImportError as msg:
Just van Rossume29310a2002-12-31 16:33:00 +0000323 self.msg(2, "ImportError:", str(msg))
324 self._add_badmodule(name, caller)
325 else:
326 if fromlist:
327 for sub in fromlist:
328 if sub in self.badmodules:
329 self._add_badmodule(sub, caller)
330 continue
331 try:
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000332 self.import_hook(name, caller, [sub], level=level)
Guido van Rossumb940e112007-01-10 16:19:56 +0000333 except ImportError as msg:
Just van Rossume29310a2002-12-31 16:33:00 +0000334 self.msg(2, "ImportError:", str(msg))
335 fullname = name + "." + sub
336 self._add_badmodule(fullname, caller)
337
Serhiy Storchaka02d9f5e2016-05-08 23:43:50 +0300338 def scan_opcodes(self, co):
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000339 # Scan the code, and yield 'interesting' opcode combinations
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000340 code = co.co_code
341 names = co.co_names
342 consts = co.co_consts
Serhiy Storchaka02d9f5e2016-05-08 23:43:50 +0300343 opargs = [(op, arg) for _, op, arg in dis._unpack_opargs(code)
344 if op != EXTENDED_ARG]
345 for i, (op, oparg) in enumerate(opargs):
346 if op in STORE_OPS:
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000347 yield "store", (names[oparg],)
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000348 continue
Serhiy Storchaka02d9f5e2016-05-08 23:43:50 +0300349 if (op == IMPORT_NAME and i >= 2
350 and opargs[i-1][0] == opargs[i-2][0] == LOAD_CONST):
351 level = consts[opargs[i-2][1]]
352 fromlist = consts[opargs[i-1][1]]
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000353 if level == 0: # absolute import
Serhiy Storchaka02d9f5e2016-05-08 23:43:50 +0300354 yield "absolute_import", (fromlist, names[oparg])
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000355 else: # relative import
Serhiy Storchaka02d9f5e2016-05-08 23:43:50 +0300356 yield "relative_import", (level, fromlist, names[oparg])
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000357 continue
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000358
Guido van Rossum3c51cf21998-03-05 05:15:07 +0000359 def scan_code(self, co, m):
360 code = co.co_code
Serhiy Storchaka02d9f5e2016-05-08 23:43:50 +0300361 scanner = self.scan_opcodes
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000362 for what, args in scanner(co):
363 if what == "store":
364 name, = args
365 m.globalnames[name] = 1
366 elif what == "absolute_import":
367 fromlist, name = args
Just van Rossume29310a2002-12-31 16:33:00 +0000368 have_star = 0
369 if fromlist is not None:
370 if "*" in fromlist:
371 have_star = 1
372 fromlist = [f for f in fromlist if f != "*"]
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000373 self._safe_import_hook(name, m, fromlist, level=0)
Just van Rossume29310a2002-12-31 16:33:00 +0000374 if have_star:
375 # We've encountered an "import *". If it is a Python module,
376 # the code has already been parsed and we can suck out the
377 # global names.
378 mm = None
379 if m.__path__:
380 # At this point we don't know whether 'name' is a
381 # submodule of 'm' or a global module. Let's just try
382 # the full name first.
383 mm = self.modules.get(m.__name__ + "." + name)
384 if mm is None:
385 mm = self.modules.get(name)
386 if mm is not None:
387 m.globalnames.update(mm.globalnames)
388 m.starimports.update(mm.starimports)
389 if mm.__code__ is None:
390 m.starimports[name] = 1
391 else:
392 m.starimports[name] = 1
Guido van Rossumfc2a0a82006-10-27 23:06:01 +0000393 elif what == "relative_import":
394 level, fromlist, name = args
395 if name:
396 self._safe_import_hook(name, m, fromlist, level=level)
397 else:
398 parent = self.determine_parent(m, level=level)
399 self._safe_import_hook(parent.__name__, None, fromlist, level=0)
400 else:
401 # We don't expect anything else from the generator.
402 raise RuntimeError(what)
403
Guido van Rossum3c51cf21998-03-05 05:15:07 +0000404 for c in co.co_consts:
405 if isinstance(c, type(co)):
406 self.scan_code(c, m)
407
Guido van Rossum75dc4961998-03-05 03:42:00 +0000408 def load_package(self, fqname, pathname):
Guido van Rossum912a14c1998-03-05 04:56:37 +0000409 self.msgin(2, "load_package", fqname, pathname)
Thomas Hellerc7aaf952002-11-14 18:45:11 +0000410 newname = replacePackageMap.get(fqname)
411 if newname:
412 fqname = newname
Guido van Rossum912a14c1998-03-05 04:56:37 +0000413 m = self.add_module(fqname)
414 m.__file__ = pathname
415 m.__path__ = [pathname]
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +0000416
Guido van Rossume7e632a1998-09-14 16:02:28 +0000417 # As per comment at top of file, simulate runtime __path__ additions.
418 m.__path__ = m.__path__ + packagePathMap.get(fqname, [])
Guido van Rossumf1b5a0e1998-05-18 20:21:56 +0000419
Guido van Rossum912a14c1998-03-05 04:56:37 +0000420 fp, buf, stuff = self.find_module("__init__", m.__path__)
Brett Cannon028011f2010-10-30 00:26:48 +0000421 try:
422 self.load_module(fqname, fp, buf, stuff)
423 self.msgout(2, "load_package ->", m)
424 return m
425 finally:
426 if fp:
427 fp.close()
Guido van Rossum75dc4961998-03-05 03:42:00 +0000428
429 def add_module(self, fqname):
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000430 if fqname in self.modules:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000431 return self.modules[fqname]
432 self.modules[fqname] = m = Module(fqname)
433 return m
Guido van Rossum75dc4961998-03-05 03:42:00 +0000434
Just van Rossumf0dfbaf2003-03-05 17:23:48 +0000435 def find_module(self, name, path, parent=None):
436 if parent is not None:
Thomas Heller2e7c8322004-05-11 15:10:59 +0000437 # assert path is not None
Just van Rossumf0dfbaf2003-03-05 17:23:48 +0000438 fullname = parent.__name__+'.'+name
Guido van Rossum03f7f082001-10-18 19:15:32 +0000439 else:
440 fullname = name
441 if fullname in self.excludes:
442 self.msgout(3, "find_module -> Excluded", fullname)
Collin Winterce36ad82007-08-30 01:19:48 +0000443 raise ImportError(name)
Guido van Rossum78fc3631998-03-20 17:37:24 +0000444
Guido van Rossum912a14c1998-03-05 04:56:37 +0000445 if path is None:
446 if name in sys.builtin_module_names:
447 return (None, None, ("", "", imp.C_BUILTIN))
Guido van Rossum78fc3631998-03-20 17:37:24 +0000448
Guido van Rossum912a14c1998-03-05 04:56:37 +0000449 path = self.path
450 return imp.find_module(name, path)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000451
452 def report(self):
Just van Rossume29310a2002-12-31 16:33:00 +0000453 """Print a report to stdout, listing the found modules with their
454 paths, as well as modules that are missing, or seem to be missing.
455 """
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000456 print()
457 print(" %-25s %s" % ("Name", "File"))
458 print(" %-25s %s" % ("----", "----"))
Guido van Rossum912a14c1998-03-05 04:56:37 +0000459 # Print modules found
Guido van Rossumd59cde82007-06-12 00:25:08 +0000460 keys = sorted(self.modules.keys())
Guido van Rossum912a14c1998-03-05 04:56:37 +0000461 for key in keys:
462 m = self.modules[key]
463 if m.__path__:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000464 print("P", end=' ')
Guido van Rossum912a14c1998-03-05 04:56:37 +0000465 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000466 print("m", end=' ')
467 print("%-25s" % key, m.__file__ or "")
Guido van Rossum75dc4961998-03-05 03:42:00 +0000468
Guido van Rossum912a14c1998-03-05 04:56:37 +0000469 # Print missing modules
Just van Rossume29310a2002-12-31 16:33:00 +0000470 missing, maybe = self.any_missing_maybe()
471 if missing:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000472 print()
473 print("Missing modules:")
Just van Rossume29310a2002-12-31 16:33:00 +0000474 for name in missing:
Guido van Rossumd59cde82007-06-12 00:25:08 +0000475 mods = sorted(self.badmodules[name].keys())
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000476 print("?", name, "imported from", ', '.join(mods))
Just van Rossume29310a2002-12-31 16:33:00 +0000477 # Print modules that may be missing, but then again, maybe not...
478 if maybe:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000479 print()
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300480 print("Submodules that appear to be missing, but could also be", end=' ')
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000481 print("global names in the parent package:")
Just van Rossume29310a2002-12-31 16:33:00 +0000482 for name in maybe:
Guido van Rossumd59cde82007-06-12 00:25:08 +0000483 mods = sorted(self.badmodules[name].keys())
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000484 print("?", name, "imported from", ', '.join(mods))
Guido van Rossum75dc4961998-03-05 03:42:00 +0000485
Guido van Rossum03f7f082001-10-18 19:15:32 +0000486 def any_missing(self):
Just van Rossume29310a2002-12-31 16:33:00 +0000487 """Return a list of modules that appear to be missing. Use
488 any_missing_maybe() if you want to know which modules are
489 certain to be missing, and which *may* be missing.
490 """
491 missing, maybe = self.any_missing_maybe()
492 return missing + maybe
493
494 def any_missing_maybe(self):
495 """Return two lists, one with modules that are certainly missing
496 and one with modules that *may* be missing. The latter names could
497 either be submodules *or* just global names in the package.
498
499 The reason it can't always be determined is that it's impossible to
500 tell which names are imported when "from module import *" is done
501 with an extension module, short of actually importing it.
502 """
Guido van Rossum03f7f082001-10-18 19:15:32 +0000503 missing = []
Just van Rossume29310a2002-12-31 16:33:00 +0000504 maybe = []
505 for name in self.badmodules:
506 if name in self.excludes:
507 continue
508 i = name.rfind(".")
509 if i < 0:
510 missing.append(name)
511 continue
512 subname = name[i+1:]
513 pkgname = name[:i]
514 pkg = self.modules.get(pkgname)
515 if pkg is not None:
516 if pkgname in self.badmodules[name]:
517 # The package tried to import this module itself and
518 # failed. It's definitely missing.
519 missing.append(name)
520 elif subname in pkg.globalnames:
521 # It's a global in the package: definitely not missing.
522 pass
523 elif pkg.starimports:
524 # It could be missing, but the package did an "import *"
525 # from a non-Python module, so we simply can't be sure.
526 maybe.append(name)
527 else:
528 # It's not a global in the package, the package didn't
529 # do funny star imports, it's very likely to be missing.
530 # The symbol could be inserted into the package from the
531 # outside, but since that's not good style we simply list
532 # it missing.
533 missing.append(name)
534 else:
535 missing.append(name)
536 missing.sort()
537 maybe.sort()
538 return missing, maybe
Guido van Rossum03f7f082001-10-18 19:15:32 +0000539
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000540 def replace_paths_in_code(self, co):
541 new_filename = original_filename = os.path.normpath(co.co_filename)
Just van Rossume29310a2002-12-31 16:33:00 +0000542 for f, r in self.replace_paths:
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000543 if original_filename.startswith(f):
Just van Rossume29310a2002-12-31 16:33:00 +0000544 new_filename = r + original_filename[len(f):]
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000545 break
546
547 if self.debug and original_filename not in self.processed_paths:
Just van Rossume29310a2002-12-31 16:33:00 +0000548 if new_filename != original_filename:
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000549 self.msgout(2, "co_filename %r changed to %r" \
550 % (original_filename,new_filename,))
551 else:
552 self.msgout(2, "co_filename %r remains unchanged" \
553 % (original_filename,))
554 self.processed_paths.append(original_filename)
555
556 consts = list(co.co_consts)
557 for i in range(len(consts)):
558 if isinstance(consts[i], type(co)):
559 consts[i] = self.replace_paths_in_code(consts[i])
560
Berker Peksag0a0d1da2014-07-07 14:58:12 +0300561 return types.CodeType(co.co_argcount, co.co_kwonlyargcount,
562 co.co_nlocals, co.co_stacksize, co.co_flags,
563 co.co_code, tuple(consts), co.co_names,
564 co.co_varnames, new_filename, co.co_name,
565 co.co_firstlineno, co.co_lnotab, co.co_freevars,
566 co.co_cellvars)
Guido van Rossum6b767ac2001-03-20 20:43:34 +0000567
Guido van Rossum75dc4961998-03-05 03:42:00 +0000568
569def test():
570 # Parse command line
571 import getopt
572 try:
Guido van Rossumbaf06031998-08-25 14:06:55 +0000573 opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:")
Guido van Rossumb940e112007-01-10 16:19:56 +0000574 except getopt.error as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000575 print(msg)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000576 return
Guido van Rossum75dc4961998-03-05 03:42:00 +0000577
578 # Process options
579 debug = 1
580 domods = 0
581 addpath = []
Guido van Rossumbaf06031998-08-25 14:06:55 +0000582 exclude = []
Guido van Rossum75dc4961998-03-05 03:42:00 +0000583 for o, a in opts:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000584 if o == '-d':
585 debug = debug + 1
586 if o == '-m':
587 domods = 1
588 if o == '-p':
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000589 addpath = addpath + a.split(os.pathsep)
Guido van Rossum912a14c1998-03-05 04:56:37 +0000590 if o == '-q':
591 debug = 0
Guido van Rossumbaf06031998-08-25 14:06:55 +0000592 if o == '-x':
593 exclude.append(a)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000594
595 # Provide default arguments
596 if not args:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000597 script = "hello.py"
Guido van Rossum75dc4961998-03-05 03:42:00 +0000598 else:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000599 script = args[0]
Guido van Rossum75dc4961998-03-05 03:42:00 +0000600
601 # Set the path based on sys.path and the script directory
602 path = sys.path[:]
603 path[0] = os.path.dirname(script)
604 path = addpath + path
605 if debug > 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000606 print("path:")
Guido van Rossum912a14c1998-03-05 04:56:37 +0000607 for item in path:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000608 print(" ", repr(item))
Guido van Rossum75dc4961998-03-05 03:42:00 +0000609
610 # Create the module finder and turn its crank
Guido van Rossumbaf06031998-08-25 14:06:55 +0000611 mf = ModuleFinder(path, debug, exclude)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000612 for arg in args[1:]:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000613 if arg == '-m':
614 domods = 1
615 continue
616 if domods:
617 if arg[-2:] == '.*':
618 mf.import_hook(arg[:-2], None, ["*"])
619 else:
620 mf.import_hook(arg)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000621 else:
Guido van Rossum912a14c1998-03-05 04:56:37 +0000622 mf.load_file(arg)
Guido van Rossum75dc4961998-03-05 03:42:00 +0000623 mf.run_script(script)
624 mf.report()
Just van Rossume29310a2002-12-31 16:33:00 +0000625 return mf # for -i debugging
Guido van Rossum75dc4961998-03-05 03:42:00 +0000626
627
628if __name__ == '__main__':
629 try:
Just van Rossume29310a2002-12-31 16:33:00 +0000630 mf = test()
Guido van Rossum75dc4961998-03-05 03:42:00 +0000631 except KeyboardInterrupt:
Éric Araujo1e3a68d2011-07-28 23:35:29 +0200632 print("\n[interrupted]")