a
    2`/bw                     @  s   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	m
Z
 d dlZddlmZ eeZdZdd Zd	d
 ZG dd dZG dd dZdS )    )annotationsN)ClassVar   )locketz.dirlockc                   C  s   t jdS )Nz#distributed.worker.use-file-locking)daskZconfigget r   r   4lib/python3.9/site-packages/distributed/diskutils.pyis_locking_enabled   s    r
   c              
   C  s^   zt |  W nJ ty    Y n: tyX } z"td|  d|  W Y d }~n
d }~0 0 d S )NzFailed to remove z: )osunlinkFileNotFoundErrorOSErrorloggererror)pather   r   r	   safe_unlink   s    r   c                   @  sH   e Zd ZU dZded< ded< ded< ddd	Zd
d Zedd ZdS )WorkDirz8
    A temporary work directory inside a WorkSpace.
    strdir_path
_lock_pathzweakref.finalize
_finalizerNc                 C  sr  |d u s|d u sJ |d u r0t j||jd| _ntj|j|| _t| j t rTztj| jt	 | _
tj| j
r~J td| j
 zD| ( t| j
| _| j  W d    n1 s0    Y  W n* ty   tjd| j
dd d | _Y n0 W n& ty$   tj| jdd  Y n0 |j| j
 t| | j|| j
| j| j| _nt| | 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)tempfileZmkdtempbase_dirr   r   r   joinmkdirr
   DIR_LOCK_EXTr   existsr   debug_global_lockr   	lock_fileZ
_lock_fileacquirer   	exception	Exceptionshutilrmtree_known_locksaddweakreffinalize	_finalizer   )self	workspacenamer   r   r   r	   __init__,   sH    
,		zWorkDir.__init__c                 C  s   |    dS )z,
        Dispose of this directory.
        N)r   r1   r   r   r	   release]   s    zWorkDir.releasec              	   C  sn   z:| | W |d ur|  |d urj|j| t| n.|d urL|  |d urh|j| t| 0 d S N)_purge_directoryr6   r,   remover   )clsr2   	lock_pathr&   r   r   r   r	   r0   c   s    
zWorkDir._finalize)NN)	__name__
__module____qualname____doc____annotations__r4   r6   classmethodr0   r   r   r   r	   r   #   s   

1r   c                   @  sp   e Zd ZU dZe Zded< dd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zd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.
    zClassVar[set[str]]r,   c                 C  s>   t j|| _|   t j| jd| _t j| jd| _d S )Nzglobal.lockz
purge.lock)r   r   abspathr   _init_workspacer    _global_lock_path_purge_lock_path)r1   r   r   r   r	   r4   z   s    zWorkSpace.__init__c                 C  s(   zt | j W n ty"   Y n0 d S r7   )r   r!   r   FileExistsErrorr5   r   r   r	   rD      s    zWorkSpace._init_workspacec                 K  s   t j| jfi |S r7   )r   r&   rE   r1   kwargsr   r   r	   r%      s    zWorkSpace._global_lockc                 K  s   t j| jfi |S r7   )r   r&   rF   rH   r   r   r	   _purge_lock   s    zWorkSpace._purge_lockc                 C  s   t  s
g S | jdd}z|  W n tjy:   g  Y S 0 zt|  }W |  n
|  0 g }| jdd}z|  W n tjy   Y n80 z*|D ]}| 	|r|
| qW |  n
|  0 |S )Nr   timeout)r
   r%   r'   r   	LockErrorlist_list_unknown_locksr6   rJ   _check_lock_or_purgeappend)r1   lockZ
candidatesZpurgedr   r   r   r	   _purge_leftovers   s,    

zWorkSpace._purge_leftoversc              	   c  sX   t  tj| jdt D ]8}zt|}W n ty>   Y q0 t|j	r|V  qd S )N*)
globr   r   r    r   r"   statr   S_ISREGst_mode)r1   pstr   r   r	   rO      s    zWorkSpace._list_unknown_locksc                 C  s   t j|| jd d S )N)onerror)r*   r+   _on_remove_error)r1   r   r   r   r	   r8      s    zWorkSpace._purge_directoryc                 C  s   | tsJ || jv rdS td| tj|dd}z|  W n tjyX   Y dS 0 z@|dt	t  }t
j|rtd| | | W |  n
|  0 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   rK   Nz/Found stale lock file and directory %r, purgingT)endswithr"   r,   r   r$   r   r&   r'   rM   lenr   r   r#   infor8   r6   r   )r1   r;   rR   r   r   r   r	   rP      s"    
zWorkSpace._check_lock_or_purgec                 C  s"   |\}}}t d||t| d S )Nz&Failed to remove %r (failed in %r): %s)r   r   r   )r1   funcr   r   typexctbr   r   r	   r\      s    
zWorkSpace._on_remove_errorc                 K  s>   z|    W n  ty,   tjddd Y n0 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   )rS   r   r   r   r   rH   r   r   r	   new_work_dir   s    zWorkSpace.new_work_dirN)r<   r=   r>   r?   setr,   r@   r4   rD   r%   rJ   rS   rO   r8   rP   r\   rd   r   r   r   r	   rB   o   s   
'rB   )Z
__future__r   rU   loggingr   r*   rV   r   r.   typingr   r    r   Z	getLoggerr<   r   r"   r
   r   r   rB   r   r   r   r	   <module>   s    

L