Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modutils.py 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2014-2016 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2014 Google, Inc.
  4. # Copyright (c) 2015 Florian Bruhin <me@the-compiler.org>
  5. # Copyright (c) 2015 Radosław Ganczarek <radoslaw@ganczarek.in>
  6. # Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net>
  7. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  8. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  9. """Python modules manipulation utility functions.
  10. :type PY_SOURCE_EXTS: tuple(str)
  11. :var PY_SOURCE_EXTS: list of possible python source file extension
  12. :type STD_LIB_DIRS: set of str
  13. :var STD_LIB_DIRS: directories where standard modules are located
  14. :type BUILTIN_MODULES: dict
  15. :var BUILTIN_MODULES: dictionary with builtin module names has key
  16. """
  17. import imp
  18. import os
  19. import platform
  20. import sys
  21. from distutils.sysconfig import get_python_lib # pylint: disable=import-error
  22. # pylint: disable=import-error, no-name-in-module
  23. from distutils.errors import DistutilsPlatformError
  24. # distutils is replaced by virtualenv with a module that does
  25. # weird path manipulations in order to get to the
  26. # real distutils module.
  27. import six
  28. from .interpreter._import import spec
  29. from .interpreter._import import util
  30. if sys.platform.startswith('win'):
  31. PY_SOURCE_EXTS = ('py', 'pyw')
  32. PY_COMPILED_EXTS = ('dll', 'pyd')
  33. else:
  34. PY_SOURCE_EXTS = ('py',)
  35. PY_COMPILED_EXTS = ('so',)
  36. try:
  37. # The explicit sys.prefix is to work around a patch in virtualenv that
  38. # replaces the 'real' sys.prefix (i.e. the location of the binary)
  39. # with the prefix from which the virtualenv was created. This throws
  40. # off the detection logic for standard library modules, thus the
  41. # workaround.
  42. STD_LIB_DIRS = set([
  43. get_python_lib(standard_lib=True, prefix=sys.prefix),
  44. # Take care of installations where exec_prefix != prefix.
  45. get_python_lib(standard_lib=True, prefix=sys.exec_prefix),
  46. get_python_lib(standard_lib=True)])
  47. # get_python_lib(standard_lib=1) is not available on pypy, set STD_LIB_DIR to
  48. # non-valid path, see https://bugs.pypy.org/issue1164
  49. except DistutilsPlatformError:
  50. STD_LIB_DIRS = set()
  51. if os.name == 'nt':
  52. STD_LIB_DIRS.add(os.path.join(sys.prefix, 'dlls'))
  53. try:
  54. # real_prefix is defined when running inside virtual environments,
  55. # created with the **virtualenv** library.
  56. STD_LIB_DIRS.add(os.path.join(sys.real_prefix, 'dlls'))
  57. except AttributeError:
  58. # sys.base_exec_prefix is always defined, but in a virtual environment
  59. # created with the stdlib **venv** module, it points to the original
  60. # installation, if the virtual env is activated.
  61. try:
  62. STD_LIB_DIRS.add(os.path.join(sys.base_exec_prefix, 'dlls'))
  63. except AttributeError:
  64. pass
  65. if platform.python_implementation() == 'PyPy':
  66. _root = os.path.join(sys.prefix, 'lib_pypy')
  67. STD_LIB_DIRS.add(_root)
  68. try:
  69. # real_prefix is defined when running inside virtualenv.
  70. STD_LIB_DIRS.add(os.path.join(sys.real_prefix, 'lib_pypy'))
  71. except AttributeError:
  72. pass
  73. del _root
  74. if os.name == 'posix':
  75. # Need the real prefix is we're under a virtualenv, otherwise
  76. # the usual one will do.
  77. try:
  78. prefix = sys.real_prefix
  79. except AttributeError:
  80. prefix = sys.prefix
  81. def _posix_path(path):
  82. base_python = 'python%d.%d' % sys.version_info[:2]
  83. return os.path.join(prefix, path, base_python)
  84. STD_LIB_DIRS.add(_posix_path('lib'))
  85. if sys.maxsize > 2**32:
  86. # This tries to fix a problem with /usr/lib64 builds,
  87. # where systems are running both 32-bit and 64-bit code
  88. # on the same machine, which reflects into the places where
  89. # standard library could be found. More details can be found
  90. # here http://bugs.python.org/issue1294959.
  91. # An easy reproducing case would be
  92. # https://github.com/PyCQA/pylint/issues/712#issuecomment-163178753
  93. STD_LIB_DIRS.add(_posix_path('lib64'))
  94. EXT_LIB_DIR = get_python_lib()
  95. IS_JYTHON = platform.python_implementation() == 'Jython'
  96. BUILTIN_MODULES = dict.fromkeys(sys.builtin_module_names, True)
  97. class NoSourceFile(Exception):
  98. """exception raised when we are not able to get a python
  99. source file for a precompiled file
  100. """
  101. def _normalize_path(path):
  102. return os.path.normcase(os.path.abspath(path))
  103. def _canonicalize_path(path):
  104. return os.path.realpath(os.path.expanduser(path))
  105. def _path_from_filename(filename, is_jython=IS_JYTHON):
  106. if not is_jython:
  107. if sys.version_info > (3, 0):
  108. return filename
  109. else:
  110. if filename.endswith(".pyc"):
  111. return filename[:-1]
  112. return filename
  113. head, has_pyclass, _ = filename.partition("$py.class")
  114. if has_pyclass:
  115. return head + ".py"
  116. return filename
  117. def _handle_blacklist(blacklist, dirnames, filenames):
  118. """remove files/directories in the black list
  119. dirnames/filenames are usually from os.walk
  120. """
  121. for norecurs in blacklist:
  122. if norecurs in dirnames:
  123. dirnames.remove(norecurs)
  124. elif norecurs in filenames:
  125. filenames.remove(norecurs)
  126. _NORM_PATH_CACHE = {}
  127. def _cache_normalize_path(path):
  128. """abspath with caching"""
  129. # _module_file calls abspath on every path in sys.path every time it's
  130. # called; on a larger codebase this easily adds up to half a second just
  131. # assembling path components. This cache alleviates that.
  132. try:
  133. return _NORM_PATH_CACHE[path]
  134. except KeyError:
  135. if not path: # don't cache result for ''
  136. return _normalize_path(path)
  137. result = _NORM_PATH_CACHE[path] = _normalize_path(path)
  138. return result
  139. def load_module_from_name(dotted_name, path=None, use_sys=True):
  140. """Load a Python module from its name.
  141. :type dotted_name: str
  142. :param dotted_name: python name of a module or package
  143. :type path: list or None
  144. :param path:
  145. optional list of path where the module or package should be
  146. searched (use sys.path if nothing or None is given)
  147. :type use_sys: bool
  148. :param use_sys:
  149. boolean indicating whether the sys.modules dictionary should be
  150. used or not
  151. :raise ImportError: if the module or package is not found
  152. :rtype: module
  153. :return: the loaded module
  154. """
  155. return load_module_from_modpath(dotted_name.split('.'), path, use_sys)
  156. def load_module_from_modpath(parts, path=None, use_sys=1):
  157. """Load a python module from its split name.
  158. :type parts: list(str) or tuple(str)
  159. :param parts:
  160. python name of a module or package split on '.'
  161. :type path: list or None
  162. :param path:
  163. optional list of path where the module or package should be
  164. searched (use sys.path if nothing or None is given)
  165. :type use_sys: bool
  166. :param use_sys:
  167. boolean indicating whether the sys.modules dictionary should be used or not
  168. :raise ImportError: if the module or package is not found
  169. :rtype: module
  170. :return: the loaded module
  171. """
  172. if use_sys:
  173. try:
  174. return sys.modules['.'.join(parts)]
  175. except KeyError:
  176. pass
  177. modpath = []
  178. prevmodule = None
  179. for part in parts:
  180. modpath.append(part)
  181. curname = '.'.join(modpath)
  182. module = None
  183. if len(modpath) != len(parts):
  184. # even with use_sys=False, should try to get outer packages from sys.modules
  185. module = sys.modules.get(curname)
  186. elif use_sys:
  187. # because it may have been indirectly loaded through a parent
  188. module = sys.modules.get(curname)
  189. if module is None:
  190. mp_file, mp_filename, mp_desc = imp.find_module(part, path)
  191. module = imp.load_module(curname, mp_file, mp_filename, mp_desc)
  192. # mp_file still needs to be closed.
  193. if mp_file:
  194. mp_file.close()
  195. if prevmodule:
  196. setattr(prevmodule, part, module)
  197. _file = getattr(module, '__file__', '')
  198. prevmodule = module
  199. if not _file and util.is_namespace(curname):
  200. continue
  201. if not _file and len(modpath) != len(parts):
  202. raise ImportError('no module in %s' % '.'.join(parts[len(modpath):]))
  203. path = [os.path.dirname(_file)]
  204. return module
  205. def load_module_from_file(filepath, path=None, use_sys=True, extrapath=None):
  206. """Load a Python module from it's path.
  207. :type filepath: str
  208. :param filepath: path to the python module or package
  209. :type path: list or None
  210. :param path:
  211. optional list of path where the module or package should be
  212. searched (use sys.path if nothing or None is given)
  213. :type use_sys: bool
  214. :param use_sys:
  215. boolean indicating whether the sys.modules dictionary should be
  216. used or not
  217. :raise ImportError: if the module or package is not found
  218. :rtype: module
  219. :return: the loaded module
  220. """
  221. modpath = modpath_from_file(filepath, extrapath)
  222. return load_module_from_modpath(modpath, path, use_sys)
  223. def check_modpath_has_init(path, mod_path):
  224. """check there are some __init__.py all along the way"""
  225. modpath = []
  226. for part in mod_path:
  227. modpath.append(part)
  228. path = os.path.join(path, part)
  229. if not _has_init(path):
  230. old_namespace = util.is_namespace('.'.join(modpath))
  231. if not old_namespace:
  232. return False
  233. return True
  234. def modpath_from_file_with_callback(filename, extrapath=None, is_package_cb=None):
  235. filename = _path_from_filename(filename)
  236. filename = os.path.realpath(os.path.expanduser(filename))
  237. base = os.path.splitext(filename)[0]
  238. if extrapath is not None:
  239. for path_ in six.moves.map(_canonicalize_path, extrapath):
  240. path = os.path.abspath(path_)
  241. if path and os.path.normcase(base[:len(path)]) == os.path.normcase(path):
  242. submodpath = [pkg for pkg in base[len(path):].split(os.sep)
  243. if pkg]
  244. if is_package_cb(path, submodpath[:-1]):
  245. return extrapath[path_].split('.') + submodpath
  246. for path in six.moves.map(_canonicalize_path, sys.path):
  247. path = _cache_normalize_path(path)
  248. if path and os.path.normcase(base).startswith(path):
  249. modpath = [pkg for pkg in base[len(path):].split(os.sep) if pkg]
  250. if is_package_cb(path, modpath[:-1]):
  251. return modpath
  252. raise ImportError('Unable to find module for %s in %s' % (
  253. filename, ', \n'.join(sys.path)))
  254. def modpath_from_file(filename, extrapath=None):
  255. """given a file path return the corresponding split module's name
  256. (i.e name of a module or package split on '.')
  257. :type filename: str
  258. :param filename: file's path for which we want the module's name
  259. :type extrapath: dict
  260. :param extrapath:
  261. optional extra search path, with path as key and package name for the path
  262. as value. This is usually useful to handle package split in multiple
  263. directories using __path__ trick.
  264. :raise ImportError:
  265. if the corresponding module's name has not been found
  266. :rtype: list(str)
  267. :return: the corresponding split module's name
  268. """
  269. return modpath_from_file_with_callback(filename, extrapath, check_modpath_has_init)
  270. def file_from_modpath(modpath, path=None, context_file=None):
  271. return file_info_from_modpath(modpath, path, context_file).location
  272. def file_info_from_modpath(modpath, path=None, context_file=None):
  273. """given a mod path (i.e. split module / package name), return the
  274. corresponding file, giving priority to source file over precompiled
  275. file if it exists
  276. :type modpath: list or tuple
  277. :param modpath:
  278. split module's name (i.e name of a module or package split
  279. on '.')
  280. (this means explicit relative imports that start with dots have
  281. empty strings in this list!)
  282. :type path: list or None
  283. :param path:
  284. optional list of path where the module or package should be
  285. searched (use sys.path if nothing or None is given)
  286. :type context_file: str or None
  287. :param context_file:
  288. context file to consider, necessary if the identifier has been
  289. introduced using a relative import unresolvable in the actual
  290. context (i.e. modutils)
  291. :raise ImportError: if there is no such module in the directory
  292. :rtype: (str or None, import type)
  293. :return:
  294. the path to the module's file or None if it's an integrated
  295. builtin module such as 'sys'
  296. """
  297. if context_file is not None:
  298. context = os.path.dirname(context_file)
  299. else:
  300. context = context_file
  301. if modpath[0] == 'xml':
  302. # handle _xmlplus
  303. try:
  304. return _spec_from_modpath(['_xmlplus'] + modpath[1:], path, context)
  305. except ImportError:
  306. return _spec_from_modpath(modpath, path, context)
  307. elif modpath == ['os', 'path']:
  308. # FIXME: currently ignoring search_path...
  309. return spec.ModuleSpec(name='os.path', location=os.path.__file__, module_type=imp.PY_SOURCE)
  310. return _spec_from_modpath(modpath, path, context)
  311. def get_module_part(dotted_name, context_file=None):
  312. """given a dotted name return the module part of the name :
  313. >>> get_module_part('astroid.as_string.dump')
  314. 'astroid.as_string'
  315. :type dotted_name: str
  316. :param dotted_name: full name of the identifier we are interested in
  317. :type context_file: str or None
  318. :param context_file:
  319. context file to consider, necessary if the identifier has been
  320. introduced using a relative import unresolvable in the actual
  321. context (i.e. modutils)
  322. :raise ImportError: if there is no such module in the directory
  323. :rtype: str or None
  324. :return:
  325. the module part of the name or None if we have not been able at
  326. all to import the given name
  327. XXX: deprecated, since it doesn't handle package precedence over module
  328. (see #10066)
  329. """
  330. # os.path trick
  331. if dotted_name.startswith('os.path'):
  332. return 'os.path'
  333. parts = dotted_name.split('.')
  334. if context_file is not None:
  335. # first check for builtin module which won't be considered latter
  336. # in that case (path != None)
  337. if parts[0] in BUILTIN_MODULES:
  338. if len(parts) > 2:
  339. raise ImportError(dotted_name)
  340. return parts[0]
  341. # don't use += or insert, we want a new list to be created !
  342. path = None
  343. starti = 0
  344. if parts[0] == '':
  345. assert context_file is not None, \
  346. 'explicit relative import, but no context_file?'
  347. path = [] # prevent resolving the import non-relatively
  348. starti = 1
  349. while parts[starti] == '': # for all further dots: change context
  350. starti += 1
  351. context_file = os.path.dirname(context_file)
  352. for i in range(starti, len(parts)):
  353. try:
  354. file_from_modpath(parts[starti:i+1], path=path,
  355. context_file=context_file)
  356. except ImportError:
  357. if i < max(1, len(parts) - 2):
  358. raise
  359. return '.'.join(parts[:i])
  360. return dotted_name
  361. def get_module_files(src_directory, blacklist, list_all=False):
  362. """given a package directory return a list of all available python
  363. module's files in the package and its subpackages
  364. :type src_directory: str
  365. :param src_directory:
  366. path of the directory corresponding to the package
  367. :type blacklist: list or tuple
  368. :param blacklist: iterable
  369. list of files or directories to ignore.
  370. :type list_all: bool
  371. :param list_all:
  372. get files from all paths, including ones without __init__.py
  373. :rtype: list
  374. :return:
  375. the list of all available python module's files in the package and
  376. its subpackages
  377. """
  378. files = []
  379. for directory, dirnames, filenames in os.walk(src_directory):
  380. if directory in blacklist:
  381. continue
  382. _handle_blacklist(blacklist, dirnames, filenames)
  383. # check for __init__.py
  384. if not list_all and '__init__.py' not in filenames:
  385. dirnames[:] = ()
  386. continue
  387. for filename in filenames:
  388. if _is_python_file(filename):
  389. src = os.path.join(directory, filename)
  390. files.append(src)
  391. return files
  392. def get_source_file(filename, include_no_ext=False):
  393. """given a python module's file name return the matching source file
  394. name (the filename will be returned identically if it's a already an
  395. absolute path to a python source file...)
  396. :type filename: str
  397. :param filename: python module's file name
  398. :raise NoSourceFile: if no source file exists on the file system
  399. :rtype: str
  400. :return: the absolute path of the source file if it exists
  401. """
  402. filename = os.path.abspath(_path_from_filename(filename))
  403. base, orig_ext = os.path.splitext(filename)
  404. for ext in PY_SOURCE_EXTS:
  405. source_path = '%s.%s' % (base, ext)
  406. if os.path.exists(source_path):
  407. return source_path
  408. if include_no_ext and not orig_ext and os.path.exists(base):
  409. return base
  410. raise NoSourceFile(filename)
  411. def is_python_source(filename):
  412. """
  413. rtype: bool
  414. return: True if the filename is a python source file
  415. """
  416. return os.path.splitext(filename)[1][1:] in PY_SOURCE_EXTS
  417. def is_standard_module(modname, std_path=None):
  418. """try to guess if a module is a standard python module (by default,
  419. see `std_path` parameter's description)
  420. :type modname: str
  421. :param modname: name of the module we are interested in
  422. :type std_path: list(str) or tuple(str)
  423. :param std_path: list of path considered has standard
  424. :rtype: bool
  425. :return:
  426. true if the module:
  427. - is located on the path listed in one of the directory in `std_path`
  428. - is a built-in module
  429. """
  430. modname = modname.split('.')[0]
  431. try:
  432. filename = file_from_modpath([modname])
  433. except ImportError:
  434. # import failed, i'm probably not so wrong by supposing it's
  435. # not standard...
  436. return False
  437. # modules which are not living in a file are considered standard
  438. # (sys and __builtin__ for instance)
  439. if filename is None:
  440. # we assume there are no namespaces in stdlib
  441. return not util.is_namespace(modname)
  442. filename = _normalize_path(filename)
  443. if filename.startswith(_cache_normalize_path(EXT_LIB_DIR)):
  444. return False
  445. if std_path is None:
  446. std_path = STD_LIB_DIRS
  447. for path in std_path:
  448. if filename.startswith(_cache_normalize_path(path)):
  449. return True
  450. return False
  451. def is_relative(modname, from_file):
  452. """return true if the given module name is relative to the given
  453. file name
  454. :type modname: str
  455. :param modname: name of the module we are interested in
  456. :type from_file: str
  457. :param from_file:
  458. path of the module from which modname has been imported
  459. :rtype: bool
  460. :return:
  461. true if the module has been imported relatively to `from_file`
  462. """
  463. if not os.path.isdir(from_file):
  464. from_file = os.path.dirname(from_file)
  465. if from_file in sys.path:
  466. return False
  467. try:
  468. stream, _, _ = imp.find_module(modname.split('.')[0], [from_file])
  469. # Close the stream to avoid ResourceWarnings.
  470. if stream:
  471. stream.close()
  472. return True
  473. except ImportError:
  474. return False
  475. # internal only functions #####################################################
  476. def _spec_from_modpath(modpath, path=None, context=None):
  477. """given a mod path (i.e. split module / package name), return the
  478. corresponding spec
  479. this function is used internally, see `file_from_modpath`'s
  480. documentation for more information
  481. """
  482. assert modpath
  483. location = None
  484. if context is not None:
  485. try:
  486. found_spec = spec.find_spec(modpath, [context])
  487. location = found_spec.location
  488. except ImportError:
  489. found_spec = spec.find_spec(modpath, path)
  490. location = found_spec.location
  491. else:
  492. found_spec = spec.find_spec(modpath, path)
  493. if found_spec.type == spec.ModuleType.PY_COMPILED:
  494. try:
  495. location = get_source_file(found_spec.location)
  496. return found_spec._replace(location=location, type=spec.ModuleType.PY_SOURCE)
  497. except NoSourceFile:
  498. return found_spec._replace(location=location)
  499. elif found_spec.type == spec.ModuleType.C_BUILTIN:
  500. # integrated builtin module
  501. return found_spec._replace(location=None)
  502. elif found_spec.type == spec.ModuleType.PKG_DIRECTORY:
  503. location = _has_init(found_spec.location)
  504. return found_spec._replace(location=location, type=spec.ModuleType.PY_SOURCE)
  505. return found_spec
  506. def _is_python_file(filename):
  507. """return true if the given filename should be considered as a python file
  508. .pyc and .pyo are ignored
  509. """
  510. for ext in ('.py', '.so', '.pyd', '.pyw'):
  511. if filename.endswith(ext):
  512. return True
  513. return False
  514. def _has_init(directory):
  515. """if the given directory has a valid __init__ file, return its path,
  516. else return None
  517. """
  518. mod_or_pack = os.path.join(directory, '__init__')
  519. for ext in PY_SOURCE_EXTS + ('pyc', 'pyo'):
  520. if os.path.exists(mod_or_pack + '.' + ext):
  521. return mod_or_pack + '.' + ext
  522. return None
  523. def is_namespace(specobj):
  524. return specobj.type == spec.ModuleType.PY_NAMESPACE
  525. def is_directory(specobj):
  526. return specobj.type == spec.ModuleType.PKG_DIRECTORY