σ
ίόΪ\c           @` 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 j e  Z d Z d   Z d   Z d	 e f d
     YZ d e f d     YZ d S(   i    (   t   print_functiont   divisiont   absolute_importNi   (   t   locket(   t   finalizes   .dirlockc           C` s   t  j j d  S(   Ns#   distributed.worker.use-file-locking(   t   daskt   configt   get(    (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyt   is_locking_enabled   s    c         C` sV   y t  j |   Wn> t k
 rQ } | j t j k rR t j d t |   qR n Xd  S(   Ns   Failed to remove %r(   t   ost   unlinkt   EnvironmentErrort   errnot   ENOENTt   loggert   errort   str(   t   patht   e(    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyt   safe_unlink   s
    t   WorkDirc           B` s5   e  Z d  Z d d d  Z d   Z e d    Z RS(   s8   
    A temporary work directory inside a WorkSpace.
    c         C` sΤ  | d  k s | d  k s t  | d  k rK t j d | d | j  |  _ n+ t j j | j |  |  _ t j	 |  j  t
   r¬yΐ t j j |  j t  |  _ t j j |  j  sΊ t  t j d |  j  y9 | j   ' t j |  j  |  _ |  j j   Wd  QXWn5 t k
 r=} t j d |  j d t d  |  _ n XWn* t k
 rkt j |  j d t   n X| j j |  j  t |  |  j | |  j |  j |  j  |  _ n$ t |  |  j | d  d  |  j  |  _ d  S(   Nt   prefixt   dirs   Locking %r...sy   Could not acquire workspace lock on path: %s .Continuing without lock. This may result in workspaces not being cleaned upt   exc_infot   ignore_errors(    t   Nonet   AssertionErrort   tempfilet   mkdtempt   base_dirt   dir_pathR	   R   t   joint   mkdirR   t   DIR_LOCK_EXTt
   _lock_patht   existsR   t   debugt   _global_lockR   t	   lock_filet
   _lock_filet   acquiret   OSErrort	   exceptiont   Truet	   Exceptiont   shutilt   rmtreet   _known_lockst   addR   t	   _finalizet
   _finalizer(   t   selft	   workspacet   nameR   R   (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyt   __init__(   sB    !		c         C` s   |  j    d S(   s,   
        Dispose of this directory.
        N(   R2   (   R3   (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyt   releaseY   s    c         C` s[   z | j  |  Wd  | d  k	 r- | j   n  | d  k	 rV | j j |  t |  n  Xd  S(   N(   t   _purge_directoryR   R7   R/   t   removeR   (   t   clsR4   t	   lock_pathR&   R   (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyR1   _   s    N(   t   __name__t
   __module__t   __doc__R   R6   R7   t   classmethodR1   (    (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyR   #   s   1	t	   WorkSpacec           B` sq   e  Z d  Z e   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z RS(   sΠ   
    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` sY   t  j j |  |  _ |  j   t  j j |  j d  |  _ t  j j |  j d  |  _ d  S(   Ns   global.locks
   purge.lock(   R	   R   t   abspathR   t   _init_workspaceR   t   _global_lock_patht   _purge_lock_path(   R3   R   (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyR6   v   s    
c         C` sF   y t  j |  j  Wn+ t k
 rA } | j t j k rB   qB n Xd  S(   N(   R	   R    R   R   R   t   EEXIST(   R3   R   (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyRB   |   s
    c         K` s   t  j |  j |  S(   N(   R   R&   RC   (   R3   t   kwargs(    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyR%      s    c         K` s   t  j |  j |  S(   N(   R   R&   RD   (   R3   RF   (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyt   _purge_lock   s    c         C` sμ   t    s g  S|  j d d  } y | j   Wn t j k
 rD g  SXz t |  j    } Wd  | j   Xg  } |  j d d  } y | j   Wn t j k
 r₯ nC Xz4 x- | D]% } |  j	 |  r° | j
 |  q° q° WWd  | j   X| S(   Nt   timeouti    (   R   R%   R(   R   t	   LockErrort   listt   _list_unknown_locksR7   RG   t   _check_lock_or_purget   append(   R3   t   lockt
   candidatest   purgedR   (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyt   _purge_leftovers   s,    	c         c` su   xn t  j  t j j |  j d t   D]G } y t j |  } Wn t k
 rR q& Xt j | j	  r& | Vq& q& Wd  S(   Nt   *(
   t   globR	   R   R   R   R!   t   statR   t   S_ISREGt   st_mode(   R3   t   pt   st(    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyRK   °   s    ,c         C` s   t  j | d |  j d  S(   Nt   onerror(   R-   R.   t   _on_remove_error(   R3   R   (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyR8   Ό   s    c         C` sΦ   | j  t  s t  | |  j k r( t St j d |  t j | d d } y | j	   Wn t j
 k
 rr t SXzG | t t   } t j j |  rΉ t j d |  |  j |  n  Wd | j   Xt |  t S(   s¬   
        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.
        s   Checking lock file %r...RH   i    s/   Found stale lock file and directory %r, purgingN(   t   endswithR!   R   R/   t   FalseR   R$   R   R&   R(   RI   t   lenR	   R   R#   t   infoR8   R7   R   R+   (   R3   R;   RN   R   (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyRL   Ώ   s"    
c         C` s/   | \ } } } t  j d | | t |   d  S(   Ns&   Failed to remove %r (failed in %r): %s(   R   R   R   (   R3   t   funcR   R   t   typt   exct   tb(    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyRZ   έ   s    c         K` sB   y |  j    Wn$ t k
 r4 t j d d t n Xt |  |  S(   s₯  
        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
        s<   Failed to clean up lingering worker directories in path: %s R   (   RQ   R)   R   R   R+   R   (   R3   RF   (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyt   new_work_dirα   s    (   R<   R=   R>   t   setR/   R6   RB   R%   RG   RQ   RK   R8   RL   RZ   Rc   (    (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyR@   k   s   						'				(   t
   __future__R    R   R   R   RS   t   loggingR	   R-   RT   R   R   t    R   t   compatibilityR   t	   getLoggerR<   R   R!   R   R   t   objectR   R@   (    (    (    s4   lib/python2.7/site-packages/distributed/diskutils.pyt   <module>   s    			H