<s> import cPickle as Pickle <EOL> import os <EOL> import struct <EOL> import sys <EOL> from eventlet . processes import Process , DeadProcess <EOL> from eventlet import pools <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> _g_debug_mode = False <EOL> if _g_debug_mode : <EOL> import traceback <EOL> import tempfile <EOL> def pythonpath_sync ( ) : <EOL> """<STR_LIT>""" <EOL> pypath = os . pathsep . join ( sys . path ) <EOL> os . environ [ '<STR_LIT>' ] = pypath <EOL> def wrap ( obj , dead_callback = None ) : <EOL> """<STR_LIT>""" <EOL> if type ( obj ) . __name__ == '<STR_LIT>' : <EOL> return wrap_module ( obj . __name__ , dead_callback ) <EOL> pythonpath_sync ( ) <EOL> if _g_debug_mode : <EOL> p = Process ( sys . executable , <EOL> [ "<STR_LIT>" , "<STR_LIT:ignore>" , __file__ , '<STR_LIT>' , <EOL> '<STR_LIT>' , os . path . join ( tempfile . gettempdir ( ) , '<STR_LIT>' ) ] , <EOL> dead_callback ) <EOL> else : <EOL> p = Process ( sys . executable , [ "<STR_LIT>" , "<STR_LIT:ignore>" , __file__ , '<STR_LIT>' ] , dead_callback ) <EOL> prox = Proxy ( ChildProcess ( p , p ) ) <EOL> prox . obj = obj <EOL> return prox . obj <EOL> def wrap_module ( fqname , dead_callback = None ) : <EOL> """<STR_LIT>""" <EOL> pythonpath_sync ( ) <EOL> global _g_debug_mode <EOL> if _g_debug_mode : <EOL> p = Process ( sys . executable , <EOL> [ "<STR_LIT>" , "<STR_LIT:ignore>" , __file__ , '<STR_LIT>' , fqname , <EOL> '<STR_LIT>' , os . path . join ( tempfile . gettempdir ( ) , '<STR_LIT>' ) ] , <EOL> dead_callback ) <EOL> else : <EOL> p = Process ( sys . executable , <EOL> [ "<STR_LIT>" , "<STR_LIT:ignore>" , __file__ , '<STR_LIT>' , fqname , ] , dead_callback ) <EOL> prox = Proxy ( ChildProcess ( p , p ) ) <EOL> return prox <EOL> def status ( proxy ) : <EOL> """<STR_LIT>""" <EOL> return proxy . __local_dict [ '<STR_LIT>' ] . make_request ( Request ( '<STR_LIT:status>' , { } ) ) <EOL> class BadResponse ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class BadRequest ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UnrecoverableError ( Exception ) : <EOL> pass <EOL> class Request ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , action , param ) : <EOL> self . _action = action <EOL> self . _param = param <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" + self . _action + "<STR_LIT>" + str ( self . _param ) <EOL> def __getitem__ ( self , name ) : <EOL> return self . _param [ name ] <EOL> def get ( self , name , default = None ) : <EOL> try : <EOL> return self [ name ] <EOL> except KeyError : <EOL> return default <EOL> def action ( self ) : <EOL> return self . _action <EOL> def _read_lp_hunk ( stream ) : <EOL> len_bytes = stream . read ( <NUM_LIT:4> ) <EOL> if len_bytes == '<STR_LIT>' : <EOL> raise EOFError ( "<STR_LIT>" % stream ) <EOL> length = struct . unpack ( '<STR_LIT:I>' , len_bytes ) [ <NUM_LIT:0> ] <EOL> body = stream . read ( length ) <EOL> return body <EOL> def _read_response ( id , attribute , input , cp ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> str = _read_lp_hunk ( input ) <EOL> _prnt ( repr ( str ) ) <EOL> response = Pickle . loads ( str ) <EOL> except ( AttributeError , DeadProcess , Pickle . UnpicklingError ) , e : <EOL> raise UnrecoverableError ( e ) <EOL> _prnt ( "<STR_LIT>" % response ) <EOL> if response [ <NUM_LIT:0> ] == '<STR_LIT:value>' : <EOL> return response [ <NUM_LIT:1> ] <EOL> elif response [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return CallableProxy ( id , attribute , cp ) <EOL> elif response [ <NUM_LIT:0> ] == '<STR_LIT:object>' : <EOL> return ObjectProxy ( cp , response [ <NUM_LIT:1> ] ) <EOL> elif response [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> exp = response [ <NUM_LIT:1> ] <EOL> raise exp <EOL> else : <EOL> raise BadResponse ( response [ <NUM_LIT:0> ] ) <EOL> def _write_lp_hunk ( stream , hunk ) : <EOL> write_length = struct . pack ( '<STR_LIT:I>' , len ( hunk ) ) <EOL> stream . write ( write_length + hunk ) <EOL> if hasattr ( stream , '<STR_LIT>' ) : <EOL> stream . flush ( ) <EOL> def _write_request ( param , output ) : <EOL> _prnt ( "<STR_LIT>" % param ) <EOL> str = Pickle . dumps ( param ) <EOL> _write_lp_hunk ( output , str ) <EOL> def _is_local ( attribute ) : <EOL> "<STR_LIT>" <EOL> if '<STR_LIT>' in attribute : <EOL> return True <EOL> return False <EOL> def _prnt ( message ) : <EOL> global _g_debug_mode <EOL> if _g_debug_mode : <EOL> print message <EOL> _g_logfile = None <EOL> def _log ( message ) : <EOL> global _g_logfile <EOL> if _g_logfile : <EOL> _g_logfile . write ( str ( os . getpid ( ) ) + '<STR_LIT:U+0020>' + message + '<STR_LIT:\n>' ) <EOL> _g_logfile . flush ( ) <EOL> def _unmunge_attr_name ( name ) : <EOL> """<STR_LIT>""" <EOL> if ( name . startswith ( '<STR_LIT>' ) ) : <EOL> name = name [ len ( '<STR_LIT>' ) : ] <EOL> if ( name . startswith ( '<STR_LIT>' ) ) : <EOL> name = name [ len ( '<STR_LIT>' ) : ] <EOL> return name <EOL> class ChildProcess ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , instr , outstr , dead_list = None ) : <EOL> """<STR_LIT>""" <EOL> _prnt ( "<STR_LIT>" ) <EOL> if dead_list is None : <EOL> dead_list = set ( ) <EOL> self . _dead_list = dead_list <EOL> self . _in = instr <EOL> self . _out = outstr <EOL> self . _lock = pools . TokenPool ( max_size = <NUM_LIT:1> ) <EOL> def make_request ( self , request , attribute = None ) : <EOL> _id = request . get ( '<STR_LIT:id>' ) <EOL> t = self . _lock . get ( ) <EOL> try : <EOL> _write_request ( request , self . _out ) <EOL> retval = _read_response ( _id , attribute , self . _in , self ) <EOL> finally : <EOL> self . _lock . put ( t ) <EOL> return retval <EOL> def __del__ ( self ) : <EOL> self . _in . close ( ) <EOL> class Proxy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cp ) : <EOL> """<STR_LIT>""" <EOL> self . __local_dict = dict ( <EOL> _cp = cp , <EOL> _id = None ) <EOL> def __getattribute__ ( self , attribute ) : <EOL> if _is_local ( attribute ) : <EOL> attribute = _unmunge_attr_name ( attribute ) <EOL> return super ( Proxy , self ) . __getattribute__ ( attribute ) <EOL> elif attribute in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return super ( Proxy , self ) . __getattribute__ ( '<STR_LIT>' ) <EOL> else : <EOL> my_cp = self . __local_dict [ '<STR_LIT>' ] <EOL> my_id = self . __local_dict [ '<STR_LIT>' ] <EOL> _dead_list = my_cp . _dead_list <EOL> for dead_object in _dead_list . copy ( ) : <EOL> request = Request ( '<STR_LIT>' , { '<STR_LIT:id>' : dead_object } ) <EOL> my_cp . make_request ( request ) <EOL> try : <EOL> _dead_list . remove ( dead_object ) <EOL> except KeyError : <EOL> pass <EOL> request = Request ( '<STR_LIT>' , { '<STR_LIT:id>' : my_id , '<STR_LIT>' : attribute } ) <EOL> return my_cp . make_request ( request , attribute = attribute ) <EOL> def __setattr__ ( self , attribute , value ) : <EOL> if _is_local ( attribute ) : <EOL> attribute = _unmunge_attr_name ( attribute ) <EOL> super ( Proxy , self ) . __getattribute__ ( '<STR_LIT>' ) [ attribute ] = value <EOL> else : <EOL> my_cp = self . __local_dict [ '<STR_LIT>' ] <EOL> my_id = self . __local_dict [ '<STR_LIT>' ] <EOL> request = Request ( '<STR_LIT>' , <EOL> { '<STR_LIT:id>' : my_id , '<STR_LIT>' : attribute , '<STR_LIT:value>' : value } ) <EOL> return my_cp . make_request ( request , attribute = attribute ) <EOL> class ObjectProxy ( Proxy ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cp , _id ) : <EOL> """<STR_LIT>""" <EOL> Proxy . __init__ ( self , cp ) <EOL> self . __local_dict [ '<STR_LIT>' ] = _id <EOL> def __del__ ( self ) : <EOL> my_id = self . __local_dict [ '<STR_LIT>' ] <EOL> self . __local_dict [ '<STR_LIT>' ] . _dead_list . add ( my_id ) <EOL> def __getitem__ ( self , key ) : <EOL> my_cp = self . __local_dict [ '<STR_LIT>' ] <EOL> my_id = self . __local_dict [ '<STR_LIT>' ] <EOL> request = Request ( '<STR_LIT>' , { '<STR_LIT:id>' : my_id , '<STR_LIT:key>' : key } ) <EOL> return my_cp . make_request ( request , attribute = key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> my_cp = self . __local_dict [ '<STR_LIT>' ] <EOL> my_id = self . __local_dict [ '<STR_LIT>' ] <EOL> request = Request ( '<STR_LIT>' , { '<STR_LIT:id>' : my_id , '<STR_LIT:key>' : key , '<STR_LIT:value>' : value } ) <EOL> return my_cp . make_request ( request , attribute = key ) <EOL> def __eq__ ( self , rhs ) : <EOL> my_cp = self . __local_dict [ '<STR_LIT>' ] <EOL> my_id = self . __local_dict [ '<STR_LIT>' ] <EOL> request = Request ( '<STR_LIT>' , { '<STR_LIT:id>' : my_id , '<STR_LIT>' : rhs . __local_dict [ '<STR_LIT>' ] } ) <EOL> return my_cp . make_request ( request ) <EOL> def __repr__ ( self ) : <EOL> val = self . __repr__ ( ) <EOL> return "<STR_LIT>" % val <EOL> def __str__ ( self ) : <EOL> return self . __str__ ( ) <EOL> def __nonzero__ ( self ) : <EOL> my_cp = self . __local_dict [ '<STR_LIT>' ] <EOL> my_id = self . __local_dict [ '<STR_LIT>' ] <EOL> request = Request ( '<STR_LIT>' , { '<STR_LIT:id>' : my_id } ) <EOL> return my_cp . make_request ( request ) <EOL> def __len__ ( self ) : <EOL> return self . __len__ ( ) <EOL> def __contains__ ( self , item ) : <EOL> return self . __contains__ ( item ) <EOL> def __deepcopy__ ( self , memo = None ) : <EOL> """<STR_LIT>""" <EOL> my_cp = self . __local_dict [ '<STR_LIT>' ] <EOL> my_id = self . __local_dict [ '<STR_LIT>' ] <EOL> request = Request ( '<STR_LIT>' , { '<STR_LIT:id>' : my_id } ) <EOL> return my_cp . make_request ( request ) <EOL> __copy__ = __deepcopy__ <EOL> def proxied_type ( self ) : <EOL> """<STR_LIT>""" <EOL> if type ( self ) is not ObjectProxy : <EOL> return type ( self ) <EOL> my_cp = self . __local_dict [ '<STR_LIT>' ] <EOL> my_id = self . __local_dict [ '<STR_LIT>' ] <EOL> request = Request ( '<STR_LIT:type>' , { '<STR_LIT:id>' : my_id } ) <EOL> return my_cp . make_request ( request ) <EOL> def getpid ( self ) : <EOL> """<STR_LIT>""" <EOL> my_cp = self . __local_dict [ '<STR_LIT>' ] <EOL> return my_cp . _in . getpid ( ) <EOL> class CallableProxy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , object_id , name , cp ) : <EOL> self . _object_id = object_id <EOL> self . _name = name <EOL> self . _cp = cp <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> request = Request ( '<STR_LIT>' , { '<STR_LIT:id>' : self . _object_id , <EOL> '<STR_LIT:name>' : self . _name , <EOL> '<STR_LIT:args>' : args , '<STR_LIT>' : kwargs } ) <EOL> return self . _cp . make_request ( request , attribute = self . _name ) <EOL> class Server ( object ) : <EOL> def __init__ ( self , input , output , export ) : <EOL> """<STR_LIT>""" <EOL> self . _in = input <EOL> self . _out = output <EOL> self . _export = export <EOL> self . _next_id = <NUM_LIT:1> <EOL> self . _objects = { } <EOL> def handle_status ( self , obj , req ) : <EOL> return { <EOL> '<STR_LIT>' : len ( self . _objects ) , <EOL> '<STR_LIT>' : self . _next_id , <EOL> '<STR_LIT>' : os . getpid ( ) } <EOL> def handle_getattr ( self , obj , req ) : <EOL> try : <EOL> return getattr ( obj , req [ '<STR_LIT>' ] ) <EOL> except AttributeError , e : <EOL> if hasattr ( obj , "<STR_LIT>" ) : <EOL> return obj [ req [ '<STR_LIT>' ] ] <EOL> else : <EOL> raise e <EOL> def handle_setattr ( self , obj , req ) : <EOL> try : <EOL> return setattr ( obj , req [ '<STR_LIT>' ] , req [ '<STR_LIT:value>' ] ) <EOL> except AttributeError , e : <EOL> if hasattr ( obj , "<STR_LIT>" ) : <EOL> return obj . __setitem__ ( req [ '<STR_LIT>' ] , req [ '<STR_LIT:value>' ] ) <EOL> else : <EOL> raise e <EOL> def handle_getitem ( self , obj , req ) : <EOL> return obj [ req [ '<STR_LIT:key>' ] ] <EOL> def handle_setitem ( self , obj , req ) : <EOL> obj [ req [ '<STR_LIT:key>' ] ] = req [ '<STR_LIT:value>' ] <EOL> return None <EOL> def handle_eq ( self , obj , req ) : <EOL> rhs = None <EOL> try : <EOL> rhs = self . _objects [ req [ '<STR_LIT>' ] ] <EOL> except KeyError : <EOL> return False <EOL> return ( obj == rhs ) <EOL> def handle_call ( self , obj , req ) : <EOL> try : <EOL> fn = getattr ( obj , req [ '<STR_LIT:name>' ] ) <EOL> except AttributeError , e : <EOL> if hasattr ( obj , "<STR_LIT>" ) : <EOL> fn = obj [ req [ '<STR_LIT:name>' ] ] <EOL> else : <EOL> raise e <EOL> return fn ( * req [ '<STR_LIT:args>' ] , ** req [ '<STR_LIT>' ] ) <EOL> def handle_del ( self , obj , req ) : <EOL> id = req [ '<STR_LIT:id>' ] <EOL> _log ( "<STR_LIT>" % ( id , self . _objects ) ) <EOL> try : <EOL> del self . _objects [ id ] <EOL> except KeyError : <EOL> pass <EOL> return None <EOL> def handle_type ( self , obj , req ) : <EOL> return type ( obj ) <EOL> def handle_nonzero ( self , obj , req ) : <EOL> return bool ( obj ) <EOL> def handle_copy ( self , obj , req ) : <EOL> return obj <EOL> def loop ( self ) : <EOL> """<STR_LIT>""" <EOL> _log ( "<STR_LIT>" ) <EOL> while True : <EOL> try : <EOL> try : <EOL> str_ = _read_lp_hunk ( self . _in ) <EOL> except EOFError : <EOL> if _g_debug_mode : <EOL> _log ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> request = Pickle . loads ( str_ ) <EOL> _log ( "<STR_LIT>" % ( request , self . _objects ) ) <EOL> req = request <EOL> id = None <EOL> obj = None <EOL> try : <EOL> id = req [ '<STR_LIT:id>' ] <EOL> if id : <EOL> id = int ( id ) <EOL> obj = self . _objects [ id ] <EOL> except Exception , e : <EOL> pass <EOL> if obj is None or id is None : <EOL> id = None <EOL> obj = self . _export ( ) <EOL> handler_name = '<STR_LIT>' % request . action ( ) <EOL> try : <EOL> handler = getattr ( self , handler_name ) <EOL> except AttributeError : <EOL> raise BadRequest , request . action ( ) <EOL> response = handler ( obj , request ) <EOL> if request . action ( ) in [ '<STR_LIT:status>' , '<STR_LIT:type>' , '<STR_LIT>' ] : <EOL> self . respond ( [ '<STR_LIT:value>' , response ] ) <EOL> elif callable ( response ) : <EOL> self . respond ( [ '<STR_LIT>' ] ) <EOL> elif self . is_value ( response ) : <EOL> self . respond ( [ '<STR_LIT:value>' , response ] ) <EOL> else : <EOL> self . _objects [ self . _next_id ] = response <EOL> self . respond ( [ '<STR_LIT:object>' , self . _next_id ] ) <EOL> self . _next_id += <NUM_LIT:1> <EOL> except ( KeyboardInterrupt , SystemExit ) , e : <EOL> raise e <EOL> except Exception , e : <EOL> self . write_exception ( e ) <EOL> def is_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return type ( value ) in ( str , unicode , int , float , long , bool , type ( None ) ) <EOL> def respond ( self , body ) : <EOL> _log ( "<STR_LIT>" % body ) <EOL> s = Pickle . dumps ( body ) <EOL> _log ( repr ( s ) ) <EOL> _write_lp_hunk ( self . _out , s ) <EOL> def write_exception ( self , e ) : <EOL> """<STR_LIT>""" <EOL> global _g_debug_mode <EOL> if _g_debug_mode : <EOL> _log ( "<STR_LIT>" % traceback . format_tb ( sys . exc_info ( ) [ <NUM_LIT:2> ] ) ) <EOL> self . respond ( [ '<STR_LIT>' , e ] ) <EOL> def raise_an_unpicklable_error ( ) : <EOL> class Unpicklable ( Exception ) : <EOL> pass <EOL> raise Unpicklable ( ) <EOL> def raise_standard_error ( ) : <EOL> raise FloatingPointError ( ) <EOL> def print_string ( str ) : <EOL> print str <EOL> def err_string ( str ) : <EOL> print >> sys . stderr , str <EOL> def named ( name ) : <EOL> """<STR_LIT>""" <EOL> toimport = name <EOL> obj = None <EOL> import_err_strings = [ ] <EOL> while toimport : <EOL> try : <EOL> obj = __import__ ( toimport ) <EOL> break <EOL> except ImportError , err : <EOL> import_err_strings . append ( err . __str__ ( ) ) <EOL> toimport = '<STR_LIT:.>' . join ( toimport . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) <EOL> if obj is None : <EOL> raise ImportError ( <EOL> '<STR_LIT>' % ( name , import_err_strings ) ) <EOL> for seg in name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] : <EOL> try : <EOL> obj = getattr ( obj , seg ) <EOL> except AttributeError : <EOL> dirobj = dir ( obj ) <EOL> dirobj . sort ( ) <EOL> raise AttributeError ( <EOL> '<STR_LIT>' % ( <EOL> seg , obj , dirobj , name , import_err_strings ) ) <EOL> return obj <EOL> def main ( ) : <EOL> import optparse <EOL> parser = optparse . OptionParser ( <EOL> usage = "<STR_LIT>" , <EOL> description = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> '<STR_LIT:-c>' , '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , type = '<STR_LIT:string>' , dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , type = '<STR_LIT:string>' , dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> options , args = parser . parse_args ( ) <EOL> global _g_logfile <EOL> if options . logfile : <EOL> _g_logfile = open ( options . logfile , '<STR_LIT:a>' ) <EOL> from eventlet import tpool <EOL> base_obj = [ None ] <EOL> if options . module : <EOL> def get_module ( ) : <EOL> if base_obj [ <NUM_LIT:0> ] is None : <EOL> base_obj [ <NUM_LIT:0> ] = named ( options . module ) <EOL> return base_obj [ <NUM_LIT:0> ] <EOL> server = Server ( tpool . Proxy ( sys . stdin ) , <EOL> tpool . Proxy ( sys . stdout ) , <EOL> get_module ) <EOL> elif options . child : <EOL> def get_base ( ) : <EOL> if base_obj [ <NUM_LIT:0> ] is None : <EOL> base_obj [ <NUM_LIT:0> ] = { } <EOL> return base_obj [ <NUM_LIT:0> ] <EOL> server = Server ( tpool . Proxy ( sys . stdin ) , <EOL> tpool . Proxy ( sys . stdout ) , <EOL> get_base ) <EOL> class NullSTDOut ( object ) : <EOL> def noop ( * args ) : <EOL> pass <EOL> def log_write ( self , message ) : <EOL> self . message = getattr ( self , '<STR_LIT:message>' , '<STR_LIT>' ) + message <EOL> if '<STR_LIT:\n>' in message : <EOL> _log ( self . message . rstrip ( ) ) <EOL> self . message = '<STR_LIT>' <EOL> write = noop <EOL> read = noop <EOL> flush = noop <EOL> sys . stderr = NullSTDOut ( ) <EOL> sys . stdout = NullSTDOut ( ) <EOL> if _g_debug_mode : <EOL> sys . stdout . write = sys . stdout . log_write <EOL> sys . stderr . write = sys . stderr . log_write <EOL> server . loop ( ) <EOL> if _g_logfile : <EOL> _g_logfile . close ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
