#!/usr/bin/python3 """Just write stdin to a temporary file, and then use gprog on it.""" import os import sys import tempfile import subprocess def usage(exit_code): """Output a usage message.""" if exit_code == 0: w = sys.stdout.write else: w = sys.stderr.write w(f'Usage: {sys.argv[0]} arg1togprog arg2togprog .. argntogprog\n') w('\n') w('Just write stdin to a temporary file, and then start gprog on it. Output goes to stdout.\n') w('This is really only useful when writing from gprog to a slow device or socket.\n') sys.exit(exit_code) while sys.argv[1:]: if sys.argv[1] in ('--help', '-h'): usage(0) else: print(f'{sys.argv[0]}: unrecognized option: {sys.argv[1]}', file=sys.stderr) usage(1) buffer_length = 2**28 (tmp_file, tmp_filename) = tempfile.mkstemp() while True: buffer = os.read(0, buffer_length) if not buffer: break os.write(tmp_file, buffer) os.close(tmp_file) with open(tmp_filename, 'rb') as file_: output = subprocess.Popen(["gprog"] + sys.argv[1:], stdin=file_).communicate()[0] os.unlink(tmp_filename)