<s> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> import os <EOL> class LockError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> LOCKTYPE_FCNTL = <NUM_LIT:1> <EOL> LOCKTYPE_MSVCRT = <NUM_LIT:2> <EOL> LOCKTYPE = None <EOL> try : <EOL> import fcntl <EOL> except : <EOL> try : <EOL> import msvcrt <EOL> except : <EOL> LOCKTYPE = None <EOL> else : <EOL> LOCKTYPE = LOCKTYPE_MSVCRT <EOL> else : <EOL> LOCKTYPE = LOCKTYPE_FCNTL <EOL> LOCK_EX = <NUM_LIT:1> <EOL> LOCK_SH = <NUM_LIT:2> <EOL> LOCK_UN = <NUM_LIT:3> <EOL> def lock_file ( f , lock = LOCK_SH ) : <EOL> try : <EOL> fd = f . fileno ( ) <EOL> if LOCKTYPE == LOCKTYPE_FCNTL : <EOL> if lock == LOCK_SH : <EOL> fcntl . flock ( fd , fcntl . LOCK_SH ) <EOL> elif lock == LOCK_EX : <EOL> fcntl . flock ( fd , fcntl . LOCK_EX ) <EOL> elif lock == LOCK_UN : <EOL> fcntl . flock ( fd , fcntl . LOCK_UN ) <EOL> else : <EOL> raise LockError , "<STR_LIT>" <EOL> elif LOCKTYPE == LOCKTYPE_MSVCRT : <EOL> if lock == LOCK_SH : <EOL> msvcrt . locking ( fd , msvcrt . LK_LOCK , <NUM_LIT:1> ) <EOL> elif lock == LOCK_EX : <EOL> msvcrt . locking ( fd , msvcrt . LK_LOCK , <NUM_LIT:1> ) <EOL> elif lock == LOCK_UN : <EOL> msvcrt . locking ( fd , msvcrt . LK_UNLCK , <NUM_LIT:1> ) <EOL> else : <EOL> raise LockError , "<STR_LIT>" <EOL> else : <EOL> raise LockError , "<STR_LIT>" <EOL> except IOError : <EOL> raise LockError ( "<STR_LIT>" % f . name ) <EOL> def unlock_file ( f ) : <EOL> lock_file ( f , LOCK_UN ) <EOL> class LockFile ( object ) : <EOL> def __init__ ( self , lockfilename ) : <EOL> self . _f = lockfilename <EOL> self . _create_flag = False <EOL> self . _checkfile ( ) <EOL> def _checkfile ( self ) : <EOL> if os . path . exists ( self . _f ) : <EOL> self . _fd = open ( self . _f , '<STR_LIT:rb>' ) <EOL> else : <EOL> dir = os . path . dirname ( self . _f ) <EOL> if dir and not os . path . exists ( dir ) : <EOL> os . makedirs ( dir ) <EOL> self . _fd = open ( self . _f , '<STR_LIT:wb>' ) <EOL> self . _create_flag = True <EOL> def lock ( self , lock_flag = LOCK_SH ) : <EOL> lock_file ( self . _fd , lock_flag ) <EOL> def close ( self ) : <EOL> unlock_file ( self . _fd ) <EOL> self . _fd . close ( ) <EOL> def delete ( self ) : <EOL> self . _fd . close ( ) <EOL> os . unlink ( self . _f ) </s>
