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