3
tûœ]�[  ã            *   @   s(  d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddl
mZ ejjdƒrrdRZdSZndTZdUZy(edejd�edejd�edd�hZW n e	k
r¼   eƒ ZY nX ejdk�rDejejjejdƒƒ yejejjejdƒƒ W nH ek
�rB   yejejjejdƒƒ W n ek
�r<   Y nX Y nX ejƒ dk�r ejjejdƒZejeƒ yejejjejdƒƒ W n ek
�rœ   Y nX [ejdk�ry
ejZW n ek
�rÒ   ejZY nX dd„ Zejedƒƒ ejdVk�rejedƒƒ eƒ edƒhZejƒ dkZ e!j"ej#dƒZ$G dd„ de%ƒZ&dd „ Z'd!d"„ Z(e fd#d$„Z)d%d&„ Z*i Z+d'd(„ Z,dWd)d*„Z-dXd+d,„Z.dYd-d.„Z/d/d0„ Z0d1d2„ Z1dZd3d4„Z2d[d5d6„Z3d\d7d8„Z4d]d9d:„Z5d^d;d<„Z6d_d>d?„Z7d`d@dA„Z8dBdC„ Z9dadDdE„Z:dFdG„ Z;dbdHdI„Z<dJdK„ Z=dLdM„ Z>dNdO„ Z?dPdQ„ Z@dS )caV  Python modules manipulation utility functions.

:type PY_SOURCE_EXTS: tuple(str)
:var PY_SOURCE_EXTS: list of possible python source file extension

:type STD_LIB_DIRS: set of str
:var STD_LIB_DIRS: directories where standard modules are located

:type BUILTIN_MODULES: dict
:var BUILTIN_MODULES: dictionary with builtin module names has key
é    N)Úget_python_lib)ÚDistutilsPlatformErroré   )Úspec)ÚutilÚwinÚpyÚpywÚdllÚpydÚsoT)Ústandard_libÚprefix)r   ÚntZdllsÚPyPyZlib_pypyÚposixc             C   s"   dt jd d…  }tjjt| |ƒS )Nzpython%d.%dé   )ÚsysÚversion_infoÚosÚpathÚjoinr   )r   Zbase_python© r   ú3/tmp/pip-build-fn42xldr/astroid/astroid/modutils.pyÚ_posix_pathh   s    r   Úlibr   é    Zlib64ÚJythonc               @   s   e Zd ZdZdS )ÚNoSourceFilezaexception raised when we are not able to get a python
    source file for a precompiled file
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   |   s   r   c             C   s   t jjt jj| ƒƒS )N)r   r   ÚnormcaseÚabspath)r   r   r   r   Ú_normalize_path‚   s    r%   c             C   s   t jjt jj| ƒƒS )N)r   r   ÚrealpathÚ
expanduser)r   r   r   r   Ú_canonicalize_path†   s    r(   c             C   s(   |s| S | j dƒ\}}}|r$|d S | S )Nz	$py.classz.py)Ú	partition)ÚfilenameZ	is_jythonÚheadZhas_pyclassÚ_r   r   r   Ú_path_from_filenameŠ   s    r-   c             C   s8   x2| D ]*}||kr|j |ƒ q||kr|j |ƒ qW dS )z`remove files/directories in the black list

    dirnames/filenames are usually from os.walk
    N)Úremove)Ú	blacklistÚdirnamesÚ	filenamesZnorecursr   r   r   Ú_handle_blacklist“   s
    
r2   c             C   s>   yt |  S  tk
r8   | s$t| ƒS t| ƒ }t | < |S X dS )zabspath with cachingN)Ú_NORM_PATH_CACHEÚKeyErrorr%   )r   Úresultr   r   r   Ú_cache_normalize_path¢   s    r6   c             C   s   t | jdƒ||ƒS )a"  Load a Python module from its name.

    :type dotted_name: str
    :param dotted_name: python name of a module or package

    :type path: list or None
    :param path:
      optional list of path where the module or package should be
      searched (use sys.path if nothing or None is given)

    :type use_sys: bool
    :param use_sys:
      boolean indicating whether the sys.modules dictionary should be
      used or not


    :raise ImportError: if the module or package is not found

    :rtype: module
    :return: the loaded module
    Ú.)Úload_module_from_modpathÚsplit)Údotted_namer   Úuse_sysr   r   r   Úload_module_from_name°   s    r<   c             C   s:  |r*yt jdj| ƒ S  tk
r(   Y nX g }d}�x | D ]ø}|j|ƒ dj|ƒ}d}t|ƒt| ƒkrtt jj|ƒ}n|r„t jj|ƒ}|dkrºtj||ƒ\}}	}
tj	|||	|
ƒ}|rº|j
ƒ  |rÊt|||ƒ t|ddƒ}|}| rìtj|ƒrìq:| �r$t|ƒt| ƒk�r$tddj| t|ƒd… ƒ ƒ‚tjj|ƒg}q:W |S )a<  Load a python module from its split name.

    :type parts: list(str) or tuple(str)
    :param parts:
      python name of a module or package split on '.'

    :type path: list or None
    :param path:
      optional list of path where the module or package should be
      searched (use sys.path if nothing or None is given)

    :type use_sys: bool
    :param use_sys:
      boolean indicating whether the sys.modules dictionary should be used or not

    :raise ImportError: if the module or package is not found

    :rtype: module
    :return: the loaded module
    r7   NÚ__file__Ú zno module in %s)r   Úmodulesr   r4   ÚappendÚlenÚgetÚimpÚfind_moduleÚload_moduleÚcloseÚsetattrÚgetattrr   Úis_namespaceÚImportErrorr   r   Údirname)Úpartsr   r;   ÚmodpathZ
prevmoduleÚpartZcurnameÚmoduleZmp_fileZmp_filenameZmp_descÚ_filer   r   r   r8   É   s<    

r8   c             C   s   t | |ƒ}t|||ƒS )a  Load a Python module from it's path.

    :type filepath: str
    :param filepath: path to the python module or package

    :type path: list or None
    :param path:
      optional list of path where the module or package should be
      searched (use sys.path if nothing or None is given)

    :type use_sys: bool
    :param use_sys:
      boolean indicating whether the sys.modules dictionary should be
      used or not


    :raise ImportError: if the module or package is not found

    :rtype: module
    :return: the loaded module
    )Úmodpath_from_filer8   )Úfilepathr   r;   Ú	extrapathrM   r   r   r   Úload_module_from_file  s    
rT   c             C   sN   g }xD|D ]<}|j |ƒ tjj| |ƒ} t| ƒs
tjdj|ƒƒ}|s
dS q
W dS )z2check there are some __init__.py all along the wayr7   FT)r@   r   r   r   Ú	_has_initr   rI   )r   Zmod_pathrM   rN   Zold_namespacer   r   r   Úcheck_modpath_has_init  s    

rV   c             C   s’   d}t jj|ƒ}t jj| ƒ}t jj|ƒj|ƒr2|}t jj| ƒ}t jj|ƒj|ƒrT|}|rŽt jj|ƒd }|t|ƒd… }dd„ |jt j	ƒD ƒS dS )a=  Extracts the relative mod path of the file to import from

    Check if a file is within the passed in path and if so, returns the
    relative mod path from the one passed in.

    If the filename is no in path_to_check, returns None

    Note this function will look for both abs and realpath of the file,
    this allows to find the relative base path even if the file is a
    symlink of a file in the passed in path

    Examples:
        _get_relative_base_path("/a/b/c/d.py", "/a/b") ->  ["c","d"]
        _get_relative_base_path("/a/b/c/d.py", "/dev") ->  None
    Nr   c             S   s   g | ]}|r|‘qS r   r   )Ú.0Úpkgr   r   r   ú
<listcomp>E  s    z+_get_relative_base_path.<locals>.<listcomp>)
r   r   r#   r$   Ú
startswithr&   ÚsplitextrA   r9   Úsep)r*   Zpath_to_checkZimportable_pathZabs_filenameZreal_filenameÚ	base_pathZrelative_base_pathr   r   r   Ú_get_relative_base_path(  s    r^   c             C   sê   t jjt| ƒƒ} |d k	rzx`tjtt|ƒ|ƒD ]J}t jj|ƒ}|sBq,t	| |ƒ}|sRq,|||d d… ƒr,|| j
dƒ| S q,W xRtjtttjƒtjƒD ]8}t|ƒ}|s¤q’t	| |ƒ}|s´q’|||d d… ƒr’|S q’W td| djtjƒf ƒ‚d S )Nr   r7   z"Unable to find module for %s in %sz, 
éÿÿÿÿr_   )r   r   r'   r-   Ú	itertoolsÚchainÚmapr(   r$   r^   r9   r   r6   rJ   r   )r*   rS   Zis_package_cbÚpath_r   Z
submodpathrM   r   r   r   Úmodpath_from_file_with_callbackJ  s,    

