
y X                 @   s>   d  d l  Z  d  d l Z d  d l Z Gd d   d e  Z d S)    Nc               @   sR   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z d S)Pidfilez    Manage a PID file. If a specific name is provided
    it and '"%s.oldpid" % name' will be used. Otherwise
    we create a temp file using os.mkstemp.
    c             C   s   | |  _  d  |  _ d  S)N)fnamepid)selfr    r   @/home/ubuntu/projects/ifolica/build/gunicorn/gunicorn/pidfile.py__init__   s    	zPidfile.__init__c             C   s  |  j    } | rJ | t j   k r( d  Sd } t | | |  j f   n  | |  _ t j j |  j  } | r t j j |  r t d |   n  t	 j
 d |  \ } } t j | d |  j j d   |  j r t j | |  j  n	 | |  _ t j |  t j |  j d  d  S)Nz5Already running on PID %s (or pid file '%s' is stale)z'%s doesn't exist. Can't create pidfile.dirz%s
zutf-8i  )validateosgetpidRuntimeErrorr   r   pathdirnameisdirtempfilemkstempwriteencoderenameclosechmod)r   r   ZoldpidmsgZfdirfdr   r   r   r   create   s"    	 		zPidfile.createc             C   s'   |  j    | |  _ |  j |  j  d  S)N)unlinkr   r   r   )r   r   r   r   r   r   /   s    
	zPidfile.renamec             C   sh   yY t  |  j d   } t | j   p* d  } Wd QX| |  j k rX t j |  j  n  Wn Yn Xd S)z delete pidfilerr   N)openr   intreadr   r   r   )r   fZpid1r   r   r   r   4   s    zPidfile.unlinkc          %   C   s  |  j  s d Sy t |  j  d   } y t | j    } Wn t k
 rS d SYn Xy t j | d  | SWnB t k
 r } z" | j d t	 j
 k r d S  WYd d } ~ Xn XWd QXWnB t k
 r } z" | j d t	 j k r d S  WYd d } ~ Xn Xd S)z- Validate pidfile and make it stale if neededNr   r   )r   r   r   r   
ValueErrorr   killOSErrorargserrnoZESRCHIOErrorENOENT)r   r    wpider   r   r   r
   ?   s&    		 zPidfile.validateN)	__name__
__module____qualname____doc__r   r   r   r   r
   r   r   r   r   r      s   r   )r%   r   r   objectr   r   r   r   r   <module>   s   