<s> import os , pprint , signal , socket , SocketServer , sys , threading , time , traceback <EOL> import cPickle as pickle <EOL> try : <EOL> import readline <EOL> except ImportError : <EOL> pass <EOL> import select , Queue <EOL> from guppy . heapy . RemoteConstants import * <EOL> from guppy . heapy . Console import Console <EOL> from guppy . sets import mutnodeset <EOL> from guppy . etc . etc import ptable <EOL> from guppy . etc import cmd <EOL> class Server ( SocketServer . ThreadingTCPServer ) : <EOL> pass <EOL> def ioready ( fd , wait ) : <EOL> r , w , x = select . select ( [ fd ] , [ ] , [ ] , wait ) <EOL> return len ( r ) <EOL> def queue_get_interruptible ( q , noblock = <NUM_LIT:0> ) : <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> return q . get ( timeout = <NUM_LIT> ) <EOL> except Queue . Empty : <EOL> if noblock : <EOL> break <EOL> CONN_CLOSED = ( '<STR_LIT>' , ) <EOL> class Handler ( SocketServer . StreamRequestHandler ) : <EOL> allow_reuse_address = <NUM_LIT:1> <EOL> def close ( self ) : <EOL> if not self . isclosed . tas ( <NUM_LIT:0> ) : <EOL> self . server . monitor . remove_connection ( self ) <EOL> self . dataq . put ( CONN_CLOSED ) <EOL> self . request . shutdown ( <NUM_LIT:2> ) <EOL> self . request . close ( ) <EOL> def send_cmd ( self , cmd ) : <EOL> if not cmd . endswith ( '<STR_LIT:\n>' ) : <EOL> cmd += '<STR_LIT:\n>' <EOL> self . request . send ( cmd ) <EOL> def browser_cmd ( self , cmd ) : <EOL> if self . prompt == '<STR_LIT>' : <EOL> self . exec_cmd ( '<STR_LIT:q>' , retdata = <NUM_LIT:1> ) <EOL> if self . prompt == '<STR_LIT>' : <EOL> self . exec_cmd ( '<STR_LIT>' , retdata = <NUM_LIT:1> ) <EOL> return self . exec_cmd ( cmd , retdata = <NUM_LIT:1> ) <EOL> def exec_cmd ( self , cmd , retdata = <NUM_LIT:0> , noblock = <NUM_LIT:0> ) : <EOL> if cmd is not None : <EOL> self . send_cmd ( cmd ) <EOL> self . promptstate = False <EOL> datas = [ ] <EOL> while <NUM_LIT:1> : <EOL> p = queue_get_interruptible ( self . dataq , noblock ) <EOL> if p is None : <EOL> if self . promptstate : <EOL> break <EOL> else : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> continue <EOL> if p is CONN_CLOSED : <EOL> raise EOFError <EOL> if p [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> self . promptstate = False <EOL> if retdata : <EOL> datas . append ( p [ <NUM_LIT:1> ] ) <EOL> else : <EOL> sys . stdout . write ( p [ <NUM_LIT:1> ] ) <EOL> elif p [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> self . prompt = p [ <NUM_LIT:1> ] <EOL> if self . dataq . empty ( ) : <EOL> self . promptstate = True <EOL> break <EOL> else : <EOL> self . promptstate = False <EOL> else : <EOL> assert <NUM_LIT:0> <EOL> if retdata : <EOL> return '<STR_LIT>' . join ( datas ) <EOL> def get_ps ( self , name ) : <EOL> for line in self . firstdata . split ( '<STR_LIT:\n>' ) : <EOL> if line . startswith ( name ) : <EOL> if '<STR_LIT:=>' in line : <EOL> ix = line . index ( '<STR_LIT:=>' ) <EOL> line = line [ ix + <NUM_LIT:1> : ] . strip ( ) <EOL> return line <EOL> return '<STR_LIT>' <EOL> def get_val ( self , expr ) : <EOL> data = self . browser_cmd ( '<STR_LIT>' % expr ) <EOL> return pickle . loads ( data ) <EOL> def handle ( self ) : <EOL> self . prompt = None <EOL> self . promptstate = False <EOL> self . isclosed = mutnodeset ( ) <EOL> self . dataq = Queue . Queue ( ) <EOL> self . server . monitor . add_connection ( self ) <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> data = self . rfile . readline ( ) <EOL> if not data : <EOL> raise EOFError , '<STR_LIT>' <EOL> if data . endswith ( DONE ) : <EOL> raise EOFError , '<STR_LIT>' <EOL> except ( EOFError , socket . error ) : <EOL> break <EOL> if data . endswith ( READLINE ) : <EOL> prompt = data [ : - len ( READLINE ) ] <EOL> self . dataq . put ( ( '<STR_LIT>' , prompt ) ) <EOL> if self . prompt is None : <EOL> self . firstdata = self . exec_cmd ( cmd = None , retdata = <NUM_LIT:1> ) <EOL> else : <EOL> self . dataq . put ( ( '<STR_LIT>' , data ) ) <EOL> self . close ( ) <EOL> class MonitorConnection ( cmd . Cmd ) : <EOL> use_raw_input = <NUM_LIT:1> <EOL> def __init__ ( self , monitor ) : <EOL> self . aliases = { } <EOL> cmd . Cmd . __init__ ( self ) <EOL> self . hno = <NUM_LIT:0> <EOL> self . isclosed = <NUM_LIT:0> <EOL> self . forceexit = <NUM_LIT:0> <EOL> self . prompt = '<STR_LIT>' <EOL> self . monitor = monitor <EOL> self . server = s = Server ( ( LOCALHOST , HEAPYPORT ) , Handler ) <EOL> self . server . monitor = monitor <EOL> self . st = threading . Thread ( target = self . run_server , <EOL> args = ( ) ) <EOL> self . st . start ( ) <EOL> def close ( self ) : <EOL> self . isclosed = <NUM_LIT:1> <EOL> self . server . socket . shutdown ( <NUM_LIT:2> ) <EOL> self . server . server_close ( ) <EOL> self . server . verify_request = lambda x , y : <NUM_LIT:0> <EOL> def default ( self , line ) : <EOL> cmd . Cmd . default ( self , line ) <EOL> cmd . Cmd . do_help ( self , '<STR_LIT>' ) <EOL> def run_server ( self ) : <EOL> s = self . server <EOL> while not self . isclosed : <EOL> s . handle_request ( ) <EOL> s . server_close ( ) <EOL> def exec_cmd ( self , cmd ) : <EOL> if not cmd : <EOL> return <EOL> line = cmd <EOL> try : <EOL> line = self . precmd ( line ) <EOL> stop = self . onecmd ( line ) <EOL> stop = self . postcmd ( stop , line ) <EOL> return stop <EOL> except : <EOL> self . handle_error ( line ) <EOL> def handle_error ( self , cmdline ) : <EOL> """<STR_LIT>""" <EOL> print >> sys . stderr , '<STR_LIT:->' * <NUM_LIT> <EOL> print >> sys . stderr , '<STR_LIT>' , <EOL> print >> sys . stderr , repr ( cmdline ) <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> print >> sys . stderr , '<STR_LIT:->' * <NUM_LIT> <EOL> def precmd ( self , line ) : <EOL> """<STR_LIT>""" <EOL> self . curline = line <EOL> if not line : <EOL> return line <EOL> args = line . split ( ) <EOL> while self . aliases . has_key ( args [ <NUM_LIT:0> ] ) : <EOL> line = self . aliases [ args [ <NUM_LIT:0> ] ] <EOL> if '<STR_LIT:%>' in line : <EOL> ii = <NUM_LIT:1> <EOL> for tmpArg in args [ <NUM_LIT:1> : ] : <EOL> line = line . replace ( "<STR_LIT:%>" + str ( ii ) , <EOL> tmpArg ) <EOL> line = line . replace ( '<STR_LIT>' + str ( ii ) , <EOL> '<STR_LIT:U+0020>' . join ( args [ ii : ] ) ) <EOL> ii = ii + <NUM_LIT:1> <EOL> line = line . replace ( "<STR_LIT>" , '<STR_LIT:U+0020>' . join ( args [ <NUM_LIT:1> : ] ) ) <EOL> else : <EOL> line = line + '<STR_LIT:U+0020>' + '<STR_LIT:U+0020>' . join ( args [ <NUM_LIT:1> : ] ) <EOL> args = line . split ( ) <EOL> if args [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> marker = line . find ( '<STR_LIT>' ) <EOL> if marker >= <NUM_LIT:0> : <EOL> next = line [ marker + <NUM_LIT:2> : ] . lstrip ( ) <EOL> self . cmdqueue . append ( next ) <EOL> line = line [ : marker ] . rstrip ( ) <EOL> return line <EOL> def do_exit ( self , arg ) : <EOL> self . forceexit = <NUM_LIT:1> <EOL> return <NUM_LIT:1> <EOL> def help_exit ( self ) : <EOL> print """<STR_LIT>""" <EOL> do_h = cmd . Cmd . do_help <EOL> def help_h ( self ) : <EOL> print """<STR_LIT>""" <EOL> def help_help ( self ) : <EOL> self . help_h ( ) <EOL> def do_int ( self , arg ) : <EOL> try : <EOL> con = Console ( stdin = self . stdin , stdout = self . stdout , <EOL> locals = self . __dict__ ) <EOL> con . interact ( <EOL> "<STR_LIT>" % <EOL> con . EOF_key_sequence ) <EOL> finally : <EOL> pass <EOL> def help_int ( self ) : <EOL> print """<STR_LIT>""" <EOL> def do_ki ( self , arg ) : <EOL> if not arg : <EOL> arg = self . conid <EOL> arg = int ( arg ) <EOL> c = self . monitor . connections [ arg ] <EOL> if c . get_ps ( '<STR_LIT>' ) : <EOL> print '''<STR_LIT>''' <EOL> else : <EOL> print '<STR_LIT>' % arg <EOL> c . send_cmd ( KEYBOARDINTERRUPT ) <EOL> def help_ki ( self ) : <EOL> print """<STR_LIT>""" <EOL> def do_lc ( self , arg ) : <EOL> table = [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> for cid , con in self . monitor . connections . items ( ) : <EOL> table . append ( [ cid , <EOL> con . get_ps ( '<STR_LIT>' ) , <EOL> con . get_ps ( '<STR_LIT>' ) ] ) <EOL> ptable ( table , self . stdout ) <EOL> def help_lc ( self ) : <EOL> print """<STR_LIT>""" <EOL> def do_sc ( self , arg ) : <EOL> if arg : <EOL> self . conid = int ( arg ) <EOL> print '<STR_LIT>' % self . conid <EOL> self . monitor . set_connection ( self . monitor . connections [ self . conid ] ) <EOL> def help_sc ( self ) : <EOL> print """<STR_LIT>""" <EOL> def do_q ( self , arg ) : <EOL> return <NUM_LIT:1> <EOL> def help_q ( self ) : <EOL> print """<STR_LIT>""" <EOL> class Monitor : <EOL> use_raw_input = <NUM_LIT:1> <EOL> def __init__ ( self ) : <EOL> self . connection = self . monitor_connection = MonitorConnection ( self ) <EOL> self . connections = { } <EOL> self . ids = <NUM_LIT:0> <EOL> self . prompt = None <EOL> def newid ( self ) : <EOL> if not self . connections : <EOL> self . ids = <NUM_LIT:1> <EOL> self . monitor_connection . conid = self . ids <EOL> else : <EOL> self . ids = max ( [ <NUM_LIT:1> ] + [ c for c in self . connections . keys ( ) ] ) + <NUM_LIT:1> <EOL> return self . ids <EOL> def add_connection ( self , connection ) : <EOL> hid = self . newid ( ) <EOL> self . connections [ hid ] = connection <EOL> connection . monitor_id = hid <EOL> self . print_async ( '<STR_LIT>' % hid ) <EOL> def print_async ( self , text ) : <EOL> """<STR_LIT>""" <EOL> if self . prompt is not None : <EOL> print '<STR_LIT:\n>' + text <EOL> sys . stdout . write ( self . prompt ) <EOL> sys . stdout . flush ( ) <EOL> def remove_connection ( self , connection ) : <EOL> del self . connections [ connection . monitor_id ] <EOL> if connection is self . connection : <EOL> self . set_connection ( self . monitor_connection ) <EOL> self . print_async ( '<STR_LIT>' % connection . monitor_id ) <EOL> def run ( self ) : <EOL> try : <EOL> stop = <NUM_LIT:0> <EOL> while not stop : <EOL> try : <EOL> while not stop : <EOL> conn = self . connection <EOL> self . prompt = conn . prompt <EOL> if conn is not self . monitor_connection : <EOL> conn . exec_cmd ( cmd = None , noblock = <NUM_LIT:1> ) <EOL> cmd = raw_input ( conn . prompt ) <EOL> self . prompt = None <EOL> conn = None <EOL> if cmd . startswith ( '<STR_LIT:.>' ) : <EOL> if cmd == '<STR_LIT:.>' : <EOL> self . connection = self . monitor_connection <EOL> else : <EOL> cmd = cmd [ <NUM_LIT:1> : ] <EOL> conn = self . monitor_connection <EOL> else : <EOL> conn = self . connection <EOL> if conn : <EOL> try : <EOL> r = conn . exec_cmd ( cmd ) <EOL> except EOFError : <EOL> r = <NUM_LIT:1> <EOL> if conn is self . monitor_connection and r : <EOL> stop = <NUM_LIT:1> <EOL> except EOFError : <EOL> '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> self . connection = self . monitor_connection <EOL> stop = <NUM_LIT:1> <EOL> except KeyboardInterrupt : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> self . connection = self . monitor_connection <EOL> continue <EOL> finally : <EOL> self . prompt = None <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> for c in self . connections . values ( ) : <EOL> try : <EOL> c . close ( ) <EOL> except socket . error : <EOL> pass <EOL> try : <EOL> self . monitor_connection . close ( ) <EOL> except socket . error : <EOL> pass <EOL> if self . monitor_connection . forceexit : <EOL> os . _exit ( <NUM_LIT:0> ) <EOL> def set_connection ( self , connection ) : <EOL> self . connection = connection <EOL> self . prompt = connection . prompt <EOL> def monitor ( ) : <EOL> """<STR_LIT>""" <EOL> from guppy . heapy import Remote <EOL> Remote . off ( ) <EOL> m = Monitor ( ) <EOL> m . run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> monitor ( ) </s>
