Skip to content

Commit 7b274c4

Browse files
committed
use python shutil to manage files
1 parent dc31666 commit 7b274c4

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

judge/testing.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import zipfile
22
import os
3+
import shutil
34
import subprocess
45
from os.path import isdir, join, exists, dirname
56
from django.core.files.storage import FileSystemStorage
@@ -84,15 +85,20 @@ def upload(self) -> bool:
8485
addtional_dir = join(self.problem_dir_path, dir)
8586
valid = self.check_files_in_dir(addtional_dir)
8687
if valid:
87-
os.system(f"cp {addtional_dir}/* {addtional_dir}/..")
88-
os.system(f"rm -rf {addtional_dir}")
88+
print(f"extract to addtional dir {addtional_dir}")
89+
for filename in os.listdir(addtional_dir):
90+
shutil.move(join(addtional_dir, filename), self.problem_dir_path)
91+
shutil.rmtree(addtional_dir)
92+
print(f"remove {addtional_dir}")
8993
else:
9094
valid = self.check_files_in_dir(self.problem_dir_path)
9195

9296
if not valid:
9397
print(self._error_message)
94-
os.system(f"rm -rf {self.problem_dir_path}")
95-
os.system(f"rm -rf {dirname(self.zip_file_path)}")
98+
shutil.rmtree(self.problem_dir_path)
99+
print(f"remove {self.problem_dir_path}")
100+
shutil.rmtree(dirname(self.zip_file_path))
101+
print(f"remove {dirname(self.zip_file_path)}")
96102

97103
return valid
98104

@@ -103,10 +109,9 @@ def __init__(self, submit_id: str, problem_id: str):
103109
self.problem_id = problem_id
104110
self.submission_dir_path = PathManager.submission_dir(submit_id)
105111
if not exists(self.submission_dir_path):
106-
os.system(f"mkdir -p {self.submission_dir_path}")
112+
os.makedirs(self.submission_dir_path, exist_ok=True)
107113

108114
def judge(self):
109-
os.system(f"cp {PathManager.problem_dir(problem_id=self.problem_id)}/* {self.submission_dir_path}")
110115
tester = join(self.submission_dir_path, "tester")
111116
if not exists(tester):
112117
raise Exception(f"running submission: tester {tester} not exists")

0 commit comments

Comments
 (0)