modutils.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. # -*- coding: utf-8 -*-
  2. # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  3. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  4. #
  5. # This file is part of logilab-common.
  6. #
  7. # logilab-common is free software: you can redistribute it and/or modify it under
  8. # the terms of the GNU Lesser General Public License as published by the Free
  9. # Software Foundation, either version 2.1 of the License, or (at your option) any
  10. # later version.
  11. #
  12. # logilab-common is distributed in the hope that it will be useful, but WITHOUT
  13. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  15. # details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License along
  18. # with logilab-common. If not, see <http://www.gnu.org/licenses/>.
  19. """Python modules manipulation utility functions.
  20. :type PY_SOURCE_EXTS: tuple(str)
  21. :var PY_SOURCE_EXTS: list of possible python source file extension
  22. :type STD_LIB_DIR: str
  23. :var STD_LIB_DIR: directory where standard modules are located
  24. :type BUILTIN_MODULES: dict
  25. :var BUILTIN_MODULES: dictionary with builtin module names has key
  26. """
  27. __docformat__ = "restructuredtext en"
  28. import sys
  29. import os
  30. from os.path import splitext, join, abspath, isdir, dirname, exists, basename
  31. from imp import find_module, load_module, C_BUILTIN, PY_COMPILED, PKG_DIRECTORY
  32. from distutils.sysconfig import get_config_var, get_python_lib, get_python_version
  33. try:
  34. import zipimport
  35. except ImportError:
  36. zipimport = None
  37. ZIPFILE = object()
  38. from logilab.common import STD_BLACKLIST, _handle_blacklist
  39. # Notes about STD_LIB_DIR
  40. # Consider arch-specific installation for STD_LIB_DIR definition
  41. # :mod:`distutils.sysconfig` contains to much hardcoded values to rely on
  42. #
  43. # :see: `Problems with /usr/lib64 builds <http://bugs.python.org/issue1294959>`_
  44. # :see: `FHS <http://www.pathname.com/fhs/pub/fhs-2.3.html#LIBLTQUALGTALTERNATEFORMATESSENTIAL>`_
  45. if sys.platform.startswith('win'):
  46. PY_SOURCE_EXTS = ('py', 'pyw')
  47. PY_COMPILED_EXTS = ('dll', 'pyd')
  48. STD_LIB_DIR = get_python_lib(standard_lib=1)
  49. else:
  50. PY_SOURCE_EXTS = ('py',)
  51. PY_COMPILED_EXTS = ('so',)
  52. # extend lib dir with some arch-dependant paths
  53. STD_LIB_DIR = join(get_config_var("LIBDIR"), "python%s" % get_python_version())
  54. BUILTIN_MODULES = dict(zip(sys.builtin_module_names,
  55. [1]*len(sys.builtin_module_names)))
  56. class NoSourceFile(Exception):
  57. """exception raised when we are not able to get a python
  58. source file for a precompiled file
  59. """
  60. class LazyObject(object):
  61. def __init__(self, module, obj):
  62. self.module = module
  63. self.obj = obj
  64. self._imported = None
  65. def _getobj(self):
  66. if self._imported is None:
  67. self._imported = getattr(load_module_from_name(self.module),
  68. self.obj)
  69. return self._imported
  70. def __getattribute__(self, attr):
  71. try:
  72. return super(LazyObject, self).__getattribute__(attr)
  73. except AttributeError, ex:
  74. return getattr(self._getobj(), attr)
  75. def __call__(self, *args, **kwargs):
  76. return self._getobj()(*args, **kwargs)
  77. def load_module_from_name(dotted_name, path=None, use_sys=1):
  78. """Load a Python module from its name.
  79. :type dotted_name: str
  80. :param dotted_name: python name of a module or package
  81. :type path: list or None
  82. :param path:
  83. optional list of path where the module or package should be
  84. searched (use sys.path if nothing or None is given)
  85. :type use_sys: bool
  86. :param use_sys:
  87. boolean indicating whether the sys.modules dictionary should be
  88. used or not
  89. :raise ImportError: if the module or package is not found
  90. :rtype: module
  91. :return: the loaded module
  92. """
  93. return load_module_from_modpath(dotted_name.split('.'), path, use_sys)
  94. def load_module_from_modpath(parts, path=None, use_sys=1):
  95. """Load a python module from its splitted name.
  96. :type parts: list(str) or tuple(str)
  97. :param parts:
  98. python name of a module or package splitted on '.'
  99. :type path: list or None
  100. :param path:
  101. optional list of path where the module or package should be
  102. searched (use sys.path if nothing or None is given)
  103. :type use_sys: bool
  104. :param use_sys:
  105. boolean indicating whether the sys.modules dictionary should be used or not
  106. :raise ImportError: if the module or package is not found
  107. :rtype: module
  108. :return: the loaded module
  109. """
  110. if use_sys:
  111. try:
  112. return sys.modules['.'.join(parts)]
  113. except KeyError:
  114. pass
  115. modpath = []
  116. prevmodule = None
  117. for part in parts:
  118. modpath.append(part)
  119. curname = '.'.join(modpath)
  120. module = None
  121. if len(modpath) != len(parts):
  122. # even with use_sys=False, should try to get outer packages from sys.modules
  123. module = sys.modules.get(curname)
  124. if module is None:
  125. mp_file, mp_filename, mp_desc = find_module(part, path)
  126. module = load_module(curname, mp_file, mp_filename, mp_desc)
  127. if prevmodule:
  128. setattr(prevmodule, part, module)
  129. _file = getattr(module, '__file__', '')
  130. if not _file and len(modpath) != len(parts):
  131. raise ImportError('no module in %s' % '.'.join(parts[len(modpath):]) )
  132. path = [dirname( _file )]
  133. prevmodule = module
  134. return module
  135. def load_module_from_file(filepath, path=None, use_sys=1, extrapath=None):
  136. """Load a Python module from it's path.
  137. :type filepath: str
  138. :param filepath: path to the python module or package
  139. :type path: list or None
  140. :param path:
  141. optional list of path where the module or package should be
  142. searched (use sys.path if nothing or None is given)
  143. :type use_sys: bool
  144. :param use_sys:
  145. boolean indicating whether the sys.modules dictionary should be
  146. used or not
  147. :raise ImportError: if the module or package is not found
  148. :rtype: module
  149. :return: the loaded module
  150. """
  151. modpath = modpath_from_file(filepath, extrapath)
  152. return load_module_from_modpath(modpath, path, use_sys)
  153. def _check_init(path, mod_path):
  154. """check there are some __init__.py all along the way"""
  155. for part in mod_path:
  156. path = join(path, part)
  157. if not _has_init(path):
  158. return False
  159. return True
  160. def modpath_from_file(filename, extrapath=None):
  161. """given a file path return the corresponding splitted module's name
  162. (i.e name of a module or package splitted on '.')
  163. :type filename: str
  164. :param filename: file's path for which we want the module's name
  165. :type extrapath: dict
  166. :param extrapath:
  167. optional extra search path, with path as key and package name for the path
  168. as value. This is usually useful to handle package splitted in multiple
  169. directories using __path__ trick.
  170. :raise ImportError:
  171. if the corresponding module's name has not been found
  172. :rtype: list(str)
  173. :return: the corresponding splitted module's name
  174. """
  175. base = splitext(abspath(filename))[0]
  176. if extrapath is not None:
  177. for path_ in extrapath:
  178. path = abspath(path_)
  179. if path and base[:len(path)] == path:
  180. submodpath = [pkg for pkg in base[len(path):].split(os.sep)
  181. if pkg]
  182. if _check_init(path, submodpath[:-1]):
  183. return extrapath[path_].split('.') + submodpath
  184. for path in sys.path:
  185. path = abspath(path)
  186. if path and base[:len(path)] == path:
  187. if filename.find('site-packages') != -1 and \
  188. path.find('site-packages') == -1:
  189. continue
  190. modpath = [pkg for pkg in base[len(path):].split(os.sep) if pkg]
  191. if _check_init(path, modpath[:-1]):
  192. return modpath
  193. raise ImportError('Unable to find module for %s in %s' % (
  194. filename, ', \n'.join(sys.path)))
  195. def file_from_modpath(modpath, path=None, context_file=None):
  196. """given a mod path (i.e. splitted module / package name), return the
  197. corresponding file, giving priority to source file over precompiled
  198. file if it exists
  199. :type modpath: list or tuple
  200. :param modpath:
  201. splitted module's name (i.e name of a module or package splitted
  202. on '.')
  203. (this means explicit relative imports that start with dots have
  204. empty strings in this list!)
  205. :type path: list or None
  206. :param path:
  207. optional list of path where the module or package should be
  208. searched (use sys.path if nothing or None is given)
  209. :type context_file: str or None
  210. :param context_file:
  211. context file to consider, necessary if the identifier has been
  212. introduced using a relative import unresolvable in the actual
  213. context (i.e. modutils)
  214. :raise ImportError: if there is no such module in the directory
  215. :rtype: str or None
  216. :return:
  217. the path to the module's file or None if it's an integrated
  218. builtin module such as 'sys'
  219. """
  220. if context_file is not None:
  221. context = dirname(context_file)
  222. else:
  223. context = context_file
  224. if modpath[0] == 'xml':
  225. # handle _xmlplus
  226. try:
  227. return _file_from_modpath(['_xmlplus'] + modpath[1:], path, context)
  228. except ImportError:
  229. return _file_from_modpath(modpath, path, context)
  230. elif modpath == ['os', 'path']:
  231. # FIXME: currently ignoring search_path...
  232. return os.path.__file__
  233. return _file_from_modpath(modpath, path, context)
  234. def get_module_part(dotted_name, context_file=None):
  235. """given a dotted name return the module part of the name :
  236. >>> get_module_part('logilab.common.modutils.get_module_part')
  237. 'logilab.common.modutils'
  238. :type dotted_name: str
  239. :param dotted_name: full name of the identifier we are interested in
  240. :type context_file: str or None
  241. :param context_file:
  242. context file to consider, necessary if the identifier has been
  243. introduced using a relative import unresolvable in the actual
  244. context (i.e. modutils)
  245. :raise ImportError: if there is no such module in the directory
  246. :rtype: str or None
  247. :return:
  248. the module part of the name or None if we have not been able at
  249. all to import the given name
  250. XXX: deprecated, since it doesn't handle package precedence over module
  251. (see #10066)
  252. """
  253. # os.path trick
  254. if dotted_name.startswith('os.path'):
  255. return 'os.path'
  256. parts = dotted_name.split('.')
  257. if context_file is not None:
  258. # first check for builtin module which won't be considered latter
  259. # in that case (path != None)
  260. if parts[0] in BUILTIN_MODULES:
  261. if len(parts) > 2:
  262. raise ImportError(dotted_name)
  263. return parts[0]
  264. # don't use += or insert, we want a new list to be created !
  265. path = None
  266. starti = 0
  267. if parts[0] == '':
  268. assert context_file is not None, \
  269. 'explicit relative import, but no context_file?'
  270. path = [] # prevent resolving the import non-relatively
  271. starti = 1
  272. while parts[starti] == '': # for all further dots: change context
  273. starti += 1
  274. context_file = dirname(context_file)
  275. for i in range(starti, len(parts)):
  276. try:
  277. file_from_modpath(parts[starti:i+1],
  278. path=path, context_file=context_file)
  279. except ImportError:
  280. if not i >= max(1, len(parts) - 2):
  281. raise
  282. return '.'.join(parts[:i])
  283. return dotted_name
  284. def get_modules(package, src_directory, blacklist=STD_BLACKLIST):
  285. """given a package directory return a list of all available python
  286. modules in the package and its subpackages
  287. :type package: str
  288. :param package: the python name for the package
  289. :type src_directory: str
  290. :param src_directory:
  291. path of the directory corresponding to the package
  292. :type blacklist: list or tuple
  293. :param blacklist:
  294. optional list of files or directory to ignore, default to
  295. the value of `logilab.common.STD_BLACKLIST`
  296. :rtype: list
  297. :return:
  298. the list of all available python modules in the package and its
  299. subpackages
  300. """
  301. modules = []
  302. for directory, dirnames, filenames in os.walk(src_directory):
  303. _handle_blacklist(blacklist, dirnames, filenames)
  304. # check for __init__.py
  305. if not '__init__.py' in filenames:
  306. dirnames[:] = ()
  307. continue
  308. if directory != src_directory:
  309. dir_package = directory[len(src_directory):].replace(os.sep, '.')
  310. modules.append(package + dir_package)
  311. for filename in filenames:
  312. if _is_python_file(filename) and filename != '__init__.py':
  313. src = join(directory, filename)
  314. module = package + src[len(src_directory):-3]
  315. modules.append(module.replace(os.sep, '.'))
  316. return modules
  317. def get_module_files(src_directory, blacklist=STD_BLACKLIST):
  318. """given a package directory return a list of all available python
  319. module's files in the package and its subpackages
  320. :type src_directory: str
  321. :param src_directory:
  322. path of the directory corresponding to the package
  323. :type blacklist: list or tuple
  324. :param blacklist:
  325. optional list of files or directory to ignore, default to the value of
  326. `logilab.common.STD_BLACKLIST`
  327. :rtype: list
  328. :return:
  329. the list of all available python module's files in the package and
  330. its subpackages
  331. """
  332. files = []
  333. for directory, dirnames, filenames in os.walk(src_directory):
  334. _handle_blacklist(blacklist, dirnames, filenames)
  335. # check for __init__.py
  336. if not '__init__.py' in filenames:
  337. dirnames[:] = ()
  338. continue
  339. for filename in filenames:
  340. if _is_python_file(filename):
  341. src = join(directory, filename)
  342. files.append(src)
  343. return files
  344. def get_source_file(filename, include_no_ext=False):
  345. """given a python module's file name return the matching source file
  346. name (the filename will be returned identically if it's a already an
  347. absolute path to a python source file...)
  348. :type filename: str
  349. :param filename: python module's file name
  350. :raise NoSourceFile: if no source file exists on the file system
  351. :rtype: str
  352. :return: the absolute path of the source file if it exists
  353. """
  354. base, orig_ext = splitext(abspath(filename))
  355. for ext in PY_SOURCE_EXTS:
  356. source_path = '%s.%s' % (base, ext)
  357. if exists(source_path):
  358. return source_path
  359. if include_no_ext and not orig_ext and exists(base):
  360. return base
  361. raise NoSourceFile(filename)
  362. def cleanup_sys_modules(directories):
  363. """remove submodules of `directories` from `sys.modules`"""
  364. for modname, module in sys.modules.items():
  365. modfile = getattr(module, '__file__', None)
  366. if modfile:
  367. for directory in directories:
  368. if modfile.startswith(directory):
  369. del sys.modules[modname]
  370. break
  371. def is_python_source(filename):
  372. """
  373. rtype: bool
  374. return: True if the filename is a python source file
  375. """
  376. return splitext(filename)[1][1:] in PY_SOURCE_EXTS
  377. def is_standard_module(modname, std_path=(STD_LIB_DIR,)):
  378. """try to guess if a module is a standard python module (by default,
  379. see `std_path` parameter's description)
  380. :type modname: str
  381. :param modname: name of the module we are interested in
  382. :type std_path: list(str) or tuple(str)
  383. :param std_path: list of path considered has standard
  384. :rtype: bool
  385. :return:
  386. true if the module:
  387. - is located on the path listed in one of the directory in `std_path`
  388. - is a built-in module
  389. """
  390. modname = modname.split('.')[0]
  391. try:
  392. filename = file_from_modpath([modname])
  393. except ImportError, ex:
  394. # import failed, i'm probably not so wrong by supposing it's
  395. # not standard...
  396. return 0
  397. # modules which are not living in a file are considered standard
  398. # (sys and __builtin__ for instance)
  399. if filename is None:
  400. return 1
  401. filename = abspath(filename)
  402. for path in std_path:
  403. path = abspath(path)
  404. if filename.startswith(path):
  405. pfx_len = len(path)
  406. if filename[pfx_len+1:pfx_len+14] != 'site-packages':
  407. return 1
  408. return 0
  409. return False
  410. def is_relative(modname, from_file):
  411. """return true if the given module name is relative to the given
  412. file name
  413. :type modname: str
  414. :param modname: name of the module we are interested in
  415. :type from_file: str
  416. :param from_file:
  417. path of the module from which modname has been imported
  418. :rtype: bool
  419. :return:
  420. true if the module has been imported relatively to `from_file`
  421. """
  422. if not isdir(from_file):
  423. from_file = dirname(from_file)
  424. if from_file in sys.path:
  425. return False
  426. try:
  427. find_module(modname.split('.')[0], [from_file])
  428. return True
  429. except ImportError:
  430. return False
  431. # internal only functions #####################################################
  432. def _file_from_modpath(modpath, path=None, context=None):
  433. """given a mod path (i.e. splitted module / package name), return the
  434. corresponding file
  435. this function is used internally, see `file_from_modpath`'s
  436. documentation for more information
  437. """
  438. assert len(modpath) > 0
  439. if context is not None:
  440. try:
  441. mtype, mp_filename = _module_file(modpath, [context])
  442. except ImportError:
  443. mtype, mp_filename = _module_file(modpath, path)
  444. else:
  445. mtype, mp_filename = _module_file(modpath, path)
  446. if mtype == PY_COMPILED:
  447. try:
  448. return get_source_file(mp_filename)
  449. except NoSourceFile:
  450. return mp_filename
  451. elif mtype == C_BUILTIN:
  452. # integrated builtin module
  453. return None
  454. elif mtype == PKG_DIRECTORY:
  455. mp_filename = _has_init(mp_filename)
  456. return mp_filename
  457. def _search_zip(modpath, pic):
  458. for filepath, importer in pic.items():
  459. if importer is not None:
  460. if importer.find_module(modpath[0]):
  461. if not importer.find_module('/'.join(modpath)):
  462. raise ImportError('No module named %s in %s/%s' % (
  463. '.'.join(modpath[1:]), file, modpath))
  464. return ZIPFILE, abspath(filepath) + '/' + '/'.join(modpath), filepath
  465. raise ImportError('No module named %s' % '.'.join(modpath))
  466. def _module_file(modpath, path=None):
  467. """get a module type / file path
  468. :type modpath: list or tuple
  469. :param modpath:
  470. splitted module's name (i.e name of a module or package splitted
  471. on '.'), with leading empty strings for explicit relative import
  472. :type path: list or None
  473. :param path:
  474. optional list of path where the module or package should be
  475. searched (use sys.path if nothing or None is given)
  476. :rtype: tuple(int, str)
  477. :return: the module type flag and the file path for a module
  478. """
  479. # egg support compat
  480. try:
  481. pic = sys.path_importer_cache
  482. _path = (path is None and sys.path or path)
  483. for __path in _path:
  484. if not __path in pic:
  485. try:
  486. pic[__path] = zipimport.zipimporter(__path)
  487. except zipimport.ZipImportError:
  488. pic[__path] = None
  489. checkeggs = True
  490. except AttributeError:
  491. checkeggs = False
  492. imported = []
  493. while modpath:
  494. try:
  495. _, mp_filename, mp_desc = find_module(modpath[0], path)
  496. except ImportError:
  497. if checkeggs:
  498. return _search_zip(modpath, pic)[:2]
  499. raise
  500. else:
  501. if checkeggs:
  502. fullabspath = [abspath(x) for x in _path]
  503. try:
  504. pathindex = fullabspath.index(dirname(abspath(mp_filename)))
  505. emtype, emp_filename, zippath = _search_zip(modpath, pic)
  506. if pathindex > _path.index(zippath):
  507. # an egg takes priority
  508. return emtype, emp_filename
  509. except ValueError:
  510. # XXX not in _path
  511. pass
  512. except ImportError:
  513. pass
  514. checkeggs = False
  515. imported.append(modpath.pop(0))
  516. mtype = mp_desc[2]
  517. if modpath:
  518. if mtype != PKG_DIRECTORY:
  519. raise ImportError('No module %s in %s' % ('.'.join(modpath),
  520. '.'.join(imported)))
  521. path = [mp_filename]
  522. return mtype, mp_filename
  523. def _is_python_file(filename):
  524. """return true if the given filename should be considered as a python file
  525. .pyc and .pyo are ignored
  526. """
  527. for ext in ('.py', '.so', '.pyd', '.pyw'):
  528. if filename.endswith(ext):
  529. return True
  530. return False
  531. def _has_init(directory):
  532. """if the given directory has a valid __init__ file, return its path,
  533. else return None
  534. """
  535. mod_or_pack = join(directory, '__init__')
  536. for ext in PY_SOURCE_EXTS + ('pyc', 'pyo'):
  537. if exists(mod_or_pack + '.' + ext):
  538. return mod_or_pack + '.' + ext
  539. return None