#!/usr/bin/env python
#!-*-coding:utf-8-*-

import sys
import os
import pty
import select
import time
import subprocess

def run_with_pty(command):
    ret = 0

    if command:
        (master, slave) = pty.openpty()
        process = subprocess.Popen(command, stdin=slave, stdout=slave, stderr=slave, shell=True)
        while True:
            r, w, e = select.select([master], [], [], 0) # timeout of 0 means "poll"
            if r:
                line = os.read(master, 1024)
                #####
                # Warning, uncomment at your own risk - several programs
                # print empty lines that will cause this to break and
                # the output will be all goofed up.
                #if not line :
                #    break
                #print output.rstrip()
                os.write(1, line)
            elif process.poll() is not None :
                break
        os.close(master)
        os.close(slave)
        process.wait()
        ret = process.returncode

    return ret

def main():
    cmd = "runtest "
    for i in range(1, len(sys.argv)):
        cmd = cmd + sys.argv[i] + " "

    sys.exit(run_with_pty(cmd))

if __name__=="__main__":
    main()
