mirror of
https://github.com/clearml/wexpect-venv
synced 2025-05-12 07:50:58 +00:00
[MRG] wexpect2 merged, the default is the legacy
This commit is contained in:
commit
1fc42b5b84
@ -12,7 +12,8 @@ child = wexpect.spawn('cmd.exe')
|
||||
child.expect('>')
|
||||
|
||||
# Prints the cmd's start message
|
||||
print(child.before)
|
||||
print(child.before, end='')
|
||||
print(child.after, end='')
|
||||
|
||||
# run list directory command
|
||||
child.sendline('ls')
|
||||
@ -21,7 +22,8 @@ child.sendline('ls')
|
||||
child.expect('>')
|
||||
|
||||
# Prints content of the directory
|
||||
print(child.before)
|
||||
print(child.before, end='')
|
||||
print(child.after, end='')
|
||||
|
||||
# Exit from cmd
|
||||
child.sendline('exit')
|
||||
|
@ -1 +1,2 @@
|
||||
pywin32>=220
|
||||
psutil>=5.0.0
|
||||
|
@ -1,14 +1,48 @@
|
||||
from .legacy_wexpect import ExceptionPexpect
|
||||
from .legacy_wexpect import EOF
|
||||
from .legacy_wexpect import TIMEOUT
|
||||
from .legacy_wexpect import spawn
|
||||
from .legacy_wexpect import run
|
||||
from .legacy_wexpect import split_command_line
|
||||
from .legacy_wexpect import join_args
|
||||
from .legacy_wexpect import ConsoleReader
|
||||
from .legacy_wexpect import __version__
|
||||
from .legacy_wexpect import searcher_string
|
||||
from .legacy_wexpect import searcher_re
|
||||
# __init__.py
|
||||
|
||||
__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'run', 'split_command_line',
|
||||
'__version__', 'ConsoleReader', 'join_args', 'searcher_string', 'searcher_re']
|
||||
import os
|
||||
|
||||
try:
|
||||
spawn_class_name = os.environ['WEXPECT_SPAWN_CLASS']
|
||||
except KeyError:
|
||||
spawn_class_name = 'legacy_wexpect'
|
||||
|
||||
if spawn_class_name == 'legacy_wexpect':
|
||||
from .legacy_wexpect import ExceptionPexpect
|
||||
from .legacy_wexpect import EOF
|
||||
from .legacy_wexpect import TIMEOUT
|
||||
from .legacy_wexpect import spawn
|
||||
from .legacy_wexpect import run
|
||||
from .legacy_wexpect import split_command_line
|
||||
from .legacy_wexpect import join_args
|
||||
from .legacy_wexpect import ConsoleReader
|
||||
from .legacy_wexpect import __version__
|
||||
from .legacy_wexpect import searcher_string
|
||||
from .legacy_wexpect import searcher_re
|
||||
|
||||
__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'run', 'split_command_line',
|
||||
'__version__', 'ConsoleReader', 'join_args', 'searcher_string', 'searcher_re']
|
||||
|
||||
else:
|
||||
|
||||
from .wexpect_util import split_command_line
|
||||
from .wexpect_util import join_args
|
||||
from .wexpect_util import ExceptionPexpect
|
||||
from .wexpect_util import EOF
|
||||
from .wexpect_util import TIMEOUT
|
||||
|
||||
from .console_reader import ConsoleReaderSocket
|
||||
from .console_reader import ConsoleReaderPipe
|
||||
|
||||
from .host import SpawnSocket
|
||||
from .host import SpawnPipe
|
||||
from .host import run
|
||||
|
||||
try:
|
||||
spawn = globals()[spawn_class_name]
|
||||
except KeyError:
|
||||
print(f'Error: no spawn class: {spawn_class_name}')
|
||||
raise
|
||||
|
||||
__all__ = ['split_command_line', 'join_args', 'ExceptionPexpect', 'EOF', 'TIMEOUT',
|
||||
'ConsoleReaderSocket', 'ConsoleReaderPipe', 'spawn', 'SpawnSocket', 'SpawnPipe', 'run']
|
||||
|
47
wexpect/__init__.py.orig
Normal file
47
wexpect/__init__.py.orig
Normal file
@ -0,0 +1,47 @@
|
||||
<<<<<<< HEAD
|
||||
from .legacy_wexpect import ExceptionPexpect
|
||||
from .legacy_wexpect import EOF
|
||||
from .legacy_wexpect import TIMEOUT
|
||||
from .legacy_wexpect import spawn
|
||||
from .legacy_wexpect import run
|
||||
from .legacy_wexpect import split_command_line
|
||||
from .legacy_wexpect import join_args
|
||||
from .legacy_wexpect import ConsoleReader
|
||||
from .legacy_wexpect import __version__
|
||||
from .legacy_wexpect import searcher_string
|
||||
from .legacy_wexpect import searcher_re
|
||||
|
||||
__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'run', 'split_command_line',
|
||||
'__version__', 'ConsoleReader', 'join_args', 'searcher_string', 'searcher_re']
|
||||
=======
|
||||
# __init__.py
|
||||
|
||||
import os
|
||||
|
||||
from .wexpect_util import split_command_line
|
||||
from .wexpect_util import join_args
|
||||
from .wexpect_util import ExceptionPexpect
|
||||
from .wexpect_util import EOF
|
||||
from .wexpect_util import TIMEOUT
|
||||
|
||||
from .console_reader import ConsoleReaderSocket
|
||||
from .console_reader import ConsoleReaderPipe
|
||||
|
||||
from .host import SpawnSocket
|
||||
from .host import SpawnPipe
|
||||
from .host import run
|
||||
|
||||
try:
|
||||
spawn_class_name = os.environ['WEXPECT_SPAWN_CLASS']
|
||||
try:
|
||||
spawn = globals()[spawn_class_name]
|
||||
except KeyError:
|
||||
print(f'Error: no spawn class: {spawn_class_name}')
|
||||
print('Using SpawnSocket.')
|
||||
spawn = SpawnSocket
|
||||
except KeyError:
|
||||
spawn = SpawnSocket
|
||||
|
||||
__all__ = ['split_command_line', 'join_args', 'ExceptionPexpect', 'EOF', 'TIMEOUT',
|
||||
'ConsoleReaderSocket', 'ConsoleReaderPipe', 'spawn', 'SpawnSocket', 'SpawnPipe', 'run']
|
||||
>>>>>>> wexpect2
|
531
wexpect/console_reader.py
Normal file
531
wexpect/console_reader.py
Normal file
@ -0,0 +1,531 @@
|
||||
"""Wexpect is a Windows variant of pexpect https://pexpect.readthedocs.io.
|
||||
|
||||
Wexpect is a Python module for spawning child applications and controlling
|
||||
them automatically.
|
||||
|
||||
console_reader Implements a virtual terminal, and starts the child program.
|
||||
The main wexpect.Spawn class connect to this class to reach the child's terminal.
|
||||
|
||||
Credits: Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett,
|
||||
Robert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids
|
||||
vander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin,
|
||||
Geoffrey Marshall, Francisco Lourenco, Glen Mabey, Karthik Gurusamy, Fernando
|
||||
Perez, Corey Minyard, Jon Cohen, Guillaume Chazarain, Andrew Ryan, Nick
|
||||
Craig-Wood, Andrew Stone, Jorgen Grahn, Benedek Racz
|
||||
|
||||
Free, open source, and all that good stuff.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
Wexpect Copyright (c) 2019 Benedek Racz
|
||||
|
||||
"""
|
||||
|
||||
import time
|
||||
import logging
|
||||
import os
|
||||
import traceback
|
||||
import pkg_resources
|
||||
import psutil
|
||||
from io import StringIO
|
||||
|
||||
import ctypes
|
||||
import win32console
|
||||
import win32process
|
||||
import win32con
|
||||
import win32file
|
||||
import win32gui
|
||||
import win32pipe
|
||||
import socket
|
||||
|
||||
from .wexpect_util import init_logger
|
||||
from .wexpect_util import EOF_CHAR
|
||||
|
||||
#
|
||||
# System-wide constants
|
||||
#
|
||||
screenbufferfillchar = '\4'
|
||||
maxconsoleY = 8000
|
||||
default_port = 4321
|
||||
|
||||
# The version is handled by the package: pbr, which derives the version from the git tags.
|
||||
try:
|
||||
__version__ = pkg_resources.require("wexpect")[0].version
|
||||
except: # pragma: no cover
|
||||
__version__ = '0.0.1.unkowndev0'
|
||||
|
||||
#
|
||||
# Create logger: We write logs only to file. Printing out logs are dangerous, because of the deep
|
||||
# console manipulation.
|
||||
#
|
||||
logger = logging.getLogger('wexpect')
|
||||
|
||||
init_logger(logger)
|
||||
|
||||
class ConsoleReaderBase:
|
||||
"""Consol class (aka. client-side python class) for the child.
|
||||
|
||||
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):
|
||||
"""Initialize the console starts the child in it and reads the console periodically.
|
||||
|
||||
Args:
|
||||
path (str): Child's executable with arguments.
|
||||
parent_pid (int): Parent (aka. host) process process-ID
|
||||
cp (:obj:, optional): Output console code page.
|
||||
"""
|
||||
self.lastRead = 0
|
||||
self.__bufferY = 0
|
||||
self.lastReadData = ""
|
||||
self.totalRead = 0
|
||||
self.__buffer = StringIO()
|
||||
self.__currentReadCo = win32console.PyCOORDType(0, 0)
|
||||
self.pipe = None
|
||||
self.connection = None
|
||||
self.consin = None
|
||||
self.consout = None
|
||||
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
|
||||
|
||||
logger.info("ConsoleReader started")
|
||||
|
||||
if cp:
|
||||
try:
|
||||
logger.info("Setting console output code page to %s" % cp)
|
||||
win32console.SetConsoleOutputCP(cp)
|
||||
logger.info("Console output code page: %s" % ctypes.windll.kernel32.GetConsoleOutputCP())
|
||||
except Exception as e:
|
||||
logger.info(e)
|
||||
|
||||
try:
|
||||
self.create_connection(**kwargs)
|
||||
logger.info('Spawning %s' % path)
|
||||
try:
|
||||
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)
|
||||
|
||||
except:
|
||||
logger.info(traceback.format_exc())
|
||||
return
|
||||
|
||||
if interact:
|
||||
self.interact()
|
||||
self.interact()
|
||||
|
||||
self.read_loop()
|
||||
except:
|
||||
logger.error(traceback.format_exc())
|
||||
time.sleep(.1)
|
||||
finally:
|
||||
try:
|
||||
self.terminate_child()
|
||||
time.sleep(.1)
|
||||
self.send_to_host(self.readConsoleToCursor())
|
||||
self.sendeof()
|
||||
time.sleep(.1)
|
||||
self.close_connection()
|
||||
logger.info('Console finished.')
|
||||
except:
|
||||
logger.error(traceback.format_exc())
|
||||
time.sleep(.1)
|
||||
|
||||
|
||||
def read_loop(self):
|
||||
paused = False
|
||||
|
||||
while True:
|
||||
if not self.isalive(self.host_process):
|
||||
logger.info('Host process has been died.')
|
||||
return
|
||||
|
||||
if win32process.GetExitCodeProcess(self.__childProcess) != win32con.STILL_ACTIVE:
|
||||
logger.info('Child finished.')
|
||||
return
|
||||
|
||||
consinfo = self.consout.GetConsoleScreenBufferInfo()
|
||||
cursorPos = consinfo['CursorPosition']
|
||||
self.send_to_host(self.readConsoleToCursor())
|
||||
s = self.get_from_host()
|
||||
if s:
|
||||
logger.debug(f'get_from_host: {s}')
|
||||
else:
|
||||
logger.spam(f'get_from_host: {s}')
|
||||
self.write(s)
|
||||
|
||||
if cursorPos.Y > maxconsoleY and not paused:
|
||||
logger.info('cursorPos %s' % cursorPos)
|
||||
self.suspendThread()
|
||||
paused = True
|
||||
|
||||
if cursorPos.Y <= maxconsoleY and paused:
|
||||
logger.info('cursorPos %s' % cursorPos)
|
||||
self.resumeThread()
|
||||
paused = False
|
||||
|
||||
time.sleep(.02)
|
||||
|
||||
def terminate_child(self):
|
||||
try:
|
||||
if self.child_process:
|
||||
self.child_process.kill()
|
||||
except psutil.NoSuchProcess:
|
||||
logger.info('The process has already died.')
|
||||
return
|
||||
|
||||
def isalive(self, process):
|
||||
"""True if the child is still alive, false otherwise"""
|
||||
try:
|
||||
process.wait(timeout=0)
|
||||
return False
|
||||
except psutil.TimeoutExpired:
|
||||
return True
|
||||
|
||||
def write(self, s):
|
||||
"""Writes input into the child consoles input buffer."""
|
||||
|
||||
if len(s) == 0:
|
||||
return 0
|
||||
if s[-1] == '\n':
|
||||
s = s[:-1]
|
||||
records = [self.createKeyEvent(c) for c in str(s)]
|
||||
if not self.consout:
|
||||
return ""
|
||||
|
||||
# Store the current cursor position to hide characters in local echo disabled mode (workaround).
|
||||
consinfo = self.consout.GetConsoleScreenBufferInfo()
|
||||
startCo = consinfo['CursorPosition']
|
||||
|
||||
# Send the string to console input
|
||||
wrote = self.consin.WriteConsoleInput(records)
|
||||
|
||||
# Wait until all input has been recorded by the console.
|
||||
ts = time.time()
|
||||
while self.consin.PeekConsoleInput(8) != ():
|
||||
if time.time() > ts + len(s) * .1 + .5:
|
||||
break
|
||||
time.sleep(.05)
|
||||
|
||||
# Hide characters in local echo disabled mode (workaround).
|
||||
if not self.local_echo:
|
||||
self.consout.FillConsoleOutputCharacter(screenbufferfillchar, len(s), startCo)
|
||||
|
||||
return wrote
|
||||
|
||||
def createKeyEvent(self, char):
|
||||
"""Creates a single key record corrosponding to
|
||||
the ascii character char."""
|
||||
|
||||
evt = win32console.PyINPUT_RECORDType(win32console.KEY_EVENT)
|
||||
evt.KeyDown = True
|
||||
evt.Char = char
|
||||
evt.RepeatCount = 1
|
||||
return evt
|
||||
|
||||
def initConsole(self, consout=None, window_size_x=80, window_size_y=25, buffer_size_x=80,
|
||||
buffer_size_y=16000):
|
||||
if not consout:
|
||||
consout=self.getConsoleOut()
|
||||
|
||||
self.consin = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)
|
||||
|
||||
rect = win32console.PySMALL_RECTType(0, 0, window_size_x-1, window_size_y-1)
|
||||
consout.SetConsoleWindowInfo(True, rect)
|
||||
size = win32console.PyCOORDType(buffer_size_x, buffer_size_y)
|
||||
consout.SetConsoleScreenBufferSize(size)
|
||||
pos = win32console.PyCOORDType(0, 0)
|
||||
# Use NUL as fill char because it displays as whitespace
|
||||
# (if we interact() with the child)
|
||||
consout.FillConsoleOutputCharacter(screenbufferfillchar, size.X * size.Y, pos)
|
||||
|
||||
consinfo = consout.GetConsoleScreenBufferInfo()
|
||||
self.__consSize = consinfo['Size']
|
||||
logger.info('self.__consSize: ' + str(self.__consSize))
|
||||
self.startCursorPos = consinfo['CursorPosition']
|
||||
|
||||
def parseData(self, s):
|
||||
"""Ensures that special characters are interpretted as
|
||||
newlines or blanks, depending on if there written over
|
||||
characters or screen-buffer-fill characters."""
|
||||
|
||||
strlist = []
|
||||
for i, c in enumerate(s):
|
||||
if c == screenbufferfillchar:
|
||||
if (self.totalRead - self.lastRead + i + 1) % self.__consSize.X == 0:
|
||||
strlist.append('\r\n')
|
||||
else:
|
||||
strlist.append(c)
|
||||
|
||||
s = ''.join(strlist)
|
||||
return s
|
||||
|
||||
def getConsoleOut(self):
|
||||
consfile = win32file.CreateFile('CONOUT$',
|
||||
win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
|
||||
None,
|
||||
win32con.OPEN_EXISTING,
|
||||
0,
|
||||
0)
|
||||
|
||||
self.consout = win32console.PyConsoleScreenBufferType(consfile)
|
||||
return self.consout
|
||||
|
||||
def getCoord(self, offset):
|
||||
"""Converts an offset to a point represented as a tuple."""
|
||||
|
||||
x = offset % self.__consSize.X
|
||||
y = offset // self.__consSize.X
|
||||
return win32console.PyCOORDType(x, y)
|
||||
|
||||
def getOffset(self, coord):
|
||||
"""Converts a tuple-point to an offset."""
|
||||
|
||||
return coord.X + coord.Y * self.__consSize.X
|
||||
|
||||
def readConsole(self, startCo, endCo):
|
||||
"""Reads the console area from startCo to endCo and returns it
|
||||
as a string."""
|
||||
|
||||
if startCo is None:
|
||||
startCo = self.startCursorPos
|
||||
startCo.Y = startCo.Y
|
||||
|
||||
if endCo is None:
|
||||
consinfo = self.consout.GetConsoleScreenBufferInfo()
|
||||
endCo = consinfo['CursorPosition']
|
||||
endCo= self.getCoord(0 + self.getOffset(endCo))
|
||||
|
||||
buff = []
|
||||
self.lastRead = 0
|
||||
|
||||
while True:
|
||||
startOff = self.getOffset(startCo)
|
||||
endOff = self.getOffset(endCo)
|
||||
readlen = endOff - startOff
|
||||
|
||||
if readlen <= 0:
|
||||
break
|
||||
|
||||
if readlen > 4000:
|
||||
readlen = 4000
|
||||
endPoint = self.getCoord(startOff + readlen)
|
||||
|
||||
s = self.consout.ReadConsoleOutputCharacter(readlen, startCo)
|
||||
self.lastRead += len(s)
|
||||
self.totalRead += len(s)
|
||||
buff.append(s)
|
||||
|
||||
startCo = endPoint
|
||||
|
||||
return ''.join(buff)
|
||||
|
||||
def readConsoleToCursor(self):
|
||||
"""Reads from the current read position to the current cursor
|
||||
position and inserts the string into self.__buffer."""
|
||||
|
||||
if not self.consout:
|
||||
return ""
|
||||
|
||||
consinfo = self.consout.GetConsoleScreenBufferInfo()
|
||||
cursorPos = consinfo['CursorPosition']
|
||||
|
||||
logger.spam('cursor: %r, current: %r' % (cursorPos, self.__currentReadCo))
|
||||
|
||||
isSameX = cursorPos.X == self.__currentReadCo.X
|
||||
isSameY = cursorPos.Y == self.__currentReadCo.Y
|
||||
isSamePos = isSameX and isSameY
|
||||
|
||||
logger.spam('isSameY: %r' % isSameY)
|
||||
logger.spam('isSamePos: %r' % isSamePos)
|
||||
|
||||
if isSameY or not self.lastReadData.endswith('\r\n'):
|
||||
# Read the current slice again
|
||||
self.totalRead -= self.lastRead
|
||||
self.__currentReadCo.X = 0
|
||||
self.__currentReadCo.Y = self.__bufferY
|
||||
|
||||
logger.spam('cursor: %r, current: %r' % (cursorPos, self.__currentReadCo))
|
||||
|
||||
raw = self.readConsole(self.__currentReadCo, cursorPos)
|
||||
rawlist = []
|
||||
while raw:
|
||||
rawlist.append(raw[:self.__consSize.X])
|
||||
raw = raw[self.__consSize.X:]
|
||||
raw = ''.join(rawlist)
|
||||
s = self.parseData(raw)
|
||||
for i, line in enumerate(reversed(rawlist)):
|
||||
if line.endswith(screenbufferfillchar):
|
||||
# Record the Y offset where the most recent line break was detected
|
||||
self.__bufferY += len(rawlist) - i
|
||||
break
|
||||
|
||||
logger.spam('lastReadData: %r' % self.lastReadData)
|
||||
if s:
|
||||
logger.debug('Read: %r' % s)
|
||||
else:
|
||||
logger.spam('Read: %r' % s)
|
||||
|
||||
if isSamePos and self.lastReadData == s:
|
||||
logger.spam('isSamePos and self.lastReadData == s')
|
||||
s = ''
|
||||
|
||||
if s:
|
||||
lastReadData = self.lastReadData
|
||||
pos = self.getOffset(self.__currentReadCo)
|
||||
self.lastReadData = s
|
||||
if isSameY or not lastReadData.endswith('\r\n'):
|
||||
# Detect changed lines
|
||||
self.__buffer.seek(pos)
|
||||
buf = self.__buffer.read()
|
||||
if raw.startswith(buf):
|
||||
# Line has grown
|
||||
rawslice = raw[len(buf):]
|
||||
# Update last read bytes so line breaks can be detected in parseData
|
||||
lastRead = self.lastRead
|
||||
self.lastRead = len(rawslice)
|
||||
s = self.parseData(rawslice)
|
||||
self.lastRead = lastRead
|
||||
else:
|
||||
# Cursor has been repositioned
|
||||
s = '\r' + s
|
||||
self.__buffer.seek(pos)
|
||||
self.__buffer.truncate()
|
||||
self.__buffer.write(raw)
|
||||
|
||||
self.__currentReadCo.X = cursorPos.X
|
||||
self.__currentReadCo.Y = cursorPos.Y
|
||||
|
||||
return s
|
||||
|
||||
def interact(self):
|
||||
"""Displays the child console for interaction."""
|
||||
|
||||
logger.debug('Start interact window')
|
||||
win32gui.ShowWindow(win32console.GetConsoleWindow(), win32con.SW_SHOW)
|
||||
|
||||
def sendeof(self):
|
||||
"""This sends an EOF to the host. This sends a character which inform the host that child
|
||||
has been finished, and all of it's output has been send to host.
|
||||
"""
|
||||
|
||||
self.send_to_host(EOF_CHAR)
|
||||
|
||||
|
||||
class ConsoleReaderSocket(ConsoleReaderBase):
|
||||
|
||||
def create_connection(self, **kwargs):
|
||||
try:
|
||||
self.port = kwargs['port']
|
||||
# Create a TCP/IP socket
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
server_address = ('localhost', self.port)
|
||||
self.sock.bind(server_address)
|
||||
logger.info(f'Socket started at port: {self.port}')
|
||||
|
||||
# Listen for incoming connections
|
||||
self.sock.settimeout(5)
|
||||
self.sock.listen(1)
|
||||
self.connection, client_address = self.sock.accept()
|
||||
self.connection.settimeout(.01)
|
||||
logger.info(f'Client connected: {client_address}')
|
||||
except:
|
||||
logger.error(f"Port: {self.port}")
|
||||
raise
|
||||
|
||||
def close_connection(self):
|
||||
if self.connection:
|
||||
self.connection.close()
|
||||
|
||||
def send_to_host(self, msg):
|
||||
# convert to bytes
|
||||
if isinstance(msg, str):
|
||||
msg = str.encode(msg)
|
||||
if msg:
|
||||
logger.debug(f'Sending msg: {msg}')
|
||||
else:
|
||||
logger.spam(f'Sending msg: {msg}')
|
||||
self.connection.sendall(msg)
|
||||
|
||||
def get_from_host(self):
|
||||
try:
|
||||
msg = self.connection.recv(4096)
|
||||
except socket.timeout as e:
|
||||
err = e.args[0]
|
||||
# this next if/else is a bit redundant, but illustrates how the
|
||||
# timeout exception is setup
|
||||
if err == 'timed out':
|
||||
logger.debug('recv timed out, retry later')
|
||||
return ''
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
if len(msg) == 0:
|
||||
raise Exception('orderly shutdown on server end')
|
||||
else:
|
||||
# got a message do something :)
|
||||
return msg.decode()
|
||||
|
||||
|
||||
class ConsoleReaderPipe(ConsoleReaderBase):
|
||||
def create_connection(self, **kwargs):
|
||||
pipe_name = 'wexpect_{}'.format(self.console_pid)
|
||||
pipe_full_path = r'\\.\pipe\{}'.format(pipe_name)
|
||||
logger.info('Start pipe server: %s', pipe_full_path)
|
||||
self.pipe = win32pipe.CreateNamedPipe(
|
||||
pipe_full_path,
|
||||
win32pipe.PIPE_ACCESS_DUPLEX,
|
||||
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT,
|
||||
1, 65536, 65536, 0, None)
|
||||
logger.info("waiting for client")
|
||||
win32pipe.ConnectNamedPipe(self.pipe, None)
|
||||
logger.info('got client')
|
||||
|
||||
def close_connection(self):
|
||||
if self.pipe:
|
||||
win32file.CloseHandle(self.pipe)
|
||||
|
||||
def send_to_host(self, msg):
|
||||
# convert to bytes
|
||||
if isinstance(msg, str):
|
||||
msg = str.encode(msg)
|
||||
if msg:
|
||||
logger.debug(f'Sending msg: {msg}')
|
||||
else:
|
||||
logger.spam(f'Sending msg: {msg}')
|
||||
win32file.WriteFile(self.pipe, msg)
|
||||
|
||||
def get_from_host(self):
|
||||
data, avail, bytes_left = win32pipe.PeekNamedPipe(self.pipe, 4096)
|
||||
logger.spam(f'data: {data} avail:{avail} bytes_left{bytes_left}')
|
||||
if avail > 0:
|
||||
resp = win32file.ReadFile(self.pipe, 4096)
|
||||
ret = resp[1]
|
||||
return ret.decode()
|
||||
else:
|
||||
return ''
|
1193
wexpect/host.py
Normal file
1193
wexpect/host.py
Normal file
File diff suppressed because it is too large
Load Diff
1959
wexpect/legacy_wexpect.py.orig
Normal file
1959
wexpect/legacy_wexpect.py.orig
Normal file
File diff suppressed because it is too large
Load Diff
177
wexpect/wexpect_util.py
Normal file
177
wexpect/wexpect_util.py
Normal file
@ -0,0 +1,177 @@
|
||||
"""Wexpect is a Windows variant of pexpect https://pexpect.readthedocs.io.
|
||||
|
||||
Wexpect is a Python module for spawning child applications and controlling
|
||||
them automatically.
|
||||
|
||||
wexpect util contains small functions, and classes, which are used in multiple classes.
|
||||
The command line argument parsers, and the Exceptions placed here.
|
||||
|
||||
Credits: Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett,
|
||||
Robert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids
|
||||
vander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin,
|
||||
Geoffrey Marshall, Francisco Lourenco, Glen Mabey, Karthik Gurusamy, Fernando
|
||||
Perez, Corey Minyard, Jon Cohen, Guillaume Chazarain, Andrew Ryan, Nick
|
||||
Craig-Wood, Andrew Stone, Jorgen Grahn, Benedek Racz
|
||||
|
||||
Free, open source, and all that good stuff.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
Wexpect Copyright (c) 2019 Benedek Racz
|
||||
|
||||
"""
|
||||
|
||||
import re
|
||||
import traceback
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
|
||||
# platform does not define VEOF so assume CTRL-D
|
||||
EOF_CHAR = b'\x04'
|
||||
|
||||
SPAM = 5
|
||||
logging.addLevelName(SPAM, "SPAM")
|
||||
def spam(self, message, *args, **kws):
|
||||
if self.isEnabledFor(SPAM):
|
||||
# Yes, logger takes its '*args' as 'args'.
|
||||
self._log(SPAM, message, args, **kws)
|
||||
logging.Logger.spam = spam
|
||||
|
||||
def init_logger(logger):
|
||||
try:
|
||||
logger_level = os.environ['WEXPECT_LOGGER_LEVEL']
|
||||
try:
|
||||
logger_filename = os.environ['WEXPECT_LOGGER_FILENAME']
|
||||
except KeyError:
|
||||
pid = os.getpid()
|
||||
logger_filename = f'./.wlog/wexpect_{pid}'
|
||||
logger.setLevel(logger_level)
|
||||
logger_filename = f'{logger_filename}.log'
|
||||
os.makedirs(os.path.dirname(logger_filename), exist_ok=True)
|
||||
fh = logging.FileHandler(logger_filename, 'w', 'utf-8')
|
||||
formatter = logging.Formatter('%(asctime)s - %(filename)s:%(lineno)d - %(levelname)s - %(message)s')
|
||||
fh.setFormatter(formatter)
|
||||
logger.addHandler(fh)
|
||||
except KeyError:
|
||||
logger.setLevel(logging.ERROR)
|
||||
|
||||
def split_command_line(command_line, escape_char = '^'):
|
||||
"""This splits a command line into a list of arguments. It splits arguments
|
||||
on spaces, but handles embedded quotes, doublequotes, and escaped
|
||||
characters. It's impossible to do this with a regular expression, so I
|
||||
wrote a little state machine to parse the command line. """
|
||||
|
||||
arg_list = []
|
||||
arg = ''
|
||||
|
||||
# Constants to name the states we can be in.
|
||||
state_basic = 0
|
||||
state_esc = 1
|
||||
state_singlequote = 2
|
||||
state_doublequote = 3
|
||||
state_whitespace = 4 # The state of consuming whitespace between commands.
|
||||
state = state_basic
|
||||
|
||||
for c in command_line:
|
||||
if state == state_basic or state == state_whitespace:
|
||||
if c == escape_char: # Escape the next character
|
||||
state = state_esc
|
||||
elif c == r"'": # Handle single quote
|
||||
state = state_singlequote
|
||||
elif c == r'"': # Handle double quote
|
||||
state = state_doublequote
|
||||
elif c.isspace():
|
||||
# Add arg to arg_list if we aren't in the middle of whitespace.
|
||||
if state == state_whitespace:
|
||||
None # Do nothing.
|
||||
else:
|
||||
arg_list.append(arg)
|
||||
arg = ''
|
||||
state = state_whitespace
|
||||
else:
|
||||
arg = arg + c
|
||||
state = state_basic
|
||||
elif state == state_esc:
|
||||
arg = arg + c
|
||||
state = state_basic
|
||||
elif state == state_singlequote:
|
||||
if c == r"'":
|
||||
state = state_basic
|
||||
else:
|
||||
arg = arg + c
|
||||
elif state == state_doublequote:
|
||||
if c == r'"':
|
||||
state = state_basic
|
||||
else:
|
||||
arg = arg + c
|
||||
|
||||
if arg != '':
|
||||
arg_list.append(arg)
|
||||
return arg_list
|
||||
|
||||
def join_args(args):
|
||||
"""Joins arguments into a command line. It quotes all arguments that contain
|
||||
spaces or any of the characters ^!$%&()[]{}=;'+,`~"""
|
||||
commandline = []
|
||||
for arg in args:
|
||||
if re.search('[\^!$%&()[\]{}=;\'+,`~\s]', arg):
|
||||
arg = '"%s"' % arg
|
||||
commandline.append(arg)
|
||||
return ' '.join(commandline)
|
||||
|
||||
|
||||
class ExceptionPexpect(Exception):
|
||||
"""Base class for all exceptions raised by this module.
|
||||
"""
|
||||
|
||||
def __init__(self, value):
|
||||
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
|
||||
return str(self.value)
|
||||
|
||||
def get_trace(self):
|
||||
"""This returns an abbreviated stack trace with lines that only concern
|
||||
the caller. In other words, the stack trace inside the Wexpect module
|
||||
is not included. """
|
||||
|
||||
tblist = traceback.extract_tb(sys.exc_info()[2])
|
||||
tblist = [item for item in tblist if self.__filter_not_wexpect(item)]
|
||||
tblist = traceback.format_list(tblist)
|
||||
return ''.join(tblist)
|
||||
|
||||
def __filter_not_wexpect(self, trace_list_item):
|
||||
"""This returns True if list item 0 the string 'wexpect.py' in it. """
|
||||
|
||||
if trace_list_item[0].find('host.py') == -1:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
class EOF(ExceptionPexpect):
|
||||
"""Raised when EOF is read from a child. This usually means the child has exited.
|
||||
The user can wait to EOF, which means he waits the end of the execution of the child process."""
|
||||
|
||||
class TIMEOUT(ExceptionPexpect):
|
||||
"""Raised when a read time exceeds the timeout. """
|
||||
|
Loading…
Reference in New Issue
Block a user