U
    b|K                     @   s  d dl mZmZmZmZ d dlmZ d dlmZm	Z	 d dl
Z
d dlmZ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Zdd	lmZ dd
lmZmZ ddlmZmZ ddlm Z m!Z! ddl"m#Z# e
$e%Z&dd Z'dlddZ(dd Z)dd Z*dd Z+dd Z,e-dddde'e'dd d!d"d#d$d%d&d'gdd(d)dd*d+Z.e-e.e(e dd,d-Z/ere-d.d/d0d1dd2d3d4d4d5d6d7d8d9d:d;ge'e'd<d=d>d?e-e.d@dAe*e)dBe-e/d@dCe-e/dDdCe-e/dEdCe-e/dFdCe-e/dGdCdHZ0n2e-e.dDdCe-e.dIdJdKe-e.dGdCe-e.dLdMdNdOZ0e# Z1Z2dPdQ Z3dmdSdTZ4eddUdVdW Z5dXdY Z6erTe7dZZ8e7d[Z9d\d] Z:n0zd d^l;mZ: W n e<k
r   d_d] Z:Y nX dndadbZ=dodddeZ>dfdg Z?dhdi Z@edjdk ZAdS )p    )absolute_importdivisionprint_functionunicode_literals)contextmanager)	lru_cachewrapsN)abspathjoinisfilebasenamedirname)environ)Path   )
CondaError)shlex_split_unicodeUtf8NamedTemporaryFile)on_win
isiterable)win_path_to_unixwhich)path_to_urlc                 C   s   | S )z<Used as a dummy path converter where no conversion necessary pathr   r   *lib/python3.8/site-packages/conda/utils.pypath_identity   s    r    c                    sr   t | dkr:d| ks.| d dkr:| ddkr:| ddS  d } fdd}t||| }td	d
d |}|S )zConvert a path or :-separated string of paths into a Windows representation

    Does not add cygdrive.  If you need that, set root_prefix to "/cygdrive"
    r   ;:/\z$(/[a-zA-Z]/(?:(?![:\s]/)[^:*?"<>])*)c                    s:   |  d}d|t d  |t d d  ddS )Nr   z{0}:{1}r      r!   r"   )groupformatlenreplace)Z
found_pathr$   root_prefixr   r   _translation&   s    
z&unix_path_to_win.<locals>._translationz:([a-zA-Z]):\\c                 S   s   d|  dd  d S )Nr   r   r   z:\)r$   )matchr   r   r   <lambda>,       z"unix_path_to_win.<locals>.<lambda>)r&   countr'   resub)r   r)   Zpath_rer*   Ztranslationr   r(   r   unix_path_to_win   s    .r1   c                 C   s
   t | dS Nz	/cygdrive)r   r   r   r   r   win_path_to_cygwin2   s    r3   c                 C   s
   t | dS r2   )r1   r   r   r   r   cygwin_path_to_win6   s    r4   c                    s   d  fdd| dD S )N
c                 3   s   | ]} |V  qd S Nr   ).0line
translatorr   r   	<genexpr>;   s     z#translate_stream.<locals>.<genexpr>)r
   split)streamr:   r   r9   r   translate_stream:   s    r>   c                 C   sT   | dk rd|  S | d }|dk r,dt | S |d }|dk rDd| S |d }d| S )a  
    Return the number of bytes n in more human readable form.

    Examples:
        >>> human_bytes(42)
        '42 B'
        >>> human_bytes(1042)
        '1 KB'
        >>> human_bytes(10004242)
        '9.5 MB'
        >>> human_bytes(100000004242)
        '93.13 GB'
    i   z%d Bz%d KBz%.1f MBz%.2f GB)round)nkmgr   r   r   human_bytes>   s    rD   z/bin/echoz.shz2>/dev/nullr    zecho $CONDA_DEFAULT_ENVz
