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. 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, 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, **kwargs): 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. """Initialize the console starts the child in it and reads the console periodically.
Args: Args:
@ -117,6 +117,16 @@ class ConsoleReaderBase:
except Exception as e: except Exception as e:
logger.info(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: try:
self.create_connection(**kwargs) self.create_connection(**kwargs)
logger.info('Spawning %s' % path) logger.info('Spawning %s' % path)
@ -378,7 +388,6 @@ class ConsoleReaderBase:
logger.spam('cursor: %r, current: %r' % (cursorPos, self.__currentReadCo)) logger.spam('cursor: %r, current: %r' % (cursorPos, self.__currentReadCo))
raw = self.readConsole(self.__currentReadCo, cursorPos) raw = self.readConsole(self.__currentReadCo, cursorPos)
logger.debug('Read: %r' % raw)
rawlist = [] rawlist = []
while raw: while raw:
rawlist.append(raw[:self.__consSize.X]) rawlist.append(raw[:self.__consSize.X])
@ -409,6 +418,8 @@ class ConsoleReaderBase:
# Detect changed lines # Detect changed lines
self.__buffer.seek(pos) self.__buffer.seek(pos)
buf = self.__buffer.read() buf = self.__buffer.read()
logger.debug('buf: %r' % buf)
logger.debug('raw: %r' % raw)
if raw.startswith(buf): if raw.startswith(buf):
# Line has grown # Line has grown
rawslice = raw[len(buf):] rawslice = raw[len(buf):]

View File

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