-
Notifications
You must be signed in to change notification settings - Fork 2
/
judge.py
116 lines (100 loc) · 3.09 KB
/
judge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import subprocess
import os
import requests
def copy_and_modify_template(judge_template_path, template_revise_path, code_path):
code_filename = os.path.basename(code_path)
# Construct the import line
import_line = f"from {code_filename.replace('.py', '')} import drawing\n"
# Read the original template
with open(judge_template_path, "r") as template_file:
template_content = template_file.read()
# Create the new content with the import line at the beginning
new_content = import_line + template_content
# new_content = template_content
# Write the new content to the destination path
with open(template_revise_path, "w") as new_template_file:
new_template_file.write(new_content)
# At here, change the main_template code and drawing template code
def run_code(
code_path,
image_url,
result_path,
team_id,
drawing_template_path,
main_drawing_path,
template_revise_path,
submission_id,
):
result_dir = os.path.dirname(result_path)
ps_file = f"{result_dir}/{submission_id}.ps"
if os.path.isfile(ps_file):
# Remove the file
os.remove(ps_file)
# png_file = f"{result_dir}/{output_filename}.png"
# Ensure the result directory exists
os.makedirs(result_dir, exist_ok=True)
copy_and_modify_template(drawing_template_path, template_revise_path, code_path)
print(f"template revise path: {template_revise_path}")
# Run the provided Python script to generate the PostScript file
# Use check to raise an exception if the script fails
try:
print(
"MAINDRAWING",
main_drawing_path,
ps_file,
submission_id,
code_path,
template_revise_path,
result_path,
image_url,
sep=" ",
)
p = subprocess.Popen(
[
"python3",
str(main_drawing_path),
str(ps_file),
str(submission_id),
str(code_path),
str(template_revise_path),
str(result_path),
str(image_url),
]
)
except subprocess.CalledProcessError as e:
error_data = {
"score": 0,
"fitness": 0,
"word_count": 0,
"execution_time": 0,
"stdout": "",
"stderr": "Process crashed with the following error message :\n\n" + e,
"status": "fail",
}
res = requests.post(
f"https://camp.mtkuo.space:2024/api/submission/store/{submission_id}/",
json=error_data,
)
print(f"Error: {e}")
def judge_submission(
code_path,
image_url,
result_path,
team_id,
drawing_template_path,
main_drawing_path,
template_revise_path,
submission_id,
):
image_url = f".{image_url}"
print(f"image_url: {image_url}\n\n")
run_code(
code_path,
image_url,
result_path,
team_id,
drawing_template_path,
main_drawing_path,
template_revise_path,
submission_id,
)