summaryrefslogtreecommitdiffstats
path: root/bin/mixer-wrapper.py
blob: 1424f362a5028df891a4e30f2bd93164e3e13219 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/python2

import os
import select
import sys
import subprocess
import termios
import pty
from itertools import chain

def main(cmd=None):
    (pipe_in, pipe_out) = pty.openpty()
    pipe = os.fdopen(pipe_in)

    mixer = subprocess.Popen(sys.argv[1], stdin=subprocess.PIPE,
                             stdout=pipe_out, stderr=subprocess.STDOUT,
                             shell=True, bufsize=0, close_fds=True)

    input = iter(sys.stdin.readline, '')
    if cmd is not None:
        input = chain([cmd], input)

    for line in input:
        mixer.stdin.write(b"%s\n" % line)
        mixer.stdin.flush()
 
        termios.tcflush(pipe_in, termios.TCIFLUSH)
        fds = select.select([pipe_in], [], [], 0.2)[0]
        if len(fds) <= 0:
            mixer.stdin.close()
            mixer.wait()
            if cmd is None or line != cmd:
                return line
            return None


if __name__ == '__main__':
    cmd = None
    while True:
        cmd = main(cmd)