B
    '\&                 @   s  d dl mZmZmZmZ d dlmZ d dlmZ d dl	Z	d dl
mZmZmZmZmZmZmZmZ d dlZd dlZddlmZmZ dd	lmZ dd
lmZ ddlmZmZmZ yd dl m!Z!m"Z" W n, e#k
r   d dl$m!Z! d dl%m"Z" Y nX ee&Z'dZ(dd Z)dd Z*dd Z+edd Z,dd Z-dd Z.dd Z/dHdd Z0d!d" Z1d#d$ Z2d%d& Z3dId'd(Z4d)d* Z5dJd,d-Z6d.d/ Z7d0d1 Z8d2d3 Z9d4d5 Z:dKd7d8Z;d9d: Z<d;d< Z=d=d> Z>d?d@ Z?dAdB Z@dLdDdEZAdFdG ZBdS )M    )absolute_importdivisionprint_functionunicode_literals)reduce)	getLoggerN)abspathbasename
expanduser
expandvarsjoinnormcasesplitsplitext   )on_winstring_types   )
CondaError)memoize)
accumulateconcattake)unquoteurlsplit)r   )r   z#\./|\.\.|~|/|[a-zA-Z]:[/\\]|\\\\|//c             C   s   d| krdS t t| S )Nz://F)rematchPATH_MATCH_REGEX)value r   0lib/python3.7/site-packages/conda/common/path.pyis_path'   s    r!   c             C   s   t tt| S )N)r   r
   r   )pathr   r   r    expand-   s    r#   c             C   s0   t rtt| tt|kS t| t|kS dS )zR
    Examples:
        >>> paths_equal('/a/b/c', '/a/b/c/d/..')
        True

    N)r   r   r   )Zpath1Zpath2r   r   r    paths_equal3   s    r$   c             C   s   t | r| S | ds"td|  t| \}}}}}ddlm} ||}|dkrd|dsd| }n,d}td	|tjr|d d
 |dd  }|| S )zlConvert a file:// URL to a path.

    Relative file URLs (i.e. `file:relative/path`) are not supported.
    zfile://z9You can only turn absolute file: urls into paths (not %s)r   )percent_decode) Z	localhostz	127.0.0.1z::1z\\z//r&   z^/([a-z])[:|]:   N)	r!   
startswithr   r   urlr%   r   r   I)r*   _Znetlocr"   r%   r   r   r    url_to_path?   s    


r-   c             C   s   t dd t| |D S )Nc             s   s   | ]\}}||kV  qd S )Nr   ).0tswr   r   r    	<genexpr>Y   s    z'tokenized_startswith.<locals>.<genexpr>)allzip)Ztest_iterableZstartswith_iterabler   r   r    tokenized_startswithX   s    r4   c             C   s   t tdd | D dh S )Nc             s   s$   | ]}t |d dd V  qdS )/N)tupler   )r.   fr   r   r    r1   ]   s    z&get_all_directories.<locals>.<genexpr>r   )sortedset)filesr   r   r    get_all_directories\   s    r<   c                sh   t | }|sdS g   fdd}t||} s> |d  nt| d sV | tdd  D S )Nr   c                s   t || s |  |S )N)r4   append)xy)leavesr   r    _processj   s    

z&get_leaf_directories.<locals>._processr6   c             s   s   | ]}d  |V  qdS )r5   N)r   )r.   Zleafr   r   r    r1   u   s    z'get_leaf_directories.<locals>.<genexpr>)r<   r   r=   r4   r7   )r;   ZdirectoriesrA   Zlastr   )r@   r    get_leaf_directories`   s    

rB   Fc                s&    fddt tfdd| D S )Nc                s    r| S |  dS )Nr5   )r   )r>   )already_splitr   r    <lambda>{       z%explode_directories.<locals>.<lambda>c             3   s    | ]}|rt t |V  qd S )N)r   r   )r.   	directory)maybe_splitr   r    r1   |   s   z&explode_directories.<locals>.<genexpr>)r:   r   )Zchild_directoriesrC   r   )rC   rG   r    explode_directoriesx   s    rH   c             C   sb   | dd}|dr| d S t| \}}t|\}}dd|||f  }|rZd|d|f S |S d	S )
a	  
    This must not return backslashes on Windows as that will break
    tests and leads to an eventual need to make url_to_path return
    backslashes too and that may end up changing files on disc or
    to the result of comparisons with the contents of them.
    .r&   2cz__pycache__/z%s.cpython-%s%scz%s%s%sr5   N)replacer)   r   r   )Zpy_pathpython_major_minor_versionZpyver_stringrF   py_fileZbasename_root	extensionZpyc_filer   r   r    pyc_path   s    
rP   c                s:   dd  D }fdd|D }t  fdd|D }|S )Nc             s   s   | ]}| d r|V  qdS )z.pyN)endswith)r.   r8   r   r   r    r1      s    z$missing_pyc_files.<locals>.<genexpr>c             3   s   | ]}|t | fV  qd S )N)rP   )r.   rN   )rM   r   r    r1      s   c             3   s   | ]}|d   kr|V  qdS )r   Nr   )r.   r   )r;   r   r    r1      s    )r7   )rM   r;   Zpy_filesZpyc_matchesresultr   )r;   rM   r    missing_pyc_files   s
    
