<s> """<STR_LIT>""" <EOL> import sys <EOL> from time import strftime <EOL> from bitmath import MiB <EOL> from eliot import ( <EOL> MessageType , fields , Logger , FileDestination , add_destination , <EOL> ) <EOL> from eliot . logwriter import ThreadedWriter <EOL> from twisted . application . service import MultiService , Service <EOL> from twisted . internet import task , reactor as global_reactor <EOL> from twisted . internet . defer import Deferred , maybeDeferred <EOL> from twisted . python import usage <EOL> from twisted . python . log import textFromEventDict , startLoggingWithObserver , err <EOL> from twisted . python import log as twisted_log <EOL> from twisted . python . logfile import LogFile <EOL> from twisted . python . filepath import FilePath <EOL> from twisted . python . usage import UsageError <EOL> from zope . interface import Interface <EOL> from . . import __version__ <EOL> try : <EOL> from eliot . journald import JournaldDestination <EOL> except ImportError as e : <EOL> JournaldDestination = None <EOL> _missing_journald_reason = str ( e ) <EOL> del e <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> LOGFILE_LENGTH = int ( MiB ( <NUM_LIT:100> ) . to_Byte ( ) . value ) <EOL> LOGFILE_COUNT = <NUM_LIT:5> <EOL> def flocker_standard_options ( cls ) : <EOL> """<STR_LIT>""" <EOL> original_init = cls . __init__ <EOL> original_postOptions = cls . postOptions <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _sys_module = kwargs . pop ( '<STR_LIT>' , sys ) <EOL> self [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self [ '<STR_LIT>' ] = None <EOL> self [ '<STR_LIT>' ] = False <EOL> original_init ( self , * args , ** kwargs ) <EOL> cls . __init__ = __init__ <EOL> def opt_version ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _sys_module . stdout . write ( __version__ . encode ( '<STR_LIT:utf-8>' ) + b'<STR_LIT:\n>' ) <EOL> raise SystemExit ( <NUM_LIT:0> ) <EOL> cls . opt_version = opt_version <EOL> def opt_verbose ( self ) : <EOL> """<STR_LIT>""" <EOL> self [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> cls . opt_verbose = opt_verbose <EOL> cls . opt_v = opt_verbose <EOL> def opt_logfile ( self , logfile_path ) : <EOL> """<STR_LIT>""" <EOL> self [ '<STR_LIT>' ] = logfile_path <EOL> opt_logfile . __doc__ = '<STR_LIT:U+0020>' . join ( opt_logfile . __doc__ . split ( ) ) <EOL> cls . opt_logfile = opt_logfile <EOL> def opt_journald ( self ) : <EOL> """<STR_LIT>""" <EOL> if JournaldDestination is None : <EOL> raise usage . UsageError ( <EOL> "<STR_LIT>" + <EOL> _missing_journald_reason <EOL> ) <EOL> self [ '<STR_LIT>' ] = True <EOL> cls . opt_journald = opt_journald <EOL> def postOptions ( self ) : <EOL> if self [ '<STR_LIT>' ] : <EOL> destination = JournaldDestination ( ) <EOL> else : <EOL> if self [ '<STR_LIT>' ] is None : <EOL> logfile = self . _sys_module . stdout <EOL> else : <EOL> logfilepath = FilePath ( self [ '<STR_LIT>' ] ) <EOL> logfilepath_directory = logfilepath . parent ( ) <EOL> if not logfilepath_directory . exists ( ) : <EOL> logfilepath_directory . makedirs ( ) <EOL> logfile = LogFile . fromFullPath ( <EOL> logfilepath . path , <EOL> rotateLength = LOGFILE_LENGTH , <EOL> maxRotatedFiles = LOGFILE_COUNT , <EOL> ) <EOL> destination = FileDestination ( file = logfile ) <EOL> self . eliot_destination = destination <EOL> original_postOptions ( self ) <EOL> cls . postOptions = postOptions <EOL> return cls <EOL> class ICommandLineScript ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def main ( reactor , options ) : <EOL> """<STR_LIT>""" <EOL> def eliot_logging_service ( destination , reactor , capture_stdout ) : <EOL> service = MultiService ( ) <EOL> ThreadedWriter ( destination , reactor ) . setServiceParent ( service ) <EOL> EliotObserver ( capture_stdout = capture_stdout ) . setServiceParent ( service ) <EOL> return service <EOL> TWISTED_LOG_MESSAGE = MessageType ( "<STR_LIT>" , <EOL> fields ( error = bool , message = unicode ) , <EOL> u"<STR_LIT>" ) <EOL> def eliot_to_stdout ( message_formats , action_formats , stdout = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> def eliot_output ( message ) : <EOL> message_type = message . get ( '<STR_LIT>' ) <EOL> action_type = message . get ( '<STR_LIT>' ) <EOL> action_status = message . get ( '<STR_LIT>' ) <EOL> message_format = '<STR_LIT>' <EOL> if message_type is not None : <EOL> if message_type == '<STR_LIT>' and message . get ( '<STR_LIT:error>' ) : <EOL> message_format = '<STR_LIT>' <EOL> else : <EOL> message_format = message_formats . get ( message_type , '<STR_LIT>' ) <EOL> elif action_type is not None : <EOL> if action_status == '<STR_LIT>' : <EOL> message_format = action_formats . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> stdout . write ( message_format % message ) <EOL> stdout . flush ( ) <EOL> add_destination ( eliot_output ) <EOL> class EliotObserver ( Service ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , publisher = twisted_log , capture_stdout = True ) : <EOL> """<STR_LIT>""" <EOL> self . logger = Logger ( ) <EOL> self . publisher = publisher <EOL> self . capture_stdout = capture_stdout <EOL> def __call__ ( self , msg ) : <EOL> error = bool ( msg . get ( "<STR_LIT>" ) ) <EOL> message = unicode ( textFromEventDict ( msg ) , "<STR_LIT>" ) <EOL> TWISTED_LOG_MESSAGE ( error = error , message = message ) . write ( self . logger ) <EOL> def startService ( self ) : <EOL> """<STR_LIT>""" <EOL> startLoggingWithObserver ( self , setStdout = self . capture_stdout ) <EOL> class FlockerScriptRunner ( object ) : <EOL> """<STR_LIT>""" <EOL> _react = staticmethod ( task . react ) <EOL> def __init__ ( self , script , options , logging = True , <EOL> reactor = None , sys_module = None ) : <EOL> """<STR_LIT>""" <EOL> self . script = script <EOL> self . options = options <EOL> self . logging = logging <EOL> if reactor is None : <EOL> reactor = global_reactor <EOL> self . _reactor = reactor <EOL> if sys_module is None : <EOL> sys_module = sys <EOL> self . sys_module = sys_module <EOL> def _parse_options ( self , arguments ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . options . parseOptions ( arguments ) <EOL> except usage . UsageError as e : <EOL> self . sys_module . stderr . write ( unicode ( self . options ) . encode ( '<STR_LIT:utf-8>' ) ) <EOL> self . sys_module . stderr . write ( <EOL> b'<STR_LIT>' + e . message . encode ( '<STR_LIT:utf-8>' ) + b'<STR_LIT:\n>' ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> return self . options <EOL> def main ( self ) : <EOL> """<STR_LIT>""" <EOL> options = self . _parse_options ( self . sys_module . argv [ <NUM_LIT:1> : ] ) <EOL> if self . logging : <EOL> log_writer = eliot_logging_service ( <EOL> options . eliot_destination , self . _reactor , True <EOL> ) <EOL> else : <EOL> log_writer = Service ( ) <EOL> log_writer . startService ( ) <EOL> def run_and_log ( reactor ) : <EOL> d = maybeDeferred ( self . script . main , reactor , options ) <EOL> def got_error ( failure ) : <EOL> if failure . check ( UsageError ) : <EOL> err ( failure . value . args ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> elif not failure . check ( SystemExit ) : <EOL> err ( failure ) <EOL> return failure <EOL> d . addErrback ( got_error ) <EOL> return d <EOL> try : <EOL> self . _react ( run_and_log , [ ] , _reactor = self . _reactor ) <EOL> finally : <EOL> log_writer . stopService ( ) <EOL> def _chain_stop_result ( service , stop ) : <EOL> """<STR_LIT>""" <EOL> maybeDeferred ( service . stopService ) . chainDeferred ( stop ) <EOL> def main_for_service ( reactor , service ) : <EOL> """<STR_LIT>""" <EOL> service . startService ( ) <EOL> stop = Deferred ( ) <EOL> reactor . addSystemEventTrigger ( <EOL> "<STR_LIT>" , "<STR_LIT>" , _chain_stop_result , service , stop ) <EOL> return stop <EOL> def enable_profiling ( profile , signal , frame ) : <EOL> """<STR_LIT>""" <EOL> profile . enable ( ) <EOL> def disable_profiling ( profile , service , signal , frame ) : <EOL> """<STR_LIT>""" <EOL> current_time = strftime ( "<STR_LIT>" ) <EOL> path = FilePath ( '<STR_LIT>' ) <EOL> path = path . child ( '<STR_LIT>' . format ( <EOL> service = service , <EOL> current_time = current_time ) <EOL> ) <EOL> profile . dump_stats ( path . path ) </s>
