ó
Y[c           @   sx   d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z e j e  Z	 d   Z
 d e f d     YZ d S(   s@   Implements file locks for locking files and directories in UNIX.i’’’’N(   t   errorsc         C   s   t  t j j |  d   S(   s3  Place a lock file on the directory at dir_path.

    The lock file is placed in the root of dir_path with the name
    .certbot.lock.

    :param str dir_path: path to directory

    :returns: the locked LockFile object
    :rtype: LockFile

    :raises errors.LockError: if unable to acquire the lock

    s   .certbot.lock(   t   LockFilet   ost   patht   join(   t   dir_path(    (    s0   /usr/lib/python2.7/dist-packages/certbot/lock.pyt   lock_dir   s    R   c           B   sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   sė   A UNIX lock file.

    This lock file is released when the locked file is closed or the
    process exits. It cannot be used to provide synchronization between
    threads. It is based on the lock_file package by Martin Horcicka.

    c         C   s3   t  t |   j   | |  _ d |  _ |  j   d S(   s£   Initialize and acquire the lock file.

        :param str path: path to the file to lock

        :raises errors.LockError: if unable to acquire the lock

        N(   t   superR   t   __init__t   _patht   Nonet   _fdt   acquire(   t   selfR   (    (    s0   /usr/lib/python2.7/dist-packages/certbot/lock.pyR   %   s    		c         C   s   x |  j  d k r t j |  j t j t j Bd  } z, |  j |  |  j |  r_ | |  _  n  Wd |  j  d k r t j	 |  n  Xq Wd S(   s   Acquire the lock file.

        :raises errors.LockError: if lock is already held
        :raises OSError: if unable to open or stat the lock file

        i  N(
   R   R
   R   t   openR	   t   O_CREATt   O_WRONLYt	   _try_lockt   _lock_successt   close(   R   t   fd(    (    s0   /usr/lib/python2.7/dist-packages/certbot/lock.pyR   3   s    "c         C   s{   y t  j | t  j t  j B WnV t k
 rv } | j t j t j f k rp t j	 d |  j
  t j d   n    n Xd S(   s{   Try to acquire the lock file without blocking.

        :param int fd: file descriptor of the opened file to lock

        s(   A lock on %s is held by another process.s/   Another instance of Certbot is already running.N(   t   fcntlt   lockft   LOCK_EXt   LOCK_NBt   IOErrort   errnot   EACCESt   EAGAINt   loggert   debugR	   R    t	   LockError(   R   R   t   err(    (    s0   /usr/lib/python2.7/dist-packages/certbot/lock.pyR   F   s    c         C   sv   y t  j |  j  } Wn, t k
 rD } | j t j k r> t S  n Xt  j |  } | j | j k ou | j	 | j	 k S(   s  Did we successfully grab the lock?

        Because this class deletes the locked file when the lock is
        released, it is possible another process removed and recreated
        the file between us opening the file and acquiring the lock.

        :param int fd: file descriptor of the opened file to lock

        :returns: True if the lock was successfully acquired
        :rtype: bool

        (
   R   t   statR	   t   OSErrorR   t   ENOENTt   Falset   fstatt   st_devt   st_ino(   R   R   t   stat1R    t   stat2(    (    s0   /usr/lib/python2.7/dist-packages/certbot/lock.pyR   V   s    c         C   sE   d j  |  j j |  j  } |  j d  k r7 | d 7} n
 | d 7} | S(   Ns
   {0}({1}) <s	   released>s	   acquired>(   t   formatt	   __class__t   __name__R	   R   R
   (   R   t   repr_str(    (    s0   /usr/lib/python2.7/dist-packages/certbot/lock.pyt   __repr__o   s
    
c         C   s=   z t  j |  j  Wd z t  j |  j  Wd d |  _ XXd S(   s)   Remove, close, and release the lock file.N(   R   t   removeR	   R   R   R
   (   R   (    (    s0   /usr/lib/python2.7/dist-packages/certbot/lock.pyt   releasew   s
    (	   R,   t
   __module__t   __doc__R   R   R   R   R.   R0   (    (    (    s0   /usr/lib/python2.7/dist-packages/certbot/lock.pyR      s   					(   R2   R   R   t   loggingR   t   certbotR    t	   getLoggerR,   R   R   t   objectR   (    (    (    s0   /usr/lib/python2.7/dist-packages/certbot/lock.pyt   <module>   s   	