git @ Cat's Eye Technologies Falderal / master script / fa-under-pty
master

Tree @master (Download .tar.gz)

fa-under-pty @masterraw · history · blame

#!/usr/bin/env python3

import pty
import sys
import os


command = sys.argv[1]
args = sys.argv[2:]

(pid, fd) = pty.fork()
if pid == 0:
    # I'm the child
    os.execvp(command, [command] + args)
else:
    # I'm the parent
    accum = ''
    SIZE = 1024
    done = False
    f = os.fdopen(fd)
    s = f.read()
    f.close()
    (pid, exitcode) = os.waitpid(pid, 0)
    core_dumped = (exitcode & 128) == 128
    exitcode = exitcode >> 8
    if core_dumped and exitcode == 0:
        # force the exitcode to non-zero if we dumped core.
        exitcode = 1
    if exitcode == 0:
        sys.stdout.write(s)
    else:
        sys.stderr.write(s)
    sys.exit(exitcode)