mirror of
https://github.com/clearml/wexpect-venv
synced 2025-03-04 11:08:11 +00:00
minimal 02
This commit is contained in:
parent
5cb27b409d
commit
0e0a68f87a
@ -18,7 +18,7 @@ install:
|
|||||||
|
|
||||||
test_script:
|
test_script:
|
||||||
- cmd: set WEXPECT_SPAWN_CLASS=SpawnPipe
|
- 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:
|
after_test:
|
||||||
- python dump_logs.py
|
- python dump_logs.py
|
||||||
|
@ -42,7 +42,6 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import traceback
|
import traceback
|
||||||
import psutil
|
import psutil
|
||||||
import signal
|
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
import ctypes
|
import ctypes
|
||||||
@ -61,7 +60,7 @@ from .wexpect_util import SIGNAL_CHARS
|
|||||||
#
|
#
|
||||||
# System-wide constants
|
# System-wide constants
|
||||||
#
|
#
|
||||||
screenbufferfillchar = '\4'
|
screenbufferfillchar = '\0'
|
||||||
maxconsoleY = 8000
|
maxconsoleY = 8000
|
||||||
default_port = 4321
|
default_port = 4321
|
||||||
|
|
||||||
@ -80,8 +79,9 @@ class ConsoleReaderBase:
|
|||||||
This class initialize the console starts the child in it and reads the console periodically.
|
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,
|
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, **kwargs):
|
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.
|
"""Initialize the console starts the child in it and reads the console periodically.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -102,10 +102,13 @@ class ConsoleReaderBase:
|
|||||||
self.local_echo = local_echo
|
self.local_echo = local_echo
|
||||||
self.console_pid = os.getpid()
|
self.console_pid = os.getpid()
|
||||||
self.host_pid = host_pid
|
self.host_pid = host_pid
|
||||||
self.host_process = psutil.Process(host_pid)
|
|
||||||
self.child_process = None
|
self.child_process = None
|
||||||
self.child_pid = None
|
self.child_pid = None
|
||||||
self.enable_signal_chars = True
|
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")
|
logger.info("ConsoleReader started")
|
||||||
|
|
||||||
@ -118,6 +121,7 @@ class ConsoleReaderBase:
|
|||||||
logger.info(e)
|
logger.info(e)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
if connect_to_host:
|
||||||
self.create_connection(**kwargs)
|
self.create_connection(**kwargs)
|
||||||
logger.info('Spawning %s' % path)
|
logger.info('Spawning %s' % path)
|
||||||
try:
|
try:
|
||||||
@ -159,7 +163,7 @@ class ConsoleReaderBase:
|
|||||||
paused = False
|
paused = False
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if not self.isalive(self.host_process):
|
if not self.isalive_host():
|
||||||
logger.info('Host process has been died.')
|
logger.info('Host process has been died.')
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -203,10 +207,13 @@ class ConsoleReaderBase:
|
|||||||
logger.info('The process has already died.')
|
logger.info('The process has already died.')
|
||||||
return
|
return
|
||||||
|
|
||||||
def isalive(self, process):
|
def isalive_host(self):
|
||||||
"""True if the child is still alive, false otherwise"""
|
"""True if the child is still alive, false otherwise"""
|
||||||
try:
|
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
|
return False
|
||||||
except psutil.TimeoutExpired:
|
except psutil.TimeoutExpired:
|
||||||
return True
|
return True
|
||||||
@ -500,6 +507,11 @@ class ConsoleReaderSocket(ConsoleReaderBase):
|
|||||||
|
|
||||||
|
|
||||||
class ConsoleReaderPipe(ConsoleReaderBase):
|
class ConsoleReaderPipe(ConsoleReaderBase):
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
self.pipe = None
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
def create_connection(self, **kwargs):
|
def create_connection(self, **kwargs):
|
||||||
pipe_name = 'wexpect_{}'.format(self.console_pid)
|
pipe_name = 'wexpect_{}'.format(self.console_pid)
|
||||||
pipe_full_path = r'\\.\pipe\{}'.format(pipe_name)
|
pipe_full_path = r'\\.\pipe\{}'.format(pipe_name)
|
||||||
@ -525,9 +537,12 @@ class ConsoleReaderPipe(ConsoleReaderBase):
|
|||||||
logger.debug(f'Sending msg: {msg}')
|
logger.debug(f'Sending msg: {msg}')
|
||||||
else:
|
else:
|
||||||
logger.spam(f'Sending msg: {msg}')
|
logger.spam(f'Sending msg: {msg}')
|
||||||
|
if self.pipe:
|
||||||
win32file.WriteFile(self.pipe, msg)
|
win32file.WriteFile(self.pipe, msg)
|
||||||
|
|
||||||
def get_from_host(self):
|
def get_from_host(self):
|
||||||
|
if self.pipe is None:
|
||||||
|
return b''
|
||||||
data, avail, bytes_left = win32pipe.PeekNamedPipe(self.pipe, 4096)
|
data, avail, bytes_left = win32pipe.PeekNamedPipe(self.pipe, 4096)
|
||||||
logger.spam(f'data: {data} avail:{avail} bytes_left{bytes_left}')
|
logger.spam(f'data: {data} avail:{avail} bytes_left{bytes_left}')
|
||||||
if avail > 0:
|
if avail > 0:
|
||||||
@ -536,3 +551,20 @@ class ConsoleReaderPipe(ConsoleReaderBase):
|
|||||||
return ret
|
return ret
|
||||||
else:
|
else:
|
||||||
return b''
|
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()
|
Loading…
Reference in New Issue
Block a user