Skip to content

Commit 38815cd

Browse files
committed
add timeout in problem model and testing
1 parent 1f6b55f commit 38815cd

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

judge/testing.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ def create_new_problem_from_template(new_problem_id: str, old_problem_id: str):
2828
new_path = PathManager.problem_dir(new_problem_id)
2929
os.makedirs(new_path, exist_ok=True)
3030
for filename in os.listdir(old_path):
31-
shutil.copy2(join(old_path, filename), new_path)
31+
filepath = join(old_path, filename)
32+
if isdir(filepath):
33+
continue
34+
shutil.copy2(filepath, new_path)
3235
print(f'create problem instance {new_path}')
3336

3437
def make_file_executable(file_path):
@@ -49,12 +52,12 @@ def make_file_executable(file_path):
4952
def run_command_with_timeout(command: list, timeout: float) -> int:
5053
try:
5154
# Use the 'sudo' command to execute the given command with elevated privileges.
52-
result = subprocess.run(['sudo'] + command, capture_output=True, text=True, check=True, timeout=timeout)
55+
result = subprocess.run(["/usr/bin/python3"] + command, capture_output=False, text=True, check=True, timeout=timeout)
5356
print("Command output:")
5457
print(result.stdout)
5558
return TestResult.Succeed
5659
except subprocess.TimeoutExpired:
57-
print('program timeout')
60+
print(f'program timeout after {timeout} seconds')
5861
return TestResult.Timeout
5962
except subprocess.CalledProcessError as e:
6063
print("Command execution failed.")
@@ -109,6 +112,9 @@ def check_files_in_dir(self, dir_path: str) -> bool:
109112
if "testcases.json" not in filenames:
110113
self._error_message = "testcases.json not in zipfile uploaded"
111114
return False
115+
if "logs" in filenames and isdir(join(dir_path, "logs")):
116+
self._error_message = "lab zip cannot include logs directory"
117+
return False
112118
return True
113119

114120
def upload(self) -> bool:
@@ -163,7 +169,10 @@ def __init__(self, submission: Submission):
163169
if not exists(prob_dir):
164170
raise Exception("problem dir {} not exists".format(prob_dir))
165171
for filename in os.listdir(prob_dir):
166-
shutil.copy2(join(prob_dir, filename), self.sub_dirpath)
172+
filepath = join(prob_dir, filename)
173+
if isdir(filepath):
174+
continue
175+
shutil.copy2(filepath, self.sub_dirpath)
167176
for index, codename in enumerate(problem.code_names):
168177
filecontent = submission.code_list[index]
169178
codepath = join(self.sub_dirpath, codename)
@@ -176,7 +185,7 @@ def judge(self):
176185
if not exists(tester_path):
177186
raise Exception("running submission: tester {} not exists".format(tester_path))
178187
print(f"running {tester_path}")
179-
res = run_command_with_timeout([tester_path, "--log", "--json"], 1)
188+
res = run_command_with_timeout([tester_path, "--log", "--json"], self.sub.problem.timeout)
180189
if res == TestResult.Timeout:
181190
self.sub.result = JudgeStatus.PROGRAM_TIMEOUT
182191
self.sub.save()

problem/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ class Problem(models.Model):
2222
# lab_config = models.JSONField(default=dict)
2323
is_public = models.BooleanField(default=True)
2424
title = models.TextField()
25+
timeout = models.IntegerField()
2526
# code segment filenames to be substituded
2627
description = RichTextField()
2728
# hint = RichTextField(null=True)
2829
languages = JSONField(default=["python"])
29-
#需要的节点数量
3030
vm_num = models.IntegerField(default=1)
31-
#各个节点所需要的端口数量
3231
port_num = models.JSONField(default=list)
33-
#学生需要编写的代码段数量
3432
code_num = models.IntegerField()
3533
code_names = models.JSONField()
3634
template = JSONField(null=True)

problem/views/admin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def post(self, request):
4141
for field in str_fields:
4242
problem_data[field] = request.POST.get(field)
4343
problem_data["code_num"] = int(request.POST.get("code_num"))
44+
problem_data["timeout"] = int(request.POST.get("timeout"))
4445
problem_data["code_names"] = request.POST.getlist("code_names")
4546
# print(problem_data)
4647
tags = request.POST.getlist("tags")
@@ -154,10 +155,7 @@ def post(self, request):
154155
if "description" not in data:
155156
data["description"] = problem.description
156157
data["visible"] = True
157-
if "hint" not in data:
158-
data["hint"] = problem.hint
159158
lab_config = data["lab_config"]
160-
data["lab_config"] = problem.lab_config
161159
if lab_config:
162160
for k, v in lab_config.items():
163161
if k in data["lab_config"].keys():
@@ -318,6 +316,7 @@ def post(self, request):
318316
data["is_public"] = True
319317
data["code_num"] = old_problem.code_num
320318
data["code_names"] = old_problem.code_names
319+
data["timeout"] = old_problem.timeout
321320

322321
tags = old_problem.tags.all()
323322
data["_id"] = data.pop("display_id")

run.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
cmd=$1
66
apps=("account" "announcement" "conf" "contest" "options" "problem" "submission")
77

8+
clean() {
9+
rm -rf ./data/zips
10+
rm -rf ./data/problems
11+
rm -rf ./data/submissions
12+
find . -type d -name 'migrations' -not -path './venv/*' | xargs rm -r;
13+
rm onl.db
14+
}
15+
816
if [[ $cmd == "make" || $cmd == "makemigrations" ]]; then
917
for app in "${apps[@]}"; do
1018
python3 manage.py makemigrations $app
@@ -13,14 +21,9 @@ elif [[ $cmd == "migrate" ]]; then
1321
python3 manage.py migrate
1422
python manage.py inituser --username=root --password=rootroot --action=create_super_admin
1523
elif [[ $cmd == "clean" ]]; then
16-
find . -type d -name 'migrations' -not -path './venv/*' | xargs rm -r;
17-
rm onl.db
24+
clean
1825
elif [[ $cmd == "rebuild" ]]; then
19-
rm -rf ./data/zips
20-
rm -rf ./data/problems
21-
rm -rf ./data/submissions
22-
find . -type d -name 'migrations' -not -path './venv/*' | xargs rm -r;
23-
rm onl.db
26+
clean
2427
for app in "${apps[@]}"; do
2528
python3 manage.py makemigrations $app
2629
done

0 commit comments

Comments
 (0)