U
    b                     @   st  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
 d dlmZ d dlZd dlmZmZmZmZmZmZ d dlZd dlmZ d dlmZmZ d dlZd d	lmZ d
dlmZ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- ddl.m/Z/ ee0Z1e2dZ3G dd de4Z5dd Z6d#ddZ7ed$e8ee8 e9dddZ:d%dd Z;d&d!d"Z<dS )'    )absolute_importdivisionprint_functionunicode_literals)contextmanager)EINVALEXDEVEPERM)	getLoggerN)dirnameisdirsplitbasenamejoinexists)move)PopenPIPE)Optional   )exp_backoff_fnmkdir_pmkdir_p_sudo_safe)rm_rf)lexists   )context)DRY_RUN_PREFIX)on_win)expand)NotWritableErrors   ^(#!((?:\\ |[^ \n\r])+)(.*))c                   @   s   e Zd ZdS )CancelOperationN)__name__
__module____qualname__ r%   r%   9lib/python3.8/site-packages/conda/gateways/disk/update.pyr!      s   r!   c                 C   s|   d }zdtt| d}td|  | }|d z ||| |  W W dS  t	k
rd   Y nX W 5 |rv|   X dS )Nzrb+z"in-place update path locked for %sr   TF)
closer   openlogtracereadseekwritetruncater!   )Zfile_full_pathcallbackZfhdatar%   r%   r&   update_file_in_place_as_binary#   s    



r1   Fc              
   C   s2  t |r|rt| t | r"td| | zt| | W n tk
r } ztrt| t|krtj	
| rttjd}t|d}t|rt| \}}t|}tdd||||gttd}	|	 \}
}ntd| n2|jtttfkrtd| ||j t| | n W 5 d }~X Y nX ntd	|  d S )
Nzrenaming %s => %sZcondabinzrename_tmp.batzcmd.exez/C)stdoutstderrzx{} is missing.  Conda was not installed correctly or has been corrupted.  Please file an issue on the conda github repo.zHCould not rename %s => %s due to errno [%s]. Falling back to copy/unlinkz.cannot rename; source path does not exist '%s')r   r   r)   r*   osrenameEnvironmentErrorr   r   pathisfiler   r   Zconda_prefixr   r   r   r   r   Zcommunicatedebugformaterrnor   r   r	   r   )source_pathdestination_pathforceeZcondabin_dirZrename_scriptZ_dirnameZ_src_fnZ_dest_fnpr2   r3   r%   r%   r&   r5   9   sF    



    r5   )sourcedestinationdry_runc              
   c   s   |dkrt  }|r6tt d|  d|  dV  dS zt| |dd dV  W n4 tk
r } zt|| dd |W 5 d}~X Y nX dS )z
    Used for removing a directory when there are dependent actions (i.e. you need to ensure
    other actions succeed before removing it).

    Example:
        with rename_context(directory):
            # Do dependent actions here
    Nz rename_context z > T)r>   )tempfileZmkdtempprintr   r5   	Exception)rA   rB   rC   excr%   r%   r&   rename_context]   s    

rH   c                 C   s   t t| || d S )N)r   r5   )r<   r=   r>   r%   r%   r&   backoff_renamex   s    rI   c              
   C   s   zt | } td|  t| r0t| d  W dS t| }t|s\|r\|rRt| qlt	| ntt| slt
t| d W 5 Q R X W dS W n6 ttfk
r } zt| |j|dW 5 d }~X Y nX d S )Nztouching path %sTaF)Z	caused_by)r   r)   r*   r   r4   utimer   r   r   r   AssertionErrorr(   IOErrorOSErrorr    r;   )r7   mkdirZ	sudo_safedirpathr?   r%   r%   r&   touch|   s"    




rQ   )F)NF)F)FF)=Z
__future__r   r   r   r   
contextlibr   r;   r   r   r	   Zloggingr
   r4   os.pathr   r   r   r   r   r   reZshutilr   
subprocessr   r   rD   typingr    r   r   r   deleter   linkr   Zbase.contextr   Zbase.constantsr   Zcommon.compatr   Zcommon.pathr   
exceptionsr    r"   r)   compileZSHEBANG_REGEXrF   r!   r1   r5   strboolrH   rI   rQ   r%   r%   r%   r&   <module>   s6    

$