echo $PATHzecho $CONDA_PROMPT_MODIFIERZPS1r!   zexport z-lz-c)r"   r!   sourcez${})binpathrE   env_script_suffixnul	path_frompath_topathsepprintdefaultenv	printpathprintps1	promptvarsepset_var
shell_argsshell_suffixslash_convertsource_setuptest_echo_extra
var_formatz_python -c "import os; print(';'.join(os.environ['PATH'].split(';')[1:]))" | cygpath --path -f -)rJ   rK   rG   rN   z@echoz%{}%z	\Scripts\Zcallz
1>NUL 2>&1zset .batz@echo %PROMPT%ZPROMPTzMIF NOT "%CONDA_DEFAULT_ENV%" == "" (
echo %CONDA_DEFAULT_ENV% ) ELSE (
echo()z@echo %PATH%cmd.exe/d/c)r!   r"   r"   r   )rE   rX   rG   rV   rW   rI   rR   rT   rH   rO   rP   rM   rN   exerS   rJ   rK   rU   rQ   rL   bash.exez	/Scripts/)r]   rG   rJ   rK   )r]   bashsh.exezsh.exezsh)rZ   cygwinr^   r_   r`   ra   rb   dash.)r]   rV   fish )r]   rL   )r_   rd   rb   rf   c                 C   s   ddl m} || S )Nr   )compute_md5sum)Zgateways.disk.readrh   )r   rh   r   r   r   md5_file   s    ri   md5c              	   C   sL   dd l }||}t| d"}|d}|s.q:|| qW 5 Q R X | S )Nr   rbi   )hashlibnewopenreadupdateZ	hexdigest)r   moderl   hZfichunkr   r   r   hashsum_file   s    

rt   )maxsizec                  C   s^   z>t tt  } | jr$| j} q| j}|j}tt|}W n t	k
rX   tj
 Y S X |S )a  Since conda is installed into non-root environments as a symlink only
    and because sys.prefix follows symlinks, this function can be used to
    get the 'unfollowed' sys.prefix.

    This value is usually the same as the prefix of the environment into
    which conda has been symlinked. An example of when this is necessary
    is when conda looks for external sub-commands in find_commands.py
    )nextitersys_current_framesvaluesf_backf_codeco_filenamer   	Exceptionprefix)framecodefilenameZ
unfollowedr   r   r   sys_prefix_unfollowed   s    
r   c                  G   s(   t | dkr t| d r | d } t| S )a$  Properly quote arguments for command line passing.

    For POSIX uses `shlex.join`, for Windows uses a custom implementation to properly escape
    metacharacters.

    :param arguments: Arguments to quote.
    :type arguments: list of str
    :return: Quoted arguments.
    :rtype: str
    r   r   )r&   r   
_args_join)	argumentsr   r   r   quote_for_shell  s    r   z["%\s^<>&|]z(["%])c                    s    dd  d  fdd| D S )*Return a shell-escaped string from *args*.c                 S   s.   | sdS t | s| S td| } d|  dS )Nz""z\1\1")
_RE_UNSAFEsearch_RE_DBLr0   )sr   r   r   quote   s    
z_args_join.<locals>.quoterg   c                 3   s   | ]} |V  qd S r6   r   r7   argr   r   r   r;   ,  s     _args_join.<locals>.<genexpr>r
   argsr   r   r   r     s    r   r   c                    s$   ddl m  d fdd| D S )r   r   r   rg   c                 3   s   | ]} |V  qd S r6   r   r   r   r   r   r;   6  s     r   )shlexr   r
   r   r   r   r   r   2  s    assertc                 C   sf   t | tr2|dkr ds2tdnt| } td t| s@| f} tdd | D rZtdt| } | S )Nr   Fz'Please ensure arguments are not stringszKPlease ensure arguments is not a string; used `shlex_split_unicode()` on itc                 S   s   g | ]}t |qS r   )r   r   r   r   r   
<listcomp>T  s     z%massage_arguments.<locals>.<listcomp>z)Individual arguments must not be iterable)	
isinstancestrAssertionErrorr   logZwarningr   anylist)r   errorsr   r   r   massage_arguments<  s    

r   Fc              	   C   s  t |}|stt|d}nd }d }d}t|dkrDd|d krDd}trFt }	|rpddlm}
 t|
dd	d
}nt	dtt| d	d
}t
d|ddd}|rdnd}|d| |d| |d| |d| |d| |r`ddlm} |d| |d|| |d|tj |d| |d| |rz|d |d |d||| |d| |r|d  |d |r|d!|d  n2td"d# |D rtd$|d%|t|  |d| |d&| |j}W 5 Q R X |	d'd(|g}nbtd)pVtd*}|d krjtd+|rtt| d,d-d.d/g}d0}|g}n"t	d1tt| d,d/g}d}g }t
d|dd2}|rddlm} |d3| d  t|d=| }|r|d6 |d7| |d8| |d9|t| |rL|d: |rj|d!d;| n|d!t|  |j}W 5 Q R X |r|d<|g}n||g}||fS )>Nz.tmpFr   r5   r   T)CONDA_PACKAGE_ROOTshellZcondabinz	conda.batZ	CONDA_BATwrY   )rq   r   suffixdeleter   @z{}ECHO OFF
z{}SET PYTHONIOENCODING=utf-8
z{}SET PYTHONUTF8=1
z_{}FOR /F "tokens=2 delims=:." %%A in ('chcp') do for %%B in (%%A) do set "_CONDA_OLD_CHCP=%%B"
z{}chcp 65001 > NUL
)CONDA_SOURCE_ROOTz{}SET CONDA_DEV=1
z{}SET PYTHONPATH={}
z{}SET CONDA_EXE={}
z{}SET _CE_M=-m
z{}SET _CE_CONDA=conda
z%echo *** environment before *** 1>&2
z	SET 1>&2
z{0}CALL "{1}" activate "{2}"
z-{}IF %ERRORLEVEL% NEQ 0 EXIT /b %ERRORLEVEL%
z$echo *** environment after *** 1>&2
z{0}
c                 s   s   | ]}d |kV  qdS )r5   Nr   r   r   r   r   r;     s     z'wrap_subprocess_call.<locals>.<genexpr>aW  Support for scripts where arguments contain newlines not implemented.
.. requires writing the script to an external file and knowing how to transform the command-line (e.g. `python -c args` => `python file`) in a tool dependent way, or attempting something like:
.. https://stackoverflow.com/a/15032476 (adds unacceptable escapingrequirements)z{0}{1}
z{}chcp %_CONDA_OLD_CHCP%>NUL
r[   r\   r_   ZshzNo compatible shell found!binZpythonz-mcondaz--devZ	CONDA_EXE)rq   r   r   z>&2 export PYTHONPATH=shell.posixhookz.>&2 echo '*** environment before ***'
>&2 env
z>&2 echo "$({0})"
zeval "$({0})"
zconda activate {0} {1}
z->&2 echo '*** environment after ***'
>&2 env
rg   z-x)r   r   )r   r	   r
   r&   r   get_comspecr   r   r   getr   writer%   r   r   rx   
