diff --git a/lib/engine.rb b/lib/engine.rb --- a/lib/engine.rb +++ b/lib/engine.rb @@ -1,5 +1,4 @@ require 'fileutils' -require 'ftools' require File.join(File.dirname(__FILE__),'dir_init') module Grader @@ -139,7 +138,7 @@ fname = File.basename(s) if !FileTest.exist?("#{script_dir}/#{fname}") copied << fname - system("cp #{s} #{script_dir}") + FileUtils.cp(s, "#{script_dir}") end end @@ -174,7 +173,7 @@ def clear_script(log,problem_home) log.each do |s| - system("rm #{problem_home}/script/#{s}") + FileUtils.rm("#{problem_home}/script/#{s}") end end diff --git a/lib/test_request_helper.rb b/lib/test_request_helper.rb --- a/lib/test_request_helper.rb +++ b/lib/test_request_helper.rb @@ -5,6 +5,23 @@ module Grader + def self.link_or_copy(src, des) + begin + FileUtils.ln_s(src, des) + rescue + FileUtils.cp(src,des) + end + end + + def self.call_and_log(error_message) + begin + yield + rescue + msg = "ERROR: #{error_message}" + raise msg + end + end + # # A TestRequestRoomMaker is a helper object for Engine # - finds grading room: in user_result_dir/(user)/test_request/ ... @@ -28,8 +45,8 @@ # to the sandbox directory later. The run script should do it. # if FileTest.exists?("#{test_request.input_file_name}.files") - cmd = "cp #{test_request.input_file_name}.files/* #{grading_room}" - system(cmd) + FileUtils.cp_r("#{test_request.input_file_name}.files/.", + "#{grading_room}") end grading_room @@ -82,11 +99,12 @@ end def copy_problem_template(template_dir,problem_home) - cmd = "cp -R #{template_dir}/* #{problem_home}" - system_and_raise_when_fail(cmd,"Test Request: cannot copy problem template") + Grader::call_and_log("Test Request: cannot copy problem template") { + FileUtils.cp_r("#{template_dir}/.","#{problem_home}") + } end - - def link_input_file(test_request,problem_home) + + def link_input_file(test_request, problem_home) input_fname = "#{test_request.input_file_name}" if !File.exists?(input_fname) raise "Test Request: input file not found." @@ -97,20 +115,14 @@ FileUtils.rm([input_fname_problem_home], :force => true) end - cmd = "ln -s #{input_fname} #{input_fname_problem_home}" - system_and_raise_when_fail(cmd,"Test Request: cannot link input file") + Grader::link_or_copy("#{input_fname}", "#{input_fname_problem_home}") end def remove_data_files(problem_home) if File.exists?("#{problem_home}/test_cases/1/input-1.txt") - cmd = "rm #{problem_home}/test_cases/1/*" - system_and_raise_when_fail(cmd,"Test Request: cannot remove data files") - end - end - - def system_and_raise_when_fail(cmd,msg) - if !system(cmd) - raise msg + Grader::call_and_log("Test Request: cannot remove data files") { + FileUtils.rm Dir.glob("#{problem_home}/test_cases/1/*") + } end end @@ -221,10 +233,7 @@ target_file_name = random_output_file_name(test_request.user, test_request.problem) FileUtils.mkdir_p(File.dirname(target_file_name)) - cmd = "ln -s #{fname} #{target_file_name}" - if !system(cmd) - raise "TestRequestReporter: cannot move output file" - end + Grader::link_or_copy("#{fname}", "#{target_file_name}") return target_file_name end diff --git a/std-script/judge b/std-script/judge --- a/std-script/judge +++ b/std-script/judge @@ -1,5 +1,7 @@ #!/usr/bin/ruby +require 'fileutils' + def log(str='') if ENV['TALKATIVE']!=nil puts str @@ -22,6 +24,21 @@ end end +def call_and_log(error_message) + begin + yield + rescue + msg = "ERROR: #{error_message}" + log msg + raise msg + end +end + +def clear_and_create_empty_dir(dir) + FileUtils.rm_rf(dir, :secure => true) + call_and_log("Cannot make directory #{dir}.") { FileUtils.mkdir(dir) } +end + # ARGV[0] --- language # ARGV[1] --- program source file # ARGV[2] --- test result directory @@ -57,9 +74,9 @@ else test_result_dir = "#{current_dir}/test-result" end + log "Test result directory: #{test_result_dir}" -system("rm -Rf #{test_result_dir}") -execute("mkdir #{test_result_dir}", "Cannot make directory #{test_result_dir}.") +clear_and_create_empty_dir(test_result_dir) if ARGV.length >= 4 sandbox_dir = ARGV[3] @@ -67,13 +84,14 @@ sandbox_dir = "#{current_dir}/sandbox" end log "Sandbox directory: #{sandbox_dir}" -system("rm -Rf #{sandbox_dir}") -execute("mkdir #{sandbox_dir}", "Cannot make directory #{sandbox_dir}") +clear_and_create_empty_dir(sandbox_dir) # Compile log log "Compiling..." -execute("cp #{source_file} #{sandbox_dir}", "Cannot copy the source file to #{sandbox_dir}") +call_and_log("Cannot copy the source file to #{sandbox_dir}") { + FileUtils.cp(source_file, sandbox_dir) +} begin Dir.chdir sandbox_dir rescue @@ -83,13 +101,17 @@ 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}.") +call_and_log("Cannot move the compiler message to #{test_result_dir}.") { + FileUtils.mv("compiler_message", test_result_dir) +} if !FileTest.exist?("a.out") log "Cannot compile the source code. See message in #{test_result_dir}/compile_message" exit(127) else - execute("mv a.out #{test_result_dir}", "Cannot move the compiled program to #{test_result_dir}") - system("rm -Rf #{sandbox_dir}/*") + call_and_log("Cannot move the compiled program to #{test_result_dir}") { + FileUtils.mv("a.out",test_result_dir) + } + FileUtils.rm_rf("#{sandbox_dir}/.") end require "#{problem_home}/script/test_dsl.rb" @@ -108,17 +130,29 @@ $stdout.flush log "Test number: #{test_num}" - execute("cp #{test_result_dir}/a.out #{sandbox_dir}", "Cannot copy the compiled program into #{sandbox_dir}") + call_and_log("Cannot copy the compiled program into #{sandbox_dir}") { + FileUtils.cp("#{test_result_dir}/a.out", sandbox_dir) + } begin execute("#{problem_home}/script/run #{language} #{test_num}", "Error occured during execution of the run script") rescue # do nothing end - 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}") - execute("mv #{sandbox_dir}/output.txt #{test_result_dir}/#{test_num}", "Cannot copy the output file into #{test_result_dir}/#{test_num}") - execute("rm -Rf #{sandbox_dir}/*", "Cannot clear #{sandbox_dir}") + call_and_log("Cannot create directory #{test_result_dir}/#{test_num}") { + FileUtils.mkdir "#{test_result_dir}/#{test_num}" + } + call_and_log("Cannot copy the result file into #{test_result_dir}/#{test_num}") { + FileUtils.mv "#{sandbox_dir}/result", "#{test_result_dir}/#{test_num}" + } + call_and_log("Cannot copy the comment file into #{test_result_dir}/#{test_num}") { + FileUtils.mv "#{sandbox_dir}/comment", "#{test_result_dir}/#{test_num}" + } + call_and_log("Cannot copy the output file into #{test_result_dir}/#{test_num}") { + FileUtils.mv "#{sandbox_dir}/output.txt", "#{test_result_dir}/#{test_num}" + } + call_and_log("Cannot clear #{sandbox_dir}") { + FileUtils.rm_rf(Dir.glob("#{sandbox_dir}/*"), :secure => true) + } end $stdout.print "[done]\n" diff --git a/test/engine_spec_helper.rb b/test/engine_spec_helper.rb --- a/test/engine_spec_helper.rb +++ b/test/engine_spec_helper.rb @@ -1,17 +1,18 @@ +require 'fileutils' + module GraderEngineHelperMethods def clear_sandbox config = Grader::Configuration.get_instance - clear_cmd = "rm -rf #{config.test_sandbox_dir}/*" - system(clear_cmd) + FileUtils.rm_rf(Dir.glob("#{config.test_sandbox_dir}/*"), + :secure => true) end def init_sandbox config = Grader::Configuration.get_instance clear_sandbox FileUtils.mkdir_p config.user_result_dir - cp_cmd = "cp -R #{config.test_data_dir}/ev #{config.test_sandbox_dir}" - system(cp_cmd) + FileUtils.cp_r("#{config.test_data_dir}/ev", "#{config.test_sandbox_dir}") end def create_submission_from_file(id, user, problem,