<s> __doc__ = """<STR_LIT>""" <EOL> __author__ = """<STR_LIT>""" <EOL> if __name__ != '<STR_LIT:__main__>' : raise ImportError ( '<STR_LIT>' ) <EOL> import os <EOL> import sys <EOL> DIRECTORY = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . abspath ( os . path . join ( DIRECTORY , '<STR_LIT:..>' , '<STR_LIT:..>' , '<STR_LIT:..>' ) ) ) <EOL> unforkedPid = os . getpid ( ) <EOL> childProcessPid = <NUM_LIT:0> <EOL> from twisted . internet import protocol , defer <EOL> from twisted . python . log import FileLogObserver , textFromEventDict <EOL> from twisted . python . util import untilConcludes <EOL> import signal <EOL> DEFAULT_REACTORS = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def set_reactor ( ) : <EOL> import platform <EOL> REACTORNAME = DEFAULT_REACTORS . get ( platform . system ( ) , '<STR_LIT>' ) <EOL> if REACTORNAME == '<STR_LIT>' : <EOL> from twisted . internet import kqreactor <EOL> kqreactor . install ( ) <EOL> elif REACTORNAME == '<STR_LIT>' : <EOL> from twisted . internet import epollreactor <EOL> epollreactor . install ( ) <EOL> elif REACTORNAME == '<STR_LIT>' : <EOL> from twisted . internet import pollreactor <EOL> pollreactor . install ( ) <EOL> else : <EOL> from twisted . internet import selectreactor <EOL> selectreactor . install ( ) <EOL> from twisted . internet import reactor <EOL> set_reactor = lambda : reactor <EOL> return reactor <EOL> class ManagedLogger ( FileLogObserver ) : <EOL> """<STR_LIT>""" <EOL> timeFormat = "<STR_LIT>" <EOL> def emit ( self , eventDict ) : <EOL> """<STR_LIT>""" <EOL> text = textFromEventDict ( eventDict ) <EOL> if text is None : return <EOL> untilConcludes ( self . write , text ) <EOL> untilConcludes ( self . flush ) <EOL> class DaemonProtocol ( protocol . ProcessProtocol ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , label , r , deferred , ** kwargs ) : <EOL> self . deferred = deferred <EOL> self . reactor = r <EOL> out = { <EOL> '<STR_LIT:type>' : '<STR_LIT>' % ( name , label ) <EOL> } <EOL> err = { <EOL> '<STR_LIT:type>' : '<STR_LIT>' % ( name , label ) <EOL> } <EOL> self . name = name <EOL> self . label = label <EOL> import droned . logging <EOL> self . log_stdout = droned . logging . logWithContext ( ** out ) <EOL> self . log_stderr = droned . logging . logWithContext ( ** err ) <EOL> def inConnectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def errReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . log_stderr ( str ( data ) ) <EOL> def outReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . log_stdout ( str ( data ) ) <EOL> def outConnectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def errConnectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def connectionMade ( self ) : <EOL> """<STR_LIT>""" <EOL> global closestdin <EOL> if closestdin : <EOL> self . transport . closeStdin ( ) <EOL> global childProcessPid <EOL> global unforkedPid <EOL> x = unforkedPid <EOL> unforkedPid = <NUM_LIT:0> <EOL> if x : self . reactor . callLater ( <NUM_LIT> , os . kill , x , signal . SIGTERM ) <EOL> childProcessPid = self . transport . pid <EOL> sys . stdout . write ( '<STR_LIT>' % ( self . name , self . label , childProcessPid ) ) <EOL> def processExited ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> sys . stdout . write ( '<STR_LIT>' % ( self . name , ) ) <EOL> if not self . deferred . called : <EOL> self . deferred . errback ( reason ) <EOL> global unforkedPid <EOL> global childProcessPid <EOL> childProcessPid = <NUM_LIT:0> <EOL> if unforkedPid : os . kill ( unforkedPid , signal . SIGTERM ) <EOL> processEnded = processExited <EOL> class DaemonWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> SIGNALS = dict ( ( k , v ) for v , k in signal . __dict__ . iteritems ( ) if v . startswith ( '<STR_LIT>' ) and not v . startswith ( '<STR_LIT>' ) ) <EOL> def __init__ ( self , r , name , label , cmd , args , env ) : <EOL> self . reactor = r <EOL> self . name = name <EOL> self . label = label <EOL> self . fqcmd = cmd <EOL> self . args = args <EOL> self . env = env <EOL> self . exitCode = <NUM_LIT:0> <EOL> self . deferred = defer . succeed ( None ) <EOL> import droned . logging <EOL> self . log = droned . logging . logWithContext ( type = '<STR_LIT>' ) <EOL> def routeSignal ( self , signum , frame ) : <EOL> """<STR_LIT>""" <EOL> if signum == signal . SIGTERM : <EOL> signal . signal ( signal . SIGTERM , signal . SIG_IGN ) <EOL> self . reactor . callLater ( <NUM_LIT> , self . reactor . stop ) <EOL> global childProcessPid <EOL> if childProcessPid : <EOL> self . log ( '<STR_LIT>' % ( self . SIGNALS [ signum ] , childProcessPid ) ) <EOL> try : os . kill ( childProcessPid , signum ) <EOL> except : <EOL> droned . logging . err ( '<STR_LIT>' % ( self . SIGNALS [ signum ] , childProcessPid ) ) <EOL> def processResult ( self , result ) : <EOL> """<STR_LIT>""" <EOL> self . reactor . callLater ( <NUM_LIT> , self . reactor . stop ) <EOL> return result <EOL> def running ( self ) : <EOL> """<STR_LIT>""" <EOL> global masksignals <EOL> if masksignals : <EOL> for signum , signame in self . SIGNALS . items ( ) : <EOL> if signame in ( '<STR_LIT>' , ) : continue <EOL> try : signal . signal ( signum , self . routeSignal ) <EOL> except RuntimeError : pass <EOL> from droned . clients import command <EOL> self . log ( '<STR_LIT>' % ( self . name , self . label ) ) <EOL> pargs = ( self . name , self . label , self . reactor ) <EOL> pkwargs = { } <EOL> global usetty <EOL> global path <EOL> self . deferred = command ( self . fqcmd , self . args , self . env , <EOL> path , usetty , { } , DaemonProtocol , <EOL> * pargs , ** pkwargs <EOL> ) <EOL> self . deferred . addBoth ( self . processResult ) <EOL> return self . deferred <EOL> env = os . environ . copy ( ) <EOL> args = tuple ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> logdir = env . pop ( '<STR_LIT>' , os . path . join ( os . path . sep , '<STR_LIT>' ) ) <EOL> masksignals = bool ( env . pop ( '<STR_LIT>' , True ) ) <EOL> closestdin = bool ( env . pop ( '<STR_LIT>' , True ) ) <EOL> name = env . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> label = env . pop ( '<STR_LIT>' , '<STR_LIT:0>' ) <EOL> usetty = bool ( env . pop ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> path = env . pop ( '<STR_LIT>' , os . path . sep ) <EOL> if name not in logdir : <EOL> t = os . path . join ( logdir , name , label ) <EOL> try : <EOL> if not os . path . exists ( t ) : <EOL> os . makedirs ( t , mode = <NUM_LIT:0> <NUM_LIT> ) <EOL> logdir = t <EOL> except : pass <EOL> if args and os . path . exists ( args [ <NUM_LIT:0> ] ) : <EOL> try : os . setsid ( ) <EOL> except : pass <EOL> if os . fork ( ) == <NUM_LIT:0> : <EOL> os . chdir ( os . path . sep ) <EOL> os . umask ( <NUM_LIT:0> ) <EOL> sys . stdout . write ( '<STR_LIT>' % ( os . getpid ( ) , ) ) <EOL> sys . stderr . flush ( ) <EOL> sys . stdout . flush ( ) <EOL> import droned . logging <EOL> sys . stdout = droned . logging . StdioKabob ( <NUM_LIT:0> ) <EOL> sys . stderr = droned . logging . StdioKabob ( <NUM_LIT:1> ) <EOL> maxfd = <NUM_LIT> <EOL> try : <EOL> import resource <EOL> maxfd = resource . getrlimit ( resource . RLIMIT_NOFILE ) [ <NUM_LIT:1> ] <EOL> if ( maxfd == resource . RLIM_INFINITY ) : <EOL> maxfd = <NUM_LIT> <EOL> except : pass <EOL> for fd in range ( <NUM_LIT:0> , maxfd ) : <EOL> try : os . close ( fd ) <EOL> except OSError : pass <EOL> os . open ( <EOL> hasattr ( os , "<STR_LIT>" ) and os . devnull or "<STR_LIT>" , <EOL> os . O_RDWR <EOL> ) <EOL> os . dup2 ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> os . dup2 ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> loggers = [ <EOL> '<STR_LIT>' % ( name , label ) , <EOL> '<STR_LIT>' % ( name , label ) , <EOL> ] <EOL> droned . logging . logToDir ( directory = logdir ) <EOL> reactor = set_reactor ( ) <EOL> droned . logging . logToDir ( <EOL> directory = logdir , <EOL> LOG_TYPE = tuple ( loggers ) , <EOL> OBSERVER = ManagedLogger <EOL> ) <EOL> dmx = DaemonWrapper ( reactor , name , label , args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> : ] , env ) <EOL> def killGroup ( ) : <EOL> """<STR_LIT>""" <EOL> dmx . log ( '<STR_LIT>' ) <EOL> signal . signal ( signal . SIGTERM , signal . SIG_IGN ) <EOL> os . kill ( - os . getpgid ( os . getpid ( ) ) , signal . SIGTERM ) <EOL> reactor . addSystemEventTrigger ( '<STR_LIT>' , '<STR_LIT>' , killGroup ) <EOL> reactor . callWhenRunning ( dmx . running ) <EOL> reactor . run ( ) <EOL> sys . exit ( dmx . exitCode ) <EOL> else : <EOL> reactor = set_reactor ( ) <EOL> reactor . callLater ( <NUM_LIT> , sys . exit , <NUM_LIT:1> ) <EOL> reactor . run ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> sys . exit ( <NUM_LIT:255> ) </s>
