Created
December 6, 2019 08:12
-
-
Save meteozond/9ce6732eb3594072bf865da87e90957a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from subprocess import PIPE, STDOUT, Popen | |
bash_commands = sys.argv[1:] | |
procs = [] | |
for n, cmd in enumerate(bash_commands): | |
kwargs = { | |
'stdout': PIPE, | |
'env': os.environ.copy() | |
} | |
if n == 0: | |
kwargs['stdin'] = PIPE | |
proc = Popen(['bash', '-c', cmd], **kwargs) | |
procs.append(proc) | |
exited = 0 | |
while exited < len(procs) - 1: | |
exited = 0 | |
for n, proc in enumerate(procs): | |
returncode = proc.poll() | |
if returncode is not None: | |
exited += 1 | |
if returncode > 0: | |
for sproc in procs: | |
if sproc.returncode is None: | |
sproc.terminate() | |
sys.exit(returncode) | |
next_proc = None | |
if n + 1 < len(procs): | |
next_proc = proc[n] | |
for line in iter(proc.stdout.readline, b''): | |
if next_proc: | |
next_proc.communicate(input=line) | |
else: | |
sys.stdout.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment