From 0e0a68f87a6de4ad91095ef9da7a7fc166af2bef Mon Sep 17 00:00:00 2001 From: Benedek Racz Date: Mon, 3 Feb 2020 13:29:47 +0100 Subject: [PATCH] minimal 02 --- appveyor.yml | 2 +- wexpect/console_reader.py | 52 +++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 142ba11..88d1ce2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -18,7 +18,7 @@ install: test_script: - cmd: set WEXPECT_SPAWN_CLASS=SpawnPipe - - cmd: python -c "import sys;sys.path = ['C:\\projects\\wexpect'] + sys.path;import wexpect;import time;wexpect.console_reader.logger.info('loggerStart.');cons = wexpect.ConsoleReaderPipe(wexpect.join_args([python, '-Wi', 'list100.py']), host_pid=2684, local_echo=True, interact=False);wexpect.console_reader.logger.info(f'Console finished2. {cons.child_exitstatus}');sys.exit(cons.child_exitstatus)" + - cmd: python -m wexpect.console_reader "python -Wi tests\\list100.py" after_test: - python dump_logs.py diff --git a/wexpect/console_reader.py b/wexpect/console_reader.py index 6435052..7b84c62 100644 --- a/wexpect/console_reader.py +++ b/wexpect/console_reader.py @@ -42,7 +42,6 @@ import logging import os import traceback import psutil -import signal from io import StringIO import ctypes @@ -61,7 +60,7 @@ from .wexpect_util import SIGNAL_CHARS # # System-wide constants # -screenbufferfillchar = '\4' +screenbufferfillchar = '\0' maxconsoleY = 8000 default_port = 4321 @@ -80,8 +79,9 @@ class ConsoleReaderBase: This class initialize the console starts the child in it and reads the console periodically. """ - def __init__(self, path, host_pid, cp=None, window_size_x=80, window_size_y=25, - buffer_size_x=80, buffer_size_y=16000, local_echo=True, interact=False, **kwargs): + def __init__(self, path, host_pid=None, cp=None, window_size_x=80, window_size_y=25, + buffer_size_x=80, buffer_size_y=16000, local_echo=True, interact=False, + connect_to_host=True, **kwargs): """Initialize the console starts the child in it and reads the console periodically. Args: @@ -102,10 +102,13 @@ class ConsoleReaderBase: self.local_echo = local_echo self.console_pid = os.getpid() self.host_pid = host_pid - self.host_process = psutil.Process(host_pid) self.child_process = None self.child_pid = None self.enable_signal_chars = True + self.host_process = None + + if self.host_pid is not None: + self.host_process = psutil.Process(host_pid) logger.info("ConsoleReader started") @@ -118,7 +121,8 @@ class ConsoleReaderBase: logger.info(e) try: - self.create_connection(**kwargs) + if connect_to_host: + self.create_connection(**kwargs) logger.info('Spawning %s' % path) try: self.initConsole() @@ -159,7 +163,7 @@ class ConsoleReaderBase: paused = False while True: - if not self.isalive(self.host_process): + if not self.isalive_host(): logger.info('Host process has been died.') return @@ -203,10 +207,13 @@ class ConsoleReaderBase: logger.info('The process has already died.') return - def isalive(self, process): + def isalive_host(self): """True if the child is still alive, false otherwise""" try: - self.exitstatus = process.wait(timeout=0) + if self.host_pid is None: + # Standalone mode: no host + return True + self.host_exitstatus = self.host_process.wait(timeout=0) return False except psutil.TimeoutExpired: return True @@ -500,6 +507,11 @@ class ConsoleReaderSocket(ConsoleReaderBase): class ConsoleReaderPipe(ConsoleReaderBase): + def __init__(self, **kwargs): + self.pipe = None + super().__init__(**kwargs) + + def create_connection(self, **kwargs): pipe_name = 'wexpect_{}'.format(self.console_pid) pipe_full_path = r'\\.\pipe\{}'.format(pipe_name) @@ -525,9 +537,12 @@ class ConsoleReaderPipe(ConsoleReaderBase): logger.debug(f'Sending msg: {msg}') else: logger.spam(f'Sending msg: {msg}') - win32file.WriteFile(self.pipe, msg) + if self.pipe: + win32file.WriteFile(self.pipe, msg) def get_from_host(self): + if self.pipe is None: + return b'' data, avail, bytes_left = win32pipe.PeekNamedPipe(self.pipe, 4096) logger.spam(f'data: {data} avail:{avail} bytes_left{bytes_left}') if avail > 0: @@ -536,3 +551,20 @@ class ConsoleReaderPipe(ConsoleReaderBase): return ret else: return b'' + +def main(): + import sys + logger.info('loggerStart.') + cmd = sys.argv[1] + try: + host_pid = int(sys.argv[2]) + except: + host_pid = None + print(f'cmd: {cmd}') + print(f'host_pid: {host_pid}') + cons = ConsoleReaderPipe(path=cmd, host_pid=host_pid, connect_to_host=False) + logger.info(f'Console finished2. {cons.child_exitstatus}') + sys.exit(cons.child_exitstatus) + +if __name__ == "__main__": + main() \ No newline at end of file