[ADD] interact feature

This commit is contained in:
Benedek Racz 2020-01-22 13:03:53 +01:00
parent 3965fd4f65
commit 9709eb82e9
2 changed files with 19 additions and 7 deletions

View File

@ -51,7 +51,7 @@ import win32console
import win32process
import win32con
import win32file
import winerror
import win32gui
import win32pipe
import socket
@ -85,7 +85,7 @@ class ConsoleReaderBase:
"""
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, **kwargs):
buffer_size_x=80, buffer_size_y=16000, local_echo=True, interact=False, **kwargs):
"""Initialize the console starts the child in it and reads the console periodically.
Args:
@ -131,6 +131,10 @@ class ConsoleReaderBase:
logger.info(traceback.format_exc())
return
if interact:
self.interact()
self.interact()
self.read_loop()
except:
logger.error(traceback.format_exc())
@ -410,6 +414,13 @@ class ConsoleReaderBase:
return s
def interact(self):
"""Displays the child console for interaction."""
logger.debug('Start interact window')
win32gui.ShowWindow(win32console.GetConsoleWindow(), win32con.SW_SHOW)
class ConsoleReaderSocket(ConsoleReaderBase):

View File

@ -204,7 +204,7 @@ def run (command, timeout=-1, withexitstatus=False, events=None, extra_args=None
class SpawnBase:
def __init__(self, command, args=[], timeout=30, maxread=60000, searchwindowsize=None,
logfile=None, cwd=None, env=None, codepage=None, echo=True, safe_exit=True, **kwargs):
logfile=None, cwd=None, env=None, codepage=None, echo=True, safe_exit=True, interact=False, **kwargs):
"""This starts the given command in a child process. This does all the
fork/exec type of stuff for a pty. This is called by __init__. If args
is empty then command will be parsed (split on spaces) and args will be
@ -249,6 +249,7 @@ class SpawnBase:
self.flag_child_finished = False
self.buffer = '' # This is the read buffer. See maxread.
self.searchwindowsize = searchwindowsize # Anything before searchwindowsize point is preserved, but not searched.
self.interact = interact
# If command is an int type then it may represent a file descriptor.
@ -914,7 +915,7 @@ class SpawnPipe(SpawnBase):
"import wexpect;"
"import time;"
"wexpect.console_reader.logger.info('loggerStart.');"
f"wexpect.ConsoleReaderPipe(wexpect.join_args({args}), {pid}, local_echo={self.echo});"
f"wexpect.ConsoleReaderPipe(wexpect.join_args({args}), {pid}, local_echo={self.echo}, interact={self.interact});"
"wexpect.console_reader.logger.info('Console finished2.');"
)
@ -927,11 +928,11 @@ class SpawnPipe(SpawnBase):
class SpawnSocket(SpawnBase):
def __init__(self, command, args=[], timeout=30, maxread=60000, searchwindowsize=None,
logfile=None, cwd=None, env=None, codepage=None, echo=True, port=4321, host='localhost'):
logfile=None, cwd=None, env=None, codepage=None, echo=True, port=4321, host='localhost', interact=False):
self.port = port
self.host = host
super().__init__(command=command, args=args, timeout=timeout, maxread=maxread,
searchwindowsize=searchwindowsize, cwd=cwd, env=env, codepage=codepage, echo=echo)
searchwindowsize=searchwindowsize, cwd=cwd, env=env, codepage=codepage, echo=echo, interact=interact)
def send(self, s):
@ -1031,7 +1032,7 @@ class SpawnSocket(SpawnBase):
"import wexpect;"
"import time;"
"wexpect.console_reader.logger.info('loggerStart.');"
f"wexpect.ConsoleReaderSocket(wexpect.join_args({args}), {pid}, port={self.port}, local_echo={self.echo});"
f"wexpect.ConsoleReaderSocket(wexpect.join_args({args}), {pid}, port={self.port}, local_echo={self.echo}, interact={self.interact});"
"wexpect.console_reader.logger.info('Console finished2.');"
)