<s> from __future__ import division , print_function , unicode_literals <EOL> import socket <EOL> from reprounzip . utils import stdout , stdout_bytes , stderr_bytes , stdin_bytes <EOL> try : <EOL> import termios <EOL> import tty <EOL> has_termios = True <EOL> except ImportError : <EOL> has_termios = False <EOL> def interactive_shell ( chan , raw = True ) : <EOL> if has_termios : <EOL> posix_shell ( chan , raw ) <EOL> else : <EOL> windows_shell ( chan ) <EOL> def posix_shell ( chan , raw ) : <EOL> import select <EOL> oldtty = termios . tcgetattr ( stdin_bytes ) <EOL> try : <EOL> if raw : <EOL> tty . setraw ( stdin_bytes . fileno ( ) ) <EOL> tty . setcbreak ( stdin_bytes . fileno ( ) ) <EOL> chan . settimeout ( <NUM_LIT:0.0> ) <EOL> while True : <EOL> r , w , e = select . select ( [ chan , stdin_bytes ] , [ ] , [ ] ) <EOL> if chan in r : <EOL> try : <EOL> if chan . recv_stderr_ready ( ) : <EOL> x = chan . recv_stderr ( <NUM_LIT> ) <EOL> if len ( x ) > <NUM_LIT:0> : <EOL> stderr_bytes . write ( x ) <EOL> stderr_bytes . flush ( ) <EOL> else : <EOL> x = chan . recv ( <NUM_LIT> ) <EOL> if len ( x ) == <NUM_LIT:0> : <EOL> break <EOL> stdout_bytes . write ( x ) <EOL> stdout_bytes . flush ( ) <EOL> except socket . timeout : <EOL> pass <EOL> if stdin_bytes in r : <EOL> x = stdin_bytes . read ( <NUM_LIT:1> ) <EOL> if len ( x ) == <NUM_LIT:0> : <EOL> break <EOL> chan . send ( x ) <EOL> finally : <EOL> if raw : <EOL> termios . tcsetattr ( stdin_bytes , termios . TCSADRAIN , oldtty ) <EOL> def windows_shell ( chan ) : <EOL> import threading <EOL> stdout . write ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> stdout . flush ( ) <EOL> out_lock = threading . RLock ( ) <EOL> def write ( recv , std ) : <EOL> while True : <EOL> data = recv ( <NUM_LIT> ) <EOL> if not data : <EOL> if std : <EOL> with out_lock : <EOL> stdout . write ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> stdout . flush ( ) <EOL> break <EOL> stream = [ stderr_bytes , stdout_bytes ] [ std ] <EOL> with out_lock : <EOL> stream . write ( data ) <EOL> stream . flush ( ) <EOL> threading . Thread ( target = write , args = ( chan . recv , True ) ) . start ( ) <EOL> threading . Thread ( target = write , args = ( chan . recv_stderr , False , ) ) . start ( ) <EOL> try : <EOL> while True : <EOL> d = stdin_bytes . read ( <NUM_LIT:1> ) <EOL> if not d : <EOL> chan . shutdown_write ( ) <EOL> break <EOL> try : <EOL> chan . send ( d ) <EOL> except socket . error : <EOL> break <EOL> except EOFError : <EOL> pass </s>
