<s> from __future__ import absolute_import , division <EOL> import time <EOL> import os <EOL> import sys <EOL> import errno <EOL> from . import ( LockBase , LockFailed , NotLocked , NotMyLock , LockTimeout , <EOL> AlreadyLocked ) <EOL> class MkdirLockFile ( LockBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path , threaded = True , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> LockBase . __init__ ( self , path , threaded , timeout ) <EOL> self . unique_name = os . path . join ( self . lock_file , <EOL> "<STR_LIT>" % ( self . hostname , <EOL> self . tname , <EOL> self . pid ) ) <EOL> def acquire ( self , timeout = None ) : <EOL> timeout = timeout is not None and timeout or self . timeout <EOL> end_time = time . time ( ) <EOL> if timeout is not None and timeout > <NUM_LIT:0> : <EOL> end_time += timeout <EOL> if timeout is None : <EOL> wait = <NUM_LIT:0.1> <EOL> else : <EOL> wait = max ( <NUM_LIT:0> , timeout / <NUM_LIT:10> ) <EOL> while True : <EOL> try : <EOL> os . mkdir ( self . lock_file ) <EOL> except OSError : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if err . errno == errno . EEXIST : <EOL> if os . path . exists ( self . unique_name ) : <EOL> return <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 ( wait ) <EOL> else : <EOL> raise LockFailed ( "<STR_LIT>" % self . lock_file ) <EOL> else : <EOL> open ( self . unique_name , "<STR_LIT:wb>" ) . close ( ) <EOL> return <EOL> def release ( self ) : <EOL> if not self . is_locked ( ) : <EOL> raise NotLocked ( "<STR_LIT>" % self . path ) <EOL> elif not os . path . exists ( self . unique_name ) : <EOL> raise NotMyLock ( "<STR_LIT>" % self . path ) <EOL> os . unlink ( self . unique_name ) <EOL> os . rmdir ( self . lock_file ) <EOL> def is_locked ( self ) : <EOL> return os . path . exists ( self . lock_file ) <EOL> def i_am_locking ( self ) : <EOL> return ( self . is_locked ( ) and <EOL> os . path . exists ( self . unique_name ) ) <EOL> def break_lock ( self ) : <EOL> if os . path . exists ( self . lock_file ) : <EOL> for name in os . listdir ( self . lock_file ) : <EOL> os . unlink ( os . path . join ( self . lock_file , name ) ) <EOL> os . rmdir ( self . lock_file ) </s>
