# HG changeset patch # User Nattee Niparnan # Date 2014-08-19 02:47:25 # Node ID 3e585749d18c9657e2492b3b8e101aafadaed3c9 # Parent 39412152581401729164a8bb3d4919c97234a41e add python support we compile it into bytecode before run (see 'judge' and 'run') diff --git a/std-script/compile b/std-script/compile --- a/std-script/compile +++ b/std-script/compile @@ -27,12 +27,15 @@ CPLUSPLUS_COMPILER = "/usr/bin/g++" PASCAL_COMPILER = "/usr/bin/fpc" JAVA_COMPILER = "/usr/bin/javac" -RUBY_INTEPRETER = "/home/dae/.rvm/rubies/ruby-1.9.2-p320/bin/ruby" +RUBY_INTERPRETER = "/home/dae/.rvm/rubies/ruby-1.9.2-p320/bin/ruby" +PYTHON_INTERPRETER = "/usr/bin/python" +PYTHON_CHECKER = "/usr/bin/pyflakes" C_OPTIONS = "-O2 -s -static -std=c99 -DCONTEST -lm -Wall" CPLUSPLUS_OPTIONS = "-O2 -s -static -DCONTEST -lm -Wall" PASCAL_OPTIONS = "-O1 -XS -dCONTEST" JAVA_OPTIONS = "" +PYTHON_OPTIONS = "" # Check for the correct number of arguments. Otherwise, print usage. if ARGV.length == 0 or ARGV.length > 4 @@ -87,20 +90,20 @@ # Compile. case params[:prog_lang] - when "c" +when "c" command = "#{C_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{C_OPTIONS} 2> #{params[:message_file]}" system(command) - when "c++" +when "c++" command = "#{CPLUSPLUS_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{CPLUSPLUS_OPTIONS} 2> #{params[:message_file]}" system(command) - when "pas" +when "pas" command = "#{PASCAL_COMPILER} #{params[:source_file]} -ooutpas #{PASCAL_OPTIONS} > #{params[:message_file]}" system(command) FileUtils.mv("output", params[:output_file]) - when "java" +when "java" #rename the file to the public class name #get the class name @@ -117,18 +120,33 @@ File.chmod(0755, params[:output_file]) end - when "ruby" - command = "#{RUBY_INTEPRETER} -c #{params[:source_file]} > #{params[:message_file]}" +when "ruby" + command = "#{RUBY_INTERPRETER} -c #{params[:source_file]} > #{params[:message_file]}" system(command) File.open(params[:output_file],"w") do |out_file| - out_file.puts "#!#{RUBY_INTEPRETER}" + out_file.puts "#!#{RUBY_INTERPRETER}" File.open(params[:source_file],"r").each do |line| out_file.print line end end File.chmod(0755, params[:output_file]) + +when "python" + command = "#{PYTHON_CHECKER} #{params[:source_file]} > #{params[:message_file]}" + if system(command) + #compile to python bytecode + command = "#{PYTHON_INTERPRETER} -m py_compile #{params[:source_file]}" + puts "compile: #{command}" + system(command) + puts "pwd: " + Dir.pwd + Dir.new('.').each {|file| puts file} + File.open(params[:output_file],"w") do |out_file| + out_file.puts "#!#{PYTHON_INTERPRETER} #{params[:source_file]}c" + end + File.chmod(0755, params[:output_file]) + end - else +else talk("ERROR: Invalid language specified!") open(params[:message_file],"w") do |f| f.puts "ERROR: Invalid language specified!" diff --git a/std-script/judge b/std-script/judge --- a/std-script/judge +++ b/std-script/judge @@ -53,7 +53,7 @@ end language = ARGV[0] -if language != "c" && language != "c++" && language != "pas" && language != "java" && language != "ruby" +if language != "c" && language != "c++" && language != "pas" && language != "java" && language != "ruby" && language != "python" log "You specified a language that is not supported: #{language}." exit(127) end @@ -111,6 +111,7 @@ call_and_log("Cannot move the compiled program to #{test_result_dir}") { FileUtils.mv("a.out",test_result_dir) if language == "java" then Dir["*.class"].each { |file| FileUtils.mv(file,test_result_dir)} end + if language == "python" then Dir["*.pyc"].each { |file| FileUtils.mv(file,test_result_dir)} end } FileUtils.rm_rf("#{sandbox_dir}/.") end @@ -135,6 +136,7 @@ call_and_log("Cannot copy the compiled program into #{sandbox_dir}") { FileUtils.cp("#{test_result_dir}/a.out", sandbox_dir, :preserve => true) if language == "java" then Dir["#{test_result_dir}/*.class"].each { |file| FileUtils.cp(file,sandbox_dir)} end + if language == "python" then Dir["#{test_result_dir}/*.pyc"].each { |file| FileUtils.cp(file,sandbox_dir)} end } begin diff --git a/std-script/run b/std-script/run --- a/std-script/run +++ b/std-script/run @@ -96,9 +96,10 @@ run_command = "#{problem_home}/script/box -T -t #{time_limit} -s getppid -s clone -s wait4 -p /usr/bin/ -p ./ -i #{input_file_name} -o output.txt #{program_name} 2>run_result" when "ruby" run_command = "#{problem_home}/script/box -T -t #{time_limit} -s getppid -s wait4 -s clone -s set_robust_list -s futex -s sigaltstack -p /dev/urandom -p ./ -p /home/dae/.rvm/rubies/ruby-1.9.2-p320/ -p #{problem_home}/ -i #{input_file_name} -o output.txt #{program_name} 2>run_result" - when "c++" - run_command = "#{problem_home}/script/box -a 2 -f -T -t #{time_limit} -m #{mem_limit} -i #{input_file_name} -o output.txt #{program_name} 2>run_result" - else + when "python" + #this code just run without any checking + run_command = "#{problem_home}/script/box -T -t #{time_limit} -p #{problem_home}/ -i #{input_file_name} -o output.txt #{program_name} 2>run_result" + else # for c++, pascal, we do the normal checking run_command = "#{problem_home}/script/box -a 2 -f -T -t #{time_limit} -m #{mem_limit} -i #{input_file_name} -o output.txt #{program_name} 2>run_result" end