
    DIe*                     X   d 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	m
Z
mZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZ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&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z- e$sddlm.Z.  ee/          Z0d Z1d Z2d Z3ddZ4e4xZ5xZ6Z7d Z8e&fdZ9d Z:dS )z6Disk utility functions for deleting files and folders.    N)ENOENT)	getLogger)environgetcwdmakedirsrenamermdirscandirunlinkwalk)	abspathbasenamedirnameexistsisdirisfilejoinnormpathsplit)STDOUTCalledProcessErrorcheck_output   )CONDA_TEMP_EXTENSION)context)on_win   )	MAX_TRIESexp_backoff_fn)islinklexists)make_writablerecursive_make_writable)whichc           
      :   t          |           } t          r	 t          d|  ddt                    }nQ#  	 d }ddlm} ddlm}  |dd	d
          5 }|                    d ||            d           |                    d           |                    d ||            d           |                    d           |j	        }d d d            n# 1 swxY w Y   t          |          5 }|                                }	| |	v sJ 	 d d d            n# 1 swxY w Y   t          d         }
d}t          |
dd|gd
d d d |          }n~# t          $ rq}|j        dk    r/t                              d                    ||                      t                              d                    |                     Y d }~nd }~ww xY wY nxY w	 t'          d           n#  Y nxY wt)          d          rt+          d          }|rk	 t          |dddt-          t/                      d          dz   | dz   gt                    }n-# t          $ r  t                              d|            Y nw xY wt1          j        d           t1          j        |            d S ) Nz
RD /S /Q "z" > NUL 2> NULT)shellstderrr   )Utf8NamedTemporaryFile)quote_for_shellwz.batF)modesuffixdeletezRD /S 
zchcp 65001
zEXIT 0
COMSPECi   z/dz/c)r&   stdoutr'   stdincreationflags   z7Removing folder {} the fast way failed.  Output was: {}z:removing dir contents the fast way failed.  Output was: {}z.emptyrsyncz-az--forcez--delete/r'   z8removing dir contents the fast way failed.  Output was: )r   r   r   r   conda.auxlib.compatr(   conda.utilsr)   writenameopenreadr   r   
returncodelogerrorformatdebugr   r   r$   r   r   shutilrmtree)pathargskwargsoutr:   r(   r)   
batch_filecontentscontentcomspecCREATE_NO_WINDOWer4   s                 :lib/python3.11/site-packages/conda/gateways/disk/delete.pyrC   rC   %   s   D>>D \$<	 1T111f  CC/	.FFFFFF777777++VE   +$$%Good.C.C%G%G%GHHH$$^444$$%Good.C.C%G%G%GHHH$$Z000%?D+ + + + + + + + + + + + + + + $ZZ +8&mmooG7?????+ + + + + + + + + + + + + + + "),#-  #dD$/"2   &   <1$$IIQXX #   
 IIT[[        	X	D
 ?? 	$'NNE &! %& 844s: 3J  &
 
 
CC *   IIXSVXX    
 M(###
M$s   6 GEA6CEC	EC	E1DED	E D	!+EG
GA'G>GGGG! !G%	<I 'I0/I0c                 `   	 t          |            t          |            dS # t          $ r 	 t          | | dz              Y dS # t          $ rZ t          r t          t          j        d          }t          |d          }t          |          rt          |           \  }}| dz   }d}t          |          r'|j        d         d| z   }|dz  }t          |          'd}	 t          dd	|||t          |          gt          
          }nk# t          $ r1 t                               d                    | |                     Y n1w xY wt                               d                    |                     t                               d                    |                      Y Y dS w xY ww xY w)zIf files are in use, especially on windows, we can't remove them.
    The fallback path is to rename them (but keep their folder the same),
    which maintains the file handle validity.  See comments at:
    https://serverfault.com/a/503769
    z.conda_trashcondabinzrename_tmp.batr   r   z.conda_trash_z	< empty >zcmd.exez/Cr6   z6renaming file path {} to trash failed.  Output was: {}zx{} is missing.  Conda was not installed correctly or has been corrupted.  Please file an issue on the conda github repo.znCould not remove or rename {}.  Please remove this file manually (you may need to reboot to free file handles)N)r"   r   OSErrorr   r   r   r   conda_prefixr   r   r   splitextr   r   r   r   r>   rA   r@   warn)rD   condabin_dirtrash_script_dirname_fndest_fncounterrG   s           rN   unlink_or_rename_to_trashr[      s$   1dt . . .-	4.////// +	 +	 +	 &  $G$8*EE#L2BCC,'' !$)$KKMHc"^3GG // %")"21"58Q8Q8Q"Q1 !// % &C* ) $ , ( # ( 1 1 $*
 
 
 .   		T[[ $c      IIUU[U[(V V   HH;;A6$<<      Q+	.sL   " F-ABF)")DF)8EF)EAF)$F-(F))F-c                    t          |           }t          |          rit          t          |          d           sPt	          |           t          |          }t          |          r!t          t          |          d           Jd S d S d S d S )N)r   r   nextr
   r	   )rD   parent_paths     rN   remove_empty_parent_pathsr_      s    $--K


 +T'+*>*>%E%E +kk** 

 +T'+*>*>%E%E + + + + + + + + +    r3   TFc                 J   	 t          |           } t                              d|            t          |           rt	          |           st          |            n:t          |           rt          |            nt                              d|            t          |           rt                              d|            dS n3# t          |           rt                              d|             Y dS w xY wt          |           rt          |            |rt          |            dS )z
    Completely delete path
    max_retries is the number of times to retry on failure. The default is 5. This only applies
    to deleting a directory.
    If removing path fails and trash is True, files will be moved to the trash directory.
    zrm_rf %sz0rm_rf failed. Not a link, file, or directory: %szrm_rf failed for %sFT)r   r>   tracer   r    backoff_rmdirr!   r[   infodelete_trashr_   )rD   max_retriestrashclean_empty_parentsrE   kws         rN   rm_rfrj      s!   t}}		*d###;; 	Pvd|| 	P$T]] 	P%d++++IIH$OOO4== 	HH*D1115	74== 	HH*D1115555555T{{ T (!$'''4s   BC ,C10C1c           	         | st           j        } ddht          | d          D ]\  }}}fd|D             |d d <   |D ]}t          j        |d          st          j        |dt          z             rit          ||          }	 t          |           t          |           d# t          $ r,}t          
                    d||j        |           Y d }~d }~ww xY wd S )	NenvspkgsTtopdownc                     g | ]}|v|	S  rq   ).0dexcludes     rN   
<listcomp>z delete_trash.<locals>.<listcomp>   s#    777aw&6&61&6&6&6r`   *.conda_trash**z%r errno %d
Cannot unlink %s.)sysprefixr   fnmatchr   r   r   r_   rQ   r>   rA   errno)ry   rootdirsfilesfnfilenamerM   rt   s          @rN   re   re      sA    vG!&$777 V VdE7777d777QQQ 		V 		VBr#344 VC..9 9 V  b>>V8$$$-h7777 V V VII>17HUUUUUUUUVV		VV Vs   B$$
C."CCc                     t          |           sd S fdfd}	 t          |            n#  Y nxY wt          | d          D ](\  }}}|D ]}t          t	          ||                      )d S )Nc                     t          |d         dd           t          k    rd S t          t          |                      | |           d S )Nr   r{   )	max_tries)getattrr   r#   r   )funcrD   exc_infor   s      rN   retryzbackoff_rmdir.<locals>.retry   sM    8A;..&88FCCCCT




r`   c                     	 t          |            t          t          |            d S # t          $ r8}|j        t
          k    rt                              d|            n Y d }~d S d }~ww xY w)N)onerrorr   zno such file or directory: %s)r#   r   rC   rQ   r{   r   r>   rb   )rD   rM   r   r   s     rN   _rmdirzbackoff_rmdir.<locals>._rmdir  s    	#D)))64)LLLLLL 	 	 	w&  		94@@@@ A@@@@@	s   ', 
A.-A))A.Frn   )r   rC   r   r[   r   )dirpathr   r   r|   r}   r~   filer   s    `     @rN   rc   rc      s    >>          w!'5999 8 8dE 	8 	8D%d4&6&67777	88 8s   0 4c                     t          |            }|sQt          |           D ]A\  }}}|D ]8}t          j        |d          s!t          j        |dt          z             s  dS 9BdS )zSometimes we can't completely remove a path because files are considered in use
    by python (hardlinking confusion).  For our tests, it is sufficient that either the
    folder doesn't exist, or nothing but temporary file copies are left.
    rv   rw   FT)r   r   rz   r   )rD   cleanr|   r}   fnsr   s         rN   path_is_cleanr     s    
 tE !#Dzz 	! 	!OD$ ! !OB(899!r31E+EFF! !555! 4r`   )r3   TF);__doc__rz   rB   rx   r{   r   loggingr   osr   r   r   r   r	   r
   r   r   os.pathr   r   r   r   r   r   r   r   r   
subprocessr   r   r   base.constantsr   base.contextr   common.compatr    r   r   linkr    r!   permissionsr"   r#   r$   __name__r>   rC   r[   r_   rj   try_rmdir_all_emptymove_to_trashmove_path_to_trashre   rc   r   rq   r`   rN   <module>r      s_   = <   



             N N N N N N N N N N N N N N N N N N N N
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 @ ? ? ? ? ? ? ? ? ? 2 2 2 2 2 2 # # # # # # # # # # # # ' ' ' ' ' ' ' ' ! ! ! ! ! ! ! ! ? ? ? ? ? ? ? ?  i` ` `F7 7 7t+ + +   8 <A @  @m&8V V V$ &/ 8 8 8 8@    r`   