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,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT) as process:
for buf in chunk: for buf in chunk:
outs, errs = process.communicate(input=buf) child.send(buf)
out += outs child.sendeof()
logger.info(out.decode('utf8')) child.expect(EOF)
logger.debug(f"Process returned {process.returncode}") retcode = child.wait()
return process logger.debug(f"Process returned {retcode}")
return retcode
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