Switch to pexpect to allow multiple buf writes

This commit is contained in:
Tim Van Baak 2020-02-11 08:46:39 -08:00
parent cbcb073dfd
commit 76346421ff
2 changed files with 19 additions and 14 deletions

View File

@ -1,11 +1,14 @@
# Standard library imports # Standard library imports
from argparse import ArgumentParser, FileType from argparse import ArgumentParser, FileType, ArgumentTypeError
import logging import logging
import shlex
from signal import signal, SIGPIPE, SIG_DFL from signal import signal, SIGPIPE, SIG_DFL
import subprocess import subprocess
import sys import sys
# Third party imports
from pexpect import EOF
from pexpect.popen_spawn import PopenSpawn as spawn
# Configure SIGPIPE signal handler # Configure SIGPIPE signal handler
signal(SIGPIPE,SIG_DFL) signal(SIGPIPE,SIG_DFL)
@ -142,17 +145,15 @@ def pipe_chunk_to_command(command, chunk):
""" """
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.info(f"Piping chunk | {command}") logger.info(f"Piping chunk | {command}")
out = b"" child = spawn(command)
with subprocess.Popen(command, child.logfile_read = sys.stdout.buffer
stdin=subprocess.PIPE, for buf in chunk:
stdout=subprocess.PIPE, child.send(buf)
stderr=subprocess.STDOUT) as process: child.sendeof()
for buf in chunk: child.expect(EOF)
outs, errs = process.communicate(input=buf) retcode = child.wait()
out += outs logger.debug(f"Process returned {retcode}")
logger.info(out.decode('utf8')) return retcode
logger.debug(f"Process returned {process.returncode}")
return process
def main(): def main():
@ -163,6 +164,7 @@ def main():
args = parse_args() args = parse_args()
logger = initialize_logging(args.verbose) logger = initialize_logging(args.verbose)
logger.debug(args)
# Skip chunks to get to the beginning of the operating section # Skip chunks to get to the beginning of the operating section
logger.debug(f"Skipping {args.skip} chunks") logger.debug(f"Skipping {args.skip} chunks")
@ -173,4 +175,4 @@ def main():
# Process each chunk in the section of the stream to operate on # Process each chunk in the section of the stream to operate on
logger.debug("Reading chunks") logger.debug("Reading chunks")
for chunk in input_chunks(sys.stdin.buffer, args.chunk, args.bufsize): for chunk in input_chunks(sys.stdin.buffer, args.chunk, args.bufsize):
pipe_chunk_to_command(shlex.split(args.exec), chunk) pipe_chunk_to_command(args.exec, chunk)

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
pexpect==4.8.0
pkg-resources==0.0.0
ptyprocess==0.6.0