<s> """<STR_LIT>""" <EOL> import cmd <EOL> import sys <EOL> import getpass <EOL> import xmlrpclib <EOL> import socket <EOL> import errno <EOL> import urlparse <EOL> import threading <EOL> from supervisor . medusa import asyncore_25 as asyncore <EOL> from supervisor . options import ClientOptions <EOL> from supervisor . options import split_namespec <EOL> from supervisor import xmlrpc <EOL> from supervisor import states <EOL> class fgthread ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , program , ctl ) : <EOL> threading . Thread . __init__ ( self ) <EOL> import http_client <EOL> self . killed = False <EOL> self . program = program <EOL> self . ctl = ctl <EOL> self . listener = http_client . Listener ( ) <EOL> self . output_handler = http_client . HTTPHandler ( self . listener , <EOL> self . ctl . options . username , <EOL> self . ctl . options . password ) <EOL> self . error_handler = http_client . HTTPHandler ( self . listener , <EOL> self . ctl . options . username , <EOL> self . ctl . options . password ) <EOL> def start ( self ) : <EOL> self . __run_backup = self . run <EOL> self . run = self . __run <EOL> threading . Thread . start ( self ) <EOL> def run ( self ) : <EOL> self . output_handler . get ( self . ctl . options . serverurl , <EOL> '<STR_LIT>' % self . program ) <EOL> self . error_handler . get ( self . ctl . options . serverurl , <EOL> '<STR_LIT>' % self . program ) <EOL> asyncore . loop ( ) <EOL> def __run ( self ) : <EOL> sys . settrace ( self . globaltrace ) <EOL> self . __run_backup ( ) <EOL> self . run = self . __run_backup <EOL> def globaltrace ( self , frame , why , arg ) : <EOL> if why == '<STR_LIT>' : <EOL> return self . localtrace <EOL> else : <EOL> return None <EOL> def localtrace ( self , frame , why , arg ) : <EOL> if self . killed : <EOL> if why == '<STR_LIT>' : <EOL> raise SystemExit ( ) <EOL> return self . localtrace <EOL> def kill ( self ) : <EOL> self . output_handler . close ( ) <EOL> self . error_handler . close ( ) <EOL> self . killed = True <EOL> class Controller ( cmd . Cmd ) : <EOL> def __init__ ( self , options , completekey = '<STR_LIT>' , stdin = None , <EOL> stdout = None ) : <EOL> self . options = options <EOL> self . prompt = self . options . prompt + '<STR_LIT>' <EOL> self . options . plugins = [ ] <EOL> self . vocab = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:start>' , '<STR_LIT>' , '<STR_LIT:version>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> cmd . Cmd . __init__ ( self , completekey , stdin , stdout ) <EOL> for name , factory , kwargs in self . options . plugin_factories : <EOL> plugin = factory ( self , ** kwargs ) <EOL> self . options . plugins . append ( plugin ) <EOL> plugin . name = name <EOL> def emptyline ( self ) : <EOL> return <EOL> def onecmd ( self , line ) : <EOL> """<STR_LIT>""" <EOL> origline = line <EOL> lines = line . split ( '<STR_LIT:;>' ) <EOL> line = lines . pop ( <NUM_LIT:0> ) <EOL> self . cmdqueue . extend ( lines ) <EOL> cmd , arg , line = self . parseline ( line ) <EOL> if not line : <EOL> return self . emptyline ( ) <EOL> if cmd is None : <EOL> return self . default ( line ) <EOL> self . lastcmd = line <EOL> if cmd == '<STR_LIT>' : <EOL> return self . default ( line ) <EOL> else : <EOL> do_func = self . _get_do_func ( cmd ) <EOL> if do_func is None : <EOL> return self . default ( line ) <EOL> try : <EOL> try : <EOL> return do_func ( arg ) <EOL> except xmlrpclib . ProtocolError , e : <EOL> if e . errcode == <NUM_LIT> : <EOL> if self . options . interactive : <EOL> self . output ( '<STR_LIT>' ) <EOL> username = raw_input ( '<STR_LIT>' ) <EOL> password = getpass . getpass ( prompt = '<STR_LIT>' ) <EOL> self . output ( '<STR_LIT>' ) <EOL> self . options . username = username <EOL> self . options . password = password <EOL> return self . onecmd ( origline ) <EOL> else : <EOL> self . options . usage ( '<STR_LIT>' ) <EOL> else : <EOL> raise <EOL> do_func ( arg ) <EOL> except SystemExit : <EOL> raise <EOL> except Exception , e : <EOL> ( file , fun , line ) , t , v , tbinfo = asyncore . compact_traceback ( ) <EOL> error = '<STR_LIT>' % ( t , v , file , line ) <EOL> self . output ( error ) <EOL> if not self . options . interactive : <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def _get_do_func ( self , cmd ) : <EOL> func_name = '<STR_LIT>' + cmd <EOL> func = getattr ( self , func_name , None ) <EOL> if not func : <EOL> for plugin in self . options . plugins : <EOL> func = getattr ( plugin , func_name , None ) <EOL> if func is not None : <EOL> break <EOL> return func <EOL> def output ( self , stuff ) : <EOL> if stuff is not None : <EOL> if isinstance ( stuff , unicode ) : <EOL> stuff = stuff . encode ( '<STR_LIT:utf-8>' ) <EOL> self . stdout . write ( stuff + '<STR_LIT:\n>' ) <EOL> def get_supervisor ( self ) : <EOL> return self . get_server_proxy ( '<STR_LIT>' ) <EOL> def get_server_proxy ( self , namespace = None ) : <EOL> proxy = self . options . getServerProxy ( ) <EOL> if namespace is None : <EOL> return proxy <EOL> else : <EOL> return getattr ( proxy , namespace ) <EOL> def upcheck ( self ) : <EOL> try : <EOL> supervisor = self . get_supervisor ( ) <EOL> api = supervisor . getVersion ( ) <EOL> from supervisor import rpcinterface <EOL> if api != rpcinterface . API_VERSION : <EOL> self . output ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( rpcinterface . API_VERSION , api ) ) <EOL> return False <EOL> except xmlrpclib . Fault , e : <EOL> if e . faultCode == xmlrpc . Faults . UNKNOWN_METHOD : <EOL> self . output ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return False <EOL> raise <EOL> except socket . error , why : <EOL> if why [ <NUM_LIT:0> ] == errno . ECONNREFUSED : <EOL> self . output ( '<STR_LIT>' % self . options . serverurl ) <EOL> return False <EOL> elif why [ <NUM_LIT:0> ] == errno . ENOENT : <EOL> self . output ( '<STR_LIT>' % self . options . serverurl ) <EOL> return False <EOL> raise <EOL> return True <EOL> def completionmatches ( self , text , line , flag = <NUM_LIT:0> ) : <EOL> groups = [ ] <EOL> programs = [ ] <EOL> groupwiseprograms = { } <EOL> info = self . get_supervisor ( ) . getAllProcessInfo ( ) <EOL> for i in info : <EOL> programs . append ( i [ '<STR_LIT:name>' ] ) <EOL> if i [ '<STR_LIT>' ] not in groups : <EOL> groups . append ( i [ '<STR_LIT>' ] ) <EOL> groupwiseprograms [ i [ '<STR_LIT>' ] ] = [ ] <EOL> groupwiseprograms [ i [ '<STR_LIT>' ] ] . append ( i [ '<STR_LIT:name>' ] ) <EOL> total = [ ] <EOL> for i in groups : <EOL> if i in programs : <EOL> total . append ( i + '<STR_LIT:U+0020>' ) <EOL> else : <EOL> for n in groupwiseprograms [ i ] : <EOL> total . append ( i + '<STR_LIT::>' + n + '<STR_LIT:U+0020>' ) <EOL> if flag : <EOL> return [ i + '<STR_LIT:U+0020>' for i in groups if i . startswith ( text ) ] <EOL> if len ( line . split ( ) ) == <NUM_LIT:1> : <EOL> return total <EOL> else : <EOL> current = line . split ( ) [ - <NUM_LIT:1> ] <EOL> if line . endswith ( '<STR_LIT:U+0020>' ) and len ( line . split ( ) ) > <NUM_LIT:1> : <EOL> results = [ i for i in total if i . startswith ( text ) ] <EOL> return results <EOL> if '<STR_LIT::>' in current : <EOL> g = current . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> results = [ i + '<STR_LIT:U+0020>' for i in groupwiseprograms [ g ] <EOL> if i . startswith ( text ) ] <EOL> return results <EOL> results = [ i for i in total if i . startswith ( text ) ] <EOL> return results <EOL> def complete ( self , text , state ) : <EOL> try : <EOL> import readline <EOL> except ImportError : <EOL> return None <EOL> line = readline . get_line_buffer ( ) <EOL> if line == '<STR_LIT>' : <EOL> results = [ i + '<STR_LIT:U+0020>' for i in self . vocab if i . startswith ( text ) ] + [ None ] <EOL> return results [ state ] <EOL> else : <EOL> exp = line . split ( ) [ <NUM_LIT:0> ] <EOL> if exp in [ '<STR_LIT:start>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if not line . endswith ( '<STR_LIT:U+0020>' ) and len ( line . split ( ) ) == <NUM_LIT:1> : <EOL> return [ text + '<STR_LIT:U+0020>' , None ] [ state ] <EOL> if exp == '<STR_LIT>' : <EOL> if line . endswith ( '<STR_LIT:U+0020>' ) and len ( line . split ( ) ) > <NUM_LIT:1> : <EOL> return None <EOL> results = self . completionmatches ( text , line ) + [ None ] <EOL> return results [ state ] <EOL> elif exp in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:version>' , '<STR_LIT>' ] : <EOL> return None <EOL> elif exp == '<STR_LIT>' : <EOL> if line . endswith ( '<STR_LIT:U+0020>' ) and len ( line . split ( ) ) > <NUM_LIT:1> : <EOL> return None <EOL> results = [ i + '<STR_LIT:U+0020>' for i in self . vocab if i . startswith ( text ) ] + [ None ] <EOL> return results [ state ] <EOL> elif exp in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> results = self . completionmatches ( text , line , flag = <NUM_LIT:1> ) + [ None ] <EOL> return results [ state ] <EOL> else : <EOL> results = [ i + '<STR_LIT:U+0020>' for i in self . vocab if i . startswith ( text ) ] + [ None ] <EOL> return results [ state ] <EOL> def do_help ( self , arg ) : <EOL> for plugin in self . options . plugins : <EOL> plugin . do_help ( arg ) <EOL> def help_help ( self ) : <EOL> self . output ( "<STR_LIT>" ) <EOL> self . output ( "<STR_LIT>" ) <EOL> def do_EOF ( self , arg ) : <EOL> self . output ( '<STR_LIT>' ) <EOL> return <NUM_LIT:1> <EOL> def help_EOF ( self ) : <EOL> self . output ( "<STR_LIT>" ) <EOL> def get_names ( inst ) : <EOL> names = [ ] <EOL> classes = [ inst . __class__ ] <EOL> while classes : <EOL> aclass = classes . pop ( <NUM_LIT:0> ) <EOL> if aclass . __bases__ : <EOL> classes = classes + list ( aclass . __bases__ ) <EOL> names = names + dir ( aclass ) <EOL> return names <EOL> class ControllerPluginBase : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self , controller ) : <EOL> self . ctl = controller <EOL> def _doc_header ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> doc_header = property ( _doc_header ) <EOL> def do_help ( self , arg ) : <EOL> if arg : <EOL> try : <EOL> func = getattr ( self , '<STR_LIT>' + arg ) <EOL> except AttributeError : <EOL> try : <EOL> doc = getattr ( self , '<STR_LIT>' + arg ) . __doc__ <EOL> if doc : <EOL> self . ctl . stdout . write ( "<STR_LIT>" % str ( doc ) ) <EOL> return <EOL> except AttributeError : <EOL> pass <EOL> self . ctl . stdout . write ( "<STR_LIT>" % str ( self . ctl . nohelp % ( arg , ) ) ) <EOL> return <EOL> func ( ) <EOL> else : <EOL> names = get_names ( self ) <EOL> cmds_doc = [ ] <EOL> cmds_undoc = [ ] <EOL> help = { } <EOL> for name in names : <EOL> if name [ : <NUM_LIT:5> ] == '<STR_LIT>' : <EOL> help [ name [ <NUM_LIT:5> : ] ] = <NUM_LIT:1> <EOL> names . sort ( ) <EOL> prevname = '<STR_LIT>' <EOL> for name in names : <EOL> if name [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> if name == prevname : <EOL> continue <EOL> prevname = name <EOL> cmd = name [ <NUM_LIT:3> : ] <EOL> if cmd in help : <EOL> cmds_doc . append ( cmd ) <EOL> del help [ cmd ] <EOL> elif getattr ( self , name ) . __doc__ : <EOL> cmds_doc . append ( cmd ) <EOL> else : <EOL> cmds_undoc . append ( cmd ) <EOL> self . ctl . stdout . write ( "<STR_LIT:\n>" ) <EOL> self . ctl . print_topics ( self . doc_header , cmds_doc , <NUM_LIT:15> , <NUM_LIT> ) <EOL> class DefaultControllerPlugin ( ControllerPluginBase ) : <EOL> name = '<STR_LIT:default>' <EOL> listener = None <EOL> def _tailf ( self , path ) : <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> username = self . ctl . options . username <EOL> password = self . ctl . options . password <EOL> try : <EOL> import http_client <EOL> if self . listener is None : <EOL> listener = http_client . Listener ( ) <EOL> else : <EOL> listener = self . listener <EOL> handler = http_client . HTTPHandler ( listener , username , password ) <EOL> handler . get ( self . ctl . options . serverurl , path ) <EOL> asyncore . loop ( ) <EOL> except KeyboardInterrupt : <EOL> handler . close ( ) <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> return <EOL> def do_tail ( self , arg ) : <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> args = arg . strip ( ) . split ( ) <EOL> if len ( args ) < <NUM_LIT:1> : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> self . help_tail ( ) <EOL> return <EOL> elif len ( args ) > <NUM_LIT:3> : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> self . help_tail ( ) <EOL> return <EOL> modifier = None <EOL> if args [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:->' ) : <EOL> modifier = args . pop ( <NUM_LIT:0> ) <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> name = args [ - <NUM_LIT:1> ] <EOL> channel = '<STR_LIT>' <EOL> else : <EOL> if args : <EOL> name = args [ <NUM_LIT:0> ] <EOL> channel = args [ - <NUM_LIT:1> ] . lower ( ) <EOL> if channel not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . ctl . output ( '<STR_LIT>' % channel ) <EOL> return <EOL> else : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> return <EOL> bytes = <NUM_LIT> <EOL> if modifier is not None : <EOL> what = modifier [ <NUM_LIT:1> : ] <EOL> if what == '<STR_LIT:f>' : <EOL> bytes = None <EOL> else : <EOL> try : <EOL> bytes = int ( what ) <EOL> except : <EOL> self . ctl . output ( '<STR_LIT>' % modifier ) <EOL> return <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> if bytes is None : <EOL> return self . _tailf ( '<STR_LIT>' % ( name , channel ) ) <EOL> else : <EOL> try : <EOL> if channel is '<STR_LIT>' : <EOL> output = supervisor . readProcessStdoutLog ( name , <EOL> - bytes , <NUM_LIT:0> ) <EOL> else : <EOL> output = supervisor . readProcessStderrLog ( name , <EOL> - bytes , <NUM_LIT:0> ) <EOL> except xmlrpclib . Fault , e : <EOL> template = '<STR_LIT>' <EOL> if e . faultCode == xmlrpc . Faults . NO_FILE : <EOL> self . ctl . output ( template % ( name , '<STR_LIT>' ) ) <EOL> elif e . faultCode == xmlrpc . Faults . FAILED : <EOL> self . ctl . output ( template % ( name , <EOL> '<STR_LIT>' ) ) <EOL> elif e . faultCode == xmlrpc . Faults . BAD_NAME : <EOL> self . ctl . output ( template % ( name , <EOL> '<STR_LIT>' ) ) <EOL> else : <EOL> self . ctl . output ( output ) <EOL> def help_tail ( self ) : <EOL> self . ctl . output ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def do_maintail ( self , arg ) : <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> args = arg . strip ( ) . split ( ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> self . help_maintail ( ) <EOL> return <EOL> elif len ( args ) == <NUM_LIT:1> : <EOL> if args [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:->' ) : <EOL> what = args [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] <EOL> if what == '<STR_LIT:f>' : <EOL> path = '<STR_LIT>' <EOL> return self . _tailf ( path ) <EOL> try : <EOL> what = int ( what ) <EOL> except : <EOL> self . ctl . output ( '<STR_LIT>' % args [ <NUM_LIT:0> ] ) <EOL> return <EOL> else : <EOL> bytes = what <EOL> else : <EOL> self . ctl . output ( '<STR_LIT>' % args [ <NUM_LIT:0> ] ) <EOL> return <EOL> else : <EOL> bytes = <NUM_LIT> <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> try : <EOL> output = supervisor . readLog ( - bytes , <NUM_LIT:0> ) <EOL> except xmlrpclib . Fault , e : <EOL> template = '<STR_LIT>' <EOL> if e . faultCode == xmlrpc . Faults . NO_FILE : <EOL> self . ctl . output ( template % ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> elif e . faultCode == xmlrpc . Faults . FAILED : <EOL> self . ctl . output ( template % ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> else : <EOL> self . ctl . output ( output ) <EOL> def help_maintail ( self ) : <EOL> self . ctl . output ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def do_quit ( self , arg ) : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def help_quit ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> do_exit = do_quit <EOL> def help_exit ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def _procrepr ( self , info ) : <EOL> template = '<STR_LIT>' <EOL> if info [ '<STR_LIT:name>' ] == info [ '<STR_LIT>' ] : <EOL> name = info [ '<STR_LIT:name>' ] <EOL> else : <EOL> name = '<STR_LIT>' % ( info [ '<STR_LIT>' ] , info [ '<STR_LIT:name>' ] ) <EOL> return template % { '<STR_LIT:name>' : name , '<STR_LIT:state>' : info [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : info [ '<STR_LIT:description>' ] } <EOL> def do_status ( self , arg ) : <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> names = arg . strip ( ) . split ( ) <EOL> if names : <EOL> for name in names : <EOL> try : <EOL> info = supervisor . getProcessInfo ( name ) <EOL> except xmlrpclib . Fault , e : <EOL> if e . faultCode == xmlrpc . Faults . BAD_NAME : <EOL> self . ctl . output ( '<STR_LIT>' % name ) <EOL> else : <EOL> raise <EOL> continue <EOL> self . ctl . output ( self . _procrepr ( info ) ) <EOL> else : <EOL> for info in supervisor . getAllProcessInfo ( ) : <EOL> self . ctl . output ( self . _procrepr ( info ) ) <EOL> def help_status ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( <EOL> "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def do_pid ( self , arg ) : <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> names = arg . strip ( ) . split ( ) <EOL> if not names : <EOL> pid = supervisor . getPID ( ) <EOL> self . ctl . output ( str ( pid ) ) <EOL> elif '<STR_LIT:all>' in names : <EOL> for info in supervisor . getAllProcessInfo ( ) : <EOL> self . ctl . output ( str ( info [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> for name in names : <EOL> try : <EOL> info = supervisor . getProcessInfo ( name ) <EOL> except xmlrpclib . Fault , e : <EOL> if e . faultCode == xmlrpc . Faults . BAD_NAME : <EOL> self . ctl . output ( '<STR_LIT>' % name ) <EOL> else : <EOL> raise <EOL> continue <EOL> self . ctl . output ( str ( info [ '<STR_LIT>' ] ) ) <EOL> def help_pid ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _startresult ( self , result ) : <EOL> name = result [ '<STR_LIT:name>' ] <EOL> code = result [ '<STR_LIT:status>' ] <EOL> template = '<STR_LIT>' <EOL> if code == xmlrpc . Faults . BAD_NAME : <EOL> return template % ( name , '<STR_LIT>' ) <EOL> elif code == xmlrpc . Faults . NO_FILE : <EOL> return template % ( name , '<STR_LIT>' ) <EOL> elif code == xmlrpc . Faults . NOT_EXECUTABLE : <EOL> return template % ( name , '<STR_LIT>' ) <EOL> elif code == xmlrpc . Faults . ALREADY_STARTED : <EOL> return template % ( name , '<STR_LIT>' ) <EOL> elif code == xmlrpc . Faults . SPAWN_ERROR : <EOL> return template % ( name , '<STR_LIT>' ) <EOL> elif code == xmlrpc . Faults . ABNORMAL_TERMINATION : <EOL> return template % ( name , '<STR_LIT>' ) <EOL> elif code == xmlrpc . Faults . SUCCESS : <EOL> return '<STR_LIT>' % name <EOL> raise ValueError ( '<STR_LIT>' % ( code , name ) ) <EOL> def do_start ( self , arg ) : <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> names = arg . strip ( ) . split ( ) <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> if not names : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . help_start ( ) <EOL> return <EOL> if '<STR_LIT:all>' in names : <EOL> results = supervisor . startAllProcesses ( ) <EOL> for result in results : <EOL> result = self . _startresult ( result ) <EOL> self . ctl . output ( result ) <EOL> else : <EOL> for name in names : <EOL> group_name , process_name = split_namespec ( name ) <EOL> if process_name is None : <EOL> results = supervisor . startProcessGroup ( group_name ) <EOL> for result in results : <EOL> result = self . _startresult ( result ) <EOL> self . ctl . output ( result ) <EOL> else : <EOL> try : <EOL> result = supervisor . startProcess ( name ) <EOL> except xmlrpclib . Fault , e : <EOL> error = self . _startresult ( { '<STR_LIT:status>' : e . faultCode , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT:description>' : e . faultString } ) <EOL> self . ctl . output ( error ) <EOL> else : <EOL> self . ctl . output ( '<STR_LIT>' % name ) <EOL> def help_start ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( <EOL> "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def _stopresult ( self , result ) : <EOL> name = result [ '<STR_LIT:name>' ] <EOL> code = result [ '<STR_LIT:status>' ] <EOL> fault_string = result [ '<STR_LIT:description>' ] <EOL> template = '<STR_LIT>' <EOL> if code == xmlrpc . Faults . BAD_NAME : <EOL> return template % ( name , '<STR_LIT>' ) <EOL> elif code == xmlrpc . Faults . NOT_RUNNING : <EOL> return template % ( name , '<STR_LIT>' ) <EOL> elif code == xmlrpc . Faults . SUCCESS : <EOL> return '<STR_LIT>' % name <EOL> elif code == xmlrpc . Faults . FAILED : <EOL> return fault_string <EOL> raise ValueError ( '<STR_LIT>' % ( code , name ) ) <EOL> def do_stop ( self , arg ) : <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> names = arg . strip ( ) . split ( ) <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> if not names : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> self . help_stop ( ) <EOL> return <EOL> if '<STR_LIT:all>' in names : <EOL> results = supervisor . stopAllProcesses ( ) <EOL> for result in results : <EOL> result = self . _stopresult ( result ) <EOL> self . ctl . output ( result ) <EOL> else : <EOL> for name in names : <EOL> group_name , process_name = split_namespec ( name ) <EOL> if process_name is None : <EOL> results = supervisor . stopProcessGroup ( group_name ) <EOL> for result in results : <EOL> result = self . _stopresult ( result ) <EOL> self . ctl . output ( result ) <EOL> else : <EOL> try : <EOL> result = supervisor . stopProcess ( name ) <EOL> except xmlrpclib . Fault , e : <EOL> error = self . _stopresult ( { '<STR_LIT:status>' : e . faultCode , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT:description>' : e . faultString } ) <EOL> self . ctl . output ( error ) <EOL> else : <EOL> self . ctl . output ( '<STR_LIT>' % name ) <EOL> def help_stop ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def do_restart ( self , arg ) : <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> names = arg . strip ( ) . split ( ) <EOL> if not names : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> self . help_restart ( ) <EOL> return <EOL> self . do_stop ( arg ) <EOL> self . do_start ( arg ) <EOL> def help_restart ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def do_shutdown ( self , arg ) : <EOL> if self . ctl . options . interactive : <EOL> yesno = raw_input ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> really = yesno . lower ( ) . startswith ( '<STR_LIT:y>' ) <EOL> else : <EOL> really = <NUM_LIT:1> <EOL> if really : <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> try : <EOL> supervisor . shutdown ( ) <EOL> except xmlrpclib . Fault , e : <EOL> if e . faultCode == xmlrpc . Faults . SHUTDOWN_STATE : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> else : <EOL> raise <EOL> except socket . error , e : <EOL> if e [ <NUM_LIT:0> ] == errno . ECONNREFUSED : <EOL> msg = '<STR_LIT>' <EOL> self . ctl . output ( msg % self . ctl . options . serverurl ) <EOL> elif e [ <NUM_LIT:0> ] == errno . ENOENT : <EOL> msg = '<STR_LIT>' <EOL> self . ctl . output ( msg % self . ctl . options . serverurl ) <EOL> else : <EOL> raise <EOL> else : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> def help_shutdown ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def do_reload ( self , arg ) : <EOL> if self . ctl . options . interactive : <EOL> yesno = raw_input ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> really = yesno . lower ( ) . startswith ( '<STR_LIT:y>' ) <EOL> else : <EOL> really = <NUM_LIT:1> <EOL> if really : <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> try : <EOL> supervisor . restart ( ) <EOL> except xmlrpclib . Fault , e : <EOL> if e . faultCode == xmlrpc . Faults . SHUTDOWN_STATE : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> else : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> def help_reload ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def _formatChanges ( self , ( added , changed , dropped ) ) : <EOL> changedict = { } <EOL> for n , t in [ ( added , '<STR_LIT>' ) , <EOL> ( changed , '<STR_LIT>' ) , <EOL> ( dropped , '<STR_LIT>' ) ] : <EOL> changedict . update ( dict ( zip ( n , [ t ] * len ( n ) ) ) ) <EOL> if changedict : <EOL> names = changedict . keys ( ) <EOL> names . sort ( ) <EOL> for name in names : <EOL> self . ctl . output ( "<STR_LIT>" % ( name , changedict [ name ] ) ) <EOL> else : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def _formatConfigInfo ( self , configinfo ) : <EOL> if configinfo [ '<STR_LIT>' ] == configinfo [ '<STR_LIT:name>' ] : <EOL> name = configinfo [ '<STR_LIT>' ] <EOL> else : <EOL> name = "<STR_LIT>" % ( configinfo [ '<STR_LIT>' ] , configinfo [ '<STR_LIT:name>' ] ) <EOL> formatted = { '<STR_LIT:name>' : name } <EOL> if configinfo [ '<STR_LIT>' ] : <EOL> formatted [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> formatted [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if configinfo [ '<STR_LIT>' ] : <EOL> formatted [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> formatted [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> formatted [ '<STR_LIT>' ] = "<STR_LIT>" % ( configinfo [ '<STR_LIT>' ] , <EOL> configinfo [ '<STR_LIT>' ] ) <EOL> template = '<STR_LIT>' <EOL> return template % formatted <EOL> def do_avail ( self , arg ) : <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> try : <EOL> configinfo = supervisor . getAllConfigInfo ( ) <EOL> except xmlrpclib . Fault , e : <EOL> if e . faultCode == xmlrpc . Faults . SHUTDOWN_STATE : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> else : <EOL> for pinfo in configinfo : <EOL> self . ctl . output ( self . _formatConfigInfo ( pinfo ) ) <EOL> def help_avail ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def do_reread ( self , arg ) : <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> try : <EOL> result = supervisor . reloadConfig ( ) <EOL> except xmlrpclib . Fault , e : <EOL> if e . faultCode == xmlrpc . Faults . SHUTDOWN_STATE : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> elif e . faultCode == xmlrpc . Faults . CANT_REREAD : <EOL> self . ctl . output ( '<STR_LIT>' % e . faultString ) <EOL> else : <EOL> raise <EOL> else : <EOL> self . _formatChanges ( result [ <NUM_LIT:0> ] ) <EOL> def help_reread ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def do_add ( self , arg ) : <EOL> names = arg . strip ( ) . split ( ) <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> for name in names : <EOL> try : <EOL> supervisor . addProcessGroup ( name ) <EOL> except xmlrpclib . Fault , e : <EOL> if e . faultCode == xmlrpc . Faults . SHUTDOWN_STATE : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> elif e . faultCode == xmlrpc . Faults . ALREADY_ADDED : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> elif e . faultCode == xmlrpc . Faults . BAD_NAME : <EOL> self . ctl . output ( <EOL> "<STR_LIT>" % name ) <EOL> else : <EOL> raise <EOL> else : <EOL> self . ctl . output ( "<STR_LIT>" % name ) <EOL> def help_add ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def do_remove ( self , arg ) : <EOL> names = arg . strip ( ) . split ( ) <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> for name in names : <EOL> try : <EOL> result = supervisor . removeProcessGroup ( name ) <EOL> except xmlrpclib . Fault , e : <EOL> if e . faultCode == xmlrpc . Faults . STILL_RUNNING : <EOL> self . ctl . output ( '<STR_LIT>' <EOL> % name ) <EOL> elif e . faultCode == xmlrpc . Faults . BAD_NAME : <EOL> self . ctl . output ( <EOL> "<STR_LIT>" % name ) <EOL> else : <EOL> raise <EOL> else : <EOL> self . ctl . output ( "<STR_LIT>" % name ) <EOL> def help_remove ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def do_update ( self , arg ) : <EOL> def log ( name , message ) : <EOL> self . ctl . output ( "<STR_LIT>" % ( name , message ) ) <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> try : <EOL> result = supervisor . reloadConfig ( ) <EOL> except xmlrpclib . Fault , e : <EOL> if e . faultCode == xmlrpc . Faults . SHUTDOWN_STATE : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> return <EOL> else : <EOL> raise e <EOL> added , changed , removed = result [ <NUM_LIT:0> ] <EOL> for gname in removed : <EOL> results = supervisor . stopProcessGroup ( gname ) <EOL> log ( gname , "<STR_LIT>" ) <EOL> fails = [ res for res in results <EOL> if res [ '<STR_LIT:status>' ] == xmlrpc . Faults . FAILED ] <EOL> if fails : <EOL> log ( gname , "<STR_LIT>" ) <EOL> continue <EOL> supervisor . removeProcessGroup ( gname ) <EOL> log ( gname , "<STR_LIT>" ) <EOL> for gname in changed : <EOL> results = supervisor . stopProcessGroup ( gname ) <EOL> log ( gname , "<STR_LIT>" ) <EOL> supervisor . removeProcessGroup ( gname ) <EOL> supervisor . addProcessGroup ( gname ) <EOL> log ( gname , "<STR_LIT>" ) <EOL> for gname in added : <EOL> supervisor . addProcessGroup ( gname ) <EOL> log ( gname , "<STR_LIT>" ) <EOL> def help_update ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def _clearresult ( self , result ) : <EOL> name = result [ '<STR_LIT:name>' ] <EOL> code = result [ '<STR_LIT:status>' ] <EOL> template = '<STR_LIT>' <EOL> if code == xmlrpc . Faults . BAD_NAME : <EOL> return template % ( name , '<STR_LIT>' ) <EOL> elif code == xmlrpc . Faults . FAILED : <EOL> return template % ( name , '<STR_LIT>' ) <EOL> elif code == xmlrpc . Faults . SUCCESS : <EOL> return '<STR_LIT>' % name <EOL> raise ValueError ( '<STR_LIT>' % ( code , name ) ) <EOL> def do_clear ( self , arg ) : <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> names = arg . strip ( ) . split ( ) <EOL> if not names : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> self . help_clear ( ) <EOL> return <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> if '<STR_LIT:all>' in names : <EOL> results = supervisor . clearAllProcessLogs ( ) <EOL> for result in results : <EOL> result = self . _clearresult ( result ) <EOL> self . ctl . output ( result ) <EOL> else : <EOL> for name in names : <EOL> try : <EOL> result = supervisor . clearProcessLogs ( name ) <EOL> except xmlrpclib . Fault , e : <EOL> error = self . _clearresult ( { '<STR_LIT:status>' : e . faultCode , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT:description>' : e . faultString } ) <EOL> self . ctl . output ( error ) <EOL> else : <EOL> self . ctl . output ( '<STR_LIT>' % name ) <EOL> def help_clear ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( <EOL> "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def do_open ( self , arg ) : <EOL> url = arg . strip ( ) <EOL> parts = urlparse . urlparse ( url ) <EOL> if parts [ <NUM_LIT:0> ] not in ( '<STR_LIT>' , '<STR_LIT:http>' ) : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> return <EOL> self . ctl . options . serverurl = url <EOL> self . do_status ( '<STR_LIT>' ) <EOL> def help_open ( self ) : <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> self . ctl . output ( "<STR_LIT>" ) <EOL> def do_version ( self , arg ) : <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> self . ctl . output ( supervisor . getSupervisorVersion ( ) ) <EOL> def help_version ( self ) : <EOL> self . ctl . output ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def do_fg ( self , args = None ) : <EOL> if not self . ctl . upcheck ( ) : <EOL> return <EOL> if not args : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> self . help_fg ( ) <EOL> return <EOL> args = args . split ( ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> return <EOL> program = args [ <NUM_LIT:0> ] <EOL> supervisor = self . ctl . get_supervisor ( ) <EOL> try : <EOL> info = supervisor . getProcessInfo ( program ) <EOL> except xmlrpclib . Fault , msg : <EOL> if msg . faultCode == xmlrpc . Faults . BAD_NAME : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> return <EOL> self . ctl . output ( str ( msg ) ) <EOL> return <EOL> if not info [ '<STR_LIT:state>' ] == states . ProcessStates . RUNNING : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> return <EOL> try : <EOL> a = fgthread ( program , self . ctl ) <EOL> a . start ( ) <EOL> while True : <EOL> inp = raw_input ( ) + '<STR_LIT:\n>' <EOL> try : <EOL> supervisor . sendProcessStdin ( program , inp ) <EOL> except xmlrpclib . Fault , msg : <EOL> if msg . faultCode == xmlrpc . Faults . NOT_RUNNING : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> a . kill ( ) <EOL> return <EOL> info = supervisor . getProcessInfo ( program ) <EOL> if not info [ '<STR_LIT:state>' ] == states . ProcessStates . RUNNING : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> a . kill ( ) <EOL> return <EOL> continue <EOL> except ( KeyboardInterrupt , EOFError ) : <EOL> a . kill ( ) <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> return <EOL> def help_fg ( self , args = None ) : <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> self . ctl . output ( '<STR_LIT>' ) <EOL> def main ( args = None , options = None ) : <EOL> if options is None : <EOL> options = ClientOptions ( ) <EOL> options . realize ( args , doc = __doc__ ) <EOL> c = Controller ( options ) <EOL> if options . args : <EOL> c . onecmd ( "<STR_LIT:U+0020>" . join ( options . args ) ) <EOL> if options . interactive : <EOL> try : <EOL> import readline <EOL> if options . history_file : <EOL> try : <EOL> readline . read_history_file ( options . history_file ) <EOL> except IOError : <EOL> pass <EOL> def save ( ) : <EOL> try : <EOL> readline . write_history_file ( options . history_file ) <EOL> except IOError : <EOL> pass <EOL> import atexit <EOL> atexit . register ( save ) <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> c . cmdqueue . append ( '<STR_LIT:status>' ) <EOL> c . cmdloop ( ) <EOL> except KeyboardInterrupt : <EOL> c . output ( '<STR_LIT>' ) <EOL> pass <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