rd   c             C   s   t | |tƒS )ai  given a file path return the corresponding split module's name
    (i.e name of a module or package split on '.')

    :type filename: str
    :param filename: file's path for which we want the module's name

    :type extrapath: dict
    :param extrapath:
      optional extra search path, with path as key and package name for the path
      as value. This is usually useful to handle package split in multiple
      directories using __path__ trick.


    :raise ImportError:
      if the corresponding module's name has not been found

    :rtype: list(str)
    :return: the corresponding split module's name
    )rd   rV   )r*   rS   r   r   r   rQ   g  s    rQ   c             C   s   t | ||ƒjS )N)Úfile_info_from_modpathÚlocation)rM   r   Úcontext_filer   r   r   Úfile_from_modpath~  s    rh   c             C   sŽ   |dk	rt jj|ƒ}n|}| d dkr`ytdg| dd…  ||ƒS  tk
r\   t| ||ƒS X n"| ddgkr‚tjdt jjtj	d	�S t| ||ƒS )
aÿ  given a mod path (i.e. split module / package name), return the
    corresponding file, giving priority to source file over precompiled
    file if it exists

    :type modpath: list or tuple
    :param modpath:
      split module's name (i.e name of a module or package split
      on '.')
      (this means explicit relative imports that start with dots have
      empty strings in this list!)

    :type path: list or None
    :param path:
      optional list of path where the module or package should be
      searched (use sys.path if nothing or None is given)

    :type context_file: str or None
    :param context_file:
      context file to consider, necessary if the identifier has been
      introduced using a relative import unresolvable in the actual
      context (i.e. modutils)

    :raise ImportError: if there is no such module in the directory

    :rtype: (str or None, import type)
    :return:
      the path to the module's file or None if it's an integrated
      builtin module such as 'sys'
    Nr   ÚxmlZ_xmlplusr   r   r   zos.path)Únamerf   Úmodule_type)
r   r   rK   Ú_spec_from_modpathrJ   r   Ú
ModuleSpecr=   rC   Ú	PY_SOURCE)rM   r   rg   Úcontextr   r   r   re   ‚  s    re   c             C   s  | j dƒrdS | jdƒ}|dk	rH|d tkrHt|ƒdkr@t| ƒ‚|d S d}d}|d dkrt|dk	sltdƒ‚g }d}x$|| dkr˜|d7 }tjj|ƒ}qvW xrt	|t|ƒƒD ]`}yt
|||d … ||d	� W qª tk
�r   |tdt|ƒd ƒk rö‚ dj|d|… ƒS X qªW | S )
a  given a dotted name return the module part of the name :

    >>> get_module_part('astroid.as_string.dump')
    'astroid.as_string'

    :type dotted_name: str
    :param dotted_name: full name of the identifier we are interested in

    :type context_file: str or None
    :param context_file:
      context file to consider, necessary if the identifier has been
      introduced using a relative import unresolvable in the actual
      context (i.e. modutils)


    :raise ImportError: if there is no such module in the directory

    :rtype: str or None
    :return:
      the module part of the name or None if we have not been able at
      all to import the given name

    XXX: deprecated, since it doesn't handle package precedence over module
    (see #10066)
    zos.pathr7   Nr   r   r>   z.explicit relative import, but no context_file?r   )r   rg   )rZ   r9   ÚBUILTIN_MODULESrA   rJ   ÚAssertionErrorr   r   rK   Úrangerh   Úmaxr   )r:   rg   rL   r   ZstartiÚir   r   r   Úget_module_part²  s6    


