/Lib/pkgutil.py

http://unladen-swallow.googlecode.com/ · Python · 583 lines · 480 code · 53 blank · 50 comment · 78 complexity · 29a1afd294aa31abd0125fdbefa2217e MD5 · raw file

  1. """Utilities to support packages."""
  2. # NOTE: This module must remain compatible with Python 2.3, as it is shared
  3. # by setuptools for distribution with Python 2.3 and up.
  4. import os
  5. import sys
  6. import imp
  7. import os.path
  8. from types import ModuleType
  9. __all__ = [
  10. 'get_importer', 'iter_importers', 'get_loader', 'find_loader',
  11. 'walk_packages', 'iter_modules',
  12. 'ImpImporter', 'ImpLoader', 'read_code', 'extend_path',
  13. ]
  14. def read_code(stream):
  15. # This helper is needed in order for the PEP 302 emulation to
  16. # correctly handle compiled files
  17. import marshal
  18. magic = stream.read(4)
  19. if magic != imp.get_magic():
  20. return None
  21. stream.read(4) # Skip timestamp
  22. return marshal.load(stream)
  23. def simplegeneric(func):
  24. """Make a trivial single-dispatch generic function"""
  25. registry = {}
  26. def wrapper(*args, **kw):
  27. ob = args[0]
  28. try:
  29. cls = ob.__class__
  30. except AttributeError:
  31. cls = type(ob)
  32. try:
  33. mro = cls.__mro__
  34. except AttributeError:
  35. try:
  36. class cls(cls, object):
  37. pass
  38. mro = cls.__mro__[1:]
  39. except TypeError:
  40. mro = object, # must be an ExtensionClass or some such :(
  41. for t in mro:
  42. if t in registry:
  43. return registry[t](*args, **kw)
  44. else:
  45. return func(*args, **kw)
  46. try:
  47. wrapper.__name__ = func.__name__
  48. except (TypeError, AttributeError):
  49. pass # Python 2.3 doesn't allow functions to be renamed
  50. def register(typ, func=None):
  51. if func is None:
  52. return lambda f: register(typ, f)
  53. registry[typ] = func
  54. return func
  55. wrapper.__dict__ = func.__dict__
  56. wrapper.__doc__ = func.__doc__
  57. wrapper.register = register
  58. return wrapper
  59. def walk_packages(path=None, prefix='', onerror=None):
  60. """Yields (module_loader, name, ispkg) for all modules recursively
  61. on path, or, if path is None, all accessible modules.
  62. 'path' should be either None or a list of paths to look for
  63. modules in.
  64. 'prefix' is a string to output on the front of every module name
  65. on output.
  66. Note that this function must import all *packages* (NOT all
  67. modules!) on the given path, in order to access the __path__
  68. attribute to find submodules.
  69. 'onerror' is a function which gets called with one argument (the
  70. name of the package which was being imported) if any exception
  71. occurs while trying to import a package. If no onerror function is
  72. supplied, ImportErrors are caught and ignored, while all other
  73. exceptions are propagated, terminating the search.
  74. Examples:
  75. # list all modules python can access
  76. walk_packages()
  77. # list all submodules of ctypes
  78. walk_packages(ctypes.__path__, ctypes.__name__+'.')
  79. """
  80. def seen(p, m={}):
  81. if p in m:
  82. return True
  83. m[p] = True
  84. for importer, name, ispkg in iter_modules(path, prefix):
  85. yield importer, name, ispkg
  86. if ispkg:
  87. try:
  88. __import__(name)
  89. except ImportError:
  90. if onerror is not None:
  91. onerror(name)
  92. except Exception:
  93. if onerror is not None:
  94. onerror(name)
  95. else:
  96. raise
  97. else:
  98. path = getattr(sys.modules[name], '__path__', None) or []
  99. # don't traverse path items we've seen before
  100. path = [p for p in path if not seen(p)]
  101. for item in walk_packages(path, name+'.', onerror):
  102. yield item
  103. def iter_modules(path=None, prefix=''):
  104. """Yields (module_loader, name, ispkg) for all submodules on path,
  105. or, if path is None, all top-level modules on sys.path.
  106. 'path' should be either None or a list of paths to look for
  107. modules in.
  108. 'prefix' is a string to output on the front of every module name
  109. on output.
  110. """
  111. if path is None:
  112. importers = iter_importers()
  113. else:
  114. importers = map(get_importer, path)
  115. yielded = {}
  116. for i in importers:
  117. for name, ispkg in iter_importer_modules(i, prefix):
  118. if name not in yielded:
  119. yielded[name] = 1
  120. yield i, name, ispkg
  121. #@simplegeneric
  122. def iter_importer_modules(importer, prefix=''):
  123. if not hasattr(importer, 'iter_modules'):
  124. return []
  125. return importer.iter_modules(prefix)
  126. iter_importer_modules = simplegeneric(iter_importer_modules)
  127. class ImpImporter:
  128. """PEP 302 Importer that wraps Python's "classic" import algorithm
  129. ImpImporter(dirname) produces a PEP 302 importer that searches that
  130. directory. ImpImporter(None) produces a PEP 302 importer that searches
  131. the current sys.path, plus any modules that are frozen or built-in.
  132. Note that ImpImporter does not currently support being used by placement
  133. on sys.meta_path.
  134. """
  135. def __init__(self, path=None):
  136. self.path = path
  137. def find_module(self, fullname, path=None):
  138. # Note: we ignore 'path' argument since it is only used via meta_path
  139. subname = fullname.split(".")[-1]
  140. if subname != fullname and self.path is None:
  141. return None
  142. if self.path is None:
  143. path = None
  144. else:
  145. path = [os.path.realpath(self.path)]
  146. try:
  147. file, filename, etc = imp.find_module(subname, path)
  148. except ImportError:
  149. return None
  150. return ImpLoader(fullname, file, filename, etc)
  151. def iter_modules(self, prefix=''):
  152. if self.path is None or not os.path.isdir(self.path):
  153. return
  154. yielded = {}
  155. import inspect
  156. filenames = os.listdir(self.path)
  157. filenames.sort() # handle packages before same-named modules
  158. for fn in filenames:
  159. modname = inspect.getmodulename(fn)
  160. if modname=='__init__' or modname in yielded:
  161. continue
  162. path = os.path.join(self.path, fn)
  163. ispkg = False
  164. if not modname and os.path.isdir(path) and '.' not in fn:
  165. modname = fn
  166. for fn in os.listdir(path):
  167. subname = inspect.getmodulename(fn)
  168. if subname=='__init__':
  169. ispkg = True
  170. break
  171. else:
  172. continue # not a package
  173. if modname and '.' not in modname:
  174. yielded[modname] = 1
  175. yield prefix + modname, ispkg
  176. class ImpLoader:
  177. """PEP 302 Loader that wraps Python's "classic" import algorithm
  178. """
  179. code = source = None
  180. def __init__(self, fullname, file, filename, etc):
  181. self.file = file
  182. self.filename = filename
  183. self.fullname = fullname
  184. self.etc = etc
  185. def load_module(self, fullname):
  186. self._reopen()
  187. try:
  188. mod = imp.load_module(fullname, self.file, self.filename, self.etc)
  189. finally:
  190. if self.file:
  191. self.file.close()
  192. # Note: we don't set __loader__ because we want the module to look
  193. # normal; i.e. this is just a wrapper for standard import machinery
  194. return mod
  195. def get_data(self, pathname):
  196. return open(pathname, "rb").read()
  197. def _reopen(self):
  198. if self.file and self.file.closed:
  199. mod_type = self.etc[2]
  200. if mod_type==imp.PY_SOURCE:
  201. self.file = open(self.filename, 'rU')
  202. elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION):
  203. self.file = open(self.filename, 'rb')
  204. def _fix_name(self, fullname):
  205. if fullname is None:
  206. fullname = self.fullname
  207. elif fullname != self.fullname:
  208. raise ImportError("Loader for module %s cannot handle "
  209. "module %s" % (self.fullname, fullname))
  210. return fullname
  211. def is_package(self, fullname):
  212. fullname = self._fix_name(fullname)
  213. return self.etc[2]==imp.PKG_DIRECTORY
  214. def get_code(self, fullname=None):
  215. fullname = self._fix_name(fullname)
  216. if self.code is None:
  217. mod_type = self.etc[2]
  218. if mod_type==imp.PY_SOURCE:
  219. source = self.get_source(fullname)
  220. self.code = compile(source, self.filename, 'exec')
  221. elif mod_type==imp.PY_COMPILED:
  222. self._reopen()
  223. try:
  224. self.code = read_code(self.file)
  225. finally:
  226. self.file.close()
  227. elif mod_type==imp.PKG_DIRECTORY:
  228. self.code = self._get_delegate().get_code()
  229. return self.code
  230. def get_source(self, fullname=None):
  231. fullname = self._fix_name(fullname)
  232. if self.source is None:
  233. mod_type = self.etc[2]
  234. if mod_type==imp.PY_SOURCE:
  235. self._reopen()
  236. try:
  237. self.source = self.file.read()
  238. finally:
  239. self.file.close()
  240. elif mod_type==imp.PY_COMPILED:
  241. if os.path.exists(self.filename[:-1]):
  242. f = open(self.filename[:-1], 'rU')
  243. self.source = f.read()
  244. f.close()
  245. elif mod_type==imp.PKG_DIRECTORY:
  246. self.source = self._get_delegate().get_source()
  247. return self.source
  248. def _get_delegate(self):
  249. return ImpImporter(self.filename).find_module('__init__')
  250. def get_filename(self, fullname=None):
  251. fullname = self._fix_name(fullname)
  252. mod_type = self.etc[2]
  253. if self.etc[2]==imp.PKG_DIRECTORY:
  254. return self._get_delegate().get_filename()
  255. elif self.etc[2] in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION):
  256. return self.filename
  257. return None
  258. try:
  259. import zipimport
  260. from zipimport import zipimporter
  261. def iter_zipimport_modules(importer, prefix=''):
  262. dirlist = zipimport._zip_directory_cache[importer.archive].keys()
  263. dirlist.sort()
  264. _prefix = importer.prefix
  265. plen = len(_prefix)
  266. yielded = {}
  267. import inspect
  268. for fn in dirlist:
  269. if not fn.startswith(_prefix):
  270. continue
  271. fn = fn[plen:].split(os.sep)
  272. if len(fn)==2 and fn[1].startswith('__init__.py'):
  273. if fn[0] not in yielded:
  274. yielded[fn[0]] = 1
  275. yield fn[0], True
  276. if len(fn)!=1:
  277. continue
  278. modname = inspect.getmodulename(fn[0])
  279. if modname=='__init__':
  280. continue
  281. if modname and '.' not in modname and modname not in yielded:
  282. yielded[modname] = 1
  283. yield prefix + modname, False
  284. iter_importer_modules.register(zipimporter, iter_zipimport_modules)
  285. except ImportError:
  286. pass
  287. def get_importer(path_item):
  288. """Retrieve a PEP 302 importer for the given path item
  289. The returned importer is cached in sys.path_importer_cache
  290. if it was newly created by a path hook.
  291. If there is no importer, a wrapper around the basic import
  292. machinery is returned. This wrapper is never inserted into
  293. the importer cache (None is inserted instead).
  294. The cache (or part of it) can be cleared manually if a
  295. rescan of sys.path_hooks is necessary.
  296. """
  297. try:
  298. importer = sys.path_importer_cache[path_item]
  299. except KeyError:
  300. for path_hook in sys.path_hooks:
  301. try:
  302. importer = path_hook(path_item)
  303. break
  304. except ImportError:
  305. pass
  306. else:
  307. importer = None
  308. sys.path_importer_cache.setdefault(path_item, importer)
  309. if importer is None:
  310. try:
  311. importer = ImpImporter(path_item)
  312. except ImportError:
  313. importer = None
  314. return importer
  315. def iter_importers(fullname=""):
  316. """Yield PEP 302 importers for the given module name
  317. If fullname contains a '.', the importers will be for the package
  318. containing fullname, otherwise they will be importers for sys.meta_path,
  319. sys.path, and Python's "classic" import machinery, in that order. If
  320. the named module is in a package, that package is imported as a side
  321. effect of invoking this function.
  322. Non PEP 302 mechanisms (e.g. the Windows registry) used by the
  323. standard import machinery to find files in alternative locations
  324. are partially supported, but are searched AFTER sys.path. Normally,
  325. these locations are searched BEFORE sys.path, preventing sys.path
  326. entries from shadowing them.
  327. For this to cause a visible difference in behaviour, there must
  328. be a module or package name that is accessible via both sys.path
  329. and one of the non PEP 302 file system mechanisms. In this case,
  330. the emulation will find the former version, while the builtin
  331. import mechanism will find the latter.
  332. Items of the following types can be affected by this discrepancy:
  333. imp.C_EXTENSION, imp.PY_SOURCE, imp.PY_COMPILED, imp.PKG_DIRECTORY
  334. """
  335. if fullname.startswith('.'):
  336. raise ImportError("Relative module names not supported")
  337. if '.' in fullname:
  338. # Get the containing package's __path__
  339. pkg = '.'.join(fullname.split('.')[:-1])
  340. if pkg not in sys.modules:
  341. __import__(pkg)
  342. path = getattr(sys.modules[pkg], '__path__', None) or []
  343. else:
  344. for importer in sys.meta_path:
  345. yield importer
  346. path = sys.path
  347. for item in path:
  348. yield get_importer(item)
  349. if '.' not in fullname:
  350. yield ImpImporter()
  351. def get_loader(module_or_name):
  352. """Get a PEP 302 "loader" object for module_or_name
  353. If the module or package is accessible via the normal import
  354. mechanism, a wrapper around the relevant part of that machinery
  355. is returned. Returns None if the module cannot be found or imported.
  356. If the named module is not already imported, its containing package
  357. (if any) is imported, in order to establish the package __path__.
  358. This function uses iter_importers(), and is thus subject to the same
  359. limitations regarding platform-specific special import locations such
  360. as the Windows registry.
  361. """
  362. if module_or_name in sys.modules:
  363. module_or_name = sys.modules[module_or_name]
  364. if isinstance(module_or_name, ModuleType):
  365. module = module_or_name
  366. loader = getattr(module, '__loader__', None)
  367. if loader is not None:
  368. return loader
  369. fullname = module.__name__
  370. else:
  371. fullname = module_or_name
  372. return find_loader(fullname)
  373. def find_loader(fullname):
  374. """Find a PEP 302 "loader" object for fullname
  375. If fullname contains dots, path must be the containing package's __path__.
  376. Returns None if the module cannot be found or imported. This function uses
  377. iter_importers(), and is thus subject to the same limitations regarding
  378. platform-specific special import locations such as the Windows registry.
  379. """
  380. for importer in iter_importers(fullname):
  381. loader = importer.find_module(fullname)
  382. if loader is not None:
  383. return loader
  384. return None
  385. def extend_path(path, name):
  386. """Extend a package's path.
  387. Intended use is to place the following code in a package's __init__.py:
  388. from pkgutil import extend_path
  389. __path__ = extend_path(__path__, __name__)
  390. This will add to the package's __path__ all subdirectories of
  391. directories on sys.path named after the package. This is useful
  392. if one wants to distribute different parts of a single logical
  393. package as multiple directories.
  394. It also looks for *.pkg files beginning where * matches the name
  395. argument. This feature is similar to *.pth files (see site.py),
  396. except that it doesn't special-case lines starting with 'import'.
  397. A *.pkg file is trusted at face value: apart from checking for
  398. duplicates, all entries found in a *.pkg file are added to the
  399. path, regardless of whether they are exist the filesystem. (This
  400. is a feature.)
  401. If the input path is not a list (as is the case for frozen
  402. packages) it is returned unchanged. The input path is not
  403. modified; an extended copy is returned. Items are only appended
  404. to the copy at the end.
  405. It is assumed that sys.path is a sequence. Items of sys.path that
  406. are not (unicode or 8-bit) strings referring to existing
  407. directories are ignored. Unicode items of sys.path that cause
  408. errors when used as filenames may cause this function to raise an
  409. exception (in line with os.path.isdir() behavior).
  410. """
  411. if not isinstance(path, list):
  412. # This could happen e.g. when this is called from inside a
  413. # frozen package. Return the path unchanged in that case.
  414. return path
  415. pname = os.path.join(*name.split('.')) # Reconstitute as relative path
  416. # Just in case os.extsep != '.'
  417. sname = os.extsep.join(name.split('.'))
  418. sname_pkg = sname + os.extsep + "pkg"
  419. init_py = "__init__" + os.extsep + "py"
  420. path = path[:] # Start with a copy of the existing path
  421. for dir in sys.path:
  422. if not isinstance(dir, basestring) or not os.path.isdir(dir):
  423. continue
  424. subdir = os.path.join(dir, pname)
  425. # XXX This may still add duplicate entries to path on
  426. # case-insensitive filesystems
  427. initfile = os.path.join(subdir, init_py)
  428. if subdir not in path and os.path.isfile(initfile):
  429. path.append(subdir)
  430. # XXX Is this the right thing for subpackages like zope.app?
  431. # It looks for a file named "zope.app.pkg"
  432. pkgfile = os.path.join(dir, sname_pkg)
  433. if os.path.isfile(pkgfile):
  434. try:
  435. f = open(pkgfile)
  436. except IOError, msg:
  437. sys.stderr.write("Can't open %s: %s\n" %
  438. (pkgfile, msg))
  439. else:
  440. for line in f:
  441. line = line.rstrip('\n')
  442. if not line or line.startswith('#'):
  443. continue
  444. path.append(line) # Don't check for existence!
  445. f.close()
  446. return path
  447. def get_data(package, resource):
  448. """Get a resource from a package.
  449. This is a wrapper round the PEP 302 loader get_data API. The package
  450. argument should be the name of a package, in standard module format
  451. (foo.bar). The resource argument should be in the form of a relative
  452. filename, using '/' as the path separator. The parent directory name '..'
  453. is not allowed, and nor is a rooted name (starting with a '/').
  454. The function returns a binary string, which is the contents of the
  455. specified resource.
  456. For packages located in the filesystem, which have already been imported,
  457. this is the rough equivalent of
  458. d = os.path.dirname(sys.modules[package].__file__)
  459. data = open(os.path.join(d, resource), 'rb').read()
  460. If the package cannot be located or loaded, or it uses a PEP 302 loader
  461. which does not support get_data(), then None is returned.
  462. """
  463. loader = get_loader(package)
  464. if loader is None or not hasattr(loader, 'get_data'):
  465. return None
  466. mod = sys.modules.get(package) or loader.load_module(package)
  467. if mod is None or not hasattr(mod, '__file__'):
  468. return None
  469. # Modify the resource name to be compatible with the loader.get_data
  470. # signature - an os.path format "filename" starting with the dirname of
  471. # the package's __file__
  472. parts = resource.split('/')
  473. parts.insert(0, os.path.dirname(mod.__file__))
  474. resource_name = os.path.join(*parts)
  475. return loader.get_data(resource_name)