3 UˆŠZc ã@s~ddlZddlZddlZddlmZmZddlmZddlZGdd„deƒZ Gdd„dƒZ dd „Z e d krzej e ƒpvdƒdS) éN)Ú EWOULDBLOCKÚEEXIST)Úbstrc@seZdZdZdS)ÚLockfileLockedExceptionz$thrown ONLY when pid file is locked.N)Ú__name__Ú __module__Ú __qualname__Ú__doc__©r r ú!/usr/lib/python3.6/rhnLockfile.pyrsrc@s*eZdZdZd dd„Zdd„Zdd„ZdS) ÚLockfilezï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. Nc CsÀtjjtjjtjj|ƒƒƒ|_||_|js4tjƒ|_tjj|jƒ}tjj |ƒs–ytj |ƒWn8t k r”t j ƒd}t|dƒrŽ|jtkrŽn‚YnXtj|jtjtjBtjBƒ|_|jƒdS)zlcreate (if need be), and acquire lock on lockfile lockfile example: '/var/run/up2date.pid' éÚerrnoN)ÚosÚpathÚabspathÚ expanduserÚ expandvarsÚlockfileÚpidÚgetpidÚdirnameÚexistsÚmakedirsÚOSErrorÚsysÚexc_infoÚhasattrrrÚopenÚO_RDWRÚO_CREATÚO_SYNCÚfÚacquire)ÚselfrrrÚer r r Ú__init__$s"   zLockfile.__init__c Cs ytj|jtjtjBƒWnDtk r^tjƒdjt krXt d|j dtjƒdƒ‚n‚YnXtj|jtj dƒt j|jdƒt j|jtt|jƒdƒƒdS)z5acquire the lock; else raise LockfileLockedException.r zcannot acquire lock on %s.NérÚ )ÚfcntlÚflockr"ZLOCK_EXZLOCK_NBÚIOErrorrrrrrrZF_SETFDrÚ ftruncateÚwriterÚstrr)r$r r r r#EszLockfile.acquirecCs,tj|jƒtj|jtjƒtj|jƒdS)N)rÚunlinkrr)r*r"ZLOCK_UNÚclose)r$r r r ÚreleaseVs zLockfile.release)N)rrrr r&r#r1r r r r r s !r c Cszy tdƒ}Wn6tk rBtjjdtjƒdƒtjd ƒYn4Xtdƒtdƒddl}|j dƒ|j ƒtd ƒdS) z test codez ./test.pidz%s r zlock acquired z...sleeping for 10 secondsrNé zlock released éÿÿÿÿ) r rrÚstderrr-rÚexitÚprintÚtimeZsleepr1)ÚLr7r r r Úmain]s  r9Ú__main__)rrr)rrrZrhn.i18nrÚ Exceptionrr r9rr5r r r r Ús B