B
    °F.\‚  ã               @   s®   d dl mZmZ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mZ e e¡ZdZdd„ Zd	d
„ ZG dd„ deƒZG dd„ deƒZdS )é    )Úprint_functionÚdivisionÚabsolute_importNé   )Úlocket)Úfinalizez.dirlockc               C   s   t j d¡S )Nz#distributed.worker.use-file-locking)ÚdaskZconfigÚget© r
   r
   ú4lib/python3.7/site-packages/distributed/diskutils.pyÚis_locking_enabled   s    r   c          
   C   sR   yt  | ¡ W n> tk
rL } z |jtjkr<t dt|ƒ¡ W d d }~X Y nX d S )NzFailed to remove %r)ÚosÚunlinkÚEnvironmentErrorÚerrnoZENOENTÚloggerÚerrorÚstr)ÚpathÚer
   r
   r   Úsafe_unlink   s
    r   c               @   s.   e Zd ZdZd	dd„Zdd„ Zedd„ ƒZdS )
ÚWorkDirz8
    A temporary work directory inside a WorkSpace.
    Nc          
   C   sl  |d ks|d kst ‚|d kr0tj||jd| _ntj |j|¡| _t | j¡ t	ƒ rPy¤tj | jt
 ¡| _tj | j¡r~t ‚t d| j¡ y0| ¡  t | j¡| _| j ¡  W d Q R X W n: tk
rö } ztjd| jdd d | _W d d }~X Y nX W n( tk
r"   tj| jdd ‚ Y nX |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)ÚAssertionErrorÚtempfileZmkdtempÚbase_dirÚdir_pathr   r   ÚjoinÚmkdirr   ÚDIR_LOCK_EXTZ
_lock_pathÚexistsr   ÚdebugÚ_global_lockr   Ú	lock_fileZ
_lock_fileÚacquireÚOSErrorZ	exceptionÚ	ExceptionÚshutilÚrmtreeÚ_known_locksÚaddr   Ú	_finalizeÚ
_finalizer)ÚselfÚ	workspaceÚnamer   r   r
   r
   r   Ú__init__(   s8    
zWorkDir.__init__c             C   s   |   ¡  dS )z,
        Dispose of this directory.
        N)r/   )r0   r
   r
   r   ÚreleaseP   s    zWorkDir.releasec          	   C   sB   z|  |¡ W d |d k	r | ¡  |d k	r<|j |¡ t|ƒ X d S )N)Ú_purge_directoryr4   r,   Úremover   )Úclsr1   Ú	lock_pathr&   r   r
   r
   r   r.   V   s    zWorkDir._finalize)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r4   Úclassmethodr.   r
   r
   r
   r   r   #   s   
(r   c               @   sf   e Zd ZdZeƒ 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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.
    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)r0   r   r
   r
   r   r3   m   s    zWorkSpace.__init__c          
   C   sF   yt  | j¡ W n0 tk
r@ } z|jtjkr0‚ W d d }~X Y nX d S )N)r   r!   r   r   r   ZEEXIST)r0   r   r
   r
   r   r@   s   s
    zWorkSpace._init_workspacec             K   s   t j| jf|ŽS )N)r   r&   rA   )r0   Úkwargsr
   r
   r   r%   z   s    zWorkSpace._global_lockc             K   s   t j| jf|ŽS )N)r   r&   rB   )r0   rC   r
   r
   r   Ú_purge_lock}   s    zWorkSpace._purge_lockc             C   sÀ   t ƒ s
g S | jdd}y| ¡  W n tjk
r8   g S X zt|  ¡ ƒ}W d | ¡  X g }| jdd}y| ¡  W n tjk
rˆ   Y n4X z&x |D ]}|  	|¡r’| 
|¡ q’W W d | ¡  X |S )Nr   )Útimeout)r   r%   r'   r   Ú	LockErrorÚlistÚ_list_unknown_locksr4   rD   Ú_check_lock_or_purgeÚappend)r0   ÚlockZ
candidatesZpurgedr   r
   r
   r   Ú_purge_leftovers€   s,    



zWorkSpace._purge_leftoversc          	   c   s^   xXt   tj | jdt ¡¡D ]:}yt |¡}W n tk
rB   Y qX t |j	¡r|V  qW d S )NÚ*)
Úglobr   r   r    r   r"   Ústatr   ÚS_ISREGÚst_mode)r0   ÚpÚstr
   r
   r   rH   §   s     zWorkSpace._list_unknown_locksc             C   s   t j|| jd d S )N)Úonerror)r*   r+   Ú_on_remove_error)r0   r   r
   r
   r   r5   ³   s    zWorkSpace._purge_directoryc             C   sª   |  t¡st‚|| jkrdS t d|¡ tj|dd}y| ¡  W n tj	k
rX   dS X z8|dt
tƒ … }tj |¡rt d|¡ |  |¡ W d| ¡  X 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   )rE   Nz/Found stale lock file and directory %r, purgingT)Úendswithr"   r   r,   r   r$   r   r&   r'   rF   Úlenr   r   r#   Úinfor5   r4   r   )r0   r8   rK   r   r
   r
   r   rI   ¶   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   )r0   Úfuncr   r   ÚtypÚexcÚtbr
   r
   r   rU   Õ   s    
zWorkSpace._on_remove_errorc             K   s<   y|   ¡  W n" tk
r.   tjddd Y nX t| f|Ž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 T)r   )rL   r(   r   r   r   )r0   rC   r
   r
   r   Únew_work_dirÚ   s    zWorkSpace.new_work_dirN)r9   r:   r;   r<   Úsetr,   r3   r@   r%   rD   rL   rH   r5   rI   rU   r]   r
   r
   r
   r   r>   b   s   'r>   )Z
__future__r   r   r   r   rN   Zloggingr   r*   rO   r   r   Ú r   Zcompatibilityr   Z	getLoggerr9   r   r"   r   r   Úobjectr   r>   r
   r
   r
   r   Ú<module>   s    
	?