run-and-prefix.py (935B)
1 #!/usr/bin/env python 2 # This Source Code Form is subject to the terms of the Mozilla Public 3 # License, v. 2.0. If a copy of the MPL was not distributed with this 4 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 6 # This script runs a process and prefixes its output with. 7 # Usage: run-and-prefix.py prefix command arg0 argv1... 8 9 import os 10 import subprocess 11 import sys 12 13 sys.stdout = os.fdopen(sys.stdout.fileno(), "wb", 0) 14 sys.stderr = os.fdopen(sys.stderr.fileno(), "wb", 0) 15 16 prefix = sys.argv[1].encode("utf-8") 17 args = sys.argv[2:] 18 19 p = subprocess.Popen( 20 args, 21 bufsize=0, 22 stdout=subprocess.PIPE, 23 stderr=subprocess.STDOUT, 24 stdin=sys.stdin.fileno(), 25 close_fds=False, 26 ) 27 28 while True: 29 data = p.stdout.readline() 30 31 if data == b"": 32 break 33 34 if data.startswith(b"BUILDSTATUS"): 35 sys.stdout.write(data) 36 else: 37 sys.stdout.write(b"%s> %s" % (prefix, data)) 38 39 sys.exit(p.wait())