3
ûZð]5	  ã               @   s,   d dl Z d dlZd dlZG dd„ deƒZdS )é    Nc               @   s8   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   ú4/tmp/pip-build-w1as9q70/gunicorn/gunicorn/pidfile.pyÚ__init__   s    zPidfile.__init__c             C   sÄ   | j ƒ }|r2|tjƒ krd S d}t||| jf ƒ‚|| _tjj| jƒ}|rdtjj|ƒ rdtd| ƒ‚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   sT   yBt | jdƒ�}t|jƒ pdƒ}W dQ R X || jkr@tj| jƒ W n   Y nX d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S y t| j dƒ�Š}yt|jƒ ƒ}W n tk
r>   dS X ytj|dƒ |S  tk
rœ } z0|jd t	j
krv|S |jd t	jkrŠdS ‚ W Y dd}~X nX W dQ R X W n8 tk
râ } z|jd t	jkrÐdS ‚ W Y dd}~X nX dS )z- Validate pidfile and make it stale if neededNr   r   )r   r   r   r   Ú
ValueErrorr   ÚkillÚOSErrorÚargsÚerrnoÚEPERMZ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   