From 76346421ff6d9043edd651a967653f0805463b30 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Tue, 11 Feb 2020 08:46:39 -0800 Subject: [PATCH] Switch to pexpect to allow multiple buf writes --- chonker.py | 30 ++++++++++++++++-------------- requirements.txt | 3 +++ 2 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 requirements.txt diff --git a/chonker.py b/chonker.py index 885ee6e..f8f1a49 100644 --- a/chonker.py +++ b/chonker.py @@ -1,11 +1,14 @@ # Standard library imports -from argparse import ArgumentParser, FileType +from argparse import ArgumentParser, FileType, ArgumentTypeError import logging -import shlex from signal import signal, SIGPIPE, SIG_DFL import subprocess import sys +# Third party imports +from pexpect import EOF +from pexpect.popen_spawn import PopenSpawn as spawn + # Configure SIGPIPE signal handler signal(SIGPIPE,SIG_DFL) @@ -142,17 +145,15 @@ def pipe_chunk_to_command(command, chunk): """ logger = logging.getLogger(__name__) logger.info(f"Piping chunk | {command}") - out = b"" - with subprocess.Popen(command, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) as process: - for buf in chunk: - outs, errs = process.communicate(input=buf) - out += outs - logger.info(out.decode('utf8')) - logger.debug(f"Process returned {process.returncode}") - return process + child = spawn(command) + child.logfile_read = sys.stdout.buffer + for buf in chunk: + child.send(buf) + child.sendeof() + child.expect(EOF) + retcode = child.wait() + logger.debug(f"Process returned {retcode}") + return retcode def main(): @@ -163,6 +164,7 @@ def main(): args = parse_args() logger = initialize_logging(args.verbose) + logger.debug(args) # Skip chunks to get to the beginning of the operating section 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 logger.debug("Reading chunks") 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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..13ef731 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +pexpect==4.8.0 +pkg-resources==0.0.0 +ptyprocess==0.6.0