Description:
removed calls to 'pwd', other uses of back quotes
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r102:a3aedb7e3776 - - 4 files changed: 7 inserted, 5 deleted

@@ -1,89 +1,91
1 1 #!/usr/bin/ruby
2 2
3 + require 'fileutils'
4 +
3 5 def talk(str)
4 6 if TALKATIVE
5 7 puts str
6 8 end
7 9 end
8 10
9 11 def save_source(submission,dir,fname)
10 12 f = File.open("#{dir}/#{fname}","w")
11 13 f.write(submission.source)
12 14 f.close
13 15 end
14 16
15 17 def call_judge(problem_home,language,problem_out_dir,fname)
16 18 ENV['PROBLEM_HOME'] = problem_home
17 19 Dir.chdir problem_out_dir
18 20 cmd = "#{problem_home}/script/judge #{language} #{fname}"
19 21 # puts "CMD: #{cmd}"
20 22 system(cmd)
21 23 end
22 24
23 25 def read_result(test_result_dir)
24 26 cmp_msg_fname = "#{test_result_dir}/compiler_message"
25 27 cmp_msg = File.open(cmp_msg_fname).read
26 28
27 29 result_fname = "#{test_result_dir}/result"
28 30 comment_fname = "#{test_result_dir}/comment"
29 31 if FileTest.exist?(result_fname)
30 32 result = File.open(result_fname).readline.to_i
31 33 comment = File.open(comment_fname).readline.chomp
32 34 return {:points => result,
33 35 :comment => comment,
34 36 :cmp_msg => cmp_msg}
35 37 else
36 38 return {:points => 0,
37 39 :comment => 'compile error',
38 40 :cmp_msg => cmp_msg}
39 41 end
40 42 end
41 43
42 44 def save_result(submission,result)
43 45 submission.graded_at = Time.now
44 46 submission.points = result[:points]
45 47 submission.grader_comment = report_comment(result[:comment])
46 48 submission.compiler_message = result[:cmp_msg]
47 49 submission.save
48 50 end
49 51
50 52 def grade(submission_id)
51 53 sub = Submission.find(submission_id)
52 54 user = sub.user
53 55 problem = sub.problem
54 56
55 57 language = sub.language.name
56 58 lang_ext = sub.language.ext
57 59 # FIX THIS
58 60 talk 'some hack on language'
59 61 if language == 'cpp'
60 62 language = 'c++'
61 63 end
62 64
63 65 user_dir = "#{USER_RESULT_DIR}/#{user.login}"
64 66 Dir.mkdir(user_dir) if !FileTest.exist?(user_dir)
65 67
66 68 problem_out_dir = "#{user_dir}/#{problem.name}"
67 69 Dir.mkdir(problem_out_dir) if !FileTest.exist?(problem_out_dir)
68 70
69 71 problem_home = "#{PROBLEMS_DIR}/#{problem.name}"
70 72 source_name = "#{problem.name}.#{lang_ext}"
71 73
72 74 save_source(sub,problem_out_dir,source_name)
73 75 call_judge(problem_home,language,problem_out_dir,source_name)
74 76 save_result(sub,read_result("#{problem_out_dir}/test-result"))
75 77 end
76 78
77 79 # reading environment and options
78 80 GRADER_ENV = 'exam'
79 81 puts "environment: #{GRADER_ENV}"
80 82 require File.dirname(__FILE__) + "/environment.rb"
81 83
82 84 #main program
83 85 talk 'Reading rails environment'
84 86
85 87 RAILS_ENV = 'development'
86 88 require RAILS_APP_DIR + '/config/environment'
87 89
88 - current_dir = `pwd`
90 + current_dir = FileUtils.pwd
89 91 grade(ARGV[0].to_i)
@@ -1,186 +1,186
1 1 require 'fileutils'
2 2 require File.join(File.dirname(__FILE__),'dir_init')
3 3
4 4 module Grader
5 5
6 6 #
7 7 # A grader engine grades a submission, against anything: a test
8 8 # data, or a user submitted test data. It uses two helpers objects:
9 9 # room_maker and reporter.
10 10 #
11 11 class Engine
12 12
13 13 attr_writer :room_maker
14 14 attr_writer :reporter
15 15
16 16 def initialize(options={})
17 17 # default options
18 18 if not options.include? :room_maker
19 19 options[:room_maker] = Grader::SubmissionRoomMaker.new
20 20 end
21 21 if not options.include? :reporter
22 22 options[:reporter] = Grader::SubmissionReporter.new
23 23 end
24 24
25 25 @config = Grader::Configuration.get_instance
26 26
27 27 @room_maker = options[:room_maker]
28 28 @reporter = options[:reporter]
29 29 end
30 30
31 31 # takes a submission, asks room_maker to produce grading directories,
32 32 # calls grader scripts, and asks reporter to save the result
33 33 def grade(submission)
34 - current_dir = `pwd`.chomp
34 + current_dir = FileUtils.pwd
35 35
36 36 user = submission.user
37 37 problem = submission.problem
38 38
39 39 # TODO: will have to create real exception for this
40 40 if user==nil or problem == nil
41 41 @reporter.report_error(submission,"Grading error: problem with submission")
42 42 #raise "engine: user or problem is nil"
43 43 end
44 44
45 45 # TODO: this is another hack so that output only task can be judged
46 46 if submission.language!=nil
47 47 language = submission.language.name
48 48 lang_ext = submission.language.ext
49 49 else
50 50 language = 'c'
51 51 lang_ext = 'c'
52 52 end
53 53
54 54 # FIX THIS
55 55 talk 'some hack on language'
56 56 if language == 'cpp'
57 57 language = 'c++'
58 58 end
59 59
60 60 # COMMENT: should it be only source.ext?
61 61 if problem!=nil
62 62 source_name = "#{problem.name}.#{lang_ext}"
63 63 else
64 64 source_name = "source.#{lang_ext}"
65 65 end
66 66
67 67 begin
68 68 grading_dir = @room_maker.produce_grading_room(submission)
69 69 @room_maker.save_source(submission,source_name)
70 70 problem_home = @room_maker.find_problem_home(submission)
71 71
72 72 # puts "GRADING DIR: #{grading_dir}"
73 73 # puts "PROBLEM DIR: #{problem_home}"
74 74
75 75 if !FileTest.exist?(problem_home)
76 76 raise "No test data."
77 77 end
78 78
79 79 dinit = DirInit::Manager.new(problem_home)
80 80
81 81 dinit.setup do
82 82 copy_log = copy_script(problem_home)
83 83 save_copy_log(problem_home,copy_log)
84 84 end
85 85
86 86 call_judge(problem_home,language,grading_dir,source_name)
87 87
88 88 @reporter.report(submission,"#{grading_dir}/test-result")
89 89
90 90 dinit.teardown do
91 91 copy_log = load_copy_log(problem_home)
92 92 clear_copy_log(problem_home)
93 93 clear_script(copy_log,problem_home)
94 94 end
95 95
96 96 rescue RuntimeError => msg
97 97 @reporter.report_error(submission, msg)
98 98
99 99 ensure
100 100 @room_maker.clean_up(submission)
101 101 Dir.chdir(current_dir) # this is really important
102 102 end
103 103 end
104 104
105 105 protected
106 106
107 107 def talk(str)
108 108 if @config.talkative
109 109 puts str
110 110 end
111 111 end
112 112
113 113 def call_judge(problem_home,language,grading_dir,fname)
114 114 ENV['PROBLEM_HOME'] = problem_home
115 115
116 116 talk grading_dir
117 117 Dir.chdir grading_dir
118 118 cmd = "#{problem_home}/script/judge #{language} #{fname}"
119 119 talk "CMD: #{cmd}"
120 120 system(cmd)
121 121 end
122 122
123 123 def get_std_script_dir
124 124 GRADER_ROOT + '/std-script'
125 125 end
126 126
127 127 def copy_script(problem_home)
128 128 script_dir = "#{problem_home}/script"
129 129 std_script_dir = get_std_script_dir
130 130
131 131 raise "std-script directory not found" if !FileTest.exist?(std_script_dir)
132 132
133 133 scripts = Dir[std_script_dir + '/*']
134 134
135 135 copied = []
136 136
137 137 scripts.each do |s|
138 138 fname = File.basename(s)
139 139 if !FileTest.exist?("#{script_dir}/#{fname}")
140 140 copied << fname
141 141 FileUtils.cp(s, "#{script_dir}")
142 142 end
143 143 end
144 144
145 145 return copied
146 146 end
147 147
148 148 def copy_log_filename(problem_home)
149 149 return File.join(problem_home, '.scripts_copied')
150 150 end
151 151
152 152 def save_copy_log(problem_home, log)
153 153 f = File.new(copy_log_filename(problem_home),"w")
154 154 log.each do |fname|
155 155 f.write("#{fname}\n")
156 156 end
157 157 f.close
158 158 end
159 159
160 160 def load_copy_log(problem_home)
161 161 f = File.new(copy_log_filename(problem_home),"r")
162 162 log = []
163 163 f.readlines.each do |line|
164 164 log << line.strip
165 165 end
166 166 f.close
167 167 log
168 168 end
169 169
170 170 def clear_copy_log(problem_home)
171 171 File.delete(copy_log_filename(problem_home))
172 172 end
173 173
174 174 def clear_script(log,problem_home)
175 175 log.each do |s|
176 176 FileUtils.rm("#{problem_home}/script/#{s}")
177 177 end
178 178 end
179 179
180 180 def mkdir_if_does_not_exist(dirname)
181 181 Dir.mkdir(dirname) if !FileTest.exist?(dirname)
182 182 end
183 183
184 184 end
185 185
186 186 end
@@ -1,172 +1,172
1 1 #!/usr/bin/ruby
2 2
3 3 require 'fileutils'
4 4
5 5 def log(str='')
6 6 if ENV['TALKATIVE']!=nil
7 7 puts str
8 8 end
9 9 if ENV['GRADER_LOGGING']!=nil
10 10 log_fname = ENV['GRADER_LOGGING']
11 11 fp = File.open(log_fname,"a")
12 12 fp.puts("judge: #{Time.new.strftime("%H:%M")} #{str}")
13 13 fp.close
14 14 end
15 15 end
16 16
17 17 problem_home = ENV['PROBLEM_HOME']
18 18
19 19 def execute(command, error_message="")
20 20 if not system(command)
21 21 msg = "ERROR: #{error_message}"
22 22 log msg
23 23 raise msg
24 24 end
25 25 end
26 26
27 27 def call_and_log(error_message)
28 28 begin
29 29 yield
30 30 rescue
31 31 msg = "ERROR: #{error_message}"
32 32 log msg
33 33 raise msg
34 34 end
35 35 end
36 36
37 37 def clear_and_create_empty_dir(dir)
38 38 FileUtils.rm_rf(dir, :secure => true)
39 39 call_and_log("Cannot make directory #{dir}.") { FileUtils.mkdir(dir) }
40 40 end
41 41
42 42 # ARGV[0] --- language
43 43 # ARGV[1] --- program source file
44 44 # ARGV[2] --- test result directory
45 45 # ARGV[3] --- sandbox directory
46 46
47 47 if ARGV.length < 2 || ARGV.length > 4
48 48 puts "Usage: judge <language> <program-source> [<test-result-directory>] [<sandbox-directory>]"
49 49 puts " <sandbox-directory> is defaulted to ./sandbox"
50 50 puts " <test-result-directory> is defaulted to ./test-result"
51 51 puts "WARNING: The judge script will forcefully create the (implicitly and explicitly) specified directories and remove anything inside it."
52 52 exit(127)
53 53 end
54 54
55 55 language = ARGV[0]
56 56 if language != "c" && language != "c++" && language != "pas"
57 57 log "You specified a language that is not supported: #{language}."
58 58 exit(127)
59 59 end
60 60
61 61 source_file = ARGV[1]
62 62 if File.exist?(source_file) == false
63 63 log "The source file does not exist."
64 64 exit(127)
65 65 end
66 66
67 67 log "Making test result and sandbox directories..."
68 68
69 - current_dir = `pwd`
69 + current_dir = FileUtils.pwd
70 70 current_dir.strip!
71 71
72 72 if ARGV.length >= 3
73 73 test_result_dir = ARGV[2]
74 74 else
75 75 test_result_dir = "#{current_dir}/test-result"
76 76 end
77 77
78 78 log "Test result directory: #{test_result_dir}"
79 79 clear_and_create_empty_dir(test_result_dir)
80 80
81 81 if ARGV.length >= 4
82 82 sandbox_dir = ARGV[3]
83 83 else
84 84 sandbox_dir = "#{current_dir}/sandbox"
85 85 end
86 86 log "Sandbox directory: #{sandbox_dir}"
87 87 clear_and_create_empty_dir(sandbox_dir)
88 88
89 89 # Compile
90 90 log
91 91 log "Compiling..."
92 92 call_and_log("Cannot copy the source file to #{sandbox_dir}") {
93 93 FileUtils.cp(source_file, sandbox_dir)
94 94 }
95 95 begin
96 96 Dir.chdir sandbox_dir
97 97 rescue
98 98 log "ERROR: Cannot change directory to #{sandbox_dir}."
99 99 exit(127)
100 100 end
101 101 execute("#{problem_home}/script/compile #{language} #{source_file}", "Compilation error!")
102 - compile_message = `cat compiler_message`
102 + compile_message = open("compiler_message").read
103 103 compile_message.strip!
104 104 call_and_log("Cannot move the compiler message to #{test_result_dir}.") {
105 105 FileUtils.mv("compiler_message", test_result_dir)
106 106 }
107 107 if !FileTest.exist?("a.out")
108 108 log "Cannot compile the source code. See message in #{test_result_dir}/compile_message"
109 109 exit(127)
110 110 else
111 111 call_and_log("Cannot move the compiled program to #{test_result_dir}") {
112 112 FileUtils.mv("a.out",test_result_dir)
113 113 }
114 114 FileUtils.rm_rf("#{sandbox_dir}/.")
115 115 end
116 116
117 117 require "#{problem_home}/script/test_dsl.rb"
118 118 load "#{problem_home}/test_cases/all_tests.cfg"
119 119 problem = Problem.get_instance
120 120
121 121 if problem.well_formed? == false
122 122 log "The problem specification is not well formed."
123 123 exit(127)
124 124 end
125 125
126 126 # Doing the testing.
127 127 (1..(problem.num_tests)).each do |test_num|
128 128
129 129 $stdout.print "[#{test_num}]"
130 130 $stdout.flush
131 131
132 132 log "Test number: #{test_num}"
133 133 call_and_log("Cannot copy the compiled program into #{sandbox_dir}") {
134 134 FileUtils.cp("#{test_result_dir}/a.out", sandbox_dir)
135 135 }
136 136 begin
137 137 execute("#{problem_home}/script/run #{language} #{test_num}", "Error occured during execution of the run script")
138 138 rescue
139 139 # do nothing
140 140 end
141 141 call_and_log("Cannot create directory #{test_result_dir}/#{test_num}") {
142 142 FileUtils.mkdir "#{test_result_dir}/#{test_num}"
143 143 }
144 144 call_and_log("Cannot copy the result file into #{test_result_dir}/#{test_num}") {
145 145 FileUtils.mv "#{sandbox_dir}/result", "#{test_result_dir}/#{test_num}"
146 146 }
147 147 call_and_log("Cannot copy the comment file into #{test_result_dir}/#{test_num}") {
148 148 FileUtils.mv "#{sandbox_dir}/comment", "#{test_result_dir}/#{test_num}"
149 149 }
150 150 call_and_log("Cannot copy the output file into #{test_result_dir}/#{test_num}") {
151 151 FileUtils.mv "#{sandbox_dir}/output.txt", "#{test_result_dir}/#{test_num}"
152 152 }
153 153 call_and_log("Cannot clear #{sandbox_dir}") {
154 154 FileUtils.rm_rf(Dir.glob("#{sandbox_dir}/*"), :secure => true)
155 155 }
156 156 end
157 157
158 158 $stdout.print "[done]\n"
159 159
160 160 # Grade
161 161 log
162 162 log "Grading..."
163 163 begin
164 164 Dir.chdir test_result_dir
165 165 rescue
166 166 log "ERROR: Cannot change directory to #{test_result_dir}."
167 167 exit(127)
168 168 end
169 169 execute("#{problem_home}/script/grade", "An error occured during grading!")
170 170
171 171 log
172 172 log "All done!"
@@ -1,155 +1,155
1 1 #!/usr/bin/ruby
2 2
3 3 def log(str='')
4 4 if ENV['TALKATIVE']!=nil
5 5 puts str
6 6 end
7 7 if ENV['GRADER_LOGGING']!=nil
8 8 log_fname = ENV['GRADER_LOGGING']
9 9 fp = File.open(log_fname,"a")
10 10 fp.puts("run: #{Time.new.strftime("%H:%M")} #{str}")
11 11 fp.close
12 12 end
13 13 end
14 14
15 15 def extract_time(t)
16 16 # puts "TIME: #{t}"
17 17 if (result=/^(.*)r(.*)u(.*)s/.match(t))
18 18 {:real => result[1], :user => result[2], :sys => result[3]}
19 19 else
20 20 #{:real => 0, :user => 0, :sys => 0}
21 21 #puts "ERROR READING RUNNING TIME: #{t}"
22 22 raise "Error reading running time: #{t}"
23 23 end
24 24 end
25 25
26 26 def compile_box(source,bin)
27 27 system("g++ #{source} -o #{bin}")
28 28 end
29 29
30 30 if ARGV.length < 2 || ARGV.length > 3
31 31 puts "Usage: run <language> <test-num> [<program-name>]"
32 32 exit(127)
33 33 end
34 34
35 35 language = ARGV[0]
36 36 test_num = ARGV[1].to_i
37 37 if ARGV.length > 2
38 38 program_name = ARGV[2]
39 39 else
40 40 program_name = "a.out"
41 41 end
42 42
43 43 problem_home = ENV['PROBLEM_HOME']
44 44 require "#{problem_home}/script/test_dsl.rb"
45 45 load "#{problem_home}/test_cases/all_tests.cfg"
46 46 problem = Problem.get_instance
47 47
48 48 if problem.well_formed? == false
49 49 log "The problem specification is not well formed."
50 50 exit(127)
51 51 end
52 52
53 53 # Check if the test number is okay.
54 54 if test_num <= 0 || test_num > problem.num_tests
55 55 log "You have specified a wrong test number."
56 56 exit(127)
57 57 end
58 58
59 59 #####################################
60 60 # Set the relavant file names here. #
61 61 #####################################
62 62
63 63 input_file_name = "#{problem_home}/test_cases/#{test_num}/input-#{test_num}.txt"
64 64
65 65 #####################################
66 66
67 67 time_limit = problem.get_time_limit test_num
68 68 mem_limit = problem.get_mem_limit(test_num) * 1024
69 69
70 70 # Copy the input file.
71 71 #`cp #{problem_home}/test_cases/#{test_num}/#{input_file_name} .`
72 72
73 73 # check if box is there, if not, compile it!
74 74 if !File.exists?("#{problem_home}/script/box")
75 75 log "WARNING: Compiling box: to increase efficiency, it should be compile manually"
76 76 compile_box("#{problem_home}/script/box.cc",
77 77 "#{problem_home}/script/box")
78 78 end
79 79
80 80 # Hide PROBLEM_HOME
81 81 ENV['PROBLEM_HOME'] = nil
82 82
83 83 # Run the program.
84 84 #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}"
85 85 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"
86 86 log "Running test #{test_num}..."
87 87 log run_command
88 88 log
89 89 system(run_command)
90 90
91 91 # Restore PROBLEM_HOME
92 92 ENV['PROBLEM_HOME'] = problem_home
93 93
94 94 # Create the result file.
95 95 result_file = File.new("result", "w")
96 96 comment_file = File.new("comment", "w")
97 97
98 98 # Check if the program actually produced any output.
99 99 run_result_file = File.new("run_result", "r")
100 100 run_result = run_result_file.readlines
101 101 run_result_file.close
102 102
103 103 run_stat = run_result[run_result.length-1]
104 104 running_time = extract_time(run_stat)
105 105
106 106 report = lambda{ |status, points, comment|
107 107 result_file.write status.strip
108 108 result_file.write "\n"
109 109 result_file.write points.to_s.strip
110 110 result_file.write "\n"
111 111 result_file.write run_stat.strip
112 112 result_file.write "\n"
113 113 result_file.close
114 - `rm run_result`
114 + FileUtils.rm "run_result"
115 115 # `rm output.txt` --- keep the output
116 116
117 117 comment_file.write comment
118 118
119 119 # added for debuggin --- jittat
120 120 comment_file.write "--run-result--\n"
121 121 run_result.each do |l|
122 122 comment_file.write l
123 123 end
124 124
125 125 comment_file.close
126 126
127 127 log "Done!"
128 128 exit(0)
129 129 }
130 130
131 131 if run_result[0][0,2] != "OK"
132 132 log "There was a runtime error."
133 133 report.call(run_result[0], 0, "No comment.\n")
134 134 end
135 135
136 136 if running_time[:user].to_f + running_time[:sys].to_f > time_limit
137 137 log "Time limit exceeded."
138 138 report.call("Time limit exceeded", 0, "No comment.\n")
139 139 end
140 140
141 141 # Run 'check' to evaluate the output.
142 142 #puts "There was no runtime error. Proceed to checking the output."
143 143 check_command = "#{problem_home}/script/check #{language} #{test_num}"
144 144 log "Checking the output..."
145 145 log check_command
146 146 if not system(check_command)
147 147 log "Problem with check script"
148 148 report.call("Incorrect",0,"Check script error.\n")
149 149 exit(127)
150 150 end
151 151
152 152 check_file = File.new("check_result", "r")
153 153 check_file_lines = check_file.readlines
154 154
155 155 report.call(check_file_lines[0], check_file_lines[1], "No comment.\n")
You need to be logged in to leave comments. Login now