<s> """<STR_LIT>""" <EOL> import os , glob , time , stat <EOL> from twisted . python import threadable <EOL> class BaseLogFile : <EOL> """<STR_LIT>""" <EOL> synchronized = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __init__ ( self , name , directory , defaultMode = None ) : <EOL> """<STR_LIT>""" <EOL> self . directory = directory <EOL> self . name = name <EOL> self . path = os . path . join ( directory , name ) <EOL> if defaultMode is None and os . path . exists ( self . path ) : <EOL> self . defaultMode = stat . S_IMODE ( os . stat ( self . path ) [ stat . ST_MODE ] ) <EOL> else : <EOL> self . defaultMode = defaultMode <EOL> self . _openFile ( ) <EOL> def fromFullPath ( cls , filename , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> logPath = os . path . abspath ( filename ) <EOL> return cls ( os . path . basename ( logPath ) , <EOL> os . path . dirname ( logPath ) , * args , ** kwargs ) <EOL> fromFullPath = classmethod ( fromFullPath ) <EOL> def shouldRotate ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def _openFile ( self ) : <EOL> """<STR_LIT>""" <EOL> self . closed = False <EOL> if os . path . exists ( self . path ) : <EOL> self . _file = file ( self . path , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _file . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> else : <EOL> if self . defaultMode is not None : <EOL> oldUmask = os . umask ( <NUM_LIT:0> <NUM_LIT> ) <EOL> try : <EOL> self . _file = file ( self . path , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> finally : <EOL> os . umask ( oldUmask ) <EOL> else : <EOL> self . _file = file ( self . path , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> if self . defaultMode is not None : <EOL> try : <EOL> os . chmod ( self . path , self . defaultMode ) <EOL> except OSError : <EOL> pass <EOL> def __getstate__ ( self ) : <EOL> state = self . __dict__ . copy ( ) <EOL> del state [ "<STR_LIT>" ] <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> self . __dict__ = state <EOL> self . _openFile ( ) <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if self . shouldRotate ( ) : <EOL> self . flush ( ) <EOL> self . rotate ( ) <EOL> self . _file . write ( data ) <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _file . flush ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . closed = True <EOL> self . _file . close ( ) <EOL> self . _file = None <EOL> def reopen ( self ) : <EOL> """<STR_LIT>""" <EOL> self . close ( ) <EOL> self . _openFile ( ) <EOL> def getCurrentLog ( self ) : <EOL> """<STR_LIT>""" <EOL> return LogReader ( self . path ) <EOL> class LogFile ( BaseLogFile ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , directory , rotateLength = <NUM_LIT> , defaultMode = None , <EOL> maxRotatedFiles = None ) : <EOL> """<STR_LIT>""" <EOL> BaseLogFile . __init__ ( self , name , directory , defaultMode ) <EOL> self . rotateLength = rotateLength <EOL> self . maxRotatedFiles = maxRotatedFiles <EOL> def _openFile ( self ) : <EOL> BaseLogFile . _openFile ( self ) <EOL> self . size = self . _file . tell ( ) <EOL> def shouldRotate ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . rotateLength and self . size >= self . rotateLength <EOL> def getLog ( self , identifier ) : <EOL> """<STR_LIT>""" <EOL> filename = "<STR_LIT>" % ( self . path , identifier ) <EOL> if not os . path . exists ( filename ) : <EOL> raise ValueError , "<STR_LIT>" <EOL> return LogReader ( filename ) <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> BaseLogFile . write ( self , data ) <EOL> self . size += len ( data ) <EOL> def rotate ( self ) : <EOL> """<STR_LIT>""" <EOL> if not ( os . access ( self . directory , os . W_OK ) and os . access ( self . path , os . W_OK ) ) : <EOL> return <EOL> logs = self . listLogs ( ) <EOL> logs . reverse ( ) <EOL> for i in logs : <EOL> if self . maxRotatedFiles is not None and i >= self . maxRotatedFiles : <EOL> os . remove ( "<STR_LIT>" % ( self . path , i ) ) <EOL> else : <EOL> os . rename ( "<STR_LIT>" % ( self . path , i ) , "<STR_LIT>" % ( self . path , i + <NUM_LIT:1> ) ) <EOL> self . _file . close ( ) <EOL> os . rename ( self . path , "<STR_LIT>" % self . path ) <EOL> self . _openFile ( ) <EOL> def listLogs ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for name in glob . glob ( "<STR_LIT>" % self . path ) : <EOL> try : <EOL> counter = int ( name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] ) <EOL> if counter : <EOL> result . append ( counter ) <EOL> except ValueError : <EOL> pass <EOL> result . sort ( ) <EOL> return result <EOL> def __getstate__ ( self ) : <EOL> state = BaseLogFile . __getstate__ ( self ) <EOL> del state [ "<STR_LIT:size>" ] <EOL> return state <EOL> threadable . synchronize ( LogFile ) <EOL> class DailyLogFile ( BaseLogFile ) : <EOL> """<STR_LIT>""" <EOL> def _openFile ( self ) : <EOL> BaseLogFile . _openFile ( self ) <EOL> self . lastDate = self . toDate ( os . stat ( self . path ) [ <NUM_LIT:8> ] ) <EOL> def shouldRotate ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . toDate ( ) > self . lastDate <EOL> def toDate ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return time . localtime ( * args ) [ : <NUM_LIT:3> ] <EOL> def suffix ( self , tupledate ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return '<STR_LIT:_>' . join ( map ( str , tupledate ) ) <EOL> except : <EOL> return '<STR_LIT:_>' . join ( map ( str , self . toDate ( tupledate ) ) ) <EOL> def getLog ( self , identifier ) : <EOL> """<STR_LIT>""" <EOL> if self . toDate ( identifier ) == self . lastDate : <EOL> return self . getCurrentLog ( ) <EOL> filename = "<STR_LIT>" % ( self . path , self . suffix ( identifier ) ) <EOL> if not os . path . exists ( filename ) : <EOL> raise ValueError , "<STR_LIT>" <EOL> return LogReader ( filename ) <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> BaseLogFile . write ( self , data ) <EOL> self . lastDate = max ( self . lastDate , self . toDate ( ) ) <EOL> def rotate ( self ) : <EOL> """<STR_LIT>""" <EOL> if not ( os . access ( self . directory , os . W_OK ) and os . access ( self . path , os . W_OK ) ) : <EOL> return <EOL> newpath = "<STR_LIT>" % ( self . path , self . suffix ( self . lastDate ) ) <EOL> if os . path . exists ( newpath ) : <EOL> return <EOL> self . _file . close ( ) <EOL> os . rename ( self . path , newpath ) <EOL> self . _openFile ( ) <EOL> def __getstate__ ( self ) : <EOL> state = BaseLogFile . __getstate__ ( self ) <EOL> del state [ "<STR_LIT>" ] <EOL> return state <EOL> threadable . synchronize ( DailyLogFile ) <EOL> class LogReader : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . _file = file ( name , "<STR_LIT:r>" ) <EOL> def readLines ( self , lines = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for i in range ( lines ) : <EOL> line = self . _file . readline ( ) <EOL> if not line : <EOL> break <EOL> result . append ( line ) <EOL> return result <EOL> def close ( self ) : <EOL> self . _file . close ( ) </s>
