Ñò ›ˆQc@s=dZddkZddkZddkZddkZeieƒZdefd„ƒYZ de fd„ƒYZ de fd„ƒYZ y&ddk Z d e fd „ƒYZWnej o dZnXy>ddkZddkZddkZd e fd „ƒYZWnej o dZnXd e fd„ƒYZdS(sØLocked file interface that should work on Unix and Windows pythons. This module first tries to use fcntl locking to ensure serialized access to a file, then falls back on a lock file if that is unavialable. Usage: f = LockedFile('filename', 'r+b', 'rb') f.open_and_lock() if f.is_locked(): print 'Acquired filename with r+b mode' f.file_handle().write('locked data') else: print 'Aquired filename with rb mode' f.unlock_and_close() iÿÿÿÿNtAlreadyLockedExceptioncBseZdZRS(sETrying to lock a file that has already been locked by the LockedFile.(t__name__t __module__t__doc__(((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyRst_OpenercBsDeZdZd„Zd„Zd„Zd„Zd„Zd„ZRS(s,Base class for different locking primitives.cCs1t|_||_||_||_d|_dS(sÒCreate an Opener. Args: filename: string, The pathname of the file. mode: string, The preferred mode to access the file with. fallback_mode: string, The mode to use if locking fails. N(tFalset_lockedt _filenamet_modet_fallback_modetNonet_fh(tselftfilenametmodet fallback_mode((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyt__init__%s     cCs|iS(sWas the file locked.(R(R ((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyt is_locked3scCs|iS(s6The file handle to the file. Valid only after opened.(R (R ((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyt file_handle7scCs|iS(s"The filename that is being locked.(R(R ((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyR ;scCsdS(s“Open the file and lock it. Args: timeout: float, How long to try to lock for. delay: float, How long to wait between retries. N((R ttimeouttdelay((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyt open_and_lock?scCsdS(sUnlock and close the file.N((R ((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pytunlock_and_closeHs( RRRRRRR RR(((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyR"s     t _PosixOpenercBs)eZdZd„Zd„Zd„ZRS(s+Lock files using Posix advisory lock files.c Cs˜|iotd|iƒ‚nt|_yt|i|iƒ|_WnEtj o9}|iti jot|i|i ƒ|_dSnX|i |iƒ}t i ƒ}xåt oÝy4ti|titiBtiBƒ|_t |_PWq¯tj o–}|itijo‚nt i ƒ||joOtid||fƒ|io|iiƒnt|i|i ƒ|_dSt i|ƒq¯Xq¯WdS(sJOpen the file and lock it. Tries to create a .lock file next to the file we're trying to open. Args: timeout: float, How long to try to lock for. delay: float, How long to wait between retries. Raises: AlreadyLockedException: if the lock is already acquired. IOError: if the open fails. sFile %s is already lockedNs'Could not acquire lock %s in %s seconds(RRRRtopenRR tIOErrorterrnotEACCESR t_posix_lockfilettimetTruetostO_CREATtO_EXCLtO_RDWRt_lock_fdtOSErrortEEXISTtloggertwarntclosetsleep(R RRtet lock_filenamet start_time((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyRPs<        cCsn|ioE|i|iƒ}ti|ƒti|iƒt|_d|_n|i o|i iƒndS(s?Unlock a file by removing the .lock file, and close the handle.N( RRRRtunlinkR(R#RR R (R R+((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyR€s     cCsd|S(s3The name of the lock file to use for posix locking.s%s.lock((R R ((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyR‹s(RRRRRR(((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyRMs 0 t _FcntlOpenercBs eZdZd„Zd„ZRS(s0Open, lock, and unlock a file using fcntl.lockf.c Cs“|iotd|iƒ‚ntiƒ}yt|i|iƒ|_WnEtj o9}|iti jot|i|i ƒ|_dSnXxût oóy-t i |iiƒt iƒt |_dSWq”tj o³}|djo |‚n|iti jo |‚ntiƒ||joRtid|i|fƒ|io|iiƒnt|i|i ƒ|_dSti|ƒq”Xq”WdS(sOpen the file and lock it. Args: timeout: float, How long to try to lock for. delay: float, How long to wait between retries Raises: AlreadyLockedException: if the lock is already acquired. IOError: if the open fails. sFile %s is already lockedNisCould not lock %s in %s seconds(RRRRRRR RRRR RtfcntltlockftfilenotLOCK_EXR&R'R(R)(R RRR,R*((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyR–s:         cCsR|io ti|iiƒtiƒnt|_|io|iiƒndS(s:Close and unlock the file using the fcntl.lockf primitive.N(RR/R0R R1tLOCK_UNRR((R ((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyRÄs     (RRRRR(((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyR.“s .t _Win32OpenercBs,eZdZdZdZd„Zd„ZRS(s7Open, lock, and unlock a file using windows primitives.i!ižc Cs¹|iotd|iƒ‚ntiƒ}yt|i|iƒ|_WnEtj o9}|iti jot|i|i ƒ|_dSnXx!t oyRt i |iiƒƒ}t i|titiBddtiƒƒt |_dSWq”tij o±}|djo |‚n|dtijo‚ntiƒ||joRtid|i|fƒ|io|iiƒnt|i|i ƒ|_dSti|ƒq”Xq”WdS(sOpen the file and lock it. Args: timeout: float, How long to try to lock for. delay: float, How long to wait between retries Raises: AlreadyLockedException: if the lock is already acquired. IOError: if the open fails. sFile %s is already lockedNiiÿÿsCould not lock %s in %s seconds(RRRRRRR RRRR Rt win32filet_get_osfhandleR1t LockFileExtwin32contLOCKFILE_FAIL_IMMEDIATELYtLOCKFILE_EXCLUSIVE_LOCKt pywintypest OVERLAPPEDterrorR4tFILE_IN_USE_ERRORR&R'R(R)(R RRR,R*thfile((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyRßsD          cCs¤|iory8ti|iiƒƒ}ti|ddtiƒƒWq|tij o$}|dt i jo‚qxq|Xnt |_|io|ii ƒndS(s4Close and unlock the file using the win32 primitive.iiÿÿN( RR5R6R R1t UnlockFileExR;R<R=R4tFILE_ALREADY_UNLOCKED_ERRORRR((R R?R*((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyRs    (RRRR>RARR(((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyR4Ôs  5t LockedFilecBsMeZdZed„Zd„Zd„Zd„Zddd„Zd„Z RS( s+Represent a file that has exclusive access.cCs}d}| oE|o>tot|||ƒ}ntot|||ƒ}qSn|pt|||ƒ}n||_dS(s#Construct a LockedFile. Args: filename: string, The path of the file to open. mode: string, The mode to try to open the file with. fallback_mode: string, The mode to use if locking fails. use_native_locking: bool, Whether or not fcntl/win32 locking is used. N(R R4R.Rt_opener(R R RRtuse_native_lockingtopener((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyR's cCs |iiS(s-Return the filename we were constructed with.(RCR(R ((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyR <scCs |iiƒS(s*Return the file_handle to the opened file.(RCR(R ((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyR@scCs |iiƒS(s/Return whether we successfully locked the file.(RCR(R ((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyRDsigš™™™™™©?cCs|ii||ƒdS(s1Open the file, trying to lock it. Args: timeout: float, The number of seconds to try to acquire the lock. delay: float, The number of seconds to wait between retry attempts. Raises: AlreadyLockedException: if the lock is already acquired. IOError: if the open fails. N(RCR(R RR((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyRHs cCs|iiƒdS(sUnlock and close a file.N(RCR(R ((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyRUs( RRRRRR RRRR(((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyRB$s     (RRtloggingRRt getLoggerRR&t ExceptionRtobjectRRR/R.t ImportErrorR R;R8R5R4RB(((si/Users/riccardo/git/gcloud/packages/gcutil-1.7.1/lib/google_api_python_client/oauth2client/locked_file.pyts*    +C 8    L