Commit 918dd37f authored by Mathieu Giraud's avatar Mathieu Giraud

tools/ tee an output both to stdout and file(s), preserving the exit code

parent 5cbe1ff2
Launches a command, copyies its output both to standard output and in zero or more files,
and finally exits with the same exit code of the command.
from __future__ import print_function
import sys
import subprocess
import shlex
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-v', '--verbose', action='store_true', default=False)
parser.add_argument('cmd', help='''command to run''')
parser.add_argument('file', nargs='*', help='''output file(s)''')
def tee(cmd, outputs):
'''Runs 'cmd', pipe the output to 'outputs', and returns the exit code of 'cmd' '''
argz = shlex.split(cmd)
p = subprocess.Popen(argz,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
for l in p.stdout:
for o in outputs:
return p.returncode
if __name__ == '__main__':
args = parser.parse_args()
outputs = [sys.stdout]
for f in args.file:
print('==> %s' % f)
outputs += [open(f, 'w')]
if args.verbose:
print('### %s' % args.cmd)
ex = tee(args.cmd, outputs)
if args.verbose:
print('##> %s' % ex)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment