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

tools/tee.py: 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,
close_fds=True)
for l in p.stdout:
for o in outputs:
o.write(l)
p.wait()
return p.returncode
if __name__ == '__main__':
args = parser.parse_args()
outputs = [sys.stdout]
print()
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)
sys.exit(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