<s> from . output import Output <EOL> import os <EOL> import gevent <EOL> import subprocess <EOL> import datetime <EOL> from . . event import Event <EOL> from . . util import Periodic <EOL> class File ( Output ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , max_size = None , max_count = <NUM_LIT:10> , compress = None ) : <EOL> super ( File , self ) . __init__ ( ) <EOL> self . filename = filename <EOL> self . max_size = max_size <EOL> self . max_count = max_count <EOL> if compress is True : <EOL> compress = '<STR_LIT>' <EOL> elif compress is None : <EOL> compress = False <EOL> assert compress in ( False , '<STR_LIT>' , ) <EOL> self . compress = compress <EOL> self . last_filename = None <EOL> self . last_event = None <EOL> if '<STR_LIT>' in self . filename : <EOL> self . periodic = Periodic ( <NUM_LIT:30> , self . _check_rotate ) <EOL> else : <EOL> self . periodic = None <EOL> def _check_rotate ( self ) : <EOL> now = datetime . datetime . utcnow ( ) <EOL> filename = self . filename . format ( timestamp = now ) <EOL> if self . last_filename and filename != self . last_filename : <EOL> self . _rotate ( self . last_filename , self . last_event , self . last_event ) <EOL> self . last_filename = filename <EOL> def start ( self ) : <EOL> super ( File , self ) . start ( ) <EOL> if self . periodic is not None : <EOL> self . periodic . start ( ) <EOL> def stop ( self ) : <EOL> if self . periodic is not None : <EOL> self . periodic . kill ( ) <EOL> self . periodic . join ( ) <EOL> super ( File , self ) . stop ( ) <EOL> def process ( self , event ) : <EOL> filename = event . format ( self . filename ) <EOL> if self . max_size and os . path . exists ( filename ) and os . path . getsize ( filename ) > self . max_size : <EOL> self . _rotate ( filename , self . last_event , event ) <EOL> if '<STR_LIT>' in self . filename and self . last_filename != filename : <EOL> if self . last_filename : <EOL> self . _rotate ( self . last_filename , self . last_event , event ) <EOL> self . last_filename = filename <EOL> self . last_event = event <EOL> dirname = os . path . dirname ( filename ) <EOL> if dirname and not os . path . exists ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> with file ( filename , '<STR_LIT:a>' ) as fout : <EOL> d = event . to_json ( ) <EOL> self . logger . debug ( '<STR_LIT>' % ( filename , d ) ) <EOL> print >> fout , d <EOL> def _rotate ( self , filename , last , trigger ) : <EOL> if not os . path . exists ( filename ) : <EOL> return <EOL> suffix = '<STR_LIT>' <EOL> if self . compress == '<STR_LIT>' : <EOL> suffix = '<STR_LIT:.>' + self . compress <EOL> roll_first = filename + '<STR_LIT>' + suffix <EOL> renames = [ ( filename , roll_first ) ] <EOL> self . logger . info ( '<STR_LIT>' % ( filename , roll_first ) ) <EOL> for n , ( oldname , newname ) in enumerate ( renames ) : <EOL> if self . max_count and n == self . max_count - <NUM_LIT:1> : <EOL> break <EOL> if os . path . exists ( newname ) : <EOL> base , num = newname . strip ( suffix ) . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> renames . append ( ( newname , '<STR_LIT>' % ( base , int ( num ) + <NUM_LIT:1> , suffix ) ) ) <EOL> if suffix : <EOL> renames [ <NUM_LIT:0> ] = ( filename , filename + '<STR_LIT>' ) <EOL> for oldname , newname in reversed ( renames ) : <EOL> self . logger . debug ( '<STR_LIT>' % ( oldname , newname ) ) <EOL> os . rename ( oldname , newname ) <EOL> if self . compress == '<STR_LIT>' : <EOL> self . _gz ( filename + '<STR_LIT>' ) <EOL> self . output . put ( Event ( tags = [ '<STR_LIT>' ] , filename = roll_first , last = last or trigger , trigger = trigger ) ) <EOL> def _gz ( self , filename ) : <EOL> self . logger . debug ( '<STR_LIT>' % ( filename , ) ) <EOL> ps = subprocess . Popen ( [ '<STR_LIT>' , filename ] ) <EOL> while ps . poll ( ) is None : <EOL> gevent . sleep ( <NUM_LIT> ) </s>