ru   Fc       	      C   s‚   g }xxt j| ƒD ]j\}}}||kr$qt|||ƒ | rLd|krLf |dd…< qx,|D ]$}t|ƒrRt jj||ƒ}|j|ƒ qRW qW |S )aN  given a package directory return a list of all available python
    module's files in the package and its subpackages

    :type src_directory: str
    :param src_directory:
      path of the directory corresponding to the package

    :type blacklist: list or tuple
    :param blacklist: iterable
      list of files or directories to ignore.

    :type list_all: bool
    :param list_all:
        get files from all paths, including ones without __init__.py

    :rtype: list
    :return:
      the list of all available python module's files in the package and
      its subpackages
    z__init__.pyN)r   Úwalkr2   Ú_is_python_filer   r   r@   )	Zsrc_directoryr/   Zlist_allÚfilesÚ	directoryr0   r1   r*   Úsrcr   r   r   Úget_module_filesï  s    
r{   c             C   sp   t jjt| ƒƒ} t jj| ƒ\}}x(tD ] }d||f }t jj|ƒr&|S q&W |rd| rdt jj|ƒrd|S t| ƒ‚dS )aš  given a python module's file name return the matching source file
    name (the filename will be returned identically if it's already an
    absolute path to a python source file...)

    :type filename: str
    :param filename: python module's file name


    :raise NoSourceFile: if no source file exists on the file system

    :rtype: str
    :return: the absolute path of the source file if it exists
    z%s.%sN)r   r   r$   r-   r[   ÚPY_SOURCE_EXTSÚexistsr   )r*   Zinclude_no_extÚbaseZorig_extÚextÚsource_pathr   r   r   Úget_source_file  s    
r�   c             C   s   t jj| ƒd dd… tkS )zN
    rtype: bool
    return: True if the filename is a python source file
    r   N)r   r   r[   r|   )r*   r   r   r   Úis_python_source-  s    r‚   c             C   sž   | j dƒd } yt| gƒ}W n tk
r0   dS X |dkrFtj| ƒ S t|ƒ}xtD ]}|jt|ƒƒrTdS qTW |dkrzt	}x|D ]}|jt|ƒƒr€dS q€W dS )aÐ  try to guess if a module is a standard python module (by default,
    see `std_path` parameter's description)

    :type modname: str
    :param modname: name of the module we are interested in

    :type std_path: list(str) or tuple(str)
    :param std_path: list of path considered has standard


    :rtype: bool
    :return:
      true if the module:
      - is located on the path listed in one of the directory in `std_path`
      - is a built-in module
    r7   r   FNT)
r9   rh   rJ   r   rI   r%   ÚEXT_LIB_DIRSrZ   r6   ÚSTD_LIB_DIRS)ÚmodnameZstd_pathr*   r   r   r   r   Úis_standard_module5  s"    

r†   c             C   sn   t jj|ƒst jj|ƒ}|tjkr&dS y.tj| jdƒd |gƒ\}}}|rR|jƒ  dS  t	k
rh   dS X dS )av  return true if the given module name is relative to the given
    file name

    :type modname: str
    :param modname: name of the module we are interested in

    :type from_file: str
    :param from_file:
      path of the module from which modname has been imported

    :rtype: bool
    :return:
      true if the module has been imported relatively to `from_file`
    Fr7   r   TN)
r   r   ÚisdirrK   r   rC   rD   r9   rF   rJ   )r…   Ú	from_fileÚstreamr,   r   r   r   Úis_relative^  s    
rŠ   c             C   sô   | st ‚d}|dk	rVytj| |gƒ}|j}W qb tk
rR   tj| |ƒ}|j}Y qbX ntj| |ƒ}|jtjjkr¬yt|jƒ}|j	|tjj
d�S  tk
r¨   |j	|d�S X nD|jtjjkrÆ|j	dd�S |jtjjkrðt|jƒ}|j	|tjj
d�S |S )zÃgiven a mod path (i.e. split module / package name), return the
    corresponding spec

    this function is used internally, see `file_from_modpath`'s
    documentation for more information
    N)rf   Útype)rf   )rq   r   Ú	find_specrf   rJ   r‹   Ú
ModuleTypeÚPY_COMPILEDr�   Ú_replacern   r   Ú	C_BUILTINÚPKG_DIRECTORYrU   )rM   r   ro   rf   Z
found_specr   r   r   rl     s.    


rl   c             C   s
   | j dƒS )zkreturn true if the given filename should be considered as a python file

    .pyc and .pyo are ignored
    ú.pyú.soú.pydú.pyw)r’   r“   r”   r•   )Úendswith)r*   r   r   r   rw   ¢  s    rw   c             C   sD   t jj| dƒ}x0td D ]$}t jj|d | ƒr|d | S qW dS )z\if the given directory has a valid __init__ file, return its path,
    else return None
    Ú__init__ÚpycÚpyor7   N)r˜   r™   )r   r   r   r|   r}   )ry   Zmod_or_packr   r   r   r   rU   ª  s
    rU   c             C   s   | j tjjkS )N)r‹   r   r�   ZPY_NAMESPACE)Úspecobjr   r   r   rI   µ  s    rI   c             C   s   | j tjjkS )N)r‹   r   r�   r‘   )rš   r   r   r   Úis_directory¹  s    r›   )r   r	   )r
   r   )r   )r   l        )NT)Nr   )NTN)NN)N)NN)NN)N)F)F)N)NN)Ar"   rC   r   Úplatformr   r`   Údistutils.sysconfigr   Zdistutils.errorsr   Zinterpreter._importr   r   rZ   r|   ZPY_COMPILED_EXTSr   Úexec_prefixr„   Úsetrj   Úaddr   r   Úreal_prefixÚAttributeErrorÚbase_exec_prefixÚpython_implementationÚ_rootr   Úmaxsizerƒ   Z	IS_JYTHONÚdictÚfromkeysÚbuiltin_module_namesrp   Ú	Exceptionr   r%   r(   r-   r2   r3   r6   r<   r8   rT   rV   r^   rd   rQ   rh   re   ru   r{   r�   r‚   r†   rŠ   rl   rw   rU   rI   r›   r   r   r   r   Ú<module>   s˜   

	

8
"



0
=
%

)!
#