B
    ß‹d‘	  ã               @   s<   d Z ddlmZ ddlZddlZddlZdd„ Zdd„ ZdS )z 
A simple module for acquiring pidfile locks (e.g., for use by daemons).

Copyright (C) 2010 by Peter F. Couvares, Syracuse University
mailto: pfcouvar@syr.edu
é    )Úprint_functionNc          
   C   sÚ   t | dƒ}yt | ¡ tjtjB ¡ W n2 tk
rX } ztd| |f ƒ‚W dd}~X Y nX | d¡ | 	¡  
¡ }| ¡ rªtj t|ƒ¡rštd| |f ƒ‚ntd| |f ƒ | d¡ | dt ¡  ¡ | ¡  t| ƒ dS )	a„  
    Tries to write a lockfile containing the current pid.  Excepts if
    the lockfile already contains the pid of a running process.

    Although this should prevent a lock from being granted twice, it
    can theoretically deny a lock unjustly in the unlikely event that
    the original process is gone but another unrelated process has
    been assigned the same pid by the OS.
    za+zfailed to lock %s: %sNr   z1pidfile %s contains pid (%s) of a running processz2pidfile %s contains stale pid %s; writing new lockz%d
T)ÚopenÚfcntlZflockÚfilenoZLOCK_EXZLOCK_NBÚIOErrorÚRuntimeErrorÚseekÚreadlineÚstripÚisdigitÚglueÚutilsZ
pid_existsÚintÚprintÚtruncateÚwriteÚosÚgetpidÚcloseÚconfirm_lock)ÚlockfileÚpidfileÚeÚpidfile_pid© r   úY/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/glue/pidfile.pyÚget_lock   s$    
"

r   c             C   sH   t | dƒ}| ¡  ¡ }| ¡  t|ƒt ¡ krDtd| t ¡ |f ƒ‚dS )z‚
    Confirm that the given lockfile contains our pid.
    Should be entirely unecessary, but paranoia always served me well.
    Úrz,pidfile %s contains pid %s; expected pid %s!T)r   r	   r
   r   r   r   r   r   )r   r   r   r   r   r   r   A   s    
r   )	Ú__doc__Ú
__future__r   r   r   Z
glue.utilsr   r   r   r   r   r   r   Ú<module>   s   
+