<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import errno <EOL> import logging <EOL> import os <EOL> import time <EOL> from oauth2client import util <EOL> logger = logging . getLogger ( __name__ ) <EOL> class CredentialsFileSymbolicLinkError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class AlreadyLockedException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def validate_file ( filename ) : <EOL> if os . path . islink ( filename ) : <EOL> raise CredentialsFileSymbolicLinkError ( <EOL> '<STR_LIT>' % filename ) <EOL> class _Opener ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , mode , fallback_mode ) : <EOL> """<STR_LIT>""" <EOL> self . _locked = False <EOL> self . _filename = filename <EOL> self . _mode = mode <EOL> self . _fallback_mode = fallback_mode <EOL> self . _fh = None <EOL> def is_locked ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _locked <EOL> def file_handle ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _fh <EOL> def filename ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _filename <EOL> def open_and_lock ( self , timeout , delay ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def unlock_and_close ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class _PosixOpener ( _Opener ) : <EOL> """<STR_LIT>""" <EOL> def open_and_lock ( self , timeout , delay ) : <EOL> """<STR_LIT>""" <EOL> if self . _locked : <EOL> raise AlreadyLockedException ( '<STR_LIT>' % <EOL> self . _filename ) <EOL> self . _locked = False <EOL> validate_file ( self . _filename ) <EOL> try : <EOL> self . _fh = open ( self . _filename , self . _mode ) <EOL> except IOError , e : <EOL> if e . errno == errno . EACCES : <EOL> self . _fh = open ( self . _filename , self . _fallback_mode ) <EOL> return <EOL> lock_filename = self . _posix_lockfile ( self . _filename ) <EOL> start_time = time . time ( ) <EOL> while True : <EOL> try : <EOL> self . _lock_fd = os . open ( lock_filename , <EOL> os . O_CREAT | os . O_EXCL | os . O_RDWR ) <EOL> self . _locked = True <EOL> break <EOL> except OSError , e : <EOL> if e . errno != errno . EEXIST : <EOL> raise <EOL> if ( time . time ( ) - start_time ) >= timeout : <EOL> logger . warn ( '<STR_LIT>' % ( <EOL> lock_filename , timeout ) ) <EOL> if self . _fh : <EOL> self . _fh . close ( ) <EOL> self . _fh = open ( self . _filename , self . _fallback_mode ) <EOL> return <EOL> time . sleep ( delay ) <EOL> def unlock_and_close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _locked : <EOL> lock_filename = self . _posix_lockfile ( self . _filename ) <EOL> os . close ( self . _lock_fd ) <EOL> os . unlink ( lock_filename ) <EOL> self . _locked = False <EOL> self . _lock_fd = None <EOL> if self . _fh : <EOL> self . _fh . close ( ) <EOL> def _posix_lockfile ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % filename <EOL> try : <EOL> import fcntl <EOL> class _FcntlOpener ( _Opener ) : <EOL> """<STR_LIT>""" <EOL> def open_and_lock ( self , timeout , delay ) : <EOL> """<STR_LIT>""" <EOL> if self . _locked : <EOL> raise AlreadyLockedException ( '<STR_LIT>' % <EOL> self . _filename ) <EOL> start_time = time . time ( ) <EOL> validate_file ( self . _filename ) <EOL> try : <EOL> self . _fh = open ( self . _filename , self . _mode ) <EOL> except IOError , e : <EOL> if e . errno == errno . EACCES : <EOL> self . _fh = open ( self . _filename , self . _fallback_mode ) <EOL> return <EOL> while True : <EOL> try : <EOL> fcntl . lockf ( self . _fh . fileno ( ) , fcntl . LOCK_EX ) <EOL> self . _locked = True <EOL> return <EOL> except IOError , e : <EOL> if timeout == <NUM_LIT:0> : <EOL> raise e <EOL> if e . errno != errno . EACCES : <EOL> raise e <EOL> if ( time . time ( ) - start_time ) >= timeout : <EOL> logger . warn ( '<STR_LIT>' % ( <EOL> self . _filename , timeout ) ) <EOL> if self . _fh : <EOL> self . _fh . close ( ) <EOL> self . _fh = open ( self . _filename , self . _fallback_mode ) <EOL> return <EOL> time . sleep ( delay ) <EOL> def unlock_and_close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _locked : <EOL> fcntl . lockf ( self . _fh . fileno ( ) , fcntl . LOCK_UN ) <EOL> self . _locked = False <EOL> if self . _fh : <EOL> self . _fh . close ( ) <EOL> except ImportError : <EOL> _FcntlOpener = None <EOL> try : <EOL> import pywintypes <EOL> import win32con <EOL> import win32file <EOL> class _Win32Opener ( _Opener ) : <EOL> """<STR_LIT>""" <EOL> FILE_IN_USE_ERROR = <NUM_LIT> <EOL> FILE_ALREADY_UNLOCKED_ERROR = <NUM_LIT> <EOL> def open_and_lock ( self , timeout , delay ) : <EOL> """<STR_LIT>""" <EOL> if self . _locked : <EOL> raise AlreadyLockedException ( '<STR_LIT>' % <EOL> self . _filename ) <EOL> start_time = time . time ( ) <EOL> validate_file ( self . _filename ) <EOL> try : <EOL> self . _fh = open ( self . _filename , self . _mode ) <EOL> except IOError , e : <EOL> if e . errno == errno . EACCES : <EOL> self . _fh = open ( self . _filename , self . _fallback_mode ) <EOL> return <EOL> while True : <EOL> try : <EOL> hfile = win32file . _get_osfhandle ( self . _fh . fileno ( ) ) <EOL> win32file . LockFileEx ( <EOL> hfile , <EOL> ( win32con . LOCKFILE_FAIL_IMMEDIATELY | <EOL> win32con . LOCKFILE_EXCLUSIVE_LOCK ) , <NUM_LIT:0> , - <NUM_LIT> , <EOL> pywintypes . OVERLAPPED ( ) ) <EOL> self . _locked = True <EOL> return <EOL> except pywintypes . error , e : <EOL> if timeout == <NUM_LIT:0> : <EOL> raise e <EOL> if e [ <NUM_LIT:0> ] != _Win32Opener . FILE_IN_USE_ERROR : <EOL> raise <EOL> if ( time . time ( ) - start_time ) >= timeout : <EOL> logger . warn ( '<STR_LIT>' % ( <EOL> self . _filename , timeout ) ) <EOL> if self . _fh : <EOL> self . _fh . close ( ) <EOL> self . _fh = open ( self . _filename , self . _fallback_mode ) <EOL> return <EOL> time . sleep ( delay ) <EOL> def unlock_and_close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _locked : <EOL> try : <EOL> hfile = win32file . _get_osfhandle ( self . _fh . fileno ( ) ) <EOL> win32file . UnlockFileEx ( hfile , <NUM_LIT:0> , - <NUM_LIT> , pywintypes . OVERLAPPED ( ) ) <EOL> except pywintypes . error , e : <EOL> if e [ <NUM_LIT:0> ] != _Win32Opener . FILE_ALREADY_UNLOCKED_ERROR : <EOL> raise <EOL> self . _locked = False <EOL> if self . _fh : <EOL> self . _fh . close ( ) <EOL> except ImportError : <EOL> _Win32Opener = None <EOL> class LockedFile ( object ) : <EOL> """<STR_LIT>""" <EOL> @ util . positional ( <NUM_LIT:4> ) <EOL> def __init__ ( self , filename , mode , fallback_mode , use_native_locking = True ) : <EOL> """<STR_LIT>""" <EOL> opener = None <EOL> if not opener and use_native_locking : <EOL> if _Win32Opener : <EOL> opener = _Win32Opener ( filename , mode , fallback_mode ) <EOL> if _FcntlOpener : <EOL> opener = _FcntlOpener ( filename , mode , fallback_mode ) <EOL> if not opener : <EOL> opener = _PosixOpener ( filename , mode , fallback_mode ) <EOL> self . _opener = opener <EOL> def filename ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _opener . _filename <EOL> def file_handle ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _opener . file_handle ( ) <EOL> def is_locked ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _opener . is_locked ( ) <EOL> def open_and_lock ( self , timeout = <NUM_LIT:0> , delay = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> self . _opener . open_and_lock ( timeout , delay ) <EOL> def unlock_and_close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _opener . unlock_and_close ( ) </s>
