<s> '''<STR_LIT>''' <EOL> import logging <EOL> import logging . handlers <EOL> import optparse <EOL> import os <EOL> import sys <EOL> import atexit <EOL> import signal <EOL> import resource <EOL> import subprocess <EOL> from octopus . worker import make_worker , settings <EOL> from octopus . worker import config <EOL> def daemonize ( username = "<STR_LIT>" ) : <EOL> soft , hard = resource . getrlimit ( resource . RLIMIT_NOFILE ) <EOL> if settings . LIMIT_OPEN_FILES <= hard : <EOL> try : <EOL> resource . setrlimit ( resource . RLIMIT_NOFILE , ( settings . LIMIT_OPEN_FILES , hard ) ) <EOL> except Exception , e : <EOL> logging . getLogger ( '<STR_LIT>' ) . error ( "<STR_LIT>" % ( soft , hard , settings . LIMIT_OPEN_FILES , hard , e ) ) <EOL> raise e <EOL> else : <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( "<STR_LIT>" % ( soft , hard ) ) <EOL> if os . fork ( ) != <NUM_LIT:0> : <EOL> os . _exit ( <NUM_LIT:0> ) <EOL> os . setsid ( ) <EOL> if os . name != '<STR_LIT>' : <EOL> import pwd <EOL> if username : <EOL> uid = pwd . getpwnam ( username ) [ <NUM_LIT:2> ] <EOL> os . setuid ( uid ) <EOL> if os . fork ( ) != <NUM_LIT:0> : <EOL> os . _exit ( <NUM_LIT:0> ) <EOL> pidfile = file ( settings . PIDFILE , "<STR_LIT:w>" ) <EOL> pidfile . write ( "<STR_LIT>" % os . getpid ( ) ) <EOL> pidfile . close ( ) <EOL> pidfile = os . path . abspath ( settings . PIDFILE ) <EOL> def delpidfile ( ) : <EOL> os . remove ( pidfile ) <EOL> atexit . register ( delpidfile ) <EOL> def delpidfileonSIGTERM ( a , b ) : <EOL> sys . exit ( ) <EOL> signal . signal ( signal . SIGTERM , delpidfileonSIGTERM ) <EOL> os . chdir ( "<STR_LIT:/>" ) <EOL> f = os . open ( os . devnull , os . O_RDONLY ) <EOL> os . dup2 ( f , sys . stdin . fileno ( ) ) <EOL> os . close ( f ) <EOL> f = os . open ( os . devnull , os . O_WRONLY ) <EOL> os . dup2 ( f , sys . stdout . fileno ( ) ) <EOL> os . close ( f ) <EOL> f = os . open ( os . devnull , os . O_WRONLY ) <EOL> os . dup2 ( f , sys . stderr . fileno ( ) ) <EOL> os . close ( f ) <EOL> def process_args ( ) : <EOL> parser = optparse . OptionParser ( ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , metavar = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:int>" , dest = "<STR_LIT>" , metavar = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , dest = "<STR_LIT>" , metavar = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , default = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> options , args = parser . parse_args ( ) <EOL> if args : <EOL> settings . loadSettingsFile ( args [ <NUM_LIT:0> ] ) <EOL> for setting in dir ( settings ) : <EOL> if hasattr ( options , setting ) and getattr ( options , setting ) is not None : <EOL> setattr ( settings , setting , getattr ( options , setting ) ) <EOL> parser . destroy ( ) <EOL> return options <EOL> def setup_logging ( options ) : <EOL> if not os . path . exists ( settings . LOGDIR ) : <EOL> os . makedirs ( settings . LOGDIR , <NUM_LIT:0> <NUM_LIT> ) <EOL> logFileName = "<STR_LIT>" % settings . PORT <EOL> logFile = os . path . join ( settings . LOGDIR , logFileName ) <EOL> fileHandler = logging . handlers . RotatingFileHandler ( logFile , '<STR_LIT:w>' , config . LOG_SIZE , config . LOG_BACKUPS , "<STR_LIT>" ) <EOL> fileHandler . setFormatter ( logging . Formatter ( "<STR_LIT>" ) ) <EOL> logger = logging . getLogger ( ) <EOL> logger . addHandler ( fileHandler ) <EOL> if options . CONSOLE : <EOL> consoleHandler = logging . StreamHandler ( ) <EOL> consoleHandler . setFormatter ( logging . Formatter ( "<STR_LIT>" ) ) <EOL> consoleHandler . setLevel ( logging . DEBUG ) <EOL> logger . setLevel ( logging . DEBUG ) <EOL> logger . addHandler ( consoleHandler ) <EOL> logLevel = logging . DEBUG if options . DEBUG else logging . INFO <EOL> logging . getLogger ( ) . setLevel ( logLevel ) <EOL> def main ( ) : <EOL> options = process_args ( ) <EOL> setup_logging ( options ) <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( "<STR_LIT>" , settings . ADDRESS , settings . PORT ) <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( "<STR_LIT>" ) <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( "<STR_LIT>" % settings . VERSION ) <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( sys . argv ) ) <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( "<STR_LIT>" % options . DAEMONIZE ) <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( "<STR_LIT>" % options . CONSOLE ) <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( "<STR_LIT>" % ( settings . DISPATCHER_ADDRESS , settings . DISPATCHER_PORT ) ) <EOL> workerApplication = make_worker ( ) <EOL> if options . DAEMONIZE : <EOL> daemonize ( settings . RUN_AS ) <EOL> workerApplication . mainLoop ( ) <EOL> logging . getLogger ( '<STR_LIT>' ) . warning ( "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
