<s> from __future__ import absolute_import <EOL> import os <EOL> import time <EOL> from . import ( LockBase , NotLocked , NotMyLock , LockTimeout , <EOL> AlreadyLocked ) <EOL> class SymlinkLockFile ( LockBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path , threaded = True , timeout = None ) : <EOL> LockBase . __init__ ( self , path , threaded , timeout ) <EOL> self . unique_name = os . path . split ( self . unique_name ) [ <NUM_LIT:1> ] <EOL> def acquire ( self , timeout = None ) : <EOL> timeout = timeout if timeout is not None else self . timeout <EOL> end_time = time . time ( ) <EOL> if timeout is not None and timeout > <NUM_LIT:0> : <EOL> end_time += timeout <EOL> while True : <EOL> try : <EOL> os . symlink ( self . unique_name , self . lock_file ) <EOL> except OSError : <EOL> if self . i_am_locking ( ) : <EOL> return <EOL> else : <EOL> if timeout is not None and time . time ( ) > end_time : <EOL> if timeout > <NUM_LIT:0> : <EOL> raise LockTimeout ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> self . path ) <EOL> else : <EOL> raise AlreadyLocked ( "<STR_LIT>" % <EOL> self . path ) <EOL> time . sleep ( timeout / <NUM_LIT:10> if timeout is not None else <NUM_LIT:0.1> ) <EOL> else : <EOL> return <EOL> def release ( self ) : <EOL> if not self . is_locked ( ) : <EOL> raise NotLocked ( "<STR_LIT>" % self . path ) <EOL> elif not self . i_am_locking ( ) : <EOL> raise NotMyLock ( "<STR_LIT>" % self . path ) <EOL> os . unlink ( self . lock_file ) <EOL> def is_locked ( self ) : <EOL> return os . path . islink ( self . lock_file ) <EOL> def i_am_locking ( self ) : <EOL> return ( os . path . islink ( self . lock_file ) and <EOL> os . readlink ( self . lock_file ) == self . unique_name ) <EOL> def break_lock ( self ) : <EOL> if os . path . islink ( self . lock_file ) : <EOL> os . unlink ( self . lock_file ) </s>