executabler   r   r   namer   r~   )r)   r   dev_modeZdebug_wrapper_scriptsr   Zuse_system_tmp_pathZ
tmp_prefixZscript_callerZ	multilinecomspecr   Z	conda_batZfhZsilencerr   Zcommand_argsZ
shell_pathZ	conda_exeZdev_argZdev_argsZhook_quotedr   r   r   wrap_subprocess_callZ  s     






r   c                  C   s|   t tdd dkrttdo0ttd ddtdoHttd ddfD ]} | rLt| rL| td<  qtqLtd td S )zReturns COMSPEC from envvars.

    Ensures COMSPEC envvar is set to cmd.exe, if not attempt to find it.

    :raises KeyError: COMSPEC is undefined and cannot be found.
    :returns: COMSPEC value.
    :rtype: str
    ZCOMSPECr   rZ   Z
SystemRootZSystem32ZwindirzFcmd.exe could not be found. Looked in SystemRoot and windir env vars.
)r   r   r   lowerr
   r   r   warn)r   r   r   r   r     s    	
r   c                    s   t   fdd}|S )zl
    Ensures that the directory exists for functions returning
    a Path object containing a directory
    c               
      sb    | |}t |tr^z|jddd W n6 tk
r\ } ztd| d| W 5 d }~X Y nX |S )NT)parentsexist_okzKError encountered while attempting to create cache directory.
  Directory: 
  Exception: )r   r   mkdirOSErrorr   )r   kwargsresultexcfuncr   r   wrapper  s    

z"ensure_dir_exists.<locals>.wrapper)r   )r   r   r   r   r   ensure_dir_exists  s    r   c               
   o   s\   zt | |}|V  W n: tk
rN } ztd| d  d| W 5 d}~X Y nX |  dS )z
    Allows us to open files while catching any exceptions
    and raise them as CondaErrors instead.

    We do this to provide a more informative/actionable error output.
    z?Error encountered while reading or writing from cache.
  File: r   r   N)rn   r   r   close)r   r   fpr   r   r   r   	safe_open  s    

r   )r   )rj   )r   )F)BZ
__future__r   r   r   r   
contextlibr   	functoolsr   r   Zloggingos.pathr	   r
   r   r   r   osr   pathlibr   r/   rx   r   r   Zauxlib.compatr   r   Zcommon.compatr   r   Zcommon.pathr   r   Z
common.urlr   Z	getLogger__name__r   r   r1   r3   r4   r>   rD   dictZunix_shell_baseZmsys2_shell_baseZshellsZurlpathZurl_pathri   rt   r   r   compiler   r   r   r   ImportErrorr   r   r   r   r   r   r   r   r   <module>   s  


     I    






$ 
t