pkgutil.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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', 'get_data',
  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. try:
  157. filenames = os.listdir(self.path)
  158. except OSError:
  159. # ignore unreadable directories like import does
  160. filenames = []
  161. filenames.sort() # handle packages before same-named modules
  162. for fn in filenames:
  163. modname = inspect.getmodulename(fn)
  164. if modname=='__init__' or modname in yielded:
  165. continue
  166. path = os.path.join(self.path, fn)
  167. ispkg = False
  168. if not modname and os.path.isdir(path) and '.' not in fn:
  169. modname = fn
  170. try:
  171. dircontents = os.listdir(path)
  172. except OSError:
  173. # ignore unreadable directories like import does
  174. dircontents = []
  175. for fn in dircontents:
  176. subname = inspect.getmodulename(fn)
  177. if subname=='__init__':
  178. ispkg = True
  179. break
  180. else:
  181. continue # not a package
  182. if modname and '.' not in modname:
  183. yielded[modname] = 1
  184. yield prefix + modname, ispkg
  185. class ImpLoader:
  186. """PEP 302 Loader that wraps Python's "classic" import algorithm
  187. """
  188. code = source = None
  189. def __init__(self, fullname, file, filename, etc):
  190. self.file = file
  191. self.filename = filename
  192. self.fullname = fullname
  193. self.etc = etc
  194. def load_module(self, fullname):
  195. self._reopen()
  196. try:
  197. mod = imp.load_module(fullname, self.file, self.filename, self.etc)
  198. finally:
  199. if self.file:
  200. self.file.close()
  201. # Note: we don't set __loader__ because we want the module to look
  202. # normal; i.e. this is just a wrapper for standard import machinery
  203. return mod
  204. def get_data(self, pathname):
  205. return open(pathname, "rb").read()
  206. def _reopen(self):
  207. if self.file and self.file.closed:
  208. mod_type = self.etc[2]
  209. if mod_type==imp.PY_SOURCE:
  210. self.file = open(self.filename, 'rU')
  211. elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION):
  212. self.file = open(self.filename, 'rb')
  213. def _fix_name(self, fullname):
  214. if fullname is None:
  215. fullname = self.fullname
  216. elif fullname != self.fullname:
  217. raise ImportError("Loader for module %s cannot handle "
  218. "module %s" % (self.fullname, fullname))
  219. return fullname
  220. def is_package(self, fullname):
  221. fullname = self._fix_name(fullname)
  222. return self.etc[2]==imp.PKG_DIRECTORY
  223. def get_code(self, fullname=None):
  224. fullname = self._fix_name(fullname)
  225. if self.code is None:
  226. mod_type = self.etc[2]
  227. if mod_type==imp.PY_SOURCE:
  228. source = self.get_source(fullname)
  229. self.code = compile(source, self.filename, 'exec')
  230. elif mod_type==imp.PY_COMPILED:
  231. self._reopen()
  232. try:
  233. self.code = read_code(self.file)
  234. finally:
  235. self.file.close()
  236. elif mod_type==imp.PKG_DIRECTORY:
  237. self.code = self._get_delegate().get_code()
  238. return self.code
  239. def get_source(self, fullname=None):
  240. fullname = self._fix_name(fullname)
  241. if self.source is None:
  242. mod_type = self.etc[2]
  243. if mod_type==imp.PY_SOURCE:
  244. self._reopen()
  245. try:
  246. self.source = self.file.read()
  247. finally:
  248. self.file.close()
  249. elif mod_type==imp.PY_COMPILED:
  250. if os.path.exists(self.filename[:-1]):
  251. f = open(self.filename[:-1], 'rU')
  252. self.source = f.read()
  253. f.close()
  254. elif mod_type==imp.PKG_DIRECTORY:
  255. self.source = self._get_delegate().get_source()
  256. return self.source
  257. def _get_delegate(self):
  258. return ImpImporter(self.filename).find_module('__init__')
  259. def get_filename(self, fullname=None):
  260. fullname = self._fix_name(fullname)
  261. mod_type = self.etc[2]
  262. if self.etc[2]==imp.PKG_DIRECTORY:
  263. return self._get_delegate().get_filename()
  264. elif self.etc[2] in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION):
  265. return self.filename
  266. return None
  267. try:
  268. import zipimport
  269. from zipimport import zipimporter
  270. def iter_zipimport_modules(importer, prefix=''):
  271. dirlist = zipimport._zip_directory_cache[importer.archive].keys()
  272. dirlist.sort()
  273. _prefix = importer.prefix
  274. plen = len(_prefix)
  275. yielded = {}
  276. import inspect
  277. for fn in dirlist:
  278. if not fn.startswith(_prefix):
  279. continue
  280. fn = fn[plen:].split(os.sep)
  281. if len(fn)==2 and fn[1].startswith('__init__.py'):
  282. if fn[0] not in yielded:
  283. yielded[fn[0]] = 1
  284. yield fn[0], True
  285. if len(fn)!=1:
  286. continue
  287. modname = inspect.getmodulename(fn[0])
  288. if modname=='__init__':
  289. continue
  290. if modname and '.' not in modname and modname not in yielded:
  291. yielded[modname] = 1
  292. yield prefix + modname, False
  293. iter_importer_modules.register(zipimporter, iter_zipimport_modules)
  294. except ImportError:
  295. pass
  296. def get_importer(path_item):
  297. """Retrieve a PEP 302 importer for the given path item
  298. The returned importer is cached in sys.path_importer_cache
  299. if it was newly created by a path hook.
  300. If there is no importer, a wrapper around the basic import
  301. machinery is returned. This wrapper is never inserted into
  302. the importer cache (None is inserted instead).
  303. The cache (or part of it) can be cleared manually if a
  304. rescan of sys.path_hooks is necessary.
  305. """
  306. try:
  307. importer = sys.path_importer_cache[path_item]
  308. except KeyError:
  309. for path_hook in sys.path_hooks:
  310. try:
  311. importer = path_hook(path_item)
  312. break
  313. except ImportError:
  314. pass
  315. else:
  316. importer = None
  317. sys.path_importer_cache.setdefault(path_item, importer)
  318. if importer is None:
  319. try:
  320. importer = ImpImporter(path_item)
  321. except ImportError:
  322. importer = None
  323. return importer
  324. def iter_importers(fullname=""):
  325. """Yield PEP 302 importers for the given module name
  326. If fullname contains a '.', the importers will be for the package
  327. containing fullname, otherwise they will be importers for sys.meta_path,
  328. sys.path, and Python's "classic" import machinery, in that order. If
  329. the named module is in a package, that package is imported as a side
  330. effect of invoking this function.
  331. Non PEP 302 mechanisms (e.g. the Windows registry) used by the
  332. standard import machinery to find files in alternative locations
  333. are partially supported, but are searched AFTER sys.path. Normally,
  334. these locations are searched BEFORE sys.path, preventing sys.path
  335. entries from shadowing them.
  336. For this to cause a visible difference in behaviour, there must
  337. be a module or package name that is accessible via both sys.path
  338. and one of the non PEP 302 file system mechanisms. In this case,
  339. the emulation will find the former version, while the builtin
  340. import mechanism will find the latter.
  341. Items of the following types can be affected by this discrepancy:
  342. imp.C_EXTENSION, imp.PY_SOURCE, imp.PY_COMPILED, imp.PKG_DIRECTORY
  343. """
  344. if fullname.startswith('.'):
  345. raise ImportError("Relative module names not supported")
  346. if '.' in fullname:
  347. # Get the containing package's __path__
  348. pkg = '.'.join(fullname.split('.')[:-1])
  349. if pkg not in sys.modules:
  350. __import__(pkg)
  351. path = getattr(sys.modules[pkg], '__path__', None) or []
  352. else:
  353. for importer in sys.meta_path:
  354. yield importer
  355. path = sys.path
  356. for item in path:
  357. yield get_importer(item)
  358. if '.' not in fullname:
  359. yield ImpImporter()
  360. def get_loader(module_or_name):
  361. """Get a PEP 302 "loader" object for module_or_name
  362. If the module or package is accessible via the normal import
  363. mechanism, a wrapper around the relevant part of that machinery
  364. is returned. Returns None if the module cannot be found or imported.
  365. If the named module is not already imported, its containing package
  366. (if any) is imported, in order to establish the package __path__.
  367. This function uses iter_importers(), and is thus subject to the same
  368. limitations regarding platform-specific special import locations such
  369. as the Windows registry.
  370. """
  371. if module_or_name in sys.modules:
  372. module_or_name = sys.modules[module_or_name]
  373. if isinstance(module_or_name, ModuleType):
  374. module = module_or_name
  375. loader = getattr(module, '__loader__', None)
  376. if loader is not None:
  377. return loader
  378. fullname = module.__name__
  379. else:
  380. fullname = module_or_name
  381. return find_loader(fullname)
  382. def find_loader(fullname):
  383. """Find a PEP 302 "loader" object for fullname
  384. If fullname contains dots, path must be the containing package's __path__.
  385. Returns None if the module cannot be found or imported. This function uses
  386. iter_importers(), and is thus subject to the same limitations regarding
  387. platform-specific special import locations such as the Windows registry.
  388. """
  389. for importer in iter_importers(fullname):
  390. loader = importer.find_module(fullname)
  391. if loader is not None:
  392. return loader
  393. return None
  394. def extend_path(path, name):
  395. """Extend a package's path.
  396. Intended use is to place the following code in a package's __init__.py:
  397. from pkgutil import extend_path
  398. __path__ = extend_path(__path__, __name__)
  399. This will add to the package's __path__ all subdirectories of
  400. directories on sys.path named after the package. This is useful
  401. if one wants to distribute different parts of a single logical
  402. package as multiple directories.
  403. It also looks for *.pkg files beginning where * matches the name
  404. argument. This feature is similar to *.pth files (see site.py),
  405. except that it doesn't special-case lines starting with 'import'.
  406. A *.pkg file is trusted at face value: apart from checking for
  407. duplicates, all entries found in a *.pkg file are added to the
  408. path, regardless of whether they are exist the filesystem. (This
  409. is a feature.)
  410. If the input path is not a list (as is the case for frozen
  411. packages) it is returned unchanged. The input path is not
  412. modified; an extended copy is returned. Items are only appended
  413. to the copy at the end.
  414. It is assumed that sys.path is a sequence. Items of sys.path that
  415. are not (unicode or 8-bit) strings referring to existing
  416. directories are ignored. Unicode items of sys.path that cause
  417. errors when used as filenames may cause this function to raise an
  418. exception (in line with os.path.isdir() behavior).
  419. """
  420. if not isinstance(path, list):
  421. # This could happen e.g. when this is called from inside a
  422. # frozen package. Return the path unchanged in that case.
  423. return path
  424. pname = os.path.join(*name.split('.')) # Reconstitute as relative path
  425. # Just in case os.extsep != '.'
  426. sname = os.extsep.join(name.split('.'))
  427. sname_pkg = sname + os.extsep + "pkg"
  428. init_py = "__init__" + os.extsep + "py"
  429. path = path[:] # Start with a copy of the existing path
  430. for dir in sys.path:
  431. if not isinstance(dir, basestring) or not os.path.isdir(dir):
  432. continue
  433. subdir = os.path.join(dir, pname)
  434. # XXX This may still add duplicate entries to path on
  435. # case-insensitive filesystems
  436. initfile = os.path.join(subdir, init_py)
  437. if subdir not in path and os.path.isfile(initfile):
  438. path.append(subdir)
  439. # XXX Is this the right thing for subpackages like zope.app?
  440. # It looks for a file named "zope.app.pkg"
  441. pkgfile = os.path.join(dir, sname_pkg)
  442. if os.path.isfile(pkgfile):
  443. try:
  444. f = open(pkgfile)
  445. except IOError, msg:
  446. sys.stderr.write("Can't open %s: %s\n" %
  447. (pkgfile, msg))
  448. else:
  449. for line in f:
  450. line = line.rstrip('\n')
  451. if not line or line.startswith('#'):
  452. continue
  453. path.append(line) # Don't check for existence!
  454. f.close()
  455. return path
  456. def get_data(package, resource):
  457. """Get a resource from a package.
  458. This is a wrapper round the PEP 302 loader get_data API. The package
  459. argument should be the name of a package, in standard module format
  460. (foo.bar). The resource argument should be in the form of a relative
  461. filename, using '/' as the path separator. The parent directory name '..'
  462. is not allowed, and nor is a rooted name (starting with a '/').
  463. The function returns a binary string, which is the contents of the
  464. specified resource.
  465. For packages located in the filesystem, which have already been imported,
  466. this is the rough equivalent of
  467. d = os.path.dirname(sys.modules[package].__file__)
  468. data = open(os.path.join(d, resource), 'rb').read()
  469. If the package cannot be located or loaded, or it uses a PEP 302 loader
  470. which does not support get_data(), then None is returned.
  471. """
  472. loader = get_loader(package)
  473. if loader is None or not hasattr(loader, 'get_data'):
  474. return None
  475. mod = sys.modules.get(package) or loader.load_module(package)
  476. if mod is None or not hasattr(mod, '__file__'):
  477. return None
  478. # Modify the resource name to be compatible with the loader.get_data
  479. # signature - an os.path format "filename" starting with the dirname of
  480. # the package's __file__
  481. parts = resource.split('/')
  482. parts.insert(0, os.path.dirname(mod.__file__))
  483. resource_name = os.path.join(*parts)
  484. return loader.get_data(resource_name)