<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division <EOL> import errno <EOL> import os <EOL> from time import time as _uniquefloat <EOL> from twisted . python . runtime import platform <EOL> from twisted . python . compat import _PY3 <EOL> def unique ( ) : <EOL> return str ( int ( _uniquefloat ( ) * <NUM_LIT:1000> ) ) <EOL> from os import rename <EOL> if not platform . isWindows ( ) : <EOL> from os import kill <EOL> from os import symlink <EOL> from os import readlink <EOL> from os import remove as rmlink <EOL> _windows = False <EOL> else : <EOL> _windows = True <EOL> try : <EOL> from win32api import OpenProcess <EOL> import pywintypes <EOL> except ImportError : <EOL> kill = None <EOL> else : <EOL> ERROR_ACCESS_DENIED = <NUM_LIT:5> <EOL> ERROR_INVALID_PARAMETER = <NUM_LIT> <EOL> def kill ( pid , signal ) : <EOL> try : <EOL> OpenProcess ( <NUM_LIT:0> , <NUM_LIT:0> , pid ) <EOL> except pywintypes . error as e : <EOL> if e . args [ <NUM_LIT:0> ] == ERROR_ACCESS_DENIED : <EOL> return <EOL> elif e . args [ <NUM_LIT:0> ] == ERROR_INVALID_PARAMETER : <EOL> raise OSError ( errno . ESRCH , None ) <EOL> raise <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> _open = open <EOL> def symlink ( value , filename ) : <EOL> """<STR_LIT>""" <EOL> newlinkname = filename + "<STR_LIT:.>" + unique ( ) + '<STR_LIT>' <EOL> newvalname = os . path . join ( newlinkname , "<STR_LIT>" ) <EOL> os . mkdir ( newlinkname ) <EOL> if _PY3 : <EOL> mode = '<STR_LIT:w>' <EOL> else : <EOL> mode = '<STR_LIT>' <EOL> with _open ( newvalname , mode ) as f : <EOL> f . write ( value ) <EOL> f . flush ( ) <EOL> try : <EOL> rename ( newlinkname , filename ) <EOL> except : <EOL> os . remove ( newvalname ) <EOL> os . rmdir ( newlinkname ) <EOL> raise <EOL> def readlink ( filename ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> fObj = _open ( os . path . join ( filename , '<STR_LIT>' ) , '<STR_LIT:r>' ) <EOL> except IOError as e : <EOL> if e . errno == errno . ENOENT or e . errno == errno . EIO : <EOL> raise OSError ( e . errno , None ) <EOL> raise <EOL> else : <EOL> result = fObj . read ( ) <EOL> fObj . close ( ) <EOL> return result <EOL> def rmlink ( filename ) : <EOL> os . remove ( os . path . join ( filename , '<STR_LIT>' ) ) <EOL> os . rmdir ( filename ) <EOL> class FilesystemLock ( object ) : <EOL> """<STR_LIT>""" <EOL> clean = None <EOL> locked = False <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def lock ( self ) : <EOL> """<STR_LIT>""" <EOL> clean = True <EOL> while True : <EOL> try : <EOL> symlink ( str ( os . getpid ( ) ) , self . name ) <EOL> except OSError as e : <EOL> if _windows and e . errno in ( errno . EACCES , errno . EIO ) : <EOL> return False <EOL> if e . errno == errno . EEXIST : <EOL> try : <EOL> pid = readlink ( self . name ) <EOL> except ( IOError , OSError ) as e : <EOL> if e . errno == errno . ENOENT : <EOL> continue <EOL> elif _windows and e . errno == errno . EACCES : <EOL> return False <EOL> raise <EOL> try : <EOL> if kill is not None : <EOL> kill ( int ( pid ) , <NUM_LIT:0> ) <EOL> except OSError as e : <EOL> if e . errno == errno . ESRCH : <EOL> try : <EOL> rmlink ( self . name ) <EOL> except OSError as e : <EOL> if e . errno == errno . ENOENT : <EOL> continue <EOL> raise <EOL> clean = False <EOL> continue <EOL> raise <EOL> return False <EOL> raise <EOL> self . locked = True <EOL> self . clean = clean <EOL> return True <EOL> def unlock ( self ) : <EOL> """<STR_LIT>""" <EOL> pid = readlink ( self . name ) <EOL> if int ( pid ) != os . getpid ( ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % ( self . name , ) ) <EOL> rmlink ( self . name ) <EOL> self . locked = False <EOL> def isLocked ( name ) : <EOL> """<STR_LIT>""" <EOL> l = FilesystemLock ( name ) <EOL> result = None <EOL> try : <EOL> result = l . lock ( ) <EOL> finally : <EOL> if result : <EOL> l . unlock ( ) <EOL> return not result <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] </s>
