
    ԋg$                        d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZ d dlZd dlZ ej                  e      ZdZd Zd Z G d d      Z G d	 d
      Zy)    )annotationsN)ClassVarz.dirlockc                 @    t         j                  j                  d      S )Nz#distributed.worker.use-file-locking)daskconfigget     5lib/python3.12/site-packages/distributed/diskutils.pyis_locking_enabledr      s    ;;??@AAr
   c                    	 t        j                  |        y # t        $ r Y y t        $ r%}t        j                  d|  d|        Y d }~y d }~ww xY w)NzFailed to remove z: )osunlinkFileNotFoundErrorOSErrorloggererror)pathes     r   safe_unlinkr      sJ    6
		$  6(b4556s    	AAAAc                  N    e Zd ZU dZded<   ded<   ded<   ddZd	 Zed
        Zy)WorkDirz8
    A temporary work directory inside a WorkSpace.
    strdir_path
_lock_pathzweakref.finalize
_finalizerNc                   ||J |'t        j                  ||j                        | _        nNt        j
                  j                  |j                  |      | _        t	        j                  | j                         t               rF	 t        j
                  j                  | j                  t        z         | _
        t        j
                  j                  | j                        rJ t        j                  d| j                         	 |j                         5  t        j                   | j                        | _        | j"                  j%                          d d d        |j0                  j3                  | j                         t5        j6                  | | j8                  || j                  | j"                  | j                        | _        y t5        j6                  | | j8                  |d d | j                        | _        y # 1 sw Y   xY w# t&        $ r, t        j)                  d| j                  d       d | _        Y w xY w# t*        $ r# t-        j.                  | j                  d        w xY w)N)prefixdirzLocking %r...zyCould not acquire workspace lock on path: %s .Continuing without lock. This may result in workspaces not being cleaned upTexc_info)ignore_errors)tempfilemkdtempbase_dirr   r   r   joinmkdirr   DIR_LOCK_EXTr   existsr   debug_global_locklocket	lock_file
_lock_fileacquirer   	exception	Exceptionshutilrmtree_known_locksaddweakreffinalize	_finalizer   )self	workspacenamer   s       r   __init__zWorkDir.__init__-   s   |v~--<$,,F	@R@RSDMGGLL););TBDMHHT]]#"$'',,t}}|/K"L77>>$//:::_doo>+"//1*0*:*:4??*K//1 2" ""&&t7%..DO &..dnnitT]]DO9 21  
+$$%
 !% %  '+DO
+  dmm4@sC   
B I H ?HH HH 2II II ,I3c                $    | j                          y)z,
        Dispose of this directory.
        N)r   )r9   s    r   releasezWorkDir.release^   s     	r
   c                   	 |j                  |       ||j                          |'|j                  j                  |       t	        |       y y # ||j                          |'|j                  j                  |       t	        |       w w xY wN)_purge_directoryr>   r4   remover   )clsr:   	lock_pathr-   r   s        r   r8   zWorkDir._finalized   s    	'&&x0$!!#$&&--i8I& % $!!#$&&--i8I& %s   A =B)NN)	__name__
__module____qualname____doc____annotations__r<   r>   classmethodr8   r	   r
   r   r   r   $   s8     MO  /b ' 'r
   r   c                      e Zd ZU dZded<   ded<   ded<    e       Zded<   ddZdd	Zd
 Z	d Z
d Zd Zej                  dk\  rd Znd Zd Zd Zd Zy)	WorkSpacez
    An on-disk workspace that tracks disposable work directories inside it.
    If a process crashes or another event left stale directories behind,
    this will be detected and the directories purged.
    r   r%   _global_lock_path_purge_lock_pathzClassVar[set[str]]r4   c                    | j                  |      | _        t        j                  j	                  | j                  d      | _        t        j                  j	                  | j                  d      | _        y )Nzglobal.lockz
