ó
æQc           @   s¡   d  d l  Z  d  d l Z d  d l Z d  d l m Z m Z d  d l Z d e f d „  ƒ  YZ d d
 d „  ƒ  YZ d „  Z	 e
 d k r e j e	 ƒ  p– d	 ƒ n  d S(   iÿÿÿÿN(   t   EWOULDBLOCKt   EEXISTt   LockfileLockedExceptionc           B   s   e  Z d  Z RS(   s$   thrown ONLY when pid file is locked.(   t   __name__t
   __module__t   __doc__(    (    (    s3   /usr/lib/python2.7/site-packages/rhn/rhnLockfile.pyR      s   t   Lockfilec           B   s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   sï   class that provides simple access to a PID-style lockfile.

    methods: __init__(lockfile), acquire(), and release()
    NOTE: currently acquires upon init
    The *.pid file will be acquired, or an LockfileLockedException is raised.
    c         C   sý   t  j j t  j j t  j j | ƒ ƒ ƒ |  _ | |  _ |  j sQ t  j ƒ  |  _ n  t  j j |  j ƒ } t  j j	 | ƒ sÆ y t  j
 | ƒ WqÆ t k
 rÂ } t | d ƒ r¼ | j t k r¼ qÃ ‚  qÆ Xn  t  j |  j t  j t  j Bt  j Bƒ |  _ |  j ƒ  d S(   sl   create (if need be), and acquire lock on lockfile

        lockfile example: '/var/run/up2date.pid'
        t   errnoN(   t   ost   patht   abspatht
   expandusert
   expandvarst   lockfilet   pidt   getpidt   dirnamet   existst   makedirst   OSErrort   hasattrR   R   t   opent   O_RDWRt   O_CREATt   O_SYNCt   ft   acquire(   t   selfR   R   R   t   e(    (    s3   /usr/lib/python2.7/site-packages/rhn/rhnLockfile.pyt   __init__#   s     				
)c         C   s¿   y! t  j |  j t  j t  j Bƒ WnK t k
 rn } | j t k rh t d |  j	 ƒ d t j ƒ  d ‚ qo ‚  n Xt  j  |  j t  j d ƒ t j |  j d ƒ t j |  j t |  j ƒ d ƒ d S(   s5   acquire the lock; else raise LockfileLockedException.s   cannot acquire lock on %s.i   i   i    s   
N(   t   fcntlt   flockR   t   LOCK_EXt   LOCK_NBt   IOErrorR   R    R   R   t   Nonet   syst   exc_infot   F_SETFDR   t	   ftruncatet   writet   strR   (   R   R   (    (    s3   /usr/lib/python2.7/site-packages/rhn/rhnLockfile.pyR   C   s    !#c         C   s:   t  j |  j ƒ t j |  j t j ƒ t  j |  j ƒ d  S(   N(   R   t   unlinkR   R   R   R   t   LOCK_UNt   close(   R   (    (    s3   /usr/lib/python2.7/site-packages/rhn/rhnLockfile.pyt   releaseT   s    N(   R   R   R   R#   R   R   R-   (    (    (    s3   /usr/lib/python2.7/site-packages/rhn/rhnLockfile.pyR      s    	c          C   s}   y t  d ƒ }  Wn4 t k
 rF } t j j d | ƒ t j d ƒ n3 Xd GHd GHd d l } | j d ƒ |  j ƒ  d GHd S(	   s	   test codes
   ./test.pids   %s
iÿÿÿÿs   lock acquired s   ...sleeping for 10 secondsNi
   s   lock released (	   R   R   R$   t   stderrR(   t   exitt   timet   sleepR-   (   t   LR   R0   (    (    s3   /usr/lib/python2.7/site-packages/rhn/rhnLockfile.pyt   main[   s    
t   __main__i    (    (   R   R$   R   R   R    R   t	   ExceptionR   R   R3   R   R/   (    (    (    s3   /usr/lib/python2.7/site-packages/rhn/rhnLockfile.pyt   <module>   s   A	