Show More
Commit Description:
[grader] import script now support raw with testruns...
Commit Description:
[grader] import script now support raw with testruns
git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@267 6386c4cd-e34a-4fa8-8920-d93eb39b512e
References:
File last commit:
Show/Diff file:
Action:
std-script/judge
| 138 lines
| 4.1 KiB
| text/plain
| TextLexer
|
|
r0 | #!/usr/bin/ruby | ||
|
r22 | def log(str='') | ||
if ENV['TALKATIVE']!=nil | ||||
puts str | ||||
end | ||||
if ENV['GRADER_LOGGING']!=nil | ||||
log_fname = ENV['GRADER_LOGGING'] | ||||
fp = File.open(log_fname,"a") | ||||
fp.puts("judge: #{Time.new.strftime("%H:%M")} #{str}") | ||||
fp.close | ||||
end | ||||
end | ||||
|
r0 | problem_home = ENV['PROBLEM_HOME'] | ||
def execute(command, error_message="") | ||||
if not system(command) | ||||
|
r31 | msg = "ERROR: #{error_message}" | ||
log msg | ||||
raise msg | ||||
|
r0 | end | ||
end | ||||
# ARGV[0] --- language | ||||
# ARGV[1] --- program source file | ||||
# ARGV[2] --- test result directory | ||||
# ARGV[3] --- sandbox directory | ||||
if ARGV.length < 2 || ARGV.length > 4 | ||||
puts "Usage: judge <language> <program-source> [<test-result-directory>] [<sandbox-directory>]" | ||||
puts " <sandbox-directory> is defaulted to ./sandbox" | ||||
puts " <test-result-directory> is defaulted to ./test-result" | ||||
puts "WARNING: The judge script will forcefully create the (implicitly and explicitly) specified directories and remove anything inside it." | ||||
exit(127) | ||||
end | ||||
language = ARGV[0] | ||||
|
r53 | if language != "c" && language != "c++" && language != "pas" | ||
log "You specified a language that is not supported: #{language}." | ||||
|
r0 | exit(127) | ||
end | ||||
source_file = ARGV[1] | ||||
if File.exist?(source_file) == false | ||||
|
r22 | log "The source file does not exist." | ||
|
r0 | exit(127) | ||
end | ||||
|
r22 | log "Making test result and sandbox directories..." | ||
|
r0 | |||
current_dir = `pwd` | ||||
current_dir.strip! | ||||
if ARGV.length >= 3 | ||||
test_result_dir = ARGV[2] | ||||
else | ||||
test_result_dir = "#{current_dir}/test-result" | ||||
end | ||||
|
r22 | log "Test result directory: #{test_result_dir}" | ||
|
r0 | system("rm -Rf #{test_result_dir}") | ||
execute("mkdir #{test_result_dir}", "Cannot make directory #{test_result_dir}.") | ||||
if ARGV.length >= 4 | ||||
sandbox_dir = ARGV[3] | ||||
else | ||||
sandbox_dir = "#{current_dir}/sandbox" | ||||
end | ||||
|
r22 | log "Sandbox directory: #{sandbox_dir}" | ||
|
r0 | system("rm -Rf #{sandbox_dir}") | ||
execute("mkdir #{sandbox_dir}", "Cannot make directory #{sandbox_dir}") | ||||
# Compile | ||||
|
r22 | log | ||
log "Compiling..." | ||||
|
r0 | execute("cp #{source_file} #{sandbox_dir}", "Cannot copy the source file to #{sandbox_dir}") | ||
begin | ||||
Dir.chdir sandbox_dir | ||||
rescue | ||||
|
r22 | log "ERROR: Cannot change directory to #{sandbox_dir}." | ||
|
r0 | exit(127) | ||
end | ||||
execute("#{problem_home}/script/compile #{language} #{source_file}", "Compilation error!") | ||||
compile_message = `cat compiler_message` | ||||
compile_message.strip! | ||||
execute("mv compiler_message #{test_result_dir}", "Cannot move the compiler message to #{test_result_dir}.") | ||||
|
r3 | if !FileTest.exist?("a.out") | ||
|
r22 | log "Cannot compile the source code. See message in #{test_result_dir}/compile_message" | ||
|
r0 | exit(127) | ||
else | ||||
execute("mv a.out #{test_result_dir}", "Cannot move the compiled program to #{test_result_dir}") | ||||
system("rm -Rf #{sandbox_dir}/*") | ||||
end | ||||
require "#{problem_home}/script/test_dsl.rb" | ||||
load "#{problem_home}/test_cases/all_tests.cfg" | ||||
problem = Problem.get_instance | ||||
if problem.well_formed? == false | ||||
|
r22 | log "The problem specification is not well formed." | ||
|
r0 | exit(127) | ||
end | ||||
# Doing the testing. | ||||
(1..(problem.num_tests)).each do |test_num| | ||||
|
r61 | |||
$stdout.print "[#{test_num}]" | ||||
$stdout.flush | ||||
|
r22 | log "Test number: #{test_num}" | ||
|
r0 | execute("cp #{test_result_dir}/a.out #{sandbox_dir}", "Cannot copy the compiled program into #{sandbox_dir}") | ||
|
r31 | begin | ||
execute("#{problem_home}/script/run #{language} #{test_num}", "Error occured during execution of the run script") | ||||
rescue | ||||
# do nothing | ||||
end | ||||
|
r0 | execute("mkdir #{test_result_dir}/#{test_num}", "Cannot create directory #{test_result_dir}/#{test_num}") | ||
execute("mv #{sandbox_dir}/result #{test_result_dir}/#{test_num}", "Cannot copy the result file into #{test_result_dir}/#{test_num}") | ||||
execute("mv #{sandbox_dir}/comment #{test_result_dir}/#{test_num}", "Cannot copy the comment file into #{test_result_dir}/#{test_num}") | ||||
|
r8 | execute("mv #{sandbox_dir}/output.txt #{test_result_dir}/#{test_num}", "Cannot copy the output file into #{test_result_dir}/#{test_num}") | ||
|
r0 | execute("rm -Rf #{sandbox_dir}/*", "Cannot clear #{sandbox_dir}") | ||
end | ||||
|
r61 | $stdout.print "[done]\n" | ||
|
r0 | # Grade | ||
|
r22 | log | ||
log "Grading..." | ||||
|
r0 | begin | ||
Dir.chdir test_result_dir | ||||
rescue | ||||
|
r22 | log "ERROR: Cannot change directory to #{test_result_dir}." | ||
|
r0 | exit(127) | ||
end | ||||
execute("#{problem_home}/script/grade", "An error occured during grading!") | ||||
|
r22 | log | ||
log "All done!" | ||||