Description:
Merge branch 'master' of gitorious.org:cafe-grader/cafe-grader-judge-scripts into win-local
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r113:c3fb576034e2 - - 1 file changed: 4 inserted, 1 deleted
@@ -1,251 +1,254 | |||||
|
1 | # |
|
1 | # |
|
2 | # This part contains various test_request helpers for interfacing |
|
2 | # This part contains various test_request helpers for interfacing |
|
3 | # with Grader::Engine. There are TestRequestRoomMaker and |
|
3 | # with Grader::Engine. There are TestRequestRoomMaker and |
|
4 | # TestRequestReporter. |
|
4 | # TestRequestReporter. |
|
5 |
|
5 | ||
|
6 | module Grader |
|
6 | module Grader |
|
7 |
|
7 | ||
|
8 | def self.link_or_copy(src, des) |
|
8 | def self.link_or_copy(src, des) |
|
9 | begin |
|
9 | begin |
|
10 | FileUtils.ln_s(src, des) |
|
10 | FileUtils.ln_s(src, des) |
|
11 | rescue NotImplementedError |
|
11 | rescue NotImplementedError |
|
12 | FileUtils.cp(src,des) |
|
12 | FileUtils.cp(src,des) |
|
13 | end |
|
13 | end |
|
14 | end |
|
14 | end |
|
15 |
|
15 | ||
|
16 | def self.call_and_log(error_message) |
|
16 | def self.call_and_log(error_message) |
|
17 | begin |
|
17 | begin |
|
18 | yield |
|
18 | yield |
|
19 | rescue |
|
19 | rescue |
|
20 | msg = "ERROR: #{error_message}" |
|
20 | msg = "ERROR: #{error_message}" |
|
21 | raise msg |
|
21 | raise msg |
|
22 | end |
|
22 | end |
|
23 | end |
|
23 | end |
|
24 |
|
24 | ||
|
25 | # |
|
25 | # |
|
26 | # A TestRequestRoomMaker is a helper object for Engine |
|
26 | # A TestRequestRoomMaker is a helper object for Engine |
|
27 | # - finds grading room: in user_result_dir/(user)/test_request/ ... |
|
27 | # - finds grading room: in user_result_dir/(user)/test_request/ ... |
|
28 | # - prepare problem configuration for grading --- basically it copy |
|
28 | # - prepare problem configuration for grading --- basically it copy |
|
29 | # all config files, and copy user's input into the testcase |
|
29 | # all config files, and copy user's input into the testcase |
|
30 | # directory. First, it finds the template from problem template |
|
30 | # directory. First, it finds the template from problem template |
|
31 | # directory; if it can't find a template, it'll use the template |
|
31 | # directory; if it can't find a template, it'll use the template |
|
32 | # from default template. |
|
32 | # from default template. |
|
33 | class TestRequestRoomMaker |
|
33 | class TestRequestRoomMaker |
|
34 | def initialize |
|
34 | def initialize |
|
35 | @config = Grader::Configuration.get_instance |
|
35 | @config = Grader::Configuration.get_instance |
|
36 | end |
|
36 | end |
|
37 |
|
37 | ||
|
38 | def produce_grading_room(test_request) |
|
38 | def produce_grading_room(test_request) |
|
39 | grading_room = grading_room_dir(test_request) |
|
39 | grading_room = grading_room_dir(test_request) |
|
40 | FileUtils.mkdir_p(grading_room) |
|
40 | FileUtils.mkdir_p(grading_room) |
|
41 |
|
41 | ||
|
42 | # |
|
42 | # |
|
43 | # Also copy additional submitted file to this directory as well. |
|
43 | # Also copy additional submitted file to this directory as well. |
|
44 | # The program would see this file only if it is copied |
|
44 | # The program would see this file only if it is copied |
|
45 | # to the sandbox directory later. The run script should do it. |
|
45 | # to the sandbox directory later. The run script should do it. |
|
46 | # |
|
46 | # |
|
47 | if FileTest.exists?("#{test_request.input_file_name}.files") |
|
47 | if FileTest.exists?("#{test_request.input_file_name}.files") |
|
48 | FileUtils.cp_r("#{test_request.input_file_name}.files/.", |
|
48 | FileUtils.cp_r("#{test_request.input_file_name}.files/.", |
|
49 | "#{grading_room}") |
|
49 | "#{grading_room}") |
|
50 | end |
|
50 | end |
|
51 |
|
51 | ||
|
52 | grading_room |
|
52 | grading_room |
|
53 | end |
|
53 | end |
|
54 |
|
54 | ||
|
55 | def find_problem_home(test_request) |
|
55 | def find_problem_home(test_request) |
|
56 | problem_name = test_request.problem_name |
|
56 | problem_name = test_request.problem_name |
|
57 |
|
57 | ||
|
58 | template_dir = "#{@config.test_request_problem_templates_dir}/" + problem_name |
|
58 | template_dir = "#{@config.test_request_problem_templates_dir}/" + problem_name |
|
59 |
|
59 | ||
|
60 | raise "Test Request: error template not found" if !File.exists?(template_dir) |
|
60 | raise "Test Request: error template not found" if !File.exists?(template_dir) |
|
61 |
|
61 | ||
|
62 | problem_home = problem_home_dir(test_request) |
|
62 | problem_home = problem_home_dir(test_request) |
|
63 | FileUtils.mkdir_p(problem_home) |
|
63 | FileUtils.mkdir_p(problem_home) |
|
64 |
|
64 | ||
|
65 | copy_problem_template(template_dir,problem_home) |
|
65 | copy_problem_template(template_dir,problem_home) |
|
66 | link_input_file(test_request,problem_home) |
|
66 | link_input_file(test_request,problem_home) |
|
67 |
|
67 | ||
|
68 | problem_home |
|
68 | problem_home |
|
69 | end |
|
69 | end |
|
70 |
|
70 | ||
|
71 | def save_source(test_request,source_name) |
|
71 | def save_source(test_request,source_name) |
|
72 | dir = self.produce_grading_room(test_request) |
|
72 | dir = self.produce_grading_room(test_request) |
|
73 | submission = test_request.submission |
|
73 | submission = test_request.submission |
|
74 | f = File.open("#{dir}/#{source_name}","w") |
|
74 | f = File.open("#{dir}/#{source_name}","w") |
|
75 | f.write(submission.source) |
|
75 | f.write(submission.source) |
|
76 | f.close |
|
76 | f.close |
|
77 | end |
|
77 | end |
|
78 |
|
78 | ||
|
79 | def clean_up(test_request) |
|
79 | def clean_up(test_request) |
|
80 | problem_home = problem_home_dir(test_request) |
|
80 | problem_home = problem_home_dir(test_request) |
|
81 | remove_data_files(problem_home) |
|
81 | remove_data_files(problem_home) |
|
82 | end |
|
82 | end |
|
83 |
|
83 | ||
|
84 | protected |
|
84 | protected |
|
85 | def grading_room_dir(test_request) |
|
85 | def grading_room_dir(test_request) |
|
86 | problem_name = test_request.problem_name |
|
86 | problem_name = test_request.problem_name |
|
87 | user = test_request.user |
|
87 | user = test_request.user |
|
88 | grading_room = "#{@config.user_result_dir}" + |
|
88 | grading_room = "#{@config.user_result_dir}" + |
|
89 | "/#{user.login}/test_request" + |
|
89 | "/#{user.login}/test_request" + |
|
90 | "/#{problem_name}/#{test_request.id}" |
|
90 | "/#{problem_name}/#{test_request.id}" |
|
91 | grading_room |
|
91 | grading_room |
|
92 | end |
|
92 | end |
|
93 |
|
93 | ||
|
94 | def problem_home_dir(test_request) |
|
94 | def problem_home_dir(test_request) |
|
95 | problem_name = test_request.problem_name |
|
95 | problem_name = test_request.problem_name |
|
96 | user = test_request.user |
|
96 | user = test_request.user |
|
97 | "#{@config.user_result_dir}" + |
|
97 | "#{@config.user_result_dir}" + |
|
98 | "/#{user.login}/test_request/#{problem_name}" |
|
98 | "/#{user.login}/test_request/#{problem_name}" |
|
99 | end |
|
99 | end |
|
100 |
|
100 | ||
|
101 | def copy_problem_template(template_dir,problem_home) |
|
101 | def copy_problem_template(template_dir,problem_home) |
|
102 | Grader::call_and_log("Test Request: cannot copy problem template") { |
|
102 | Grader::call_and_log("Test Request: cannot copy problem template") { |
|
103 | FileUtils.cp_r("#{template_dir}/.","#{problem_home}") |
|
103 | FileUtils.cp_r("#{template_dir}/.","#{problem_home}") |
|
104 | } |
|
104 | } |
|
105 | end |
|
105 | end |
|
106 |
|
106 | ||
|
107 | def link_input_file(test_request, problem_home) |
|
107 | def link_input_file(test_request, problem_home) |
|
108 | input_fname = "#{test_request.input_file_name}" |
|
108 | input_fname = "#{test_request.input_file_name}" |
|
109 | if !File.exists?(input_fname) |
|
109 | if !File.exists?(input_fname) |
|
110 | raise "Test Request: input file not found." |
|
110 | raise "Test Request: input file not found." |
|
111 | end |
|
111 | end |
|
112 |
|
112 | ||
|
113 | input_fname_problem_home = "#{problem_home}/test_cases/1/input-1.txt" |
|
113 | input_fname_problem_home = "#{problem_home}/test_cases/1/input-1.txt" |
|
114 | if File.exists?(input_fname_problem_home) |
|
114 | if File.exists?(input_fname_problem_home) |
|
115 | FileUtils.rm([input_fname_problem_home], :force => true) |
|
115 | FileUtils.rm([input_fname_problem_home], :force => true) |
|
116 | end |
|
116 | end |
|
117 |
|
117 | ||
|
118 | Grader::link_or_copy("#{input_fname}", "#{input_fname_problem_home}") |
|
118 | Grader::link_or_copy("#{input_fname}", "#{input_fname_problem_home}") |
|
119 | end |
|
119 | end |
|
120 |
|
120 | ||
|
121 | def remove_data_files(problem_home) |
|
121 | def remove_data_files(problem_home) |
|
122 | if File.exists?("#{problem_home}/test_cases/1/input-1.txt") |
|
122 | if File.exists?("#{problem_home}/test_cases/1/input-1.txt") |
|
123 | Grader::call_and_log("Test Request: cannot remove data files") { |
|
123 | Grader::call_and_log("Test Request: cannot remove data files") { |
|
124 | FileUtils.rm Dir.glob("#{problem_home}/test_cases/1/*") |
|
124 | FileUtils.rm Dir.glob("#{problem_home}/test_cases/1/*") |
|
125 | } |
|
125 | } |
|
126 | end |
|
126 | end |
|
127 | end |
|
127 | end |
|
128 |
|
128 | ||
|
129 | end |
|
129 | end |
|
130 |
|
130 | ||
|
131 | class TestRequestReporter |
|
131 | class TestRequestReporter |
|
132 | def initialize |
|
132 | def initialize |
|
133 | @config = Grader::Configuration.get_instance |
|
133 | @config = Grader::Configuration.get_instance |
|
134 | end |
|
134 | end |
|
135 |
|
135 | ||
|
136 | def report(test_request,test_result_dir) |
|
136 | def report(test_request,test_result_dir) |
|
137 | save_result(test_request,read_result(test_result_dir)) |
|
137 | save_result(test_request,read_result(test_result_dir)) |
|
138 | end |
|
138 | end |
|
139 |
|
139 | ||
|
140 | def report_error(test_request, msg) |
|
140 | def report_error(test_request, msg) |
|
141 | save_result(test_request, {:running_stat => { |
|
141 | save_result(test_request, {:running_stat => { |
|
142 | :msg => "#{msg}", |
|
142 | :msg => "#{msg}", |
|
143 | :running_time => nil, |
|
143 | :running_time => nil, |
|
144 | :exit_status => "Some error occured. Program did not run", |
|
144 | :exit_status => "Some error occured. Program did not run", |
|
145 | :memory_usage => nil |
|
145 | :memory_usage => nil |
|
146 | }}) |
|
146 | }}) |
|
147 | end |
|
147 | end |
|
148 |
|
148 | ||
|
149 | protected |
|
149 | protected |
|
150 | def read_result(test_result_dir) |
|
150 | def read_result(test_result_dir) |
|
151 | # TODO: |
|
151 | # TODO: |
|
152 | cmp_msg_fname = "#{test_result_dir}/compiler_message" |
|
152 | cmp_msg_fname = "#{test_result_dir}/compiler_message" |
|
153 | cmp_file = File.open(cmp_msg_fname) |
|
153 | cmp_file = File.open(cmp_msg_fname) |
|
154 | cmp_msg = cmp_file.read |
|
154 | cmp_msg = cmp_file.read |
|
155 | cmp_file.close |
|
155 | cmp_file.close |
|
156 |
|
156 | ||
|
157 | result_file_name = "#{test_result_dir}/1/result" |
|
157 | result_file_name = "#{test_result_dir}/1/result" |
|
158 |
|
158 | ||
|
159 | if File.exists?(result_file_name) |
|
159 | if File.exists?(result_file_name) |
|
160 | output_file_name = "#{test_result_dir}/1/output.txt" |
|
160 | output_file_name = "#{test_result_dir}/1/output.txt" |
|
161 | - results = File.open("#{test_result_dir}/1/result").readlines |
|
161 | + results = [] |
|
|
162 | + File.open("#{test_result_dir}/1/result") do |f| | ||
|
|
163 | + results = f.readlines | ||
|
|
164 | + end | ||
|
162 | stat = extract_running_stat(results) |
|
165 | stat = extract_running_stat(results) |
|
163 |
|
166 | ||
|
164 | return { |
|
167 | return { |
|
165 | :output_file_name => output_file_name, |
|
168 | :output_file_name => output_file_name, |
|
166 | :running_stat => stat, |
|
169 | :running_stat => stat, |
|
167 | :comment => "", |
|
170 | :comment => "", |
|
168 | :cmp_msg => cmp_msg} |
|
171 | :cmp_msg => cmp_msg} |
|
169 | else |
|
172 | else |
|
170 | return { |
|
173 | return { |
|
171 | :running_stat => nil, |
|
174 | :running_stat => nil, |
|
172 | :comment => "Compilation error", |
|
175 | :comment => "Compilation error", |
|
173 | :cmp_msg => cmp_msg} |
|
176 | :cmp_msg => cmp_msg} |
|
174 | end |
|
177 | end |
|
175 | end |
|
178 | end |
|
176 |
|
179 | ||
|
177 | def extract_running_stat(results) |
|
180 | def extract_running_stat(results) |
|
178 | running_stat_line = results[-1] |
|
181 | running_stat_line = results[-1] |
|
179 |
|
182 | ||
|
180 | # extract exit status line |
|
183 | # extract exit status line |
|
181 | run_stat = "" |
|
184 | run_stat = "" |
|
182 | if !(/[Cc]orrect/.match(results[0])) |
|
185 | if !(/[Cc]orrect/.match(results[0])) |
|
183 | run_stat = results[0].chomp |
|
186 | run_stat = results[0].chomp |
|
184 | else |
|
187 | else |
|
185 | run_stat = 'Program exited normally' |
|
188 | run_stat = 'Program exited normally' |
|
186 | end |
|
189 | end |
|
187 |
|
190 | ||
|
188 | # extract running time |
|
191 | # extract running time |
|
189 | if res = /r(.*)u(.*)s/.match(running_stat_line) |
|
192 | if res = /r(.*)u(.*)s/.match(running_stat_line) |
|
190 | seconds = (res[1].to_f + res[2].to_f) |
|
193 | seconds = (res[1].to_f + res[2].to_f) |
|
191 | time_stat = "Time used: #{seconds} sec." |
|
194 | time_stat = "Time used: #{seconds} sec." |
|
192 | else |
|
195 | else |
|
193 | seconds = nil |
|
196 | seconds = nil |
|
194 | time_stat = "Time used: n/a sec." |
|
197 | time_stat = "Time used: n/a sec." |
|
195 | end |
|
198 | end |
|
196 |
|
199 | ||
|
197 | # extract memory usage |
|
200 | # extract memory usage |
|
198 | if res = /s(.*)m/.match(running_stat_line) |
|
201 | if res = /s(.*)m/.match(running_stat_line) |
|
199 | memory_used = res[1].to_i |
|
202 | memory_used = res[1].to_i |
|
200 | else |
|
203 | else |
|
201 | memory_used = -1 |
|
204 | memory_used = -1 |
|
202 | end |
|
205 | end |
|
203 |
|
206 | ||
|
204 | return { |
|
207 | return { |
|
205 | :msg => "#{run_stat}\n#{time_stat}", |
|
208 | :msg => "#{run_stat}\n#{time_stat}", |
|
206 | :running_time => seconds, |
|
209 | :running_time => seconds, |
|
207 | :exit_status => run_stat, |
|
210 | :exit_status => run_stat, |
|
208 | :memory_usage => memory_used |
|
211 | :memory_usage => memory_used |
|
209 | } |
|
212 | } |
|
210 | end |
|
213 | end |
|
211 |
|
214 | ||
|
212 | def save_result(test_request,result) |
|
215 | def save_result(test_request,result) |
|
213 | if result[:output_file_name]!=nil |
|
216 | if result[:output_file_name]!=nil |
|
214 | test_request.output_file_name = link_output_file(test_request, |
|
217 | test_request.output_file_name = link_output_file(test_request, |
|
215 | result[:output_file_name]) |
|
218 | result[:output_file_name]) |
|
216 | end |
|
219 | end |
|
217 | test_request.graded_at = Time.now |
|
220 | test_request.graded_at = Time.now |
|
218 | test_request.compiler_message = (result[:cmp_msg] or '') |
|
221 | test_request.compiler_message = (result[:cmp_msg] or '') |
|
219 | test_request.grader_comment = (result[:comment] or '') |
|
222 | test_request.grader_comment = (result[:comment] or '') |
|
220 | if result[:running_stat]!=nil |
|
223 | if result[:running_stat]!=nil |
|
221 | test_request.running_stat = (result[:running_stat][:msg] or '') |
|
224 | test_request.running_stat = (result[:running_stat][:msg] or '') |
|
222 | test_request.running_time = (result[:running_stat][:running_time] or nil) |
|
225 | test_request.running_time = (result[:running_stat][:running_time] or nil) |
|
223 | test_request.exit_status = result[:running_stat][:exit_status] |
|
226 | test_request.exit_status = result[:running_stat][:exit_status] |
|
224 | test_request.memory_usage = result[:running_stat][:memory_usage] |
|
227 | test_request.memory_usage = result[:running_stat][:memory_usage] |
|
225 | else |
|
228 | else |
|
226 | test_request.running_stat = '' |
|
229 | test_request.running_stat = '' |
|
227 | end |
|
230 | end |
|
228 | test_request.save |
|
231 | test_request.save |
|
229 | end |
|
232 | end |
|
230 |
|
233 | ||
|
231 | protected |
|
234 | protected |
|
232 | def link_output_file(test_request, fname) |
|
235 | def link_output_file(test_request, fname) |
|
233 | target_file_name = random_output_file_name(test_request.user, |
|
236 | target_file_name = random_output_file_name(test_request.user, |
|
234 | test_request.problem) |
|
237 | test_request.problem) |
|
235 | FileUtils.mkdir_p(File.dirname(target_file_name)) |
|
238 | FileUtils.mkdir_p(File.dirname(target_file_name)) |
|
236 | Grader::link_or_copy("#{fname}", "#{target_file_name}") |
|
239 | Grader::link_or_copy("#{fname}", "#{target_file_name}") |
|
237 | return target_file_name |
|
240 | return target_file_name |
|
238 | end |
|
241 | end |
|
239 |
|
242 | ||
|
240 | def random_output_file_name(user,problem) |
|
243 | def random_output_file_name(user,problem) |
|
241 | problem_name = TestRequest.name_of(problem) |
|
244 | problem_name = TestRequest.name_of(problem) |
|
242 | begin |
|
245 | begin |
|
243 | tmpname = "#{@config.test_request_output_base_dir}" + |
|
246 | tmpname = "#{@config.test_request_output_base_dir}" + |
|
244 | "/#{user.login}/#{problem_name}/#{rand(10000)}" |
|
247 | "/#{user.login}/#{problem_name}/#{rand(10000)}" |
|
245 | end while File.exists?(tmpname) |
|
248 | end while File.exists?(tmpname) |
|
246 | tmpname |
|
249 | tmpname |
|
247 | end |
|
250 | end |
|
248 |
|
251 | ||
|
249 | end |
|
252 | end |
|
250 |
|
253 | ||
|
251 | end |
|
254 | end |
You need to be logged in to leave comments.
Login now