
    0Fie$                        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dS )    )annotationsN)ClassVarz.dirlockc                 @    t           j                            d          S )Nz#distributed.worker.use-file-locking)daskconfigget     5lib/python3.11/site-packages/distributed/diskutils.pyis_locking_enabledr      s    ;??@AAAr
   c                    	 t          j        |            d S # t          $ r Y d S t          $ r+}t                              d|  d|            Y d }~d S d }~ww xY w)NzFailed to remove z: )osunlinkFileNotFoundErrorOSErrorloggererror)pathes     r   safe_unlinkr      s    6
	$    6 6 6444445555555556s    
A	A AAc                  V    e Zd ZU dZded<   ded<   ded<   ddZd	 Zed
             ZdS )WorkDirz8
    A temporary work directory inside a WorkSpace.
    strdir_path
_lock_pathzweakref.finalize
_finalizerNc                t   ||J |!t          j        ||j                  | _        nCt          j                            |j        |          | _        t	          j        | j                   t                      r	 t          j                            | j        t          z             | _
        t          j                            | j
                  rJ t                              d| j
                   	 |                                5  t          j        | j
                  | _        | j                                         d d d            n# 1 swxY w Y   n9# t&          $ r, t                              d| j
        d           d | _        Y nw xY wn*# t*          $ r t-          j        | j        d            w xY w|j                            | j
                   t5          j        | | j        || j
        | j        | j                  | _        d S t5          j        | | j        |d d | j                  | _        d S )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__-   sX   |v~~~<$,F	@RSSSDMMGLL);TBBDMHT]### &	"$',,t}|/K"L"L7>>$/:::::_do>>>+"//11 2 2*0*:4?*K*K//1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2  
+ 
+ 
+$$%
 !% %    '+DOOO
+    dm4@@@@ "&&t777%. DOOO &.dnitT] DOOOsU   =A7F 5E 	8EE EE EE F 3FF FF 'F:c                .    |                                   dS )z,
        Dispose of this directory.
        N)r   )r9   s    r   releasezWorkDir.release^   s     	r
   c                F   	 |                     |           ||                                 |+|j                            |           t	          |           d S d S # ||                                 |*|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00$!!###$&--i888I&&&&& %$ $!!###$&--i888I&&&& %s   A AB )NN)	__name__
__module____qualname____doc____annotations__r<   r>   classmethodr8   r	   r
   r   r   r   $   s           MMMOOO    / / / /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dS )	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                    |                      |          | _        t          j                            | j        d          | _        t          j                            | j        d          | _        d S )Nzglobal.lockz
purge.lock)_init_workspacer%   r   r   r&   rM   rN   )r9   r%   s     r   r<   zWorkSpace.__init__   sQ    ,,X66!#dm]!K!K "T]L I Ir
   returnc                   t           j                            |          }|g}t          j        dk    r+|                    | dt          j                                |D ]l}	 t          j        |           nQ# t          $ rD 	 t          j
        |          5  	 ddd           n# 1 swxY w Y   n# t          $ r Y Y _w xY wY nw xY w|c S t          j        |dz             S )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   sP    7??8,,:<7"" OOx77")++77888 		 		GG$$$$"   !/G<<<                &   HH	 NNN
 x#~6666sZ   #A88
CB2B&B2&B**B2-B*.B21C2
C <C?C  CCc                0    t          j        | j        fi |S r@   )r,   r-   rM   r9   kwargss     r   r+   zWorkSpace._global_lock   s     6AA&AAAr
   c                0    t          j        | j        fi |S r@   )r,   r-   rN   ra   s     r   _purge_lockzWorkSpace._purge_lock   s     5@@@@@r
   c                   t                      sg S |                     d          }	 |                                 	 t          |                                           }|                                 n0# |                                 w xY w# t          j        $ r g cY S w xY wg }|                     d          }	 |                                 	 |D ],}| 	                    |          r|
                    |           -	 |                                 n.# |                                 w xY w# t          j        $ r Y nw xY w|S )Nr   timeout)r   r+   r/   list_list_unknown_locksr>   r,   	LockErrorrd   _check_lock_or_purgerX   )r9   lock
candidatespurgedr   s        r   _purge_leftoverszWorkSpace._purge_leftovers   si   !## 	I
    ++
	LLNNN!$":":"<"<==
  	 	 	 III	 **	LLNNN
& , ,D0066 ,d+++,   	 	 	D	 s:   B !A3 3B	B B <D/ /D D,/E Ec              #    K   t          j         t          j                            | j        dt
          z                       D ]D}	 t          j        |          }t          j        |j                  r|V  5# t          $ r Y Aw xY wd S )N*)
globr   r   r&   r%   r(   statS_ISREGst_moder   )r9   psts      r   ri   zWorkSpace._list_unknown_locks   s      27<<s\7IJJKK 		 		AWQZZ <
++ GGG    		 		s   A77
BB)      c                <    t          j        || j                   d S )N)onexc)r2   r3   _on_remove_error)r9   r   s     r   rA   zWorkSpace._purge_directory   s     M($*?@@@@@@r
   c                >      fd}t          j        ||           d S )Nc                >                         | ||d                   S )N   )r|   )funcr   r!   r9   s      r   onerrorz+WorkSpace._purge_directory.<locals>.onerror   s    ,,T4!EEEr
   )r   )r2   r3   )r9   r   r   s   `  r   rA   zWorkSpace._purge_directory   s>    F F F F F M(G444444r
   c                b   |                     t                    sJ || j        v rdS t                              d|           t          j        |d          }	 |                                 n# t
          j        $ r Y dS w xY w	 |dt          t                              }t          j                            |          r0t                              d|           |                     |           |                                 n# |                                 w xY wt!          |           dS )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   rf   Nz/Found stale lock file and directory %r, purgingT)endswithr(   r4   r   r*   r,   r-   r/   rj   lenr   r   r)   inforA   r>   r   )r9   rD   rl   r   s       r   rk   zWorkSpace._check_lock_or_purge   s'    !!,/////)))5/;;;	1555	LLNNNN 	 	 	55		 !5C$5$5#5!56Hw~~h'' 0MxXXX%%h///LLNNNNDLLNNNNIts   A/ /BBA,D Dc                Z    t                               d||t          |                     d S )Nz&Failed to remove %r (failed in %r): %s)r   r   r   )r9   r   r   excs       r   r|   zWorkSpace._on_remove_error  s'    =tT3s88TTTTTr
   c                    	 |                                   n,# t          $ r t                              dd           Y nw xY wt	          | fi |S )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    )ro   r   r   r   r   ra   s     r   new_work_dirzWorkSpace.new_work_dir  sz    	!!#### 	 	 	LLN      	
 t&&v&&&s    &A A N)r%   r   )r%   r   rQ   r   )rE   rF   rG   rH   rI   setr4   r<   rP   r+   rd   ro   ri   rV   version_inforA   rk   r|   r   r	   r
   r   rL   rL   p   s2          MMM (+suuL,,,,J J J J
7 7 7 78B B BA A A% % %N
 
 
 7""	A 	A 	A 	A
	5 	5 	5  <U U U' ' ' ' 'r
   rL   )
__future__r   rr   loggingr   r2   rs   rV   r#   r6   typingr   r,   r   	getLoggerrE   r   r(   r   r   r   rL   r	   r
   r   <module>r      s(   " " " " " "   				   



          		8	$	$B B B6 6 6I' I' I' I' I' I' I' I'Xk' k' k' k' k' k' k' k' k' k'r
   