Description:
added --dry option to grader prob mode
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r91:5df0d71260f4 - - 2 files changed: 42 inserted, 16 deleted
@@ -1,217 +1,239 | |||
|
1 | 1 | #!/usr/bin/ruby |
|
2 | 2 | |
|
3 | 3 | def stop_grader(id) |
|
4 | 4 | if id==:all |
|
5 | 5 | File.open(File.dirname(__FILE__) + "/stop.all",'w').close |
|
6 | 6 | else |
|
7 | 7 | File.open(File.dirname(__FILE__) + "/stop.#{id}",'w').close |
|
8 | 8 | end |
|
9 | 9 | end |
|
10 | 10 | |
|
11 | 11 | def check_stopfile |
|
12 | 12 | FileTest.exist?(File.dirname(__FILE__) + "/stop.all") or |
|
13 | 13 | FileTest.exist?(File.dirname(__FILE__) + "/stop.#{Process.pid}") |
|
14 | 14 | end |
|
15 | 15 | |
|
16 | 16 | def clear_stopfile |
|
17 | 17 | if FileTest.exist?(File.dirname(__FILE__) + "/stop.#{Process.pid}") |
|
18 | 18 | system("rm " + File.dirname(__FILE__) + "/stop.#{Process.pid}") |
|
19 | 19 | end |
|
20 | 20 | end |
|
21 | 21 | |
|
22 | 22 | def config |
|
23 | 23 | Grader::Configuration.get_instance |
|
24 | 24 | end |
|
25 | 25 | |
|
26 | 26 | def log_file_name |
|
27 | 27 | if !File.exists?(config.log_dir) |
|
28 | 28 | raise "Log directory does not exist: #{config.log_dir}" |
|
29 | 29 | end |
|
30 | 30 | config.log_dir + |
|
31 | 31 | "/#{GRADER_ENV}_#{config.grader_mode}.#{Process.pid}" |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def log(str) |
|
35 | 35 | if config.talkative |
|
36 | 36 | puts str |
|
37 | 37 | end |
|
38 | 38 | if config.logging |
|
39 | 39 | fp = File.open(log_file_name,"a") |
|
40 | 40 | fp.puts("GRADER: #{Time.new.strftime("%H:%M")} #{str}") |
|
41 | 41 | fp.close |
|
42 | 42 | end |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def display_manual |
|
46 | 46 | puts <<USAGE |
|
47 | 47 | Grader. |
|
48 | 48 | using: (1) grader |
|
49 | 49 | (2) grader environment [mode] |
|
50 | 50 | (3) grader stop [all|pids-list] |
|
51 | 51 | (4) grader --help |
|
52 | 52 | (1) call grader with environment = 'exam', mode = 'queue' |
|
53 | 53 | (2) possible modes are: 'queue', 'prob', 'test_request' |
|
54 | 54 | (3) create stop-file to stop running grader in queue mode |
|
55 | 55 | (4) You are here. |
|
56 | 56 | USAGE |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | ######################################### |
|
60 | 60 | # main program |
|
61 | 61 | ######################################### |
|
62 | 62 | |
|
63 | - # with --help | |
|
63 | + # Reading environment and options. | |
|
64 | + | |
|
65 | + def process_options_and_stop_file | |
|
66 | + # The list of options are: | |
|
67 | + # - stop [all|process ids] | |
|
68 | + # - | |
|
69 | + | |
|
70 | + # Process 'help' option | |
|
64 | 71 | if (ARGV.length==1) and (/help/.match(ARGV[0])) |
|
65 | 72 | display_manual |
|
66 | 73 | exit(0) |
|
67 | 74 | end |
|
68 | 75 | |
|
69 | - # reading environment and options | |
|
76 | + # Process 'stop' option. | |
|
70 | 77 | if (ARGV.length >= 1) and (ARGV[0]=='stop') |
|
71 | 78 | if ARGV.length==1 |
|
72 | 79 | puts "you should specify pid-list or 'all'" |
|
73 | 80 | display_manual |
|
74 | 81 | elsif (ARGV.length==2) and (ARGV[1]=='all') |
|
75 | 82 | stop_grader(:all) |
|
76 | 83 | puts "A global stop file ('stop.all') created." |
|
77 | 84 | puts "You should remove it manually later." |
|
78 | 85 | else |
|
79 | 86 | (1..ARGV.length-1).each do |i| |
|
80 | 87 | stop_grader(ARGV[i]) |
|
81 | 88 | end |
|
82 | 89 | puts "stop file(s) created" |
|
83 | 90 | end |
|
84 | 91 | exit(0) |
|
85 | 92 | end |
|
86 | 93 | |
|
94 | + # Check stop file. | |
|
87 | 95 | if check_stopfile |
|
88 | 96 | puts "Stop file exists. Terminated." |
|
89 | 97 | clear_stopfile |
|
90 | 98 | exit(0) |
|
91 | 99 | end |
|
92 | 100 | |
|
93 | - grader_mode = 'queue' | |
|
101 | + #default options | |
|
102 | + options = { | |
|
103 | + :mode => 'queue', | |
|
104 | + :environment => 'exam', | |
|
105 | + :dry_run => false, | |
|
106 | + } | |
|
107 | + | |
|
108 | + # Process mode and environment option | |
|
109 | + if ARGV.length >= 1 | |
|
110 | + options[:environment] = ARGV.shift | |
|
94 | 111 |
if ARGV.length >= |
|
95 | - GRADER_ENV = ARGV[0] | |
|
96 | - if ARGV.length >=2 | |
|
97 | - grader_mode = ARGV[1] | |
|
112 | + options[:mode] = ARGV.shift | |
|
113 | + end | |
|
114 | + end | |
|
115 | + | |
|
116 | + options[:dry_run] = (ARGV.delete('--dry') != nil) | |
|
117 | + if options[:dry_run] and (not options[:mode] == 'prob') | |
|
118 | + puts "Dry run currently works for 'prob' mode." | |
|
119 | + exit(0) | |
|
98 | 120 | end |
|
99 | - else | |
|
100 | - GRADER_ENV = 'exam' | |
|
121 | + | |
|
122 | + return options | |
|
101 | 123 | end |
|
102 | 124 | |
|
125 | + # ======= Main ======== | |
|
126 | + options = process_options_and_stop_file | |
|
127 | + GRADER_ENV = options[:environment] | |
|
128 | + grader_mode = options[:mode] | |
|
129 | + dry_run = options[:dry_run] | |
|
130 | + | |
|
103 | 131 | puts "environment: #{GRADER_ENV}" |
|
104 | 132 | require File.join(File.dirname(__FILE__),'config/environment') |
|
105 | 133 | |
|
106 | 134 | # add grader_mode to config |
|
107 | 135 | # this is needed because method log needs it. TODO: clean this up |
|
108 | 136 | class << config |
|
109 | 137 | attr_accessor :grader_mode |
|
110 | 138 | end |
|
111 | 139 | config.grader_mode = grader_mode |
|
112 | 140 | |
|
113 | 141 | # reading rails environment |
|
114 | 142 | log 'Reading rails environment' |
|
115 | 143 | |
|
116 | 144 | RAILS_ENV = config.rails_env |
|
117 | 145 | require RAILS_ROOT + '/config/environment' |
|
118 | 146 | |
|
119 | 147 | # register grader process |
|
120 | 148 | if config.report_grader |
|
121 | 149 | grader_proc = GraderProcess.register(config.grader_hostname, |
|
122 | 150 | Process.pid, |
|
123 | 151 | grader_mode) |
|
124 | 152 | else |
|
125 | 153 | grader_proc = nil |
|
126 | 154 | end |
|
127 | 155 | |
|
128 | 156 | #set loggin environment |
|
129 | 157 | ENV['GRADER_LOGGING'] = log_file_name |
|
130 | 158 | |
|
131 | 159 | # register exit handler to report inactive, and terminated |
|
132 | 160 | at_exit do |
|
133 | 161 | if grader_proc!=nil |
|
134 | 162 | grader_proc.report_inactive |
|
135 | 163 | grader_proc.terminate |
|
136 | 164 | end |
|
137 | 165 | end |
|
138 | 166 | |
|
139 | 167 | # |
|
140 | 168 | # MAIN LOOP |
|
141 | 169 | # |
|
142 | 170 | |
|
143 | 171 | case grader_mode |
|
144 | 172 | when "queue", "test_request" |
|
145 | 173 | log "Grader: #{grader_mode}" |
|
146 | 174 | if grader_mode=="queue" |
|
147 | 175 | engine = Grader::Engine.new |
|
148 | 176 | else |
|
149 | 177 | engine = Grader::Engine.new(Grader::TestRequestRoomMaker.new, |
|
150 | 178 | Grader::TestRequestReporter.new) |
|
151 | 179 | end |
|
152 | 180 | |
|
153 | 181 | runner = Grader::Runner.new(engine, grader_proc) |
|
154 | 182 | while true |
|
155 | 183 | |
|
156 | 184 | if check_stopfile # created by calling grader stop |
|
157 | 185 | clear_stopfile |
|
158 | 186 | log "stopped (with stop file)" |
|
159 | 187 | break |
|
160 | 188 | end |
|
161 | 189 | |
|
162 | 190 | if grader_mode=="queue" |
|
163 | 191 | task = runner.grade_oldest_task |
|
164 | 192 | else |
|
165 | 193 | task = runner.grade_oldest_test_request |
|
166 | 194 | end |
|
167 | 195 | if task==nil |
|
168 | 196 | sleep(1) |
|
169 | 197 | end |
|
170 | 198 | end |
|
171 | 199 | |
|
172 | 200 | when "prob" |
|
173 | - engine = Grader::Engine.new | |
|
201 | + engine = Grader::Engine.new(nil, Grader::SubmissionReporter.new(dry_run)) | |
|
174 | 202 | runner = Grader::Runner.new(engine, grader_proc) |
|
175 | 203 | |
|
176 | 204 | grader_proc.report_active if grader_proc!=nil |
|
177 | 205 | |
|
178 | - ARGV.shift | |
|
179 | - ARGV.shift | |
|
180 | - | |
|
181 | 206 | ARGV.each do |prob_name| |
|
182 | 207 | prob = Problem.find_by_name(prob_name) |
|
183 | 208 | if prob==nil |
|
184 | 209 | puts "cannot find problem: #{prob_name}" |
|
185 | 210 | else |
|
186 | 211 | runner.grade_problem(prob) |
|
187 | 212 | end |
|
188 | 213 | end |
|
189 | 214 | |
|
190 | 215 | when "sub" |
|
191 | 216 | engine = Grader::Engine.new |
|
192 | 217 | runner = Grader::Runner.new(engine, grader_proc) |
|
193 | 218 | |
|
194 | 219 | grader_proc.report_active if grader_proc!=nil |
|
195 | 220 | |
|
196 | - ARGV.shift | |
|
197 | - ARGV.shift | |
|
198 | - | |
|
199 | 221 | ARGV.each do |sub_id| |
|
200 | 222 | puts "Grading #{sub_id}" |
|
201 | 223 | begin |
|
202 | 224 | submission = Submission.find(sub_id.to_i) |
|
203 | 225 | rescue ActiveRecord::RecordNotFound |
|
204 | 226 | puts "Record not found" |
|
205 | 227 | submission = nil |
|
206 | 228 | end |
|
207 | 229 | |
|
208 | 230 | if submission!=nil |
|
209 | 231 | runner.grade_submission(submission) |
|
210 | 232 | end |
|
211 | 233 | end |
|
212 | 234 | |
|
213 | 235 | else |
|
214 | 236 | display_manual |
|
215 | 237 | exit(0) |
|
216 | 238 | end |
|
217 | 239 |
@@ -1,123 +1,127 | |||
|
1 | 1 | module Grader |
|
2 | 2 | |
|
3 | 3 | class SubmissionRoomMaker |
|
4 | 4 | def initialize |
|
5 | 5 | @config = Grader::Configuration.get_instance |
|
6 | 6 | end |
|
7 | 7 | |
|
8 | 8 | def produce_grading_room(submission) |
|
9 | 9 | user = submission.user |
|
10 | 10 | problem = submission.problem |
|
11 | 11 | grading_room = "#{@config.user_result_dir}/" + |
|
12 | 12 | "#{user.login}/#{problem.name}/#{submission.id}" |
|
13 | 13 | |
|
14 | 14 | FileUtils.mkdir_p(grading_room) |
|
15 | 15 | grading_room |
|
16 | 16 | end |
|
17 | 17 | |
|
18 | 18 | def find_problem_home(submission) |
|
19 | 19 | problem = submission.problem |
|
20 | 20 | "#{@config.problems_dir}/#{problem.name}" |
|
21 | 21 | end |
|
22 | 22 | |
|
23 | 23 | def save_source(submission,source_name) |
|
24 | 24 | dir = self.produce_grading_room(submission) |
|
25 | 25 | f = File.open("#{dir}/#{source_name}","w") |
|
26 | 26 | f.write(submission.source) |
|
27 | 27 | f.close |
|
28 | 28 | end |
|
29 | 29 | |
|
30 | 30 | def clean_up(submission) |
|
31 | 31 | end |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | class SubmissionReporter |
|
35 | - def initialize | |
|
35 | + def initialize(dry_run=false) | |
|
36 | 36 | @config = Grader::Configuration.get_instance |
|
37 | + @dry_run = dry_run | |
|
37 | 38 | end |
|
38 | 39 | |
|
39 | 40 | def report(sub,test_result_dir) |
|
40 | 41 | save_result(sub,read_result(test_result_dir)) |
|
41 | 42 | end |
|
42 | 43 | |
|
43 | 44 | def report_error(sub,msg) |
|
44 | 45 | save_result(sub,{:points => 0, |
|
45 | 46 | :comment => "Grading error: #{msg}" }) |
|
46 | 47 | end |
|
47 | 48 | |
|
48 | 49 | protected |
|
49 | 50 | def read_result(test_result_dir) |
|
50 | 51 | cmp_msg_fname = "#{test_result_dir}/compiler_message" |
|
51 | 52 | if FileTest.exist?(cmp_msg_fname) |
|
52 | 53 | cmp_file = File.open(cmp_msg_fname) |
|
53 | 54 | cmp_msg = cmp_file.read |
|
54 | 55 | cmp_file.close |
|
55 | 56 | else |
|
56 | 57 | cmp_msg = "" |
|
57 | 58 | end |
|
58 | 59 | |
|
59 | 60 | result_fname = "#{test_result_dir}/result" |
|
60 | 61 | comment_fname = "#{test_result_dir}/comment" |
|
61 | 62 | if FileTest.exist?(result_fname) |
|
62 | 63 | comment = "" |
|
63 | 64 | begin |
|
64 | 65 | result_file = File.open(result_fname) |
|
65 | 66 | result = result_file.readline.to_i |
|
66 | 67 | result_file.close |
|
67 | 68 | rescue |
|
68 | 69 | result = 0 |
|
69 | 70 | comment = "error reading result file." |
|
70 | 71 | end |
|
71 | 72 | |
|
72 | 73 | begin |
|
73 | 74 | comment_file = File.open(comment_fname) |
|
74 | 75 | comment += comment_file.readline.chomp |
|
75 | 76 | comment_file.close |
|
76 | 77 | rescue |
|
77 | 78 | comment += "" |
|
78 | 79 | end |
|
79 | 80 | |
|
80 | 81 | return {:points => result, |
|
81 | 82 | :comment => comment, |
|
82 | 83 | :cmp_msg => cmp_msg} |
|
83 | 84 | else |
|
84 | 85 | if FileTest.exist?("#{test_result_dir}/a.out") |
|
85 | 86 | return {:points => 0, |
|
86 | 87 | :comment => 'error during grading', |
|
87 | 88 | :cmp_msg => cmp_msg} |
|
88 | 89 | else |
|
89 | 90 | return {:points => 0, |
|
90 | 91 | :comment => 'compilation error', |
|
91 | 92 | :cmp_msg => cmp_msg} |
|
92 | 93 | end |
|
93 | 94 | end |
|
94 | 95 | end |
|
95 | 96 | |
|
96 | 97 | def save_result(submission,result) |
|
97 | 98 | problem = submission.problem |
|
98 | 99 | submission.graded_at = Time.now.gmtime |
|
99 | 100 | points = result[:points] |
|
100 | 101 | submission.points = points |
|
101 | 102 | comment = @config.report_comment(result[:comment]) |
|
102 | 103 | |
|
103 | 104 | # |
|
104 | 105 | # TODO: FIX THIS MESSAGE |
|
105 | 106 | # |
|
106 | 107 | if problem == nil |
|
107 | 108 | submission.grader_comment = 'PASSED: ' + comment + '(problem is nil)' |
|
108 | 109 | elsif points == problem.full_score |
|
109 | 110 | #submission.grader_comment = 'PASSED: ' + comment |
|
110 | 111 | submission.grader_comment = comment |
|
111 | 112 | elsif result[:comment].chomp =~ /^[\[\]P]+$/ |
|
112 | 113 | submission.grader_comment = 'PASSED: ' + comment + '(inconsistent score)' |
|
113 | 114 | else |
|
114 | 115 | #submission.grader_comment = 'FAILED: ' + comment |
|
115 | 116 | submission.grader_comment = comment |
|
116 | 117 | end |
|
117 | 118 | submission.compiler_message = result[:cmp_msg] or '' |
|
119 | + | |
|
120 | + if not @dry_run | |
|
118 | 121 | submission.save |
|
119 | 122 | end |
|
123 | + end | |
|
120 | 124 | |
|
121 | 125 | end |
|
122 | 126 | |
|
123 | 127 | end |
You need to be logged in to leave comments.
Login now