Show More
Commit Description:
handle the case when problem id or submission id is null. Grader will simply skip such request. Add more report on console (for command line grading)...
Commit Description:
handle the case when problem id or submission id is null. Grader will simply skip such request. Add more report on console (for command line grading) (mercurial grafted from d233105d3965c5368c9b33125f390e39b25f910e)
File last commit:
Show/Diff file:
Action:
lib/test_request_helper.rb | 254 lines | 7.6 KiB | text/x-ruby | RubyLexer |
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 #
# This part contains various test_request helpers for interfacing
# with Grader::Engine. There are TestRequestRoomMaker and
# TestRequestReporter.
module Grader
Jittat Fakcharoenphol
removed many 'system' invokations
r101 def self.link_or_copy(src, des)
begin
FileUtils.ln_s(src, des)
Jittat Fakcharoenphol
locks dir based on temp file, does not copy dir when copying scripts, added proper rescue for ln_s
r103 rescue NotImplementedError
Jittat Fakcharoenphol
removed many 'system' invokations
r101 FileUtils.cp(src,des)
end
end
def self.call_and_log(error_message)
begin
yield
rescue
msg = "ERROR: #{error_message}"
raise msg
end
end
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 #
# A TestRequestRoomMaker is a helper object for Engine
# - finds grading room: in user_result_dir/(user)/test_request/ ...
# - prepare problem configuration for grading --- basically it copy
# all config files, and copy user's input into the testcase
# directory. First, it finds the template from problem template
# directory; if it can't find a template, it'll use the template
# from default template.
class TestRequestRoomMaker
def initialize
@config = Grader::Configuration.get_instance
end
def produce_grading_room(test_request)
grading_room = grading_room_dir(test_request)
FileUtils.mkdir_p(grading_room)
jittat
[grader] change test_request_helper so that it copies additional submitted file...
r55
#
# Also copy additional submitted file to this directory as well.
# The program would see this file only if it is copied
# to the sandbox directory later. The run script should do it.
#
jittat
fixed test run scoring bug: now it takes the minimum of each test case's score...
r66 if FileTest.exists?("#{test_request.input_file_name}.files")
Jittat Fakcharoenphol
removed many 'system' invokations
r101 FileUtils.cp_r("#{test_request.input_file_name}.files/.",
"#{grading_room}")
jittat
fixed test run scoring bug: now it takes the minimum of each test case's score...
r66 end
jittat
[grader] change test_request_helper so that it copies additional submitted file...
r55
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 grading_room
end
def find_problem_home(test_request)
problem_name = test_request.problem_name
template_dir = "#{@config.test_request_problem_templates_dir}/" + problem_name
raise "Test Request: error template not found" if !File.exists?(template_dir)
problem_home = problem_home_dir(test_request)
FileUtils.mkdir_p(problem_home)
copy_problem_template(template_dir,problem_home)
link_input_file(test_request,problem_home)
problem_home
end
def save_source(test_request,source_name)
dir = self.produce_grading_room(test_request)
submission = test_request.submission
f = File.open("#{dir}/#{source_name}","w")
f.write(submission.source)
f.close
end
def clean_up(test_request)
problem_home = problem_home_dir(test_request)
remove_data_files(problem_home)
end
protected
def grading_room_dir(test_request)
problem_name = test_request.problem_name
user = test_request.user
jittat
[grader] change test_request_helper so that it copies additional submitted file...
r55 grading_room = "#{@config.user_result_dir}" +
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 "/#{user.login}/test_request" +
"/#{problem_name}/#{test_request.id}"
jittat
[grader] change test_request_helper so that it copies additional submitted file...
r55 grading_room
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 end
def problem_home_dir(test_request)
problem_name = test_request.problem_name
user = test_request.user
"#{@config.user_result_dir}" +
"/#{user.login}/test_request/#{problem_name}"
end
def copy_problem_template(template_dir,problem_home)
Jittat Fakcharoenphol
removed many 'system' invokations
r101 Grader::call_and_log("Test Request: cannot copy problem template") {
FileUtils.cp_r("#{template_dir}/.","#{problem_home}")
}
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 end
Jittat Fakcharoenphol
removed many 'system' invokations
r101
def link_input_file(test_request, problem_home)
jittat
[grader] test_request: fixed error when input file is not found, or input file remains in problem home...
r45 input_fname = "#{test_request.input_file_name}"
if !File.exists?(input_fname)
raise "Test Request: input file not found."
end
input_fname_problem_home = "#{problem_home}/test_cases/1/input-1.txt"
if File.exists?(input_fname_problem_home)
FileUtils.rm([input_fname_problem_home], :force => true)
end
Jittat Fakcharoenphol
removed many 'system' invokations
r101 Grader::link_or_copy("#{input_fname}", "#{input_fname_problem_home}")
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 end
def remove_data_files(problem_home)
if File.exists?("#{problem_home}/test_cases/1/input-1.txt")
Jittat Fakcharoenphol
removed many 'system' invokations
r101 Grader::call_and_log("Test Request: cannot remove data files") {
FileUtils.rm Dir.glob("#{problem_home}/test_cases/1/*")
}
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 end
end
end
class TestRequestReporter
def initialize
@config = Grader::Configuration.get_instance
end
def report(test_request,test_result_dir)
save_result(test_request,read_result(test_result_dir))
end
def report_error(test_request, msg)
jittat
[grader] test_request: fixed error when input file is not found, or input file remains in problem home...
r45 save_result(test_request, {:running_stat => {
:msg => "#{msg}",
:running_time => nil,
:exit_status => "Some error occured. Program did not run",
:memory_usage => nil
}})
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 end
protected
def read_result(test_result_dir)
# TODO:
cmp_msg_fname = "#{test_result_dir}/compiler_message"
cmp_file = File.open(cmp_msg_fname)
cmp_msg = cmp_file.read
cmp_file.close
result_file_name = "#{test_result_dir}/1/result"
if File.exists?(result_file_name)
output_file_name = "#{test_result_dir}/1/output.txt"
Jittat Fakcharoenphol
closes result file in test request
r112 results = []
File.open("#{test_result_dir}/1/result") do |f|
results = f.readlines
end
jittat
[grader] added memory usage status report, better running time report...
r42 stat = extract_running_stat(results)
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23
return {
:output_file_name => output_file_name,
:running_stat => stat,
:comment => "",
:cmp_msg => cmp_msg}
else
return {
jittat
[grader] extracted test_request info into new stat columns...
r41 :running_stat => nil,
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 :comment => "Compilation error",
:cmp_msg => cmp_msg}
end
end
jittat
[grader] added memory usage status report, better running time report...
r42 def extract_running_stat(results)
running_stat_line = results[-1]
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23
jittat
[grader] extracted test_request info into new stat columns...
r41 # extract exit status line
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 run_stat = ""
if !(/[Cc]orrect/.match(results[0]))
run_stat = results[0].chomp
jittat
[grader] extracted test_request info into new stat columns...
r41 else
run_stat = 'Program exited normally'
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 end
jittat
[grader] extracted test_request info into new stat columns...
r41 # extract running time
jittat
[grader] added memory usage status report, better running time report...
r42 if res = /r(.*)u(.*)s/.match(running_stat_line)
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 seconds = (res[1].to_f + res[2].to_f)
time_stat = "Time used: #{seconds} sec."
else
jittat
[grader] extracted test_request info into new stat columns...
r41 seconds = nil
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 time_stat = "Time used: n/a sec."
end
jittat
[grader] added memory usage status report, better running time report...
r42
# extract memory usage
if res = /s(.*)m/.match(running_stat_line)
memory_used = res[1].to_i
else
memory_used = -1
end
jittat
[grader] extracted test_request info into new stat columns...
r41 return {
:msg => "#{run_stat}\n#{time_stat}",
:running_time => seconds,
jittat
[grader] added memory usage status report, better running time report...
r42 :exit_status => run_stat,
:memory_usage => memory_used
jittat
[grader] extracted test_request info into new stat columns...
r41 }
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 end
def save_result(test_request,result)
if result[:output_file_name]!=nil
test_request.output_file_name = link_output_file(test_request,
result[:output_file_name])
end
test_request.graded_at = Time.now
test_request.compiler_message = (result[:cmp_msg] or '')
test_request.grader_comment = (result[:comment] or '')
jittat
[grader] extracted test_request info into new stat columns...
r41 if result[:running_stat]!=nil
test_request.running_stat = (result[:running_stat][:msg] or '')
test_request.running_time = (result[:running_stat][:running_time] or nil)
jittat
[grader] added memory usage status report, better running time report...
r42 test_request.exit_status = result[:running_stat][:exit_status]
test_request.memory_usage = result[:running_stat][:memory_usage]
jittat
[grader] extracted test_request info into new stat columns...
r41 else
test_request.running_stat = ''
end
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 test_request.save
end
protected
def link_output_file(test_request, fname)
target_file_name = random_output_file_name(test_request.user,
test_request.problem)
FileUtils.mkdir_p(File.dirname(target_file_name))
Jittat Fakcharoenphol
removed many 'system' invokations
r101 Grader::link_or_copy("#{fname}", "#{target_file_name}")
jittat
[grader] [MERGED] Merged new-arch-branch changes 74:105 into the trunk...
r23 return target_file_name
end
def random_output_file_name(user,problem)
problem_name = TestRequest.name_of(problem)
begin
tmpname = "#{@config.test_request_output_base_dir}" +
"/#{user.login}/#{problem_name}/#{rand(10000)}"
end while File.exists?(tmpname)
tmpname
end
end
end