@contextlib.contextmanager
def wait_lock(path, lock_fn=None, timeout=5, sleep=0.1, time_start=None):
 if (not isinstance(path, six.string_types)):
  raise FileLockError('path must be a string')
 if (lock_fn is None):
  lock_fn = (path + '.w')
 if (time_start is None):
  time_start = time.time()
 obtained_lock = False
 def _raise_error(msg, race=False):
  '\n        Raise a FileLockError\n        '
  raise FileLockError(msg, time_start=time_start)
 try:
  if (os.path.exists(lock_fn) and (not os.path.isfile(lock_fn))):
   _raise_error('lock_fn {0} exists and is not a file'.format(lock_fn))
  open_flags = ((os.O_CREAT | os.O_EXCL) | os.O_WRONLY)
  while ((time.time() - time_start) < timeout):
   try:
    fh_ = os.open(lock_fn, open_flags)
   except (IOError, OSError) as exc:
    if (exc.errno != errno.EEXIST):
     _raise_error('Error {0} encountered obtaining file lock {1}: {2}'.format(exc.errno, lock_fn, exc.strerror))
    log.trace('Lock file %s exists, sleeping %f seconds', lock_fn, sleep)
    time.sleep(sleep)
   else:
    with os.fdopen(fh_, 'w'):
     pass
    log.trace('Write lock %s obtained', lock_fn)
    obtained_lock = True
    (yield)
    break
  else:
   _raise_error('Timeout of {0} seconds exceeded waiting for lock_fn {1} to be released'.format(timeout, lock_fn))
 except FileLockError:
  raise
 except Exception as exc:
  _raise_error('Error encountered obtaining file lock {0}: {1}'.format(lock_fn, exc))
 finally:
  if obtained_lock:
   os.remove(lock_fn)
   log.trace('Write lock for %s (%s) released', path, lock_fn)

@ contextlib . contextmanager def wait_lock ( path , lock_fn = None , timeout = 5 , sleep = 0.1 , time_start = None ) : if ( not isinstance ( path , six . string_types ) ) : raise FileLockError ( 'path must be a string' ) if ( lock_fn is None ) : lock_fn = ( path + '.w' ) if ( time_start is None ) : time_start = time . time ( ) obtained_lock = False def _raise_error ( msg , race = False ) : raise FileLockError ( msg , time_start = time_start ) try : if ( os . path . exists ( lock_fn ) and ( not os . path . isfile ( lock_fn ) ) ) : _raise_error ( 'lock_fn {0} exists and is not a file' . format ( lock_fn ) ) open_flags = ( ( os . O_CREAT | os . O_EXCL ) | os . O_WRONLY ) while ( ( time . time ( ) - time_start ) < timeout ) : try : fh_ = os . open ( lock_fn , open_flags ) except ( IOError , OSError ) as exc : if ( exc . errno != errno . EEXIST ) : _raise_error ( 'Error {0} encountered obtaining file lock {1}: {2}' . format ( exc . errno , lock_fn , exc . strerror ) ) log . trace ( 'Lock file %s exists, sleeping %f seconds' , lock_fn , sleep ) time . sleep ( sleep ) else : with os . fdopen ( fh_ , 'w' ) : pass log . trace ( 'Write lock %s obtained' , lock_fn ) obtained_lock = True ( yield ) break else : _raise_error ( 'Timeout of {0} seconds exceeded waiting for lock_fn {1} to be released' . format ( timeout , lock_fn ) ) except FileLockError : raise except Exception as exc : _raise_error ( 'Error encountered obtaining file lock {0}: {1}' . format ( lock_fn , exc ) ) finally : if obtained_lock : os . remove ( lock_fn ) log . trace ( 'Write lock for %s (%s) released' , path , lock_fn )
if the write lock is there .