rS   c             C   sF   |  dd\}}| dd\}}| | |   }}}|||fS )Nr'   r   =)rsplitstrip)Zep_definitionZcmd_modfuncZcommandmoduler   r   r    parse_entry_point_def   s    rY   c             C   s0   t rdS | rd| krd| } tdd| p*d S )Nz
python.exerI   binzpython%sr&   )r   r   )python_versionr   r   r    get_python_short_path   s
    
r\   c             C   s(   | d krd S t rdS t| }d| S d S )NzLib/site-packageszlib/python%s/site-packages)r   get_major_minor_version)r[   Zpy_verr   r   r    #get_python_site_packages_short_path   s    r^   Tc             C   sN   t | tstttddd | D }t|dkrJ|r@d|S d|S d S )Nr   c             s   s   | ]}|  r|V  qd S )N)isdigit)r.   rK   r   r   r    r1      s    z*get_major_minor_version.<locals>.<genexpr>rI   r&   )
isinstancer   AssertionErrorr7   r   lenr   )stringZwith_dotZdigitsr   r   r    r]      s
    r]   c               C   s   t rdS dS )NZScriptsrZ   )r   r   r   r   r    get_bin_directory_short_path   s    rd   c             C   s   t r| ddS | S )Nr5   \)r   rL   )r"   r   r   r    win_path_ok   s    rf   c             C   s   t r| ddS | S )Nre   z\\)r   rL   )r"   r   r   r    win_path_double_escape   s    rg   c             C   s   t dd| ddS )Nz	(\\(?! ))r5   z:////z://)r   subrL   )r"   r   r   r    win_path_backout   s    ri   r,   c             C   s:   | r$| d | d   kr |kr(n n| S d|| |f S dS )z

    Examples:
        >>> ensure_pad('conda')
        '_conda_'
        >>> ensure_pad('_conda')
        '__conda_'
        >>> ensure_pad('')
        ''

    r   r6   z%s%s%sNr   )nameZpadr   r   r    
ensure_pad   s    $rk   c             C   s$   | o"| d | d   kodkS   S )z

    Examples:
        >>> is_private_env_name("_conda")
        False
        >>> is_private_env_name("_conda_")
        True

    r   r6   r,   r   )env_namer   r   r    is_private_env_name   s    
rm   c             C   s0   | dk	r,t | \}}t|dkr$dS t|S dS )z

    Examples:
        >>> is_private_env_path('/some/path/to/envs/_conda_')
        True
        >>> is_private_env_path('/not/an/envs_dir/_conda_')
        False

    NZenvsF)r   r	   rm   )Zenv_pathZenvs_directoryrl   r   r   r    is_private_env_path   s    
rn   c             C   s   |  tjr| S | tj S )N)rQ   ossep)r"   r   r   r    right_pad_os_sep  s    rq   c             C   s(   t | \}}d|kr |pd |fS | d fS )NrI   )r   )Zpath_or_urlZdnfnr   r   r    split_filename  s    rs   c             C   sB   |  dr|}| d|dS |  dr:t }| d|dS | S d S )Nzsite-packages/zsite-packagesr   zpython-scripts/zpython-scripts)r)   rL   rd   )Zsource_short_pathZtarget_site_packages_short_pathZsp_dirZbin_dirr   r   r    get_python_noarch_target_path  s    

rt   r&   c          
      s   | sdS t d}|r6tjdtjtj|d}ntjdd}y$t|d| g	d
dd } W nZ tk
r } z<tjd	| d
d  fdd}d}t||| dd} W d d }~X Y nX | S )Nr&   bashZCYGPATHzcygpath.exez-upascii
r   z%rT)exc_infoc                s.   |  ddddddd} d | S )Nr   re   r5   r'   r&   z//)grouprL   )Z
found_pathfound)root_prefixr   r    _translation.  s    "z&win_path_to_unix.<locals>._translationzW(?<![:/^a-zA-Z])([a-zA-Z]:[\/\\]+(?:[^:*?"<>|]+[\/\\]+)*[^:*?"<>|;\/\\]+?(?![a-zA-Z]:))z;/z:/)whichro   environgetr"   r   dirname
subprocessZcheck_outputdecoder   	Exceptionlogdebugr   rh   rL   )r"   r{   ru   Zcygpather|   Zpath_rer   )r{   r    win_path_to_unix  s    "$(r   c             C   s   ddl m} || S )Nr   )find_executable)Zdistutils.spawnr   )
executabler   r   r   r    r}   6  s    r}   )F)N)T)r,   )r&   )CZ
__future__r   r   r   r   	functoolsr   Zloggingr   ro   os.pathr   r	   r
   r   r   r   r   r   r   r   compatr   r   r&   r   Z_vendor.auxlib.decoratorsr   Z_vendor.toolzr   r   r   Zurllib.parser   r   ImportErrorZurllibZurlparse__name__r   r   r!   r#   r$   r-   r4   r<   rB   rH   rP   rS   rY   r\   r^   r]   rd   rf   rg   ri   rk   rm   rn   rq   rs   rt   r   r}   r   r   r   r    <module>   sV   (

	