purge.lock)_init_workspacer%   r   r   r&   rM   rN   )r9   r%   s     r   r<   zWorkSpace.__init__   sJ    ,,X6!#dmm]!K "T]]L Ir
   c                   t         j                  j                  |      }|g}t        j                  dk7  r(|j                  | dt        j                                 |D ]  }	 t        j                  |       |c S  t        j                  |dz         S # t        $ rE 	 t        j                  |      5  	 ddd       n# 1 sw Y   nxY wn# t        $ r Y Y {w xY wY |c S w xY w)zyCreate base_dir if it doesn't exist.
        If base_dir already exists but it's not writeable, change the name.
        win32-)r   N)r   )r   r   abspathsysplatformappendgetuidmakedirsFileExistsErrorr#   TemporaryFilePermissionErrorr$   )r9   r%   try_dirstry_dirs       r   rP   zWorkSpace._init_workspace   s     77??8,:<<7" OOxj"))+78GG$ N   x#~66 # !//G< =<<& NsN   $B	C%!C7C9	CCCC%	CC%CC%$C%c                B    t        j                  | j                  fi |S r@   )r,   r-   rM   r9   kwargss     r   r+   zWorkSpace._global_lock   s     6 6A&AAr
   c                B    t        j                  | j                  fi |S r@   )r,   r-   rN   r`   s     r   _purge_lockzWorkSpace._purge_lock   s     5 5@@@r
   c                8   t               sg S | j                  d      }	 |j                          	 t        | j	                               }|j                          g }| j                  d      }	 |j                          	 |D ]%  }| j                  |      s|j                  |       ' 	 |j                          |S # |j                          w xY w# t        j                  $ r g cY S w xY w# |j                          w xY w# t        j                  $ r Y |S w xY w)Nr   timeout)r   r+   r/   list_list_unknown_locksr>   r,   	LockErrorrc   _check_lock_or_purgerW   )r9   lock
candidatespurgedr   s        r   _purge_leftoverszWorkSpace._purge_leftovers   s   !#I
    +
	LLN!$":":"<=

 *	LLN
&D006d+ ' '   	 I	4   	 	s@   C B= /D  C- C- =CC*)C*-C?DDc              #  0  K   t        j                   t        j                  j                  | j                  dt
        z               D ]<  }	 t        j                  |      }t        j                  |j                        s9| > y # t        $ r Y Kw xY ww)N*)
globr   r   r&   r%   r(   statS_ISREGst_moder   )r9   psts      r   rh   zWorkSpace._list_unknown_locks   sn     277<<s\7IJKAWWQZ <<

+G L  s0   ABB B B	BBBB)      c                F    t        j                  || j                         y )N)onexc)r2   r3   _on_remove_error)r9   r   s     r   rA   zWorkSpace._purge_directory   s    MM($*?*?@r
   c                >      fd}t        j                  ||       y )Nc                0    j                  | ||d         S )N   )r{   )funcr   r!   r9   s      r   onerrorz+WorkSpace._purge_directory.<locals>.onerror   s    ,,T4!EEr
   )r   )r2   r3   )r9   r   r   s   `  r   rA   zWorkSpace._purge_directory   s    F MM(G4r
   c                   |j                  t              sJ || j                  v ryt        j	                  d|       t        j                  |d      }	 |j                          	 |dt        t                }t        j                  j                  |      r't        j                  d|       | j                  |       |j                          t!        |       y# t
        j                  $ r Y yw xY w# |j                          w xY w)z
        Try locking the given path, if it fails it's in use,
        otherwise the corresponding directory is deleted.

        Return True if the lock was stale.
        FzChecking lock file %r...r   re   Nz/Found stale lock file and directory %r, purgingT)endswithr(   r4   r   r*   r,   r-   r/   ri   lenr   r   r)   inforA   r>   r   )r9   rD   rk   r   s       r   rj   zWorkSpace._check_lock_or_purge   s     !!,///)))/;	15	LLN	 !5C$5#56Hww~~h'MxX%%h/LLNI  		 LLNs   C &AC4 C10C14Dc                F    t         j                  d||t        |             y )Nz&Failed to remove %r (failed in %r): %s)r   r   r   )r9   r   r   excs       r   r{   zWorkSpace._on_remove_error  s    =tT3s8Tr
   c                    	 | j                          t	        | fi |S # t        $ r t        j                  dd       Y .w xY w)a  
        Create and return a new WorkDir in this WorkSpace.
        Either the *prefix* or *name* parameter should be given
        (*prefix* is preferred as it avoids potential collisions)

        Parameters
        ----------
        prefix : str (optional)
            The prefix of the temporary subdirectory name for the workdir
        name : str (optional)
            The subdirectory name for the workdir
        z<Failed to clean up lingering worker directories in path: %s Tr    )rn   r   r   r   r   r`   s     r   new_work_dirzWorkSpace.new_work_dir  sM    	!!# t&v&&  	LLN  	s     A AN)r%   r   )r%   r   returnr   )rE   rF   rG   rH   rI   setr4   r<   rP   r+   rc   rn   rh   rU   version_inforA   rj   r{   r   r	   r
   r   rL   rL   p   sv     M (+uL$,J
78BA%N
 7"	A
	5<U'r
   rL   )
__future__r   rq   loggingr   r2   rr   rU   r#   r6   typingr   r,   r   	getLoggerrE   r   r(   r   r   r   rL   r	   r
   r   <module>r      sd    "   	   
     			8	$B6I' I'Xk' k'r
   