#!/usr/bin/env python3
# runs an executable under a pty, to capture its raw output.
# SPDX-FileCopyrightText: Chris Pressey, the original author of this work, has dedicated it to the public domain.
# For more information, please refer to <https://unlicense.org/>
# SPDX-License-Identifier: Unlicense
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)