Example #1
0
def run_interactive(case_func, func):
    g = Manager().Namespace()

    tempdir = TemporaryDirectory()
    g.file_a = os.path.join(tempdir.name, 'a')
    g.file_b = os.path.join(tempdir.name, 'b')
    os.mkfifo(g.file_a)
    os.mkfifo(g.file_b)

    log_file = NamedTemporaryFile()
    g.log = log_file.name

    g.success = None
    g.error = ''

    def func_wrapper(_func):
        def inner():
            with open(g.file_a,
                      'r') as _file_a, open(g.file_b,
                                            'w') as _file_b, open(g.log,
                                                                  'a') as log:
                _stdin, _stdout = sys.stdin, sys.stdout
                sys.stdin, sys.stdout = _file_a, MultiWriterProxy(_file_b, log)
                _func()
                sys.stdin, sys.stdout = _stdin, _stdout

        return inner

    def case_wrapper(_case):
        def inner():
            func_process = Process(target=func_wrapper(func))
            func_process.start()
            with open(g.file_a,
                      'w') as _file_a, open(g.file_b,
                                            'r') as _file_b, open(g.log,
                                                                  'a') as log:
                _stdin, _stdout = sys.stdin, sys.stdout
                sys.stdin, sys.stdout = _file_b, MultiWriterProxy(_file_a, log)
                result = _case()
                sys.stdin, sys.stdout = _stdin, _stdout
                if result is True:
                    g.success = True
                else:
                    g.success = False
                    g.error = result
                if func_process.is_alive():
                    func_process.kill()

        return inner

    case_process = Process(target=case_wrapper(case_func))

    case_process.start()
    case_process.join()

    if g.success:
        print('AC')
    else:
        print('WA')
        print(g.error)

        log_file.seek(0)
        print('log:')
        print(TextIOWrapper(log_file).read())

    os.unlink(g.file_a)
    os.unlink(g.file_b)
    log_file.close()
    tempdir.cleanup()