@@ -23,9 +23,34 @@ def create_new_problem_from_template(new_problem_id: str, old_problem_id: str):
23
23
new_path = PathManager .problem_dir (new_problem_id )
24
24
os .makedirs (new_path , exist_ok = True )
25
25
for filename in os .listdir (old_path ):
26
- shutil .copyfile (join (old_path , filename ), join ( new_path , filename ) )
26
+ shutil .copy2 (join (old_path , filename ), new_path )
27
27
print (f'create problem instance { new_path } ' )
28
28
29
+ def make_file_executable (file_path ):
30
+ try :
31
+ # Define the executable permission in octal format (e.g., 0o755).
32
+ # This gives read, write, and execute permissions to the owner,
33
+ # and read and execute permissions to the group and others.
34
+ executable_permission = 0o755
35
+
36
+ # Change the file's permission to make it executable.
37
+ os .chmod (file_path , executable_permission )
38
+ return True
39
+ except Exception as e :
40
+ print (f"Error: { e } " )
41
+ return False
42
+
43
+
44
+ def run_command_with_sudo (command ):
45
+ try :
46
+ # Use the 'sudo' command to execute the given command with elevated privileges.
47
+ result = subprocess .run (['sudo' ] + command , capture_output = True , text = True , check = True )
48
+ return result
49
+ except subprocess .CalledProcessError as e :
50
+ print (f"Error: { e } " )
51
+ return None
52
+
53
+
29
54
class PathManager :
30
55
@staticmethod
31
56
def problem_dir (problem_id : str ) -> str :
@@ -111,6 +136,8 @@ def upload(self) -> bool:
111
136
shutil .rmtree (dirname (self .zip_file_path ))
112
137
print (f"remove { dirname (self .zip_file_path )} " )
113
138
139
+ make_file_executable (join (self .problem_dir_path , TESTER_NAME ))
140
+
114
141
return valid
115
142
116
143
@@ -121,22 +148,31 @@ def __init__(self, submission: Submission):
121
148
if not exists (self .sub_dirpath ):
122
149
os .makedirs (self .sub_dirpath , exist_ok = True )
123
150
prob_dir = PathManager .problem_dir (submission .problem ._id ) # use display id
151
+ print ('problem id:' , submission .problem ._id )
152
+ print ('problem dir:' , prob_dir )
124
153
if not exists (prob_dir ):
125
154
raise Exception ("problem dir {} not exists" .format (prob_dir ))
126
155
for filename in os .listdir (prob_dir ):
127
- shutil .copyfile (join (prob_dir , filename ), self .sub_dirpath )
156
+ print (f'copy { join (prob_dir , filename )} to { self .sub_dirpath } ' )
157
+ shutil .copy2 (join (prob_dir , filename ), self .sub_dirpath )
128
158
129
159
def judge (self ):
130
160
tester_path = join (self .sub_dirpath , TESTER_NAME )
131
161
if not exists (tester_path ):
132
162
raise Exception ("running submission: tester {} not exists" .format (tester_path ))
133
163
print (f"running { tester_path } " )
134
- subprocess .run ([tester_path , '--log' , '--json' ])
164
+ result = run_command_with_sudo ([tester_path , "--log" , "--json" ])
165
+ if result :
166
+ print ("Command output:" )
167
+ print (result .stdout )
168
+ else :
169
+ print ("Command execution failed." )
170
+
135
171
log_path = join (self .sub_dirpath , 'logs' )
136
172
if not exists (log_path ):
137
173
raise Exception (f"logs path { log_path } not exists" )
138
174
failed_info = []
139
- with open (join (log_path , "results.txt " )) as fp :
175
+ with open (join (log_path , "results.json " )) as fp :
140
176
res = json .load (fp )
141
177
self .sub .grade = res ['grade' ]
142
178
for idx in res ['failed' ]:
0 commit comments