Description:
add python support
we compile it into bytecode before run
(see 'judge' and 'run')
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r154:3e585749d18c - - 3 files changed: 34 inserted, 13 deleted
@@ -1,144 +1,162 | |||||
|
1 | #!/usr/bin/env ruby |
|
1 | #!/usr/bin/env ruby |
|
2 |
|
2 | ||
|
3 | require 'fileutils' |
|
3 | require 'fileutils' |
|
4 |
|
4 | ||
|
5 | ############################## |
|
5 | ############################## |
|
6 | # |
|
6 | # |
|
7 | # Standard Compile Script |
|
7 | # Standard Compile Script |
|
8 | # |
|
8 | # |
|
9 | # Supported compilers: |
|
9 | # Supported compilers: |
|
10 | # gcc, g++, and fpc. |
|
10 | # gcc, g++, and fpc. |
|
11 | # |
|
11 | # |
|
12 | ############################## |
|
12 | ############################## |
|
13 |
|
13 | ||
|
14 | def talk(str='') |
|
14 | def talk(str='') |
|
15 | if ENV['TALKATIVE']!=nil |
|
15 | if ENV['TALKATIVE']!=nil |
|
16 | puts str |
|
16 | puts str |
|
17 | end |
|
17 | end |
|
18 | if ENV['GRADER_LOGGING']!=nil |
|
18 | if ENV['GRADER_LOGGING']!=nil |
|
19 | log_fname = ENV['GRADER_LOGGING'] |
|
19 | log_fname = ENV['GRADER_LOGGING'] |
|
20 | fp = File.open(log_fname,"a") |
|
20 | fp = File.open(log_fname,"a") |
|
21 | fp.puts("run: #{Time.new.strftime("%H:%M")} #{str}") |
|
21 | fp.puts("run: #{Time.new.strftime("%H:%M")} #{str}") |
|
22 | fp.close |
|
22 | fp.close |
|
23 | end |
|
23 | end |
|
24 | end |
|
24 | end |
|
25 |
|
25 | ||
|
26 | C_COMPILER = "/usr/bin/gcc" |
|
26 | C_COMPILER = "/usr/bin/gcc" |
|
27 | CPLUSPLUS_COMPILER = "/usr/bin/g++" |
|
27 | CPLUSPLUS_COMPILER = "/usr/bin/g++" |
|
28 | PASCAL_COMPILER = "/usr/bin/fpc" |
|
28 | PASCAL_COMPILER = "/usr/bin/fpc" |
|
29 | JAVA_COMPILER = "/usr/bin/javac" |
|
29 | JAVA_COMPILER = "/usr/bin/javac" |
|
30 | - RUBY_INTEPRETER = "/home/dae/.rvm/rubies/ruby-1.9.2-p320/bin/ruby" |
|
30 | + RUBY_INTERPRETER = "/home/dae/.rvm/rubies/ruby-1.9.2-p320/bin/ruby" |
|
|
31 | + PYTHON_INTERPRETER = "/usr/bin/python" | ||
|
|
32 | + PYTHON_CHECKER = "/usr/bin/pyflakes" | ||
|
31 |
|
33 | ||
|
32 | C_OPTIONS = "-O2 -s -static -std=c99 -DCONTEST -lm -Wall" |
|
34 | C_OPTIONS = "-O2 -s -static -std=c99 -DCONTEST -lm -Wall" |
|
33 | CPLUSPLUS_OPTIONS = "-O2 -s -static -DCONTEST -lm -Wall" |
|
35 | CPLUSPLUS_OPTIONS = "-O2 -s -static -DCONTEST -lm -Wall" |
|
34 | PASCAL_OPTIONS = "-O1 -XS -dCONTEST" |
|
36 | PASCAL_OPTIONS = "-O1 -XS -dCONTEST" |
|
35 | JAVA_OPTIONS = "" |
|
37 | JAVA_OPTIONS = "" |
|
|
38 | + PYTHON_OPTIONS = "" | ||
|
36 |
|
39 | ||
|
37 | # Check for the correct number of arguments. Otherwise, print usage. |
|
40 | # Check for the correct number of arguments. Otherwise, print usage. |
|
38 | if ARGV.length == 0 or ARGV.length > 4 |
|
41 | if ARGV.length == 0 or ARGV.length > 4 |
|
39 | puts "Usage: compile <language> [<source-file>] [<output-file>] [<message-file>]" |
|
42 | puts "Usage: compile <language> [<source-file>] [<output-file>] [<message-file>]" |
|
40 | puts |
|
43 | puts |
|
41 | puts "<source-file> is defaulted to \"source\"." |
|
44 | puts "<source-file> is defaulted to \"source\"." |
|
42 | puts "<output-file> is defaulted to \"a.out\"." |
|
45 | puts "<output-file> is defaulted to \"a.out\"." |
|
43 | puts "<message-file> is defaulted to \"compiler_message\"." |
|
46 | puts "<message-file> is defaulted to \"compiler_message\"." |
|
44 | puts |
|
47 | puts |
|
45 | exit(127) |
|
48 | exit(127) |
|
46 | end |
|
49 | end |
|
47 |
|
50 | ||
|
48 | PARAMS = { |
|
51 | PARAMS = { |
|
49 | :source_file => [1,'source'], |
|
52 | :source_file => [1,'source'], |
|
50 | :output_file => [2,'a.out'], |
|
53 | :output_file => [2,'a.out'], |
|
51 | :message_file => [3,'compiler_message'] |
|
54 | :message_file => [3,'compiler_message'] |
|
52 | } |
|
55 | } |
|
53 |
|
56 | ||
|
54 | params = {} |
|
57 | params = {} |
|
55 | params[:prog_lang] = ARGV[0] |
|
58 | params[:prog_lang] = ARGV[0] |
|
56 | PARAMS.each_key do |param_name| |
|
59 | PARAMS.each_key do |param_name| |
|
57 | index, default = PARAMS[param_name] |
|
60 | index, default = PARAMS[param_name] |
|
58 | if ARGV.length > index |
|
61 | if ARGV.length > index |
|
59 | params[param_name] = ARGV[index] |
|
62 | params[param_name] = ARGV[index] |
|
60 | else |
|
63 | else |
|
61 | params[param_name] = default |
|
64 | params[param_name] = default |
|
62 | end |
|
65 | end |
|
63 | talk "#{param_name}: #{params[param_name]}" |
|
66 | talk "#{param_name}: #{params[param_name]}" |
|
64 | end |
|
67 | end |
|
65 |
|
68 | ||
|
66 | # Remove any remaining output files or message files. |
|
69 | # Remove any remaining output files or message files. |
|
67 | if FileTest.exists? params[:output_file] |
|
70 | if FileTest.exists? params[:output_file] |
|
68 | FileUtils.rm(params[:output_file]) |
|
71 | FileUtils.rm(params[:output_file]) |
|
69 | end |
|
72 | end |
|
70 | if FileTest.exists? params[:message_file] |
|
73 | if FileTest.exists? params[:message_file] |
|
71 | FileUtils.rm(params[:message_file]) |
|
74 | FileUtils.rm(params[:message_file]) |
|
72 | end |
|
75 | end |
|
73 |
|
76 | ||
|
74 | # Check if the source file exists before attempt compiling. |
|
77 | # Check if the source file exists before attempt compiling. |
|
75 | if !FileTest.exists? params[:source_file] |
|
78 | if !FileTest.exists? params[:source_file] |
|
76 | talk("ERROR: The source file does not exist!") |
|
79 | talk("ERROR: The source file does not exist!") |
|
77 | open(params[:message_file],"w") do |f| |
|
80 | open(params[:message_file],"w") do |f| |
|
78 | f.puts "ERROR: The source file did not exist." |
|
81 | f.puts "ERROR: The source file did not exist." |
|
79 | end |
|
82 | end |
|
80 | exit(127) |
|
83 | exit(127) |
|
81 | end |
|
84 | end |
|
82 |
|
85 | ||
|
83 | if params[:prog_lang]=='cpp' |
|
86 | if params[:prog_lang]=='cpp' |
|
84 | params[:prog_lang] = 'c++' |
|
87 | params[:prog_lang] = 'c++' |
|
85 | end |
|
88 | end |
|
86 |
|
89 | ||
|
87 | # Compile. |
|
90 | # Compile. |
|
88 | case params[:prog_lang] |
|
91 | case params[:prog_lang] |
|
89 |
|
92 | ||
|
90 |
- |
|
93 | + when "c" |
|
91 | command = "#{C_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{C_OPTIONS} 2> #{params[:message_file]}" |
|
94 | command = "#{C_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{C_OPTIONS} 2> #{params[:message_file]}" |
|
92 | system(command) |
|
95 | system(command) |
|
93 |
|
96 | ||
|
94 |
- |
|
97 | + when "c++" |
|
95 | command = "#{CPLUSPLUS_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{CPLUSPLUS_OPTIONS} 2> #{params[:message_file]}" |
|
98 | command = "#{CPLUSPLUS_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{CPLUSPLUS_OPTIONS} 2> #{params[:message_file]}" |
|
96 | system(command) |
|
99 | system(command) |
|
97 |
|
100 | ||
|
98 |
- |
|
101 | + when "pas" |
|
99 | command = "#{PASCAL_COMPILER} #{params[:source_file]} -ooutpas #{PASCAL_OPTIONS} > #{params[:message_file]}" |
|
102 | command = "#{PASCAL_COMPILER} #{params[:source_file]} -ooutpas #{PASCAL_OPTIONS} > #{params[:message_file]}" |
|
100 | system(command) |
|
103 | system(command) |
|
101 | FileUtils.mv("output", params[:output_file]) |
|
104 | FileUtils.mv("output", params[:output_file]) |
|
102 |
|
105 | ||
|
103 |
- |
|
106 | + when "java" |
|
104 | #rename the file to the public class name |
|
107 | #rename the file to the public class name |
|
105 |
|
108 | ||
|
106 | #get the class name |
|
109 | #get the class name |
|
107 | classname = 'DUMMY' |
|
110 | classname = 'DUMMY' |
|
108 | File.foreach(params[:source_file]) do |line| |
|
111 | File.foreach(params[:source_file]) do |line| |
|
109 | md = /\s*public\s*class\s*(\w*)/.match(line) |
|
112 | md = /\s*public\s*class\s*(\w*)/.match(line) |
|
110 | classname=md[1] if md |
|
113 | classname=md[1] if md |
|
111 | end |
|
114 | end |
|
112 | system("cp #{params[:source_file]} #{classname}.java") |
|
115 | system("cp #{params[:source_file]} #{classname}.java") |
|
113 | command = "#{JAVA_COMPILER} #{classname}.java 2> #{params[:message_file]}" |
|
116 | command = "#{JAVA_COMPILER} #{classname}.java 2> #{params[:message_file]}" |
|
114 | system(command) |
|
117 | system(command) |
|
115 | if File.exists?(classname + ".class") |
|
118 | if File.exists?(classname + ".class") |
|
116 | File.open(params[:output_file],"w") {|file| file.write("#!/bin/sh\n/usr/bin/java #{classname}\n")} |
|
119 | File.open(params[:output_file],"w") {|file| file.write("#!/bin/sh\n/usr/bin/java #{classname}\n")} |
|
117 | File.chmod(0755, params[:output_file]) |
|
120 | File.chmod(0755, params[:output_file]) |
|
118 | end |
|
121 | end |
|
119 |
|
122 | ||
|
120 |
- |
|
123 | + when "ruby" |
|
121 | - command = "#{RUBY_INTEPRETER} -c #{params[:source_file]} > #{params[:message_file]}" |
|
124 | + command = "#{RUBY_INTERPRETER} -c #{params[:source_file]} > #{params[:message_file]}" |
|
122 | system(command) |
|
125 | system(command) |
|
123 | File.open(params[:output_file],"w") do |out_file| |
|
126 | File.open(params[:output_file],"w") do |out_file| |
|
124 | - out_file.puts "#!#{RUBY_INTEPRETER}" |
|
127 | + out_file.puts "#!#{RUBY_INTERPRETER}" |
|
125 | File.open(params[:source_file],"r").each do |line| |
|
128 | File.open(params[:source_file],"r").each do |line| |
|
126 | out_file.print line |
|
129 | out_file.print line |
|
127 | end |
|
130 | end |
|
128 | end |
|
131 | end |
|
129 | File.chmod(0755, params[:output_file]) |
|
132 | File.chmod(0755, params[:output_file]) |
|
|
133 | + | ||
|
|
134 | + when "python" | ||
|
|
135 | + command = "#{PYTHON_CHECKER} #{params[:source_file]} > #{params[:message_file]}" | ||
|
|
136 | + if system(command) | ||
|
|
137 | + #compile to python bytecode | ||
|
|
138 | + command = "#{PYTHON_INTERPRETER} -m py_compile #{params[:source_file]}" | ||
|
|
139 | + puts "compile: #{command}" | ||
|
|
140 | + system(command) | ||
|
|
141 | + puts "pwd: " + Dir.pwd | ||
|
|
142 | + Dir.new('.').each {|file| puts file} | ||
|
|
143 | + File.open(params[:output_file],"w") do |out_file| | ||
|
|
144 | + out_file.puts "#!#{PYTHON_INTERPRETER} #{params[:source_file]}c" | ||
|
|
145 | + end | ||
|
|
146 | + File.chmod(0755, params[:output_file]) | ||
|
|
147 | + end | ||
|
130 |
|
148 | ||
|
131 |
- |
|
149 | + else |
|
132 | talk("ERROR: Invalid language specified!") |
|
150 | talk("ERROR: Invalid language specified!") |
|
133 | open(params[:message_file],"w") do |f| |
|
151 | open(params[:message_file],"w") do |f| |
|
134 | f.puts "ERROR: Invalid language specified!" |
|
152 | f.puts "ERROR: Invalid language specified!" |
|
135 | end |
|
153 | end |
|
136 | exit(127) |
|
154 | exit(127) |
|
137 | end |
|
155 | end |
|
138 |
|
156 | ||
|
139 | # Report success or failure. |
|
157 | # Report success or failure. |
|
140 | if FileTest.exists? params[:output_file] |
|
158 | if FileTest.exists? params[:output_file] |
|
141 | talk "Compilation was successful!" |
|
159 | talk "Compilation was successful!" |
|
142 | else |
|
160 | else |
|
143 | talk "ERROR: Something was wrong during the compilation!" |
|
161 | talk "ERROR: Something was wrong during the compilation!" |
|
144 | end |
|
162 | end |
@@ -1,177 +1,179 | |||||
|
1 | #!/usr/bin/env ruby |
|
1 | #!/usr/bin/env ruby |
|
2 |
|
2 | ||
|
3 | require 'fileutils' |
|
3 | require 'fileutils' |
|
4 |
|
4 | ||
|
5 | def log(str='') |
|
5 | def log(str='') |
|
6 | if ENV['TALKATIVE']!=nil |
|
6 | if ENV['TALKATIVE']!=nil |
|
7 | puts str |
|
7 | puts str |
|
8 | end |
|
8 | end |
|
9 | if ENV['GRADER_LOGGING']!=nil |
|
9 | if ENV['GRADER_LOGGING']!=nil |
|
10 | log_fname = ENV['GRADER_LOGGING'] |
|
10 | log_fname = ENV['GRADER_LOGGING'] |
|
11 | fp = File.open(log_fname,"a") |
|
11 | fp = File.open(log_fname,"a") |
|
12 | fp.puts("judge: #{Time.new.strftime("%H:%M")} #{str}") |
|
12 | fp.puts("judge: #{Time.new.strftime("%H:%M")} #{str}") |
|
13 | fp.close |
|
13 | fp.close |
|
14 | end |
|
14 | end |
|
15 | end |
|
15 | end |
|
16 |
|
16 | ||
|
17 | problem_home = ENV['PROBLEM_HOME'] |
|
17 | problem_home = ENV['PROBLEM_HOME'] |
|
18 |
|
18 | ||
|
19 | def execute(command, error_message="") |
|
19 | def execute(command, error_message="") |
|
20 | if not system(command) |
|
20 | if not system(command) |
|
21 | msg = "ERROR: #{error_message}" |
|
21 | msg = "ERROR: #{error_message}" |
|
22 | log msg |
|
22 | log msg |
|
23 | raise(msg) |
|
23 | raise(msg) |
|
24 | end |
|
24 | end |
|
25 | end |
|
25 | end |
|
26 |
|
26 | ||
|
27 | def call_and_log(error_message) |
|
27 | def call_and_log(error_message) |
|
28 | begin |
|
28 | begin |
|
29 | yield |
|
29 | yield |
|
30 | rescue |
|
30 | rescue |
|
31 | msg = "ERROR: #{error_message}" |
|
31 | msg = "ERROR: #{error_message}" |
|
32 | log msg |
|
32 | log msg |
|
33 | raise msg |
|
33 | raise msg |
|
34 | end |
|
34 | end |
|
35 | end |
|
35 | end |
|
36 |
|
36 | ||
|
37 | def clear_and_create_empty_dir(dir) |
|
37 | def clear_and_create_empty_dir(dir) |
|
38 | FileUtils.rm_rf(dir, :secure => true) |
|
38 | FileUtils.rm_rf(dir, :secure => true) |
|
39 | call_and_log("Cannot make directory #{dir}.") { FileUtils.mkdir(dir) } |
|
39 | call_and_log("Cannot make directory #{dir}.") { FileUtils.mkdir(dir) } |
|
40 | end |
|
40 | end |
|
41 |
|
41 | ||
|
42 | # ARGV[0] --- language |
|
42 | # ARGV[0] --- language |
|
43 | # ARGV[1] --- program source file |
|
43 | # ARGV[1] --- program source file |
|
44 | # ARGV[2] --- test result directory |
|
44 | # ARGV[2] --- test result directory |
|
45 | # ARGV[3] --- sandbox directory |
|
45 | # ARGV[3] --- sandbox directory |
|
46 |
|
46 | ||
|
47 | if ARGV.length < 2 || ARGV.length > 4 |
|
47 | if ARGV.length < 2 || ARGV.length > 4 |
|
48 | puts "Usage: judge <language> <program-source> [<test-result-directory>] [<sandbox-directory>]" |
|
48 | puts "Usage: judge <language> <program-source> [<test-result-directory>] [<sandbox-directory>]" |
|
49 | puts " <sandbox-directory> is defaulted to ./sandbox" |
|
49 | puts " <sandbox-directory> is defaulted to ./sandbox" |
|
50 | puts " <test-result-directory> is defaulted to ./test-result" |
|
50 | puts " <test-result-directory> is defaulted to ./test-result" |
|
51 | puts "WARNING: The judge script will forcefully create the (implicitly and explicitly) specified directories and remove anything inside it." |
|
51 | puts "WARNING: The judge script will forcefully create the (implicitly and explicitly) specified directories and remove anything inside it." |
|
52 | exit(127) |
|
52 | exit(127) |
|
53 | end |
|
53 | end |
|
54 |
|
54 | ||
|
55 | language = ARGV[0] |
|
55 | language = ARGV[0] |
|
56 | - if language != "c" && language != "c++" && language != "pas" && language != "java" && language != "ruby" |
|
56 | + if language != "c" && language != "c++" && language != "pas" && language != "java" && language != "ruby" && language != "python" |
|
57 | log "You specified a language that is not supported: #{language}." |
|
57 | log "You specified a language that is not supported: #{language}." |
|
58 | exit(127) |
|
58 | exit(127) |
|
59 | end |
|
59 | end |
|
60 |
|
60 | ||
|
61 | source_file = ARGV[1] |
|
61 | source_file = ARGV[1] |
|
62 | if File.exist?(source_file) == false |
|
62 | if File.exist?(source_file) == false |
|
63 | log "The source file does not exist." |
|
63 | log "The source file does not exist." |
|
64 | exit(127) |
|
64 | exit(127) |
|
65 | end |
|
65 | end |
|
66 |
|
66 | ||
|
67 | log "Making test result and sandbox directories..." |
|
67 | log "Making test result and sandbox directories..." |
|
68 |
|
68 | ||
|
69 | current_dir = FileUtils.pwd |
|
69 | current_dir = FileUtils.pwd |
|
70 | current_dir.strip! |
|
70 | current_dir.strip! |
|
71 |
|
71 | ||
|
72 | if ARGV.length >= 3 |
|
72 | if ARGV.length >= 3 |
|
73 | test_result_dir = ARGV[2] |
|
73 | test_result_dir = ARGV[2] |
|
74 | else |
|
74 | else |
|
75 | test_result_dir = "#{current_dir}/test-result" |
|
75 | test_result_dir = "#{current_dir}/test-result" |
|
76 | end |
|
76 | end |
|
77 |
|
77 | ||
|
78 | log "Test result directory: #{test_result_dir}" |
|
78 | log "Test result directory: #{test_result_dir}" |
|
79 | clear_and_create_empty_dir(test_result_dir) |
|
79 | clear_and_create_empty_dir(test_result_dir) |
|
80 |
|
80 | ||
|
81 | if ARGV.length >= 4 |
|
81 | if ARGV.length >= 4 |
|
82 | sandbox_dir = ARGV[3] |
|
82 | sandbox_dir = ARGV[3] |
|
83 | else |
|
83 | else |
|
84 | sandbox_dir = "#{current_dir}/sandbox" |
|
84 | sandbox_dir = "#{current_dir}/sandbox" |
|
85 | end |
|
85 | end |
|
86 | log "Sandbox directory: #{sandbox_dir}" |
|
86 | log "Sandbox directory: #{sandbox_dir}" |
|
87 | clear_and_create_empty_dir(sandbox_dir) |
|
87 | clear_and_create_empty_dir(sandbox_dir) |
|
88 |
|
88 | ||
|
89 | # Compile |
|
89 | # Compile |
|
90 | log |
|
90 | log |
|
91 | log "Compiling..." |
|
91 | log "Compiling..." |
|
92 | call_and_log("Cannot copy the source file to #{sandbox_dir}") { |
|
92 | call_and_log("Cannot copy the source file to #{sandbox_dir}") { |
|
93 | FileUtils.cp(source_file, sandbox_dir) |
|
93 | FileUtils.cp(source_file, sandbox_dir) |
|
94 | } |
|
94 | } |
|
95 | begin |
|
95 | begin |
|
96 | Dir.chdir sandbox_dir |
|
96 | Dir.chdir sandbox_dir |
|
97 | rescue |
|
97 | rescue |
|
98 | log "ERROR: Cannot change directory to #{sandbox_dir}." |
|
98 | log "ERROR: Cannot change directory to #{sandbox_dir}." |
|
99 | exit(127) |
|
99 | exit(127) |
|
100 | end |
|
100 | end |
|
101 | execute("#{problem_home}/script/compile #{language} #{source_file}", "Compilation error!") |
|
101 | execute("#{problem_home}/script/compile #{language} #{source_file}", "Compilation error!") |
|
102 | compile_message = open("compiler_message").read |
|
102 | compile_message = open("compiler_message").read |
|
103 | compile_message.strip! |
|
103 | compile_message.strip! |
|
104 | call_and_log("Cannot move the compiler message to #{test_result_dir}.") { |
|
104 | call_and_log("Cannot move the compiler message to #{test_result_dir}.") { |
|
105 | FileUtils.mv("compiler_message", test_result_dir) |
|
105 | FileUtils.mv("compiler_message", test_result_dir) |
|
106 | } |
|
106 | } |
|
107 | if !FileTest.exist?("a.out") |
|
107 | if !FileTest.exist?("a.out") |
|
108 | log "Cannot compile the source code. See message in #{test_result_dir}/compile_message" |
|
108 | log "Cannot compile the source code. See message in #{test_result_dir}/compile_message" |
|
109 | exit(127) |
|
109 | exit(127) |
|
110 | else |
|
110 | else |
|
111 | call_and_log("Cannot move the compiled program to #{test_result_dir}") { |
|
111 | call_and_log("Cannot move the compiled program to #{test_result_dir}") { |
|
112 | FileUtils.mv("a.out",test_result_dir) |
|
112 | FileUtils.mv("a.out",test_result_dir) |
|
113 | if language == "java" then Dir["*.class"].each { |file| FileUtils.mv(file,test_result_dir)} end |
|
113 | if language == "java" then Dir["*.class"].each { |file| FileUtils.mv(file,test_result_dir)} end |
|
|
114 | + if language == "python" then Dir["*.pyc"].each { |file| FileUtils.mv(file,test_result_dir)} end | ||
|
114 | } |
|
115 | } |
|
115 | FileUtils.rm_rf("#{sandbox_dir}/.") |
|
116 | FileUtils.rm_rf("#{sandbox_dir}/.") |
|
116 | end |
|
117 | end |
|
117 |
|
118 | ||
|
118 | require "#{problem_home}/script/test_dsl.rb" |
|
119 | require "#{problem_home}/script/test_dsl.rb" |
|
119 | load "#{problem_home}/test_cases/all_tests.cfg" |
|
120 | load "#{problem_home}/test_cases/all_tests.cfg" |
|
120 | problem = Problem.get_instance |
|
121 | problem = Problem.get_instance |
|
121 |
|
122 | ||
|
122 | if problem.well_formed? == false |
|
123 | if problem.well_formed? == false |
|
123 | log "The problem specification is not well formed." |
|
124 | log "The problem specification is not well formed." |
|
124 | exit(127) |
|
125 | exit(127) |
|
125 | end |
|
126 | end |
|
126 |
|
127 | ||
|
127 | # Doing the testing. |
|
128 | # Doing the testing. |
|
128 | (1..(problem.num_tests)).each do |test_num| |
|
129 | (1..(problem.num_tests)).each do |test_num| |
|
129 |
|
130 | ||
|
130 | $stdout.print "[#{test_num}]" |
|
131 | $stdout.print "[#{test_num}]" |
|
131 | $stdout.flush |
|
132 | $stdout.flush |
|
132 |
|
133 | ||
|
133 | log "Test number: #{test_num}" |
|
134 | log "Test number: #{test_num}" |
|
134 |
|
135 | ||
|
135 | call_and_log("Cannot copy the compiled program into #{sandbox_dir}") { |
|
136 | call_and_log("Cannot copy the compiled program into #{sandbox_dir}") { |
|
136 | FileUtils.cp("#{test_result_dir}/a.out", sandbox_dir, :preserve => true) |
|
137 | FileUtils.cp("#{test_result_dir}/a.out", sandbox_dir, :preserve => true) |
|
137 | if language == "java" then Dir["#{test_result_dir}/*.class"].each { |file| FileUtils.cp(file,sandbox_dir)} end |
|
138 | if language == "java" then Dir["#{test_result_dir}/*.class"].each { |file| FileUtils.cp(file,sandbox_dir)} end |
|
|
139 | + if language == "python" then Dir["#{test_result_dir}/*.pyc"].each { |file| FileUtils.cp(file,sandbox_dir)} end | ||
|
138 | } |
|
140 | } |
|
139 |
|
141 | ||
|
140 | begin |
|
142 | begin |
|
141 | execute("#{problem_home}/script/run #{language} #{test_num}", "Error occured during execution of the run script") |
|
143 | execute("#{problem_home}/script/run #{language} #{test_num}", "Error occured during execution of the run script") |
|
142 | rescue |
|
144 | rescue |
|
143 | # do nothing |
|
145 | # do nothing |
|
144 | end |
|
146 | end |
|
145 |
|
147 | ||
|
146 | call_and_log("Cannot create directory #{test_result_dir}/#{test_num}") { |
|
148 | call_and_log("Cannot create directory #{test_result_dir}/#{test_num}") { |
|
147 | FileUtils.mkdir "#{test_result_dir}/#{test_num}" |
|
149 | FileUtils.mkdir "#{test_result_dir}/#{test_num}" |
|
148 | } |
|
150 | } |
|
149 | call_and_log("Cannot copy the result file into #{test_result_dir}/#{test_num}") { |
|
151 | call_and_log("Cannot copy the result file into #{test_result_dir}/#{test_num}") { |
|
150 | FileUtils.mv "#{sandbox_dir}/result", "#{test_result_dir}/#{test_num}" |
|
152 | FileUtils.mv "#{sandbox_dir}/result", "#{test_result_dir}/#{test_num}" |
|
151 | } |
|
153 | } |
|
152 | call_and_log("Cannot copy the comment file into #{test_result_dir}/#{test_num}") { |
|
154 | call_and_log("Cannot copy the comment file into #{test_result_dir}/#{test_num}") { |
|
153 | FileUtils.mv "#{sandbox_dir}/comment", "#{test_result_dir}/#{test_num}" |
|
155 | FileUtils.mv "#{sandbox_dir}/comment", "#{test_result_dir}/#{test_num}" |
|
154 | } |
|
156 | } |
|
155 | call_and_log("Cannot copy the output file into #{test_result_dir}/#{test_num}") { |
|
157 | call_and_log("Cannot copy the output file into #{test_result_dir}/#{test_num}") { |
|
156 | FileUtils.mv "#{sandbox_dir}/output.txt", "#{test_result_dir}/#{test_num}" |
|
158 | FileUtils.mv "#{sandbox_dir}/output.txt", "#{test_result_dir}/#{test_num}" |
|
157 | } |
|
159 | } |
|
158 | call_and_log("Cannot clear #{sandbox_dir}") { |
|
160 | call_and_log("Cannot clear #{sandbox_dir}") { |
|
159 | FileUtils.rm_rf(Dir.glob("#{sandbox_dir}/*"), :secure => true) |
|
161 | FileUtils.rm_rf(Dir.glob("#{sandbox_dir}/*"), :secure => true) |
|
160 | } |
|
162 | } |
|
161 | end |
|
163 | end |
|
162 |
|
164 | ||
|
163 | $stdout.print "[done]\n" |
|
165 | $stdout.print "[done]\n" |
|
164 |
|
166 | ||
|
165 | # Grade |
|
167 | # Grade |
|
166 | log |
|
168 | log |
|
167 | log "Grading..." |
|
169 | log "Grading..." |
|
168 | begin |
|
170 | begin |
|
169 | Dir.chdir test_result_dir |
|
171 | Dir.chdir test_result_dir |
|
170 | rescue |
|
172 | rescue |
|
171 | log "ERROR: Cannot change directory to #{test_result_dir}." |
|
173 | log "ERROR: Cannot change directory to #{test_result_dir}." |
|
172 | exit(127) |
|
174 | exit(127) |
|
173 | end |
|
175 | end |
|
174 | execute("#{problem_home}/script/grade", "An error occured during grading!") |
|
176 | execute("#{problem_home}/script/grade", "An error occured during grading!") |
|
175 |
|
177 | ||
|
176 | log |
|
178 | log |
|
177 | log "All done!" |
|
179 | log "All done!" |
@@ -3,173 +3,174 | |||||
|
3 | require 'fileutils' |
|
3 | require 'fileutils' |
|
4 |
|
4 | ||
|
5 | def log(str='') |
|
5 | def log(str='') |
|
6 | if ENV['TALKATIVE']!=nil |
|
6 | if ENV['TALKATIVE']!=nil |
|
7 | puts str |
|
7 | puts str |
|
8 | end |
|
8 | end |
|
9 | if ENV['GRADER_LOGGING']!=nil |
|
9 | if ENV['GRADER_LOGGING']!=nil |
|
10 | log_fname = ENV['GRADER_LOGGING'] |
|
10 | log_fname = ENV['GRADER_LOGGING'] |
|
11 | fp = File.open(log_fname,"a") |
|
11 | fp = File.open(log_fname,"a") |
|
12 | fp.puts("run: #{Time.new.strftime("%H:%M")} #{str}") |
|
12 | fp.puts("run: #{Time.new.strftime("%H:%M")} #{str}") |
|
13 | fp.close |
|
13 | fp.close |
|
14 | end |
|
14 | end |
|
15 | end |
|
15 | end |
|
16 |
|
16 | ||
|
17 | def extract_time(t) |
|
17 | def extract_time(t) |
|
18 | #puts "TIME: #{t}" |
|
18 | #puts "TIME: #{t}" |
|
19 | #if (result=/^(.*)real(.*)wall(.*)s/.match(t)) |
|
19 | #if (result=/^(.*)real(.*)wall(.*)s/.match(t)) |
|
20 | if (result=/^OK \((.*) sec real\, (.*) sec wall,.*MB, (.*) syscalls/.match(t)) |
|
20 | if (result=/^OK \((.*) sec real\, (.*) sec wall,.*MB, (.*) syscalls/.match(t)) |
|
21 | {:real => result[1], :user => result[2], :sys => result[3]} |
|
21 | {:real => result[1], :user => result[2], :sys => result[3]} |
|
22 | else |
|
22 | else |
|
23 | #{:real => 0, :user => 0, :sys => 0} |
|
23 | #{:real => 0, :user => 0, :sys => 0} |
|
24 | #puts "ERROR READING RUNNING TIME: #{t}" |
|
24 | #puts "ERROR READING RUNNING TIME: #{t}" |
|
25 | raise "Error reading running time: #{t}" |
|
25 | raise "Error reading running time: #{t}" |
|
26 | end |
|
26 | end |
|
27 | end |
|
27 | end |
|
28 |
|
28 | ||
|
29 | def compile_box(source,bin) |
|
29 | def compile_box(source,bin) |
|
30 | system("g++ #{source} -o #{bin}") |
|
30 | system("g++ #{source} -o #{bin}") |
|
31 | end |
|
31 | end |
|
32 |
|
32 | ||
|
33 | if ARGV.length < 2 || ARGV.length > 3 |
|
33 | if ARGV.length < 2 || ARGV.length > 3 |
|
34 | puts "Usage: run <language> <test-num> [<program-name>]" |
|
34 | puts "Usage: run <language> <test-num> [<program-name>]" |
|
35 | exit(127) |
|
35 | exit(127) |
|
36 | end |
|
36 | end |
|
37 |
|
37 | ||
|
38 | language = ARGV[0] |
|
38 | language = ARGV[0] |
|
39 | test_num = ARGV[1].to_i |
|
39 | test_num = ARGV[1].to_i |
|
40 | if ARGV.length > 2 |
|
40 | if ARGV.length > 2 |
|
41 | program_name = ARGV[2] |
|
41 | program_name = ARGV[2] |
|
42 | else |
|
42 | else |
|
43 | program_name = "a.out" |
|
43 | program_name = "a.out" |
|
44 | end |
|
44 | end |
|
45 |
|
45 | ||
|
46 | problem_home = ENV['PROBLEM_HOME'] |
|
46 | problem_home = ENV['PROBLEM_HOME'] |
|
47 | require "#{problem_home}/script/test_dsl.rb" |
|
47 | require "#{problem_home}/script/test_dsl.rb" |
|
48 | load "#{problem_home}/test_cases/all_tests.cfg" |
|
48 | load "#{problem_home}/test_cases/all_tests.cfg" |
|
49 | problem = Problem.get_instance |
|
49 | problem = Problem.get_instance |
|
50 |
|
50 | ||
|
51 | if problem.well_formed? == false |
|
51 | if problem.well_formed? == false |
|
52 | log "The problem specification is not well formed." |
|
52 | log "The problem specification is not well formed." |
|
53 | exit(127) |
|
53 | exit(127) |
|
54 | end |
|
54 | end |
|
55 |
|
55 | ||
|
56 | # Check if the test number is okay. |
|
56 | # Check if the test number is okay. |
|
57 | if test_num <= 0 || test_num > problem.num_tests |
|
57 | if test_num <= 0 || test_num > problem.num_tests |
|
58 | log "You have specified a wrong test number." |
|
58 | log "You have specified a wrong test number." |
|
59 | exit(127) |
|
59 | exit(127) |
|
60 | end |
|
60 | end |
|
61 |
|
61 | ||
|
62 | ##################################### |
|
62 | ##################################### |
|
63 | # Set the relavant file names here. # |
|
63 | # Set the relavant file names here. # |
|
64 | ##################################### |
|
64 | ##################################### |
|
65 |
|
65 | ||
|
66 | input_file_name = "#{problem_home}/test_cases/#{test_num}/input-#{test_num}.txt" |
|
66 | input_file_name = "#{problem_home}/test_cases/#{test_num}/input-#{test_num}.txt" |
|
67 |
|
67 | ||
|
68 | ##################################### |
|
68 | ##################################### |
|
69 |
|
69 | ||
|
70 | time_limit = problem.get_time_limit test_num |
|
70 | time_limit = problem.get_time_limit test_num |
|
71 | mem_limit = problem.get_mem_limit(test_num) * 1024 |
|
71 | mem_limit = problem.get_mem_limit(test_num) * 1024 |
|
72 |
|
72 | ||
|
73 | # Copy the input file. |
|
73 | # Copy the input file. |
|
74 | #`cp #{problem_home}/test_cases/#{test_num}/#{input_file_name} .` |
|
74 | #`cp #{problem_home}/test_cases/#{test_num}/#{input_file_name} .` |
|
75 |
|
75 | ||
|
76 | # check if box is there, if not, compile it! |
|
76 | # check if box is there, if not, compile it! |
|
77 | if !File.exists?("#{problem_home}/script/box") |
|
77 | if !File.exists?("#{problem_home}/script/box") |
|
78 | log "WARNING: Compiling box: to increase efficiency, it should be compile manually" |
|
78 | log "WARNING: Compiling box: to increase efficiency, it should be compile manually" |
|
79 | compile_box("#{problem_home}/script/box.cc", |
|
79 | compile_box("#{problem_home}/script/box.cc", |
|
80 | "#{problem_home}/script/box") |
|
80 | "#{problem_home}/script/box") |
|
81 | end |
|
81 | end |
|
82 |
|
82 | ||
|
83 | # Hide PROBLEM_HOME |
|
83 | # Hide PROBLEM_HOME |
|
84 | ENV['PROBLEM_HOME'] = nil |
|
84 | ENV['PROBLEM_HOME'] = nil |
|
85 |
|
85 | ||
|
86 | # Run the program. |
|
86 | # Run the program. |
|
87 | #run_command = "/usr/bin/time -f \"#{time_output_format}\" 2>run_result #{problem_home}/script/box_new -a 2 -f -t #{time_limit} -m #{mem_limit} -i #{input_file_name} -o output.txt #{program_name}" |
|
87 | #run_command = "/usr/bin/time -f \"#{time_output_format}\" 2>run_result #{problem_home}/script/box_new -a 2 -f -t #{time_limit} -m #{mem_limit} -i #{input_file_name} -o output.txt #{program_name}" |
|
88 | # |
|
88 | # |
|
89 |
|
89 | ||
|
90 |
|
90 | ||
|
91 |
|
91 | ||
|
92 |
|
92 | ||
|
93 | case language |
|
93 | case language |
|
94 | when "java" |
|
94 | when "java" |
|
95 | # for java, we have to add additional systemcall and we don't check the mem limit (dunno how to fix...) |
|
95 | # for java, we have to add additional systemcall and we don't check the mem limit (dunno how to fix...) |
|
96 | 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" |
|
96 | 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" |
|
97 | when "ruby" |
|
97 | when "ruby" |
|
98 | 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" |
|
98 | 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" |
|
99 |
- when " |
|
99 | + when "python" |
|
100 | - 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" |
|
100 | + #this code just run without any checking |
|
101 | - else |
|
101 | + 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" |
|
|
102 | + else # for c++, pascal, we do the normal checking | ||
|
102 | 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" |
|
103 | 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" |
|
103 | end |
|
104 | end |
|
104 |
|
105 | ||
|
105 |
|
106 | ||
|
106 | log "Running test #{test_num}..." |
|
107 | log "Running test #{test_num}..." |
|
107 | log run_command |
|
108 | log run_command |
|
108 | log |
|
109 | log |
|
109 | system(run_command) |
|
110 | system(run_command) |
|
110 |
|
111 | ||
|
111 | # Restore PROBLEM_HOME |
|
112 | # Restore PROBLEM_HOME |
|
112 | ENV['PROBLEM_HOME'] = problem_home |
|
113 | ENV['PROBLEM_HOME'] = problem_home |
|
113 |
|
114 | ||
|
114 | # Create the result file. |
|
115 | # Create the result file. |
|
115 | result_file = File.new("result", "w") |
|
116 | result_file = File.new("result", "w") |
|
116 | comment_file = File.new("comment", "w") |
|
117 | comment_file = File.new("comment", "w") |
|
117 |
|
118 | ||
|
118 | # Check if the program actually produced any output. |
|
119 | # Check if the program actually produced any output. |
|
119 | run_result_file = File.new("run_result", "r") |
|
120 | run_result_file = File.new("run_result", "r") |
|
120 | run_result = run_result_file.readlines |
|
121 | run_result = run_result_file.readlines |
|
121 | run_result_file.close |
|
122 | run_result_file.close |
|
122 |
|
123 | ||
|
123 | run_stat = run_result[run_result.length-1] |
|
124 | run_stat = run_result[run_result.length-1] |
|
124 | running_time = extract_time(run_stat) |
|
125 | running_time = extract_time(run_stat) |
|
125 |
|
126 | ||
|
126 | report = lambda{ |status, points, comment| |
|
127 | report = lambda{ |status, points, comment| |
|
127 | result_file.write status.strip |
|
128 | result_file.write status.strip |
|
128 | result_file.write "\n" |
|
129 | result_file.write "\n" |
|
129 | result_file.write points.to_s.strip |
|
130 | result_file.write points.to_s.strip |
|
130 | result_file.write "\n" |
|
131 | result_file.write "\n" |
|
131 | result_file.write run_stat.strip |
|
132 | result_file.write run_stat.strip |
|
132 | result_file.write "\n" |
|
133 | result_file.write "\n" |
|
133 | result_file.close |
|
134 | result_file.close |
|
134 | FileUtils.rm "run_result" |
|
135 | FileUtils.rm "run_result" |
|
135 | # `rm output.txt` --- keep the output |
|
136 | # `rm output.txt` --- keep the output |
|
136 |
|
137 | ||
|
137 | comment_file.write comment |
|
138 | comment_file.write comment |
|
138 |
|
139 | ||
|
139 | # added for debuggin --- jittat |
|
140 | # added for debuggin --- jittat |
|
140 | comment_file.write "--run-result--\n" |
|
141 | comment_file.write "--run-result--\n" |
|
141 | run_result.each do |l| |
|
142 | run_result.each do |l| |
|
142 | comment_file.write l |
|
143 | comment_file.write l |
|
143 | end |
|
144 | end |
|
144 |
|
145 | ||
|
145 | comment_file.close |
|
146 | comment_file.close |
|
146 |
|
147 | ||
|
147 | log "Done!" |
|
148 | log "Done!" |
|
148 | exit(0) |
|
149 | exit(0) |
|
149 | } |
|
150 | } |
|
150 |
|
151 | ||
|
151 | if run_result[0][0,2] != "OK" |
|
152 | if run_result[0][0,2] != "OK" |
|
152 | log "There was a runtime error." |
|
153 | log "There was a runtime error." |
|
153 | report.call(run_result[0], 0, "No comment.\n") |
|
154 | report.call(run_result[0], 0, "No comment.\n") |
|
154 | end |
|
155 | end |
|
155 |
|
156 | ||
|
156 | if running_time[:user].to_f > time_limit |
|
157 | if running_time[:user].to_f > time_limit |
|
157 | log "Time limit exceeded." |
|
158 | log "Time limit exceeded." |
|
158 | report.call("Time limit exceeded", 0, "No comment.\n") |
|
159 | report.call("Time limit exceeded", 0, "No comment.\n") |
|
159 | end |
|
160 | end |
|
160 |
|
161 | ||
|
161 | # Run 'check' to evaluate the output. |
|
162 | # Run 'check' to evaluate the output. |
|
162 | #puts "There was no runtime error. Proceed to checking the output." |
|
163 | #puts "There was no runtime error. Proceed to checking the output." |
|
163 | check_command = "#{problem_home}/script/check #{language} #{test_num}" |
|
164 | check_command = "#{problem_home}/script/check #{language} #{test_num}" |
|
164 | log "Checking the output..." |
|
165 | log "Checking the output..." |
|
165 | log check_command |
|
166 | log check_command |
|
166 | if not system(check_command) |
|
167 | if not system(check_command) |
|
167 | log "Problem with check script" |
|
168 | log "Problem with check script" |
|
168 | report.call("Incorrect",0,"Check script error.\n") |
|
169 | report.call("Incorrect",0,"Check script error.\n") |
|
169 | exit(127) |
|
170 | exit(127) |
|
170 | end |
|
171 | end |
|
171 |
|
172 | ||
|
172 | check_file = File.new("check_result", "r") |
|
173 | check_file = File.new("check_result", "r") |
|
173 | check_file_lines = check_file.readlines |
|
174 | check_file_lines = check_file.readlines |
|
174 |
|
175 | ||
|
175 | report.call(check_file_lines[0], check_file_lines[1], "No comment.\n") |
|
176 | report.call(check_file_lines[0], check_file_lines[1], "No comment.\n") |
You need to be logged in to leave comments.
Login now