Commit 8ca8504d authored by 王宇航's avatar 王宇航

Harden Lab2 evaluation scripts

parent 7926be65
#!/usr/bin/env python3 #!/usr/bin/env python3
from pathlib import Path
import subprocess import subprocess
# 17 # 17
lv0_1 = { lv0_1 = {
...@@ -110,6 +111,11 @@ suite = [ ...@@ -110,6 +111,11 @@ suite = [
] ]
def cleanup_case(test_path):
for path in (test_path, test_path + ".o", test_path + ".ll"):
Path(path).unlink(missing_ok=True)
def eval(): def eval():
f = open("eval_result", 'w') f = open("eval_result", 'w')
EXE_PATH = "../../../build/cminusfc" EXE_PATH = "../../../build/cminusfc"
...@@ -129,6 +135,7 @@ def eval(): ...@@ -129,6 +135,7 @@ def eval():
ANSWER_PATH = ANSWER_BASE_PATH + level_name + "/" + case ANSWER_PATH = ANSWER_BASE_PATH + level_name + "/" + case
score = cases[case][0] score = cases[case][0]
need_input = cases[case][1] need_input = cases[case][1]
cleanup_case(TEST_PATH)
COMMAND = [TEST_PATH] COMMAND = [TEST_PATH]
...@@ -136,12 +143,21 @@ def eval(): ...@@ -136,12 +143,21 @@ def eval():
result = subprocess.run([EXE_PATH, "-o", TEST_PATH + ".ll", "-emit-llvm", result = subprocess.run([EXE_PATH, "-o", TEST_PATH + ".ll", "-emit-llvm",
TEST_PATH + ".cminus"], stderr=subprocess.PIPE, timeout=1) TEST_PATH + ".cminus"], stderr=subprocess.PIPE, timeout=1)
except Exception as _: except Exception as _:
cleanup_case(TEST_PATH)
f.write('\tFail\n') f.write('\tFail\n')
continue continue
if result.returncode == 0: if result.returncode == 0:
subprocess.run(["clang", "-O0", "-w", "-no-pie", TEST_PATH + try:
compile_result = subprocess.run(["clang", "-O0", "-w", "-no-pie", TEST_PATH +
".ll", "-o", TEST_PATH, "-L", "../../../build", "-lcminus_io"]) ".ll", "-o", TEST_PATH, "-L", "../../../build", "-lcminus_io"])
except OSError:
compile_result = None
if compile_result is None or compile_result.returncode != 0:
f.write('\tFail\n')
has_bonus = False
cleanup_case(TEST_PATH)
continue
input_option = None input_option = None
if need_input: if need_input:
with open(ANSWER_PATH + ".in", "rb") as fin: with open(ANSWER_PATH + ".in", "rb") as fin:
...@@ -161,10 +177,10 @@ def eval(): ...@@ -161,10 +177,10 @@ def eval():
f.write('\tFail\n') f.write('\tFail\n')
has_bonus = False has_bonus = False
finally: finally:
subprocess.call( cleanup_case(TEST_PATH)
["rm", "-rf", TEST_PATH, TEST_PATH + ".o", TEST_PATH + ".ll"])
else: else:
cleanup_case(TEST_PATH)
f.write('\tFail\n') f.write('\tFail\n')
has_bonus = False has_bonus = False
......
#!/bin/bash #!/bin/bash
cd "$(dirname "$0")" || exit 1
python3 eval_lab2.py python3 eval_lab2.py
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment