def process_input_buffer(): """Send the content of the input buffer to all remote processes, this must be called in the main thread""" from polysh.control_commands_helpers import handle_control_command data = the_stdin_thread.input_buffer.get() remote_dispatcher.log('> ' + data) if data.startswith(':'): handle_control_command(data[1:-1]) return if data.startswith('!'): try: retcode = subprocess.call(data[1:], shell=True) except OSError, e: if e.errno == errno.EINTR: console_output('Child was interrupted\n') retcode = 0 else: raise if retcode > 128 and retcode <= 192: retcode = 128 - retcode if retcode > 0: console_output('Child returned %d\n' % retcode) elif retcode < 0: console_output('Child was terminated by signal %d\n' % -retcode) return
def console_output(msg: bytes, logging_msg: Optional[bytes] = None) -> None: """Use instead of print, to clear the status information before printing""" from polysh import remote_dispatcher remote_dispatcher.log(logging_msg or msg) if remote_dispatcher.options.interactive: from polysh.stdin import the_stdin_thread the_stdin_thread.no_raw_input() global last_status_length if last_status_length: safe_write('\r{}\r'.format(last_status_length * ' ').encode()) last_status_length = 0 safe_write(msg)
def console_output(msg, logging_msg=None): """Use instead of print, to clear the status information before printing""" from polysh import remote_dispatcher remote_dispatcher.log(logging_msg or msg) if remote_dispatcher.options.interactive: from polysh.stdin import the_stdin_thread the_stdin_thread.no_raw_input() global last_status_length if last_status_length: safe_write(sys.stdout, '\r' + last_status_length * ' ' + '\r') last_status_length = 0 safe_write(sys.stdout, msg)
def process_input_buffer() -> None: """Send the content of the input buffer to all remote processes, this must be called in the main thread""" from polysh.control_commands_helpers import handle_control_command data = the_stdin_thread.input_buffer.get() remote_dispatcher.log(b'> ' + data) if data.startswith(b':'): try: handle_control_command(data[1:-1].decode()) except UnicodeDecodeError as e: console_output(b'Could not decode command.') return if data.startswith(b'!'): try: retcode = subprocess.call(data[1:], shell=True) except OSError as e: if e.errno == errno.EINTR: console_output(b'Child was interrupted\n') retcode = 0 else: raise if retcode > 128 and retcode <= 192: retcode = 128 - retcode if retcode > 0: console_output('Child returned {:d}\n'.format(retcode).encode()) elif retcode < 0: console_output('Child was terminated by signal {:d}\n'.format( -retcode).encode()) return for r in dispatchers.all_instances(): try: r.dispatch_command(data) except asyncore.ExitNow as e: raise e except Exception as msg: raise msg console_output('{} for {}, disconnecting\n'.format( str(msg), r.display_name).encode()) r.disconnect() else: if r.enabled and r.state is remote_dispatcher.STATE_IDLE: r.change_state(remote_dispatcher.STATE_RUNNING)
def loop(interactive): histfile = os.path.expanduser("~/.polysh_history") init_history(histfile) next_signal = None last_status = None while True: try: if next_signal: current_signal = next_signal next_signal = None sig2chr = {signal.SIGINT: 'C', signal.SIGTSTP: 'Z'} ctrl = sig2chr[current_signal] remote_dispatcher.log('> ^{}\n'.format(ctrl).encode()) control_commands.do_send_ctrl(ctrl) console_output(b'') stdin.the_stdin_thread.prepend_text = None while dispatchers.count_awaited_processes()[0] and \ remote_dispatcher.main_loop_iteration(timeout=0.2): pass # Now it's quiet for r in dispatchers.all_instances(): r.print_unfinished_line() current_status = dispatchers.count_awaited_processes() if current_status != last_status: console_output(b'') if remote_dispatcher.options.interactive: stdin.the_stdin_thread.want_raw_input() last_status = current_status if dispatchers.all_terminated(): # Clear the prompt console_output(b'') raise asyncore.ExitNow(remote_dispatcher.options.exit_code) if not next_signal: # possible race here with the signal handler remote_dispatcher.main_loop_iteration() except KeyboardInterrupt: if interactive: next_signal = signal.SIGINT else: kill_all() os.kill(0, signal.SIGINT) except asyncore.ExitNow as e: console_output(b'') save_history(histfile) sys.exit(e.args[0])
def main_loop(): global next_signal last_status = None while True: try: if next_signal: current_signal = next_signal next_signal = None sig2chr = {signal.SIGINT: 'c', signal.SIGTSTP: 'z'} ctrl = sig2chr[current_signal] remote_dispatcher.log('> ^%c\n' % ctrl.upper()) control_commands.do_send_ctrl(ctrl) console_output('') the_stdin_thread.prepend_text = None while dispatchers.count_awaited_processes()[0] and \ remote_dispatcher.main_loop_iteration(timeout=0.2): pass # Now it's quiet for r in dispatchers.all_instances(): r.print_unfinished_line() current_status = dispatchers.count_awaited_processes() if current_status != last_status: console_output('') if remote_dispatcher.options.interactive: the_stdin_thread.want_raw_input() last_status = current_status if dispatchers.all_terminated(): # Clear the prompt console_output('') raise asyncore.ExitNow(remote_dispatcher.options.exit_code) if not next_signal: # possible race here with the signal handler remote_dispatcher.main_loop_iteration() except asyncore.ExitNow, e: console_output('') sys.exit(e.args[0])