Description:
[grader] test_request: fixed error when input file is not found, or input file remains in problem home git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@171 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r45:dafc790ec9b7 - - 1 file changed: 17 inserted, 2 deleted

@@ -1,203 +1,218
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 grading_room
24 grading_room
25 end
25 end
26
26
27 def find_problem_home(test_request)
27 def find_problem_home(test_request)
28 problem_name = test_request.problem_name
28 problem_name = test_request.problem_name
29
29
30 template_dir = "#{@config.test_request_problem_templates_dir}/" + problem_name
30 template_dir = "#{@config.test_request_problem_templates_dir}/" + problem_name
31
31
32 raise "Test Request: error template not found" if !File.exists?(template_dir)
32 raise "Test Request: error template not found" if !File.exists?(template_dir)
33
33
34 problem_home = problem_home_dir(test_request)
34 problem_home = problem_home_dir(test_request)
35 FileUtils.mkdir_p(problem_home)
35 FileUtils.mkdir_p(problem_home)
36
36
37 copy_problem_template(template_dir,problem_home)
37 copy_problem_template(template_dir,problem_home)
38 link_input_file(test_request,problem_home)
38 link_input_file(test_request,problem_home)
39
39
40 problem_home
40 problem_home
41 end
41 end
42
42
43 def save_source(test_request,source_name)
43 def save_source(test_request,source_name)
44 dir = self.produce_grading_room(test_request)
44 dir = self.produce_grading_room(test_request)
45 submission = test_request.submission
45 submission = test_request.submission
46 f = File.open("#{dir}/#{source_name}","w")
46 f = File.open("#{dir}/#{source_name}","w")
47 f.write(submission.source)
47 f.write(submission.source)
48 f.close
48 f.close
49 end
49 end
50
50
51 def clean_up(test_request)
51 def clean_up(test_request)
52 problem_home = problem_home_dir(test_request)
52 problem_home = problem_home_dir(test_request)
53 remove_data_files(problem_home)
53 remove_data_files(problem_home)
54 end
54 end
55
55
56 protected
56 protected
57 def grading_room_dir(test_request)
57 def grading_room_dir(test_request)
58 problem_name = test_request.problem_name
58 problem_name = test_request.problem_name
59 user = test_request.user
59 user = test_request.user
60 "#{@config.user_result_dir}" +
60 "#{@config.user_result_dir}" +
61 "/#{user.login}/test_request" +
61 "/#{user.login}/test_request" +
62 "/#{problem_name}/#{test_request.id}"
62 "/#{problem_name}/#{test_request.id}"
63 end
63 end
64
64
65 def problem_home_dir(test_request)
65 def problem_home_dir(test_request)
66 problem_name = test_request.problem_name
66 problem_name = test_request.problem_name
67 user = test_request.user
67 user = test_request.user
68 "#{@config.user_result_dir}" +
68 "#{@config.user_result_dir}" +
69 "/#{user.login}/test_request/#{problem_name}"
69 "/#{user.login}/test_request/#{problem_name}"
70 end
70 end
71
71
72 def copy_problem_template(template_dir,problem_home)
72 def copy_problem_template(template_dir,problem_home)
73 cmd = "cp -R #{template_dir}/* #{problem_home}"
73 cmd = "cp -R #{template_dir}/* #{problem_home}"
74 system_and_raise_when_fail(cmd,"Test Request: cannot copy problem template")
74 system_and_raise_when_fail(cmd,"Test Request: cannot copy problem template")
75 end
75 end
76
76
77 def link_input_file(test_request,problem_home)
77 def link_input_file(test_request,problem_home)
78 - cmd = "ln -s #{test_request.input_file_name} #{problem_home}/test_cases/1/input-1.txt"
78 + input_fname = "#{test_request.input_file_name}"
79 + if !File.exists?(input_fname)
80 + raise "Test Request: input file not found."
81 + end
82 +
83 + input_fname_problem_home = "#{problem_home}/test_cases/1/input-1.txt"
84 + if File.exists?(input_fname_problem_home)
85 + FileUtils.rm([input_fname_problem_home], :force => true)
86 + end
87 +
88 + cmd = "ln -s #{input_fname} #{input_fname_problem_home}"
79 system_and_raise_when_fail(cmd,"Test Request: cannot link input file")
89 system_and_raise_when_fail(cmd,"Test Request: cannot link input file")
80 end
90 end
81
91
82 def remove_data_files(problem_home)
92 def remove_data_files(problem_home)
83 if File.exists?("#{problem_home}/test_cases/1/input-1.txt")
93 if File.exists?("#{problem_home}/test_cases/1/input-1.txt")
84 cmd = "rm #{problem_home}/test_cases/1/*"
94 cmd = "rm #{problem_home}/test_cases/1/*"
85 system_and_raise_when_fail(cmd,"Test Request: cannot remove data files")
95 system_and_raise_when_fail(cmd,"Test Request: cannot remove data files")
86 end
96 end
87 end
97 end
88
98
89 def system_and_raise_when_fail(cmd,msg)
99 def system_and_raise_when_fail(cmd,msg)
90 if !system(cmd)
100 if !system(cmd)
91 raise msg
101 raise msg
92 end
102 end
93 end
103 end
94
104
95 end
105 end
96
106
97 class TestRequestReporter
107 class TestRequestReporter
98 def initialize
108 def initialize
99 @config = Grader::Configuration.get_instance
109 @config = Grader::Configuration.get_instance
100 end
110 end
101
111
102 def report(test_request,test_result_dir)
112 def report(test_request,test_result_dir)
103 save_result(test_request,read_result(test_result_dir))
113 save_result(test_request,read_result(test_result_dir))
104 end
114 end
105
115
106 def report_error(test_request, msg)
116 def report_error(test_request, msg)
107 - save_result(test_request, {:running_stat => {:msg => "#{msg}"}})
117 + save_result(test_request, {:running_stat => {
118 + :msg => "#{msg}",
119 + :running_time => nil,
120 + :exit_status => "Some error occured. Program did not run",
121 + :memory_usage => nil
122 + }})
108 end
123 end
109
124
110 protected
125 protected
111 def read_result(test_result_dir)
126 def read_result(test_result_dir)
112 # TODO:
127 # TODO:
113 cmp_msg_fname = "#{test_result_dir}/compiler_message"
128 cmp_msg_fname = "#{test_result_dir}/compiler_message"
114 cmp_file = File.open(cmp_msg_fname)
129 cmp_file = File.open(cmp_msg_fname)
115 cmp_msg = cmp_file.read
130 cmp_msg = cmp_file.read
116 cmp_file.close
131 cmp_file.close
117
132
118 result_file_name = "#{test_result_dir}/1/result"
133 result_file_name = "#{test_result_dir}/1/result"
119
134
120 if File.exists?(result_file_name)
135 if File.exists?(result_file_name)
121 output_file_name = "#{test_result_dir}/1/output.txt"
136 output_file_name = "#{test_result_dir}/1/output.txt"
122 results = File.open("#{test_result_dir}/1/result").readlines
137 results = File.open("#{test_result_dir}/1/result").readlines
123 stat = extract_running_stat(results)
138 stat = extract_running_stat(results)
124
139
125 return {
140 return {
126 :output_file_name => output_file_name,
141 :output_file_name => output_file_name,
127 :running_stat => stat,
142 :running_stat => stat,
128 :comment => "",
143 :comment => "",
129 :cmp_msg => cmp_msg}
144 :cmp_msg => cmp_msg}
130 else
145 else
131 return {
146 return {
132 :running_stat => nil,
147 :running_stat => nil,
133 :comment => "Compilation error",
148 :comment => "Compilation error",
134 :cmp_msg => cmp_msg}
149 :cmp_msg => cmp_msg}
135 end
150 end
136 end
151 end
137
152
138 def extract_running_stat(results)
153 def extract_running_stat(results)
139 running_stat_line = results[-1]
154 running_stat_line = results[-1]
140
155
141 # extract exit status line
156 # extract exit status line
142 run_stat = ""
157 run_stat = ""
143 if !(/[Cc]orrect/.match(results[0]))
158 if !(/[Cc]orrect/.match(results[0]))
144 run_stat = results[0].chomp
159 run_stat = results[0].chomp
145 else
160 else
146 run_stat = 'Program exited normally'
161 run_stat = 'Program exited normally'
147 end
162 end
148
163
149 # extract running time
164 # extract running time
150 if res = /r(.*)u(.*)s/.match(running_stat_line)
165 if res = /r(.*)u(.*)s/.match(running_stat_line)
151 seconds = (res[1].to_f + res[2].to_f)
166 seconds = (res[1].to_f + res[2].to_f)
152 time_stat = "Time used: #{seconds} sec."
167 time_stat = "Time used: #{seconds} sec."
153 else
168 else
154 seconds = nil
169 seconds = nil
155 time_stat = "Time used: n/a sec."
170 time_stat = "Time used: n/a sec."
156 end
171 end
157
172
158 # extract memory usage
173 # extract memory usage
159 if res = /s(.*)m/.match(running_stat_line)
174 if res = /s(.*)m/.match(running_stat_line)
160 memory_used = res[1].to_i
175 memory_used = res[1].to_i
161 else
176 else
162 memory_used = -1
177 memory_used = -1
163 end
178 end
164
179
165 return {
180 return {
166 :msg => "#{run_stat}\n#{time_stat}",
181 :msg => "#{run_stat}\n#{time_stat}",
167 :running_time => seconds,
182 :running_time => seconds,
168 :exit_status => run_stat,
183 :exit_status => run_stat,
169 :memory_usage => memory_used
184 :memory_usage => memory_used
170 }
185 }
171 end
186 end
172
187
173 def save_result(test_request,result)
188 def save_result(test_request,result)
174 if result[:output_file_name]!=nil
189 if result[:output_file_name]!=nil
175 test_request.output_file_name = link_output_file(test_request,
190 test_request.output_file_name = link_output_file(test_request,
176 result[:output_file_name])
191 result[:output_file_name])
177 end
192 end
178 test_request.graded_at = Time.now
193 test_request.graded_at = Time.now
179 test_request.compiler_message = (result[:cmp_msg] or '')
194 test_request.compiler_message = (result[:cmp_msg] or '')
180 test_request.grader_comment = (result[:comment] or '')
195 test_request.grader_comment = (result[:comment] or '')
181 if result[:running_stat]!=nil
196 if result[:running_stat]!=nil
182 test_request.running_stat = (result[:running_stat][:msg] or '')
197 test_request.running_stat = (result[:running_stat][:msg] or '')
183 test_request.running_time = (result[:running_stat][:running_time] or nil)
198 test_request.running_time = (result[:running_stat][:running_time] or nil)
184 test_request.exit_status = result[:running_stat][:exit_status]
199 test_request.exit_status = result[:running_stat][:exit_status]
185 test_request.memory_usage = result[:running_stat][:memory_usage]
200 test_request.memory_usage = result[:running_stat][:memory_usage]
186 else
201 else
187 test_request.running_stat = ''
202 test_request.running_stat = ''
188 end
203 end
189 test_request.save
204 test_request.save
190 end
205 end
191
206
192 protected
207 protected
193 def link_output_file(test_request, fname)
208 def link_output_file(test_request, fname)
194 target_file_name = random_output_file_name(test_request.user,
209 target_file_name = random_output_file_name(test_request.user,
195 test_request.problem)
210 test_request.problem)
196 FileUtils.mkdir_p(File.dirname(target_file_name))
211 FileUtils.mkdir_p(File.dirname(target_file_name))
197 cmd = "ln -s #{fname} #{target_file_name}"
212 cmd = "ln -s #{fname} #{target_file_name}"
198 if !system(cmd)
213 if !system(cmd)
199 raise "TestRequestReporter: cannot move output file"
214 raise "TestRequestReporter: cannot move output file"
200 end
215 end
201 return target_file_name
216 return target_file_name
202 end
217 end
203
218
You need to be logged in to leave comments. Login now