This commit is contained in:
Benedek Racz 2020-02-03 16:13:02 +01:00
parent 6258e195e9
commit 751a511610
2 changed files with 35 additions and 24 deletions

View File

@ -80,8 +80,8 @@ 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, tid=None, cp=None, window_size_x=80, window_size_y=25,
buffer_size_x=80, buffer_size_y=16000, local_echo=True, interact=False, just_init=False, **kwargs):
"""Initialize the console starts the child in it and reads the console periodically.
Args:
@ -117,6 +117,16 @@ class ConsoleReaderBase:
except Exception as e:
logger.info(e)
if just_init:
self.initConsole()
si = win32process.GetStartupInfo()
self.__childProcess, _, self.child_pid, self.__tid = win32process.CreateProcess(None, path, None, None, False,
0, None, None, si)
self.child_process = psutil.Process(self.child_pid)
logger.info(f'Child pid: {self.child_pid} Console pid: {self.console_pid}')
time.sleep(5)
return
try:
self.create_connection(**kwargs)
logger.info('Spawning %s' % path)
@ -378,7 +388,6 @@ class ConsoleReaderBase:
logger.spam('cursor: %r, current: %r' % (cursorPos, self.__currentReadCo))
raw = self.readConsole(self.__currentReadCo, cursorPos)
logger.debug('Read: %r' % raw)
rawlist = []
while raw:
rawlist.append(raw[:self.__consSize.X])
@ -409,6 +418,8 @@ class ConsoleReaderBase:
# Detect changed lines
self.__buffer.seek(pos)
buf = self.__buffer.read()
logger.debug('buf: %r' % buf)
logger.debug('raw: %r' % raw)
if raw.startswith(buf):
# Line has grown
rawslice = raw[len(buf):]

View File

@ -1131,31 +1131,31 @@ class Wtty:
logger.info(f"Fetch child's process and pid...")
while True:
msg = win32gui.GetMessage(0, 0, 0)
childPid = msg[1][2]
# while True:
# msg = win32gui.GetMessage(0, 0, 0)
# childPid = msg[1][2]
# Sometimes win32gui.GetMessage returns a bogus PID, so keep calling it
# until we can successfully connect to the child or timeout is
# reached
if childPid:
try:
self.__childProcess = win32api.OpenProcess(
win32con.PROCESS_TERMINATE | win32con.PROCESS_QUERY_INFORMATION, False, childPid)
self.__conProcess = win32api.OpenProcess(
win32con.PROCESS_TERMINATE | win32con.PROCESS_QUERY_INFORMATION, False, self.conpid)
except pywintypes.error:
if time.time() > ts + self.timeout:
break
else:
self.pid = childPid
break
time.sleep(.05)
# if childPid:
# try:
# self.__childProcess = win32api.OpenProcess(
# win32con.PROCESS_TERMINATE | win32con.PROCESS_QUERY_INFORMATION, False, childPid)
self.__conProcess = win32api.OpenProcess(
win32con.PROCESS_TERMINATE | win32con.PROCESS_QUERY_INFORMATION, False, self.conpid)
# except pywintypes.error:
# if time.time() > ts + self.timeout:
# break
# else:
# self.pid = childPid
# break
# time.sleep(.05)
logger.info(f"Child's pid: {self.pid}")
if not self.__childProcess:
logger.info('ExceptionPexpect: The process ' + args[0] + ' could not be started.')
raise ExceptionPexpect ('The process ' + args[0] + ' could not be started.')
# if not self.__childProcess:
# logger.info('ExceptionPexpect: The process ' + args[0] + ' could not be started.')
# raise ExceptionPexpect ('The process ' + args[0] + ' could not be started.')
@ -1225,7 +1225,7 @@ class Wtty:
' '.join(pyargs),
f"import sys; sys.path = {spath} + sys.path;"
f"args = {args}; import wexpect;"
f"wexpect.ConsoleReader(wexpect.join_args(args), {pid}, {tid}, cp={cp}, logdir={logdir})"
f"wexpect.ConsoleReaderPipe(wexpect.join_args(args), {pid}, {tid}, cp={cp}, logdir={logdir}, just_init=True)"
)
logger.info(f'CreateProcess: {commandLine}')
@ -1604,7 +1604,7 @@ class Wtty:
finally:
self.switchBack()
def isalive(self, console=False):
def isalive(self, console=True):
"""True if the child is still alive, false otherwise"""
if console: