Description:
merge fix utf8
Commit status:
[Not Reviewed]
References:
merge algo
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r188:4805f3ab73c4 - - 2 files changed: 4 inserted, 2 deleted

@@ -18,170 +18,171
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_INTERPRETER = "/usr/bin/ruby"
30 RUBY_INTERPRETER = "/usr/bin/ruby"
31 PYTHON_INTERPRETER = "/usr/bin/python"
31 PYTHON_INTERPRETER = "/usr/bin/python"
32 PYTHON_CHECKER = "/usr/bin/pyflakes"
32 PYTHON_CHECKER = "/usr/bin/pyflakes"
33 PHP_INTERPRETER = "/usr/bin/php"
33 PHP_INTERPRETER = "/usr/bin/php"
34
34
35 C_OPTIONS = "-O2 -s -static -std=c99 -DCONTEST -lm -Wall"
35 C_OPTIONS = "-O2 -s -static -std=c99 -DCONTEST -lm -Wall"
36 CPLUSPLUS_OPTIONS = "-O2 -s -std=c++11 -static -DCONTEST -lm -Wall"
36 CPLUSPLUS_OPTIONS = "-O2 -s -std=c++11 -static -DCONTEST -lm -Wall"
37 PASCAL_OPTIONS = "-O1 -XS -dCONTEST"
37 PASCAL_OPTIONS = "-O1 -XS -dCONTEST"
38 JAVA_OPTIONS = ""
38 JAVA_OPTIONS = ""
39 PYTHON_OPTIONS = ""
39 PYTHON_OPTIONS = ""
40 PHP_OPTIONS = "-l"
40 PHP_OPTIONS = "-l"
41
41
42 # Check for the correct number of arguments. Otherwise, print usage.
42 # Check for the correct number of arguments. Otherwise, print usage.
43 if ARGV.length == 0 or ARGV.length > 4
43 if ARGV.length == 0 or ARGV.length > 4
44 puts "Usage: compile <language> [<source-file>] [<output-file>] [<message-file>]"
44 puts "Usage: compile <language> [<source-file>] [<output-file>] [<message-file>]"
45 puts
45 puts
46 puts "<source-file> is defaulted to \"source\"."
46 puts "<source-file> is defaulted to \"source\"."
47 puts "<output-file> is defaulted to \"a.out\"."
47 puts "<output-file> is defaulted to \"a.out\"."
48 puts "<message-file> is defaulted to \"compiler_message\"."
48 puts "<message-file> is defaulted to \"compiler_message\"."
49 puts
49 puts
50 exit(127)
50 exit(127)
51 end
51 end
52
52
53 PARAMS = {
53 PARAMS = {
54 :source_file => [1,'source'],
54 :source_file => [1,'source'],
55 :output_file => [2,'a.out'],
55 :output_file => [2,'a.out'],
56 :message_file => [3,'compiler_message']
56 :message_file => [3,'compiler_message']
57 }
57 }
58
58
59 params = {}
59 params = {}
60 params[:prog_lang] = ARGV[0]
60 params[:prog_lang] = ARGV[0]
61 PARAMS.each_key do |param_name|
61 PARAMS.each_key do |param_name|
62 index, default = PARAMS[param_name]
62 index, default = PARAMS[param_name]
63 if ARGV.length > index
63 if ARGV.length > index
64 params[param_name] = ARGV[index]
64 params[param_name] = ARGV[index]
65 else
65 else
66 params[param_name] = default
66 params[param_name] = default
67 end
67 end
68 talk "#{param_name}: #{params[param_name]}"
68 talk "#{param_name}: #{params[param_name]}"
69 end
69 end
70
70
71 # Remove any remaining output files or message files.
71 # Remove any remaining output files or message files.
72 if FileTest.exists? params[:output_file]
72 if FileTest.exists? params[:output_file]
73 FileUtils.rm(params[:output_file])
73 FileUtils.rm(params[:output_file])
74 end
74 end
75 if FileTest.exists? params[:message_file]
75 if FileTest.exists? params[:message_file]
76 FileUtils.rm(params[:message_file])
76 FileUtils.rm(params[:message_file])
77 end
77 end
78
78
79 # Check if the source file exists before attempt compiling.
79 # Check if the source file exists before attempt compiling.
80 if !FileTest.exists? params[:source_file]
80 if !FileTest.exists? params[:source_file]
81 talk("ERROR: The source file does not exist!")
81 talk("ERROR: The source file does not exist!")
82 open(params[:message_file],"w") do |f|
82 open(params[:message_file],"w") do |f|
83 f.puts "ERROR: The source file did not exist."
83 f.puts "ERROR: The source file did not exist."
84 end
84 end
85 exit(127)
85 exit(127)
86 end
86 end
87
87
88 if params[:prog_lang]=='cpp'
88 if params[:prog_lang]=='cpp'
89 params[:prog_lang] = 'c++'
89 params[:prog_lang] = 'c++'
90 end
90 end
91
91
92 # Compile.
92 # Compile.
93 case params[:prog_lang]
93 case params[:prog_lang]
94
94
95 when "c"
95 when "c"
96 command = "#{C_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{C_OPTIONS}"
96 command = "#{C_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{C_OPTIONS}"
97 system(command, err: params[:message_file])
97 system(command, err: params[:message_file])
98
98
99 when "c++"
99 when "c++"
100 command = "#{CPLUSPLUS_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{CPLUSPLUS_OPTIONS}"
100 command = "#{CPLUSPLUS_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{CPLUSPLUS_OPTIONS}"
101 system(command, err: params[:message_file])
101 system(command, err: params[:message_file])
102
102
103 when "pas"
103 when "pas"
104 command = "#{PASCAL_COMPILER} #{params[:source_file]} -ooutpas #{PASCAL_OPTIONS}"
104 command = "#{PASCAL_COMPILER} #{params[:source_file]} -ooutpas #{PASCAL_OPTIONS}"
105 system(command,out: params[:message_file])
105 system(command,out: params[:message_file])
106 FileUtils.mv("output", params[:output_file])
106 FileUtils.mv("output", params[:output_file])
107
107
108 when "java"
108 when "java"
109 #rename the file to the public class name
109 #rename the file to the public class name
110
110
111 #get the class name
111 #get the class name
112 classname = 'DUMMY'
112 classname = 'DUMMY'
113 source = Array.new
113 source = Array.new
114 - File.foreach(params[:source_file]) do |line|
114 + File.foreach(params[:source_file],'r:UTF-8') do |line|
115 + line.encode!('UTF-8','UTF-8',invalid: :replace, replace: '')
115 md = /\s*public\s*class\s*(\w*)/.match(line)
116 md = /\s*public\s*class\s*(\w*)/.match(line)
116 classname=md[1] if md
117 classname=md[1] if md
117 source << line unless line =~ /\s*package\s*\w+\s*\;/
118 source << line unless line =~ /\s*package\s*\w+\s*\;/
118 end
119 end
119 File.open("#{classname}.java","w") do |file|
120 File.open("#{classname}.java","w") do |file|
120 source.each do |s|
121 source.each do |s|
121 file.puts s
122 file.puts s
122 end
123 end
123 end
124 end
124 #system("cp #{params[:source_file]} #{classname}.java")
125 #system("cp #{params[:source_file]} #{classname}.java")
125 - command = "#{JAVA_COMPILER} #{classname}.java"
126 + command = "#{JAVA_COMPILER} -encoding utf8 #{classname}.java"
126 system(command, err: params[:message_file])
127 system(command, err: params[:message_file])
127 if File.exists?(classname + ".class")
128 if File.exists?(classname + ".class")
128 File.open(params[:output_file],"w") {|file| file.write("#{classname}")}
129 File.open(params[:output_file],"w") {|file| file.write("#{classname}")}
129 end
130 end
130 if classname == 'DUMMY'
131 if classname == 'DUMMY'
131 File.open(params[:message_file],"w") {|file| file.write("Cannot find any public class in the source code\n")}
132 File.open(params[:message_file],"w") {|file| file.write("Cannot find any public class in the source code\n")}
132 end
133 end
133
134
134 when "ruby"
135 when "ruby"
135 command = "#{RUBY_INTERPRETER} -c #{params[:source_file]}"
136 command = "#{RUBY_INTERPRETER} -c #{params[:source_file]}"
136 if system(command, err: params[:message_file])
137 if system(command, err: params[:message_file])
137 File.open(params[:output_file],"w") do |out_file|
138 File.open(params[:output_file],"w") do |out_file|
138 out_file.puts "#!#{RUBY_INTERPRETER}"
139 out_file.puts "#!#{RUBY_INTERPRETER}"
139 File.open(params[:source_file],"r").each do |line|
140 File.open(params[:source_file],"r").each do |line|
140 out_file.print line
141 out_file.print line
141 end
142 end
142 end
143 end
143 File.chmod(0755, params[:output_file])
144 File.chmod(0755, params[:output_file])
144 end
145 end
145
146
146 when "python"
147 when "python"
147 command = "#{PYTHON_CHECKER} #{params[:source_file]}"
148 command = "#{PYTHON_CHECKER} #{params[:source_file]}"
148 if system(command, out: params[:message_file])
149 if system(command, out: params[:message_file])
149 #compile to python bytecode
150 #compile to python bytecode
150 command = "#{PYTHON_INTERPRETER} -m py_compile #{params[:source_file]}"
151 command = "#{PYTHON_INTERPRETER} -m py_compile #{params[:source_file]}"
151 puts "compile: #{command}"
152 puts "compile: #{command}"
152 system(command)
153 system(command)
153 puts "pwd: " + Dir.pwd
154 puts "pwd: " + Dir.pwd
154 Dir.new('.').each {|file| puts file}
155 Dir.new('.').each {|file| puts file}
155 File.open(params[:output_file],"w") do |out_file|
156 File.open(params[:output_file],"w") do |out_file|
156 out_file.puts "#!#{PYTHON_INTERPRETER} #{params[:source_file]}c"
157 out_file.puts "#!#{PYTHON_INTERPRETER} #{params[:source_file]}c"
157 end
158 end
158 File.chmod(0755, params[:output_file])
159 File.chmod(0755, params[:output_file])
159 FileUtils.cp("#{params[:source_file]}c",params[:output_file])
160 FileUtils.cp("#{params[:source_file]}c",params[:output_file])
160 end
161 end
161
162
162 when "php"
163 when "php"
163 command = "#{PHP_INTERPRETER} #{PHP_OPTIONS} #{params[:source_file]}"
164 command = "#{PHP_INTERPRETER} #{PHP_OPTIONS} #{params[:source_file]}"
164 if system(command, err: params[:message_file])
165 if system(command, err: params[:message_file])
165 File.open(params[:output_file],"w") do |out_file|
166 File.open(params[:output_file],"w") do |out_file|
166 out_file.puts "#!#{PHP_INTERPRETER}"
167 out_file.puts "#!#{PHP_INTERPRETER}"
167 File.open(params[:source_file],"r").each do |line|
168 File.open(params[:source_file],"r").each do |line|
168 out_file.print line
169 out_file.print line
169 end
170 end
170 end
171 end
171 File.chmod(0755, params[:output_file])
172 File.chmod(0755, params[:output_file])
172 end
173 end
173
174
174 else
175 else
175 talk("ERROR: Invalid language specified!")
176 talk("ERROR: Invalid language specified!")
176 open(params[:message_file],"w") do |f|
177 open(params[:message_file],"w") do |f|
177 f.puts "ERROR: Invalid language specified!"
178 f.puts "ERROR: Invalid language specified!"
178 end
179 end
179 exit(127)
180 exit(127)
180 end
181 end
181
182
182 # Report success or failure.
183 # Report success or failure.
183 if FileTest.exists? params[:output_file]
184 if FileTest.exists? params[:output_file]
184 talk "Compilation was successful!"
185 talk "Compilation was successful!"
185 else
186 else
186 talk "ERROR: Something was wrong during the compilation!"
187 talk "ERROR: Something was wrong during the compilation!"
187 end
188 end
@@ -36,98 +36,99
36 if (result=/^(.*)r(.*)u(.*)s(.*)kbytes/.match(t))
36 if (result=/^(.*)r(.*)u(.*)s(.*)kbytes/.match(t))
37 {:real => result[1], :user => result[2], :sys => result[3], :mem => result[4]}
37 {:real => result[1], :user => result[2], :sys => result[3], :mem => result[4]}
38 else
38 else
39 #{:real => 0, :user => 0, :sys => 0}
39 #{:real => 0, :user => 0, :sys => 0}
40 #puts "ERROR READING RUNNING TIME: #{t}"
40 #puts "ERROR READING RUNNING TIME: #{t}"
41 raise "Error reading running time: #{t}"
41 raise "Error reading running time: #{t}"
42 end
42 end
43 end
43 end
44
44
45 problem_home = ENV['PROBLEM_HOME']
45 problem_home = ENV['PROBLEM_HOME']
46 require "#{problem_home}/script/test_dsl.rb"
46 require "#{problem_home}/script/test_dsl.rb"
47 load "#{problem_home}/test_cases/all_tests.cfg"
47 load "#{problem_home}/test_cases/all_tests.cfg"
48 problem = Problem.get_instance
48 problem = Problem.get_instance
49
49
50 if problem.well_formed? == false
50 if problem.well_formed? == false
51 log "The problem specification is not well formed."
51 log "The problem specification is not well formed."
52 exit(127)
52 exit(127)
53 end
53 end
54
54
55 all_score = 0
55 all_score = 0
56 all_comment = ''
56 all_comment = ''
57 peak_memory = -1
57 peak_memory = -1
58 max_runtime = -1
58 max_runtime = -1
59 (1..(problem.runs.length-1)).each do |k|
59 (1..(problem.runs.length-1)).each do |k|
60 log "grade run #{k}"
60 log "grade run #{k}"
61 run = problem.runs[k]
61 run = problem.runs[k]
62 run_score = nil
62 run_score = nil
63 run_comment = ''
63 run_comment = ''
64 run_comment_short = ''
64 run_comment_short = ''
65 run.tests.each do |test_num|
65 run.tests.each do |test_num|
66 result_file_name = "#{test_num}/result"
66 result_file_name = "#{test_num}/result"
67 if not File.exists?(result_file_name)
67 if not File.exists?(result_file_name)
68 run_comment += "result file for test #{test_num} not found\n"
68 run_comment += "result file for test #{test_num} not found\n"
69 run_comment_short += RUN_ERROR_MARK
69 run_comment_short += RUN_ERROR_MARK
70 log "Cannot find the file #{test_num}/result!"
70 log "Cannot find the file #{test_num}/result!"
71 else
71 else
72 result_file = File.new(result_file_name, "r")
72 result_file = File.new(result_file_name, "r")
73 result_file_lines = result_file.readlines
73 result_file_lines = result_file.readlines
74 if result_file_lines.length>=3
74 if result_file_lines.length>=3
75 current_run_score = result_file_lines[1].to_i
75 current_run_score = result_file_lines[1].to_i
76 run_comment += result_file_lines[0]
76 run_comment += result_file_lines[0]
77 run_comment_short += char_comment(result_file_lines[0].chomp)
77 run_comment_short += char_comment(result_file_lines[0].chomp)
78
78
79 #update max runtime & memory
79 #update max runtime & memory
80 run_stat = extract_time result_file_lines[2]
80 run_stat = extract_time result_file_lines[2]
81 peak_memory = [peak_memory,run_stat[:mem].to_i].max
81 peak_memory = [peak_memory,run_stat[:mem].to_i].max
82 max_runtime = [max_runtime,run_stat[:user].to_f + run_stat[:sys].to_f].max
82 max_runtime = [max_runtime,run_stat[:user].to_f + run_stat[:sys].to_f].max
83 else
83 else
84 current_run_score = 0
84 current_run_score = 0
85 run_comment += "result file for test #{test_num} error\n"
85 run_comment += "result file for test #{test_num} error\n"
86 run_comment_short += RUN_ERROR_MARK
86 run_comment_short += RUN_ERROR_MARK
87 log "Error in #{test_num}/result!"
87 log "Error in #{test_num}/result!"
88 end
88 end
89
89
90 # the score of this run should be the minimum of the score for
90 # the score of this run should be the minimum of the score for
91 # each test case
91 # each test case
92 if (run_score==nil) or (run_score>current_run_score)
92 if (run_score==nil) or (run_score>current_run_score)
93 run_score = current_run_score
93 run_score = current_run_score
94 end
94 end
95 result_file.close
95 result_file.close
96 end
96 end
97 end
97 end
98
98
99 run_result_file = File.new("result-#{k}", "w")
99 run_result_file = File.new("result-#{k}", "w")
100 run_result_file.write run_score
100 run_result_file.write run_score
101 run_result_file.write "\n"
101 run_result_file.write "\n"
102 run_result_file.close
102 run_result_file.close
103
103
104 run_comment_file = File.new("comment-#{k}", "w")
104 run_comment_file = File.new("comment-#{k}", "w")
105 run_comment_file.write "#{run_comment}\n"
105 run_comment_file.write "#{run_comment}\n"
106 run_comment_file.close
106 run_comment_file.close
107
107
108 all_score = all_score + run_score
108 all_score = all_score + run_score
109
109
110 # append comment for test run with many test cases
110 # append comment for test run with many test cases
111 if run.tests.length > 1
111 if run.tests.length > 1
112 run_comment_short = '[' + run_comment_short + ']'
112 run_comment_short = '[' + run_comment_short + ']'
113 end
113 end
114 all_comment += run_comment_short
114 all_comment += run_comment_short
115 end
115 end
116
116
117 result_file = File.new("result", "w")
117 result_file = File.new("result", "w")
118 result_file.write all_score
118 result_file.write all_score
119 result_file.write "\n"
119 result_file.write "\n"
120 result_file.close
120 result_file.close
121
121
122 comment_file = File.new("comment", "w")
122 comment_file = File.new("comment", "w")
123 comment_file.write "#{all_comment}\n"
123 comment_file.write "#{all_comment}\n"
124 comment_file.close
124 comment_file.close
125
125
126
126
127 File.open("run_stat","w") do |file|
127 File.open("run_stat","w") do |file|
128 file.puts max_runtime
128 file.puts max_runtime
129 file.puts peak_memory
129 file.puts peak_memory
130 end
130 end
131
131
132 + puts "#{all_score} #{all_comment}"
132 log "score = #{all_score}\ncomment = #{all_comment}"
133 log "score = #{all_score}\ncomment = #{all_comment}"
133 log "max_runtime = #{max_runtime}\npeak_memory = #{peak_memory}"
134 log "max_runtime = #{max_runtime}\npeak_memory = #{peak_memory}"
You need to be logged in to leave comments. Login now