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
@@ -65,187 +65,190 | |||
|
65 | 65 | copy_problem_template(template_dir,problem_home) |
|
66 | 66 | link_input_file(test_request,problem_home) |
|
67 | 67 | |
|
68 | 68 | problem_home |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def save_source(test_request,source_name) |
|
72 | 72 | dir = self.produce_grading_room(test_request) |
|
73 | 73 | submission = test_request.submission |
|
74 | 74 | f = File.open("#{dir}/#{source_name}","w") |
|
75 | 75 | f.write(submission.source) |
|
76 | 76 | f.close |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | def clean_up(test_request) |
|
80 | 80 | problem_home = problem_home_dir(test_request) |
|
81 | 81 | remove_data_files(problem_home) |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | protected |
|
85 | 85 | def grading_room_dir(test_request) |
|
86 | 86 | problem_name = test_request.problem_name |
|
87 | 87 | user = test_request.user |
|
88 | 88 | grading_room = "#{@config.user_result_dir}" + |
|
89 | 89 | "/#{user.login}/test_request" + |
|
90 | 90 | "/#{problem_name}/#{test_request.id}" |
|
91 | 91 | grading_room |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def problem_home_dir(test_request) |
|
95 | 95 | problem_name = test_request.problem_name |
|
96 | 96 | user = test_request.user |
|
97 | 97 | "#{@config.user_result_dir}" + |
|
98 | 98 | "/#{user.login}/test_request/#{problem_name}" |
|
99 | 99 | end |
|
100 | 100 | |
|
101 | 101 | def copy_problem_template(template_dir,problem_home) |
|
102 | 102 | Grader::call_and_log("Test Request: cannot copy problem template") { |
|
103 | 103 | FileUtils.cp_r("#{template_dir}/.","#{problem_home}") |
|
104 | 104 | } |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | def link_input_file(test_request, problem_home) |
|
108 | 108 | input_fname = "#{test_request.input_file_name}" |
|
109 | 109 | if !File.exists?(input_fname) |
|
110 | 110 | raise "Test Request: input file not found." |
|
111 | 111 | end |
|
112 | 112 | |
|
113 | 113 | input_fname_problem_home = "#{problem_home}/test_cases/1/input-1.txt" |
|
114 | 114 | if File.exists?(input_fname_problem_home) |
|
115 | 115 | FileUtils.rm([input_fname_problem_home], :force => true) |
|
116 | 116 | end |
|
117 | 117 | |
|
118 | 118 | Grader::link_or_copy("#{input_fname}", "#{input_fname_problem_home}") |
|
119 | 119 | end |
|
120 | 120 | |
|
121 | 121 | def remove_data_files(problem_home) |
|
122 | 122 | if File.exists?("#{problem_home}/test_cases/1/input-1.txt") |
|
123 | 123 | Grader::call_and_log("Test Request: cannot remove data files") { |
|
124 | 124 | FileUtils.rm Dir.glob("#{problem_home}/test_cases/1/*") |
|
125 | 125 | } |
|
126 | 126 | end |
|
127 | 127 | end |
|
128 | 128 | |
|
129 | 129 | end |
|
130 | 130 | |
|
131 | 131 | class TestRequestReporter |
|
132 | 132 | def initialize |
|
133 | 133 | @config = Grader::Configuration.get_instance |
|
134 | 134 | end |
|
135 | 135 | |
|
136 | 136 | def report(test_request,test_result_dir) |
|
137 | 137 | save_result(test_request,read_result(test_result_dir)) |
|
138 | 138 | end |
|
139 | 139 | |
|
140 | 140 | def report_error(test_request, msg) |
|
141 | 141 | save_result(test_request, {:running_stat => { |
|
142 | 142 | :msg => "#{msg}", |
|
143 | 143 | :running_time => nil, |
|
144 | 144 | :exit_status => "Some error occured. Program did not run", |
|
145 | 145 | :memory_usage => nil |
|
146 | 146 | }}) |
|
147 | 147 | end |
|
148 | 148 | |
|
149 | 149 | protected |
|
150 | 150 | def read_result(test_result_dir) |
|
151 | 151 | # TODO: |
|
152 | 152 | cmp_msg_fname = "#{test_result_dir}/compiler_message" |
|
153 | 153 | cmp_file = File.open(cmp_msg_fname) |
|
154 | 154 | cmp_msg = cmp_file.read |
|
155 | 155 | cmp_file.close |
|
156 | 156 | |
|
157 | 157 | result_file_name = "#{test_result_dir}/1/result" |
|
158 | 158 | |
|
159 | 159 | if File.exists?(result_file_name) |
|
160 | 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 | 165 | stat = extract_running_stat(results) |
|
163 | 166 | |
|
164 | 167 | return { |
|
165 | 168 | :output_file_name => output_file_name, |
|
166 | 169 | :running_stat => stat, |
|
167 | 170 | :comment => "", |
|
168 | 171 | :cmp_msg => cmp_msg} |
|
169 | 172 | else |
|
170 | 173 | return { |
|
171 | 174 | :running_stat => nil, |
|
172 | 175 | :comment => "Compilation error", |
|
173 | 176 | :cmp_msg => cmp_msg} |
|
174 | 177 | end |
|
175 | 178 | end |
|
176 | 179 | |
|
177 | 180 | def extract_running_stat(results) |
|
178 | 181 | running_stat_line = results[-1] |
|
179 | 182 | |
|
180 | 183 | # extract exit status line |
|
181 | 184 | run_stat = "" |
|
182 | 185 | if !(/[Cc]orrect/.match(results[0])) |
|
183 | 186 | run_stat = results[0].chomp |
|
184 | 187 | else |
|
185 | 188 | run_stat = 'Program exited normally' |
|
186 | 189 | end |
|
187 | 190 | |
|
188 | 191 | # extract running time |
|
189 | 192 | if res = /r(.*)u(.*)s/.match(running_stat_line) |
|
190 | 193 | seconds = (res[1].to_f + res[2].to_f) |
|
191 | 194 | time_stat = "Time used: #{seconds} sec." |
|
192 | 195 | else |
|
193 | 196 | seconds = nil |
|
194 | 197 | time_stat = "Time used: n/a sec." |
|
195 | 198 | end |
|
196 | 199 | |
|
197 | 200 | # extract memory usage |
|
198 | 201 | if res = /s(.*)m/.match(running_stat_line) |
|
199 | 202 | memory_used = res[1].to_i |
|
200 | 203 | else |
|
201 | 204 | memory_used = -1 |
|
202 | 205 | end |
|
203 | 206 | |
|
204 | 207 | return { |
|
205 | 208 | :msg => "#{run_stat}\n#{time_stat}", |
|
206 | 209 | :running_time => seconds, |
|
207 | 210 | :exit_status => run_stat, |
|
208 | 211 | :memory_usage => memory_used |
|
209 | 212 | } |
|
210 | 213 | end |
|
211 | 214 | |
|
212 | 215 | def save_result(test_request,result) |
|
213 | 216 | if result[:output_file_name]!=nil |
|
214 | 217 | test_request.output_file_name = link_output_file(test_request, |
|
215 | 218 | result[:output_file_name]) |
|
216 | 219 | end |
|
217 | 220 | test_request.graded_at = Time.now |
|
218 | 221 | test_request.compiler_message = (result[:cmp_msg] or '') |
|
219 | 222 | test_request.grader_comment = (result[:comment] or '') |
|
220 | 223 | if result[:running_stat]!=nil |
|
221 | 224 | test_request.running_stat = (result[:running_stat][:msg] or '') |
|
222 | 225 | test_request.running_time = (result[:running_stat][:running_time] or nil) |
|
223 | 226 | test_request.exit_status = result[:running_stat][:exit_status] |
|
224 | 227 | test_request.memory_usage = result[:running_stat][:memory_usage] |
|
225 | 228 | else |
|
226 | 229 | test_request.running_stat = '' |
|
227 | 230 | end |
|
228 | 231 | test_request.save |
|
229 | 232 | end |
|
230 | 233 | |
|
231 | 234 | protected |
|
232 | 235 | def link_output_file(test_request, fname) |
|
233 | 236 | target_file_name = random_output_file_name(test_request.user, |
|
234 | 237 | test_request.problem) |
|
235 | 238 | FileUtils.mkdir_p(File.dirname(target_file_name)) |
|
236 | 239 | Grader::link_or_copy("#{fname}", "#{target_file_name}") |
|
237 | 240 | return target_file_name |
|
238 | 241 | end |
|
239 | 242 | |
|
240 | 243 | def random_output_file_name(user,problem) |
|
241 | 244 | problem_name = TestRequest.name_of(problem) |
|
242 | 245 | begin |
|
243 | 246 | tmpname = "#{@config.test_request_output_base_dir}" + |
|
244 | 247 | "/#{user.login}/#{problem_name}/#{rand(10000)}" |
|
245 | 248 | end while File.exists?(tmpname) |
|
246 | 249 | tmpname |
|
247 | 250 | end |
|
248 | 251 | |
|
249 | 252 | end |
|
250 | 253 | |
|
251 | 254 | end |
You need to be logged in to leave comments.
Login now