<s> """<STR_LIT>""" <EOL> import py <EOL> import sys , os , posixpath , errno , stat , time <EOL> from pypy . lib import marshal <EOL> from pypy . rpython . module . ll_os_stat import s_StatResult <EOL> from pypy . tool . ansi_print import AnsiLog <EOL> from pypy . rlib . rarithmetic import r_longlong <EOL> from py . compat import subprocess <EOL> from pypy . tool . killsubprocess import killsubprocess <EOL> class MyAnsiLog ( AnsiLog ) : <EOL> KW_TO_COLOR = { <EOL> '<STR_LIT>' : ( ( <NUM_LIT> , ) , False ) , <EOL> '<STR_LIT:result>' : ( ( <NUM_LIT> , ) , False ) , <EOL> '<STR_LIT>' : ( ( <NUM_LIT> , ) , False ) , <EOL> '<STR_LIT>' : ( ( <NUM_LIT> , ) , False ) , <EOL> '<STR_LIT>' : ( ( <NUM_LIT:1> , <NUM_LIT> ) , True ) , <EOL> } <EOL> log = py . log . Producer ( "<STR_LIT>" ) <EOL> py . log . setconsumer ( "<STR_LIT>" , MyAnsiLog ( ) ) <EOL> def read_message ( f , timeout = None ) : <EOL> if timeout is not None : <EOL> import select <EOL> iwtd , owtd , ewtd = select . select ( [ f ] , [ ] , [ ] , timeout ) <EOL> if not iwtd : <EOL> raise EOFError ( "<STR_LIT>" ) <EOL> return marshal . load ( f ) <EOL> def write_message ( g , msg , resulttype = None ) : <EOL> if resulttype is None : <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:4> ) : <EOL> marshal . dump ( msg , g ) <EOL> else : <EOL> marshal . dump ( msg , g , <NUM_LIT:0> ) <EOL> else : <EOL> from pypy . rlib . rmarshal import get_marshaller <EOL> buf = [ ] <EOL> get_marshaller ( resulttype ) ( buf , msg ) <EOL> g . write ( '<STR_LIT>' . join ( buf ) ) <EOL> EXCEPTION_TABLE = [ <EOL> ( <NUM_LIT:1> , OSError ) , <EOL> ( <NUM_LIT:2> , IOError ) , <EOL> ( <NUM_LIT:3> , OverflowError ) , <EOL> ( <NUM_LIT:4> , ValueError ) , <EOL> ( <NUM_LIT:5> , ZeroDivisionError ) , <EOL> ( <NUM_LIT:6> , MemoryError ) , <EOL> ( <NUM_LIT:7> , KeyError ) , <EOL> ( <NUM_LIT:8> , IndexError ) , <EOL> ( <NUM_LIT:9> , RuntimeError ) , <EOL> ] <EOL> def write_exception ( g , exception , tb = None ) : <EOL> for i , excclass in EXCEPTION_TABLE : <EOL> if isinstance ( exception , excclass ) : <EOL> write_message ( g , i ) <EOL> if excclass is OSError : <EOL> error = exception . errno <EOL> if error is None : <EOL> error = errno . EPERM <EOL> write_message ( g , error ) <EOL> g . flush ( ) <EOL> break <EOL> else : <EOL> raise exception . __class__ , exception , tb <EOL> def shortrepr ( x ) : <EOL> r = repr ( x ) <EOL> if len ( r ) >= <NUM_LIT> : <EOL> r = r [ : <NUM_LIT:20> ] + '<STR_LIT>' + r [ - <NUM_LIT:8> : ] <EOL> return r <EOL> def signal_name ( n ) : <EOL> import signal <EOL> for key , value in signal . __dict__ . items ( ) : <EOL> if key . startswith ( '<STR_LIT>' ) and not key . startswith ( '<STR_LIT>' ) and value == n : <EOL> return key <EOL> return '<STR_LIT>' % ( n , ) <EOL> class SandboxedProc ( object ) : <EOL> """<STR_LIT>""" <EOL> debug = False <EOL> os_level_sandboxing = False <EOL> def __init__ ( self , args , executable = None ) : <EOL> """<STR_LIT>""" <EOL> self . popen = subprocess . Popen ( args , executable = executable , <EOL> bufsize = - <NUM_LIT:1> , <EOL> stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , <EOL> close_fds = True , <EOL> env = { } ) <EOL> self . popenlock = None <EOL> self . currenttimeout = None <EOL> self . currentlyidlefrom = None <EOL> def withlock ( self , function , * args , ** kwds ) : <EOL> lock = self . popenlock <EOL> if lock is not None : <EOL> lock . acquire ( ) <EOL> try : <EOL> return function ( * args , ** kwds ) <EOL> finally : <EOL> if lock is not None : <EOL> lock . release ( ) <EOL> def settimeout ( self , timeout , interrupt_main = False ) : <EOL> """<STR_LIT>""" <EOL> import thread <EOL> def _waiting_thread ( ) : <EOL> while True : <EOL> while self . currentlyidlefrom is not None : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> t = self . currenttimeout <EOL> if t is None : <EOL> return <EOL> delay = t - time . time ( ) <EOL> if delay <= <NUM_LIT:0.0> : <EOL> break <EOL> time . sleep ( min ( delay * <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> log . timeout ( "<STR_LIT>" ) <EOL> self . kill ( ) <EOL> def _settimeout ( ) : <EOL> need_new_thread = self . currenttimeout is None <EOL> self . currenttimeout = time . time ( ) + timeout <EOL> if need_new_thread : <EOL> thread . start_new_thread ( _waiting_thread , ( ) ) <EOL> if self . popenlock is None : <EOL> self . popenlock = thread . allocate_lock ( ) <EOL> self . withlock ( _settimeout ) <EOL> def canceltimeout ( self ) : <EOL> """<STR_LIT>""" <EOL> self . currenttimeout = None <EOL> self . currentlyidlefrom = None <EOL> def enter_idle ( self ) : <EOL> self . currentlyidlefrom = time . time ( ) <EOL> def leave_idle ( self ) : <EOL> def _postpone_timeout ( ) : <EOL> t = self . currentlyidlefrom <EOL> if t is not None and self . currenttimeout is not None : <EOL> self . currenttimeout += time . time ( ) - t <EOL> try : <EOL> self . withlock ( _postpone_timeout ) <EOL> finally : <EOL> self . currentlyidlefrom = None <EOL> def poll ( self ) : <EOL> returncode = self . withlock ( self . popen . poll ) <EOL> if returncode is not None : <EOL> self . canceltimeout ( ) <EOL> return returncode <EOL> def wait ( self ) : <EOL> returncode = self . withlock ( self . popen . wait ) <EOL> if returncode is not None : <EOL> self . canceltimeout ( ) <EOL> return returncode <EOL> def kill ( self ) : <EOL> self . withlock ( killsubprocess , self . popen ) <EOL> def handle_forever ( self ) : <EOL> returncode = self . handle_until_return ( ) <EOL> if returncode != <NUM_LIT:0> : <EOL> raise OSError ( "<STR_LIT>" % ( <EOL> returncode , ) ) <EOL> def handle_until_return ( self ) : <EOL> child_stdin = self . popen . stdin <EOL> child_stdout = self . popen . stdout <EOL> if self . os_level_sandboxing and sys . platform . startswith ( '<STR_LIT>' ) : <EOL> import select <EOL> select . select ( [ child_stdout ] , [ ] , [ ] ) <EOL> f = open ( '<STR_LIT>' % self . popen . pid , '<STR_LIT:w>' ) <EOL> print >> f , <NUM_LIT:1> <EOL> f . close ( ) <EOL> while True : <EOL> try : <EOL> fnname = read_message ( child_stdout ) <EOL> args = read_message ( child_stdout ) <EOL> except EOFError , e : <EOL> break <EOL> if self . debug and not self . is_spam ( fnname , * args ) : <EOL> log . call ( '<STR_LIT>' % ( fnname , <EOL> '<STR_LIT:U+002CU+0020>' . join ( [ shortrepr ( x ) for x in args ] ) ) ) <EOL> try : <EOL> answer , resulttype = self . handle_message ( fnname , * args ) <EOL> except Exception , e : <EOL> tb = sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> write_exception ( child_stdin , e , tb ) <EOL> if self . debug : <EOL> if str ( e ) : <EOL> log . exception ( '<STR_LIT>' % ( e . __class__ . __name__ , e ) ) <EOL> else : <EOL> log . exception ( '<STR_LIT:%s>' % ( e . __class__ . __name__ , ) ) <EOL> else : <EOL> if self . debug and not self . is_spam ( fnname , * args ) : <EOL> log . result ( shortrepr ( answer ) ) <EOL> try : <EOL> write_message ( child_stdin , <NUM_LIT:0> ) <EOL> write_message ( child_stdin , answer , resulttype ) <EOL> child_stdin . flush ( ) <EOL> except ( IOError , OSError ) : <EOL> if self . poll ( ) is not None : <EOL> break <EOL> else : <EOL> raise <EOL> returncode = self . wait ( ) <EOL> return returncode <EOL> def is_spam ( self , fnname , * args ) : <EOL> return ( fnname in ( '<STR_LIT>' , '<STR_LIT>' ) and <EOL> args [ <NUM_LIT:0> ] in ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> def handle_message ( self , fnname , * args ) : <EOL> if '<STR_LIT>' in fnname : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> handler = getattr ( self , '<STR_LIT>' + fnname . replace ( '<STR_LIT:.>' , '<STR_LIT>' ) ) <EOL> except AttributeError : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> resulttype = getattr ( handler , '<STR_LIT>' , None ) <EOL> return handler ( * args ) , resulttype <EOL> class SimpleIOSandboxedProc ( SandboxedProc ) : <EOL> """<STR_LIT>""" <EOL> _input = None <EOL> _output = None <EOL> _error = None <EOL> inputlogfile = None <EOL> def communicate ( self , input = None ) : <EOL> """<STR_LIT>""" <EOL> import cStringIO <EOL> if input : <EOL> if isinstance ( input , str ) : <EOL> input = cStringIO . StringIO ( input ) <EOL> self . _input = input <EOL> self . _output = cStringIO . StringIO ( ) <EOL> self . _error = cStringIO . StringIO ( ) <EOL> self . handle_forever ( ) <EOL> output = self . _output . getvalue ( ) <EOL> self . _output = None <EOL> error = self . _error . getvalue ( ) <EOL> self . _error = None <EOL> return ( output , error ) <EOL> def interact ( self , stdin = None , stdout = None , stderr = None ) : <EOL> """<STR_LIT>""" <EOL> import sys <EOL> self . _input = stdin or sys . stdin <EOL> self . _output = stdout or sys . stdout <EOL> self . _error = stderr or sys . stderr <EOL> returncode = self . handle_until_return ( ) <EOL> if returncode != <NUM_LIT:0> : <EOL> if os . name == '<STR_LIT>' and returncode < <NUM_LIT:0> : <EOL> print >> self . _error , "<STR_LIT>" % ( <EOL> signal_name ( - returncode ) , ) <EOL> else : <EOL> print >> self . _error , "<STR_LIT>" % ( <EOL> returncode , ) <EOL> self . _input = None <EOL> self . _output = None <EOL> self . _error = None <EOL> return returncode <EOL> def setlogfile ( self , filename ) : <EOL> self . inputlogfile = open ( filename , '<STR_LIT:a>' ) <EOL> def do_ll_os__ll_os_read ( self , fd , size ) : <EOL> if fd == <NUM_LIT:0> : <EOL> if self . _input is None : <EOL> return "<STR_LIT>" <EOL> elif ( getattr ( self , '<STR_LIT>' , False ) or <EOL> self . _input . isatty ( ) ) : <EOL> self . enter_idle ( ) <EOL> try : <EOL> inputdata = self . _input . readline ( size ) <EOL> finally : <EOL> self . leave_idle ( ) <EOL> else : <EOL> inputdata = self . _input . read ( size ) <EOL> if self . inputlogfile is not None : <EOL> self . inputlogfile . write ( inputdata ) <EOL> return inputdata <EOL> raise OSError ( "<STR_LIT>" % ( fd , ) ) <EOL> def do_ll_os__ll_os_write ( self , fd , data ) : <EOL> if fd == <NUM_LIT:1> : <EOL> self . _output . write ( data ) <EOL> return len ( data ) <EOL> if fd == <NUM_LIT:2> : <EOL> self . _error . write ( data ) <EOL> return len ( data ) <EOL> raise OSError ( "<STR_LIT>" % ( fd , ) ) <EOL> def do_ll_time__ll_time_sleep ( self , seconds ) : <EOL> while seconds > <NUM_LIT> : <EOL> time . sleep ( <NUM_LIT> ) <EOL> seconds -= <NUM_LIT> <EOL> if self . poll ( ) is not None : <EOL> return <EOL> time . sleep ( seconds ) <EOL> def do_ll_time__ll_time_time ( self ) : <EOL> return time . time ( ) <EOL> def do_ll_time__ll_time_clock ( self ) : <EOL> try : <EOL> starttime = self . starttime <EOL> except AttributeError : <EOL> starttime = self . starttime = time . time ( ) <EOL> return time . time ( ) - starttime <EOL> class VirtualizedSandboxedProc ( SandboxedProc ) : <EOL> """<STR_LIT>""" <EOL> virtual_env = { } <EOL> virtual_cwd = '<STR_LIT>' <EOL> virtual_console_isatty = False <EOL> virtual_fd_range = range ( <NUM_LIT:3> , <NUM_LIT:50> ) <EOL> def __init__ ( self , * args , ** kwds ) : <EOL> super ( VirtualizedSandboxedProc , self ) . __init__ ( * args , ** kwds ) <EOL> self . virtual_root = self . build_virtual_root ( ) <EOL> self . open_fds = { } <EOL> def build_virtual_root ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def do_ll_os__ll_os_envitems ( self ) : <EOL> return self . virtual_env . items ( ) <EOL> def do_ll_os__ll_os_getenv ( self , name ) : <EOL> return self . virtual_env . get ( name ) <EOL> def translate_path ( self , vpath ) : <EOL> vpath = posixpath . normpath ( posixpath . join ( self . virtual_cwd , vpath ) ) <EOL> dirnode = self . virtual_root <EOL> components = [ component for component in vpath . split ( '<STR_LIT:/>' ) ] <EOL> for component in components [ : - <NUM_LIT:1> ] : <EOL> if component : <EOL> dirnode = dirnode . join ( component ) <EOL> if dirnode . kind != stat . S_IFDIR : <EOL> raise OSError ( errno . ENOTDIR , component ) <EOL> return dirnode , components [ - <NUM_LIT:1> ] <EOL> def get_node ( self , vpath ) : <EOL> dirnode , name = self . translate_path ( vpath ) <EOL> if name : <EOL> node = dirnode . join ( name ) <EOL> else : <EOL> node = dirnode <EOL> log . vpath ( '<STR_LIT>' % ( vpath , node ) ) <EOL> return node <EOL> def do_ll_os__ll_os_stat ( self , vpathname ) : <EOL> node = self . get_node ( vpathname ) <EOL> return node . stat ( ) <EOL> do_ll_os__ll_os_stat . resulttype = s_StatResult <EOL> do_ll_os__ll_os_lstat = do_ll_os__ll_os_stat <EOL> def do_ll_os__ll_os_isatty ( self , fd ) : <EOL> return self . virtual_console_isatty and fd in ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> def allocate_fd ( self , f ) : <EOL> for fd in self . virtual_fd_range : <EOL> if fd not in self . open_fds : <EOL> self . open_fds [ fd ] = f <EOL> return fd <EOL> else : <EOL> raise OSError ( errno . EMFILE , "<STR_LIT>" ) <EOL> def get_file ( self , fd ) : <EOL> try : <EOL> return self . open_fds [ fd ] <EOL> except KeyError : <EOL> raise OSError ( errno . EBADF , "<STR_LIT>" ) <EOL> def do_ll_os__ll_os_open ( self , vpathname , flags , mode ) : <EOL> node = self . get_node ( vpathname ) <EOL> if flags & ( os . O_RDONLY | os . O_WRONLY | os . O_RDWR ) != os . O_RDONLY : <EOL> raise OSError ( errno . EPERM , "<STR_LIT>" ) <EOL> f = node . open ( ) <EOL> return self . allocate_fd ( f ) <EOL> def do_ll_os__ll_os_close ( self , fd ) : <EOL> f = self . get_file ( fd ) <EOL> del self . open_fds [ fd ] <EOL> f . close ( ) <EOL> def do_ll_os__ll_os_read ( self , fd , size ) : <EOL> try : <EOL> f = self . open_fds [ fd ] <EOL> except KeyError : <EOL> return super ( VirtualizedSandboxedProc , self ) . do_ll_os__ll_os_read ( <EOL> fd , size ) <EOL> else : <EOL> if not ( <NUM_LIT:0> <= size <= sys . maxint ) : <EOL> raise OSError ( errno . EINVAL , "<STR_LIT>" ) <EOL> return f . read ( min ( size , <NUM_LIT> * <NUM_LIT> ) ) <EOL> def do_ll_os__ll_os_lseek ( self , fd , pos , how ) : <EOL> f = self . get_file ( fd ) <EOL> f . seek ( pos , how ) <EOL> return f . tell ( ) <EOL> do_ll_os__ll_os_lseek . resulttype = r_longlong <EOL> def do_ll_os__ll_os_getcwd ( self ) : <EOL> return self . virtual_cwd <EOL> def do_ll_os__ll_os_strerror ( self , errnum ) : <EOL> return os . strerror ( errnum ) or ( '<STR_LIT>' % ( errnum , ) ) <EOL> def do_ll_os__ll_os_listdir ( self , vpathname ) : <EOL> node = self . get_node ( vpathname ) <EOL> return node . keys ( ) <EOL> class VirtualizedSocketProc ( VirtualizedSandboxedProc ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwds ) : <EOL> super ( VirtualizedSocketProc , self ) . __init__ ( * args , ** kwds ) <EOL> self . sockets = { } <EOL> def do_ll_os__ll_os_open ( self , name , flags , mode ) : <EOL> if not name . startswith ( "<STR_LIT>" ) : <EOL> return super ( VirtualizedSocketProc , self ) . do_ll_os__ll_os_open ( <EOL> name , flags , mode ) <EOL> import socket <EOL> host , port = name [ <NUM_LIT:6> : ] . split ( "<STR_LIT::>" ) <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> sock . connect ( ( host , int ( port ) ) ) <EOL> fd = self . allocate_fd ( sock ) <EOL> self . sockets [ fd ] = True <EOL> return fd <EOL> def do_ll_os__ll_os_read ( self , fd , size ) : <EOL> if fd in self . sockets : <EOL> return self . open_fds [ fd ] . recv ( size ) <EOL> return super ( VirtualizedSocketProc , self ) . do_ll_os__ll_os_read ( <EOL> fd , size ) <EOL> def do_ll_os__ll_os_write ( self , fd , data ) : <EOL> if fd in self . sockets : <EOL> return self . open_fds [ fd ] . send ( data ) <EOL> return super ( VirtualizedSocketProc , self ) . do_ll_os__ll_os_write ( <EOL> fd , data ) </s>
