verify.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:rp2017-codegolf 作者: ReflectionsProjections 项目源码 文件源码
def cpp_verify(data, user_file):
    answer = open(user_file).read()
    # write to temp file
    with open('answer.cpp', 'w') as temp:
        temp.write(answer)
    try:
        test = open('task/test.h').read()
        with open('test.h', 'w') as temp:
            temp.write(test)
    except IOError: # user has not requested task yet
        print("A task folder could not be found. Request a task\nusing python client.py request <task_id>")
        sys.exit(1) # terminate program
    proc = sp.Popen(['g++', 'answer.cpp', 'test.h', '-o', 'test'], stderr=sp.DEVNULL, stdout=sp.DEVNULL)
    proc.wait()
    if(proc.returncode):
        print("Answer failed to compile.")
        sys.exit(1)
    proc = sp.Popen(['./test'], stdin=sp.PIPE, stdout=sp.PIPE)
    test_result, _ = proc.communicate(input=data)
    # dispose of temporary files
    os.remove('answer.cpp')
    os.remove('test.h')
    os.remove('test')

    return test_result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号