U
    b                      @   s  d dl mZmZmZmZ d dlmZ d dl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 ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z% dddefddZ&ddefddZ'dd Z(dd Z)d6ddZ*d7ddZ+e,d ej-Z.d!d" Z/d#d$ Z0d8d%d&Z1d'd( Z2d)d* Z3ed+d, Z4d9d.d/Z5d:d0d1Z6d2d3 Z7d4d5 Z8dS );    )absolute_importdivisionprint_functionunicode_literals)	getLogger)basenamedirnameisdirisfilejoinnormcaseN)warn   )dals)ROOT_ENV_NAME)contextenv_name)NULL)swallow_broken_pipe)paths_equal)	json_dump)	MatchSpec)EnvironmentLocationNotFound"DirectoryNotACondaEnvironmentErrorZProceedyesnor   c                 C   s   ||kst ||tkrtjs"|r4ddlm} | g }|D ].}||kr\|d|d   q<||d  q<d| d|f } dd |D }||d	< tj	
|  tj	  tj   }||krtd
|  qtj	
d tj	  || S qd S )Nr   
DryRunExit[%s]r   z	%s (%s)? /c                 S   s$   i | ]}||d  fD ]
}||qqS )r    ).0choiceZaltr!   r!   /lib/python3.8/site-packages/conda/cli/common.py
<dictcomp>$   s
     zconfirm.<locals>.<dictcomp> zInvalid choice: %s
)AssertionErrorr   r   dry_run
exceptionsr   appendr   sysstdoutwriteflushstdinreadlinestriplowerprint)messagechoicesdefaultr)   r   ZoptionsZoptionZuser_choicer!   r!   r$   confirm   s,    

r8   c                 C   s   |t krtjs|r$ddlm} | tjr.dS zt| d||d}W n( tk
rj   ddlm} |dY nX |dkrddlm} |d	dS )
Nr   r   Tr   )r5   r6   r7   r)   )CondaSystemExitz
Operation aborted.  Exiting.r   zExiting.)	r   r   r)   r*   r   Z
always_yesr8   KeyboardInterruptr9   )r5   r7   r)   r   r#   r9   r!   r!   r$   
confirm_yn5   s    r;   c                 C   s2   t dt | js.| js.ddlm} |d| d S )NzRconda.cli.common.ensure_name_or_prefix is pending deprecation in a future release.r   CondaValueErrorzOeither -n NAME or -p PREFIX option required,
try "conda %s -h" for more details)r   PendingDeprecationWarningnameprefixr*   r=   )argsZcommandr=   r!   r!   r$   ensure_name_or_prefixF   s    rB   c                 C   s"   t | tjp t| tttjkS )z
    Determines whether the args we pass in are pointing to the active prefix.
    Can be used a validation step to make sure operations are not being
    performed on the active prefix.
    )r   r   active_prefixr   r   r@   r!   r!   r$   is_active_prefixP   s    rE   Fc                 C   sz   zt | }W n$   ddlm} |d|  Y nX |j}| sr|rrddlm} |d|dt| t|  | f t|S )Nr   r<   z!invalid package specification: %szeversion specifications not allowed with 'update'; use
    conda update  %s%s  or
    conda install %s )r   r*   r=   r?   Z
_is_simplelenstr)argjsonupdatespecr=   r?   r!   r!   r$   arg2spec]   s    rM   c                    s    fdd| D S )Nc                    s   g | ]}t | d qS )rJ   )rM   )r"   rI   rN   r!   r$   
<listcomp>o   s     z#specs_from_args.<locals>.<listcomp>r!   )rA   rJ   r!   rN   r$   specs_from_argsn   s    rP   a  
    (?P<name>[^=<>!\s]+)                # package name
    \s*                                 # ignore spaces
    (
        (?P<cc>=[^=]+(=[^=]+)?)         # conda constraint
        |
        (?P<pc>(?:[=!]=|[><]=?|~=).+)   # new pip-style constraints
    )?$
    c                 C   s   |  dd  S )N#r   )splitrstrip)liner!   r!   r$   strip_comment   s    rU   c                 C   s   t t| }|d krd S |d |d|d  }}}|rT||dd S |r|dr|ddks~td		| |d
std	| |dd}d

|d
d d d }|d | d | S |d |dd S n|S d S )Nr?   ccpc=rF   z~= z~=   z7Overly complex 'Compatible release' spec not handled {}.z)No '.' in 'Compatible release' version {}r&   z.*z >=z,==)spec_patmatchrU   groupr3   replace
startswithcountr(   formatr   rR   )rT   mr?   rV   rW   ZverZver2r!   r!   r$   spec_from_line   s"    &
rd   c                 C   s   ddl m} d}|| dd}g }z|t|D ]n}| }|r,|drHq,|dkrTd}|rd|| q,t|}|d krddlm} |d	|| f || q,W n8 t	k
r }	 zdd
lm
}
 |
||	W 5 d }	~	X Y nX W 5 Q R X |S )Nr   )TmpDownloadF)verboserQ   z	@EXPLICITTr<   zcould not parse '%s' in: %s)CondaFileIOError)Zgateways.connection.downloadre   openr2   r`   r+   rd   r*   r=   IOErrorrg   )ZurlrJ   re   ZexplicitpathspecsrT   rL   r=   erg   r!   r!   r$   specs_from_url   s2    
&rm   c                    s   t  fdd|D S )Nc                 3   s   | ]}|  d   kV  qdS )r   N)rR   )r"   rL   namesr!   r$   	<genexpr>   s     z!names_in_specs.<locals>.<genexpr>)any)ro   rk   r!   rn   r$   names_in_specs   s    rr   c                 C   s   | rdd |  S dS d S )Nr   rF   r&   )r   )Zfeaturesr!   r!   r$   disp_features   s    rs   c                 C   s   t dt|  d S )Nzconda.stdout)r   infor   )dr!   r!   r$   stdout_json   s    rv   Tc                 K   sr   d| i}| dd }|r\d|kr6dd |d D |d< d|krTdd |d D |d< ||d< || t| d S )NsuccessactionsZLINKc                 S   s   g | ]}|  qS r!   Zdist_fields_dumpr"   Zprecr!   r!   r$   rO      s     z'stdout_json_success.<locals>.<listcomp>ZUNLINKc                 S   s   g | ]}|  qS r!   ry   rz   r!   r!   r$   rO      s     )poprK   rv   )rw   kwargsresultrx   r!   r!   r$   stdout_json_success   s    
r~   c                    sB    rt d t d  fdd}| D ]}|| q$ r>t d d S )Nz# conda environments:rQ   c                    sf   d} t jkrdnd} t jkr&t}n&t fddt jD rHt }nd}rbt||| f  d S )Nz%-20s  %s  %s*rF   c                 3   s   | ]}t |t V  qd S )N)r   r   )r"   Zenvs_dirrD   r!   r$   rp      s     z4print_envs_list.<locals>.disp_env.<locals>.<genexpr>r&   )r   rC   Zroot_prefixr   rq   Z	envs_dirsr   r4   )r@   ZfmtZactiver?   outputrD   r$   disp_env   s    

z!print_envs_list.<locals>.disp_envr&   )r4   )Zknown_conda_prefixesr   r   r@   r!   r   r$   print_envs_list   s    
r   c                  C   s4   ddl m}  tjs0|  s0ddlm} |tdd S )Nr   )is_admin)OperationNotAllowedz
            The create, install, update, and remove operations have been disabled
            on your system for non-privileged users.
        )Z
common._osr   r   Znon_admin_enabledr*   r   r   )r   r   r!   r!   r$   check_non_admin   s    r   c                 C   s.   t | r"tt| dds*t| nt| | S )a  Verifies the prefix is a valid conda environment.

    :raises EnvironmentLocationNotFound: Non-existent path or not a directory.
    :raises DirectoryNotACondaEnvironmentError: Directory is not a conda environment.
    :returns: Valid prefix.
    :rtype: str
    z
conda-metahistory)r	   r
   r   r   r   rD   r!   r!   r$   validate_prefix   s
    
r   )FF)F)F)T)T)9Z
__future__r   r   r   r   Zloggingr   os.pathr   r   r	   r
   r   r   rer,   warningsr   Z
auxlib.ishr   Zbase.constantsr   Zbase.contextr   r   Zcommon.constantsr   Z	common.ior   Zcommon.pathr   Zcommon.serializer   Zmodels.match_specr   r*   r   r   r8   r;   rB   rE   rM   rP   compileVERBOSEr\   rU   rd   rm   rr   rs   rv   r~   r   r   r   r!   r!   r!   r$   <module>   sF    


	



	