forked from cafe-grader-team/cafe-grader-judge-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrader
executable file
·443 lines (364 loc) · 10.5 KB
/
grader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/env ruby
def stop_grader(id)
if id==:all
File.open(File.dirname(__FILE__) + "/stop.all",'w').close
else
File.open(File.dirname(__FILE__) + "/stop.#{id}",'w').close
end
end
def check_stopfile
FileTest.exist?(File.dirname(__FILE__) + "/stop.all") or
FileTest.exist?(File.dirname(__FILE__) + "/stop.#{Process.pid}")
end
def clear_stopfile
if FileTest.exist?(File.dirname(__FILE__) + "/stop.#{Process.pid}")
File.delete(File.dirname(__FILE__) + "/stop.#{Process.pid}")
end
end
def config
Grader::Configuration.get_instance
end
def log_file_name
if !File.exists?(config.log_dir)
raise "Log directory does not exist: #{config.log_dir}"
end
config.log_dir +
"/#{GRADER_ENV}_#{config.grader_mode}.#{Process.pid}"
end
def log(str)
if config.talkative
puts str
end
if config.logging
fp = File.open(log_file_name,"a")
fp.puts("GRADER: #{Time.new.strftime("%H:%M")} #{str}")
fp.close
end
end
def display_manual
puts <<USAGE
Grader.
using: (1) grader
(2) grader environment [mode]
(3) grader stop [all|pids-list]
(4) grader --help
(1) call grader with environment = 'exam', mode = 'queue'
(2) possible modes are: 'queue', 'test_request', 'prob', 'sub', 'contest', and 'autonew'
(3) create stop-file to stop running grader in queue mode
(4) You are here.
USAGE
end
def process_options_and_stop_file
# The list of options are:
# - stop [all|process ids]
# -
# Process 'help' option
if (ARGV.length==1) and (/help/.match(ARGV[0]))
display_manual
exit(0)
end
# Process 'stop' option.
if (ARGV.length >= 1) and (ARGV[0]=='stop')
if ARGV.length==1
puts "you should specify pid-list or 'all'"
display_manual
elsif (ARGV.length==2) and (ARGV[1]=='all')
stop_grader(:all)
puts "A global stop file ('stop.all') created."
puts "You should remove it manually later."
else
(1..ARGV.length-1).each do |i|
stop_grader(ARGV[i])
end
puts "stop file(s) created"
end
exit(0)
end
# Check stop file.
if check_stopfile
puts "Stop file exists. Terminated."
clear_stopfile
exit(0)
end
#default options
options = {
:mode => 'queue',
:environment => 'exam',
:dry_run => false,
}
# Process mode and environment option
if ARGV.length >= 1
options[:environment] = ARGV.shift
if ARGV.length >=1
options[:mode] = ARGV.shift
end
end
options[:dry_run] = (ARGV.delete('--dry') != nil)
if options[:dry_run] and (not ['prob','contest','autonew'].include? options[:mode])
puts "Dry run currently works only for 'prob' or 'contest' modes."
exit(0)
end
options[:report] = (ARGV.delete('--report') != nil)
if options[:report] and (not ['prob','contest','autonew'].include? options[:mode])
puts "Report currently works only for 'prob' or 'contest' modes."
exit(0)
end
return options
end
class ResultCollector
def initialize
@results = {}
@problems = {}
@users = {}
end
def after_save_hook(submission, grading_result)
end
def save(submission, grading_result)
user = submission.user
problem = submission.problem
if not @problems.has_key? problem.id
@problems[problem.id] = problem
end
if not @users.has_key? user.id
@users[user.id] = user
end
@results[[user.id, problem.id]] = grading_result
after_save_hook(submission, grading_result)
end
def print_report_by_user
puts "---------------------"
puts " REPORT"
puts "---------------------"
print "login,email"
@problems.each_value do |problem|
print ",#{problem.name}"
end
print "\n"
@users.each_value do |user|
print "#{user.login},#{user.email}"
@problems.each_value do |problem|
if @results.has_key? [user.id, problem.id]
print ",#{@results[[user.id,problem.id]][:points]}"
else
print ","
end
end
print "\n"
end
end
end
def grader_general_loop(engine, grader_proc, options)
runner = Grader::Runner.new(engine, grader_proc)
while true
if check_stopfile # created by calling grader stop
clear_stopfile
log "stopped (with stop file)"
break
end
task = yield(runner)
if task==nil
sleep(1)
end
end
end
def grader_queue_loop(grader_proc, options)
log "Grader: queue"
engine = Grader::Engine.new
grader_general_loop(engine, grader_proc, options) do |runner|
runner.grade_oldest_task
end
end
def grader_test_request_loop(grader_proc, options)
log "Grader: test_request"
engine = Grader::Engine.new(:room_maker => Grader::TestRequestRoomMaker.new,
:reporter => Grader::TestRequestReporter.new)
grader_general_loop(engine, grader_proc, options) do |runner|
runner.grade_oldest_test_request
end
end
def grader_autonew_loop(grader_proc, options)
log "Grader: autonew"
if options[:report]
result_collector = ResultCollector.new
else
result_collector = nil
end
if options[:dry_run]
puts "Running in dry mode"
end
prob_reporter = Grader::SubmissionReporter.new(:dry_run => options[:dry_run],
:result_collector => result_collector)
engine = Grader::Engine.new(:reporter => prob_reporter)
runner = Grader::Runner.new(engine, grader_proc)
grader_proc.report_active if grader_proc!=nil
latest_submitted_at = nil
graded_submission_ids = {}
while true
if check_stopfile # created by calling grader stop
clear_stopfile
log "stopped (with stop file)"
break
end
if latest_submitted_at==nil
submissions = Submission.all
else
submissions = Submission.all(:conditions => ["submitted_at >= :latest",
{:latest => latest_submitted_at}])
end
graded_any = false
if submissions.length != 0
submissions.each do |submission|
if (submission.problem == nil) or (!submission.problem.available)
next
end
if ! graded_submission_ids[submission.id]
runner.grade_submission(submission)
graded_submission_ids[submission.id] = true
if (!latest_submitted_at or
latest_submitted_at < submission.submitted_at)
latest_submitted_at = submission.submitted_at
end
puts "graded: #{submission.id}"
puts "latest: #{latest_submitted_at}"
graded_any = true
end
end
end
if ! graded_any
sleep(1)
end
end
end
def grader_grade_problems(grader_proc, options)
if options[:report]
result_collector = ResultCollector.new
else
result_collector = nil
end
if options[:dry_run]
puts "Running in dry mode"
end
prob_reporter = Grader::SubmissionReporter.new(:dry_run => options[:dry_run],
:result_collector => result_collector)
engine = Grader::Engine.new(:reporter => prob_reporter)
runner = Grader::Runner.new(engine, grader_proc)
grader_proc.report_active if grader_proc!=nil
ARGV.each do |prob_name|
prob = Problem.find_by_name(prob_name)
if prob==nil
puts "cannot find problem: #{prob_name}"
else
runner.grade_problem(prob)
end
end
if options[:report]
result_collector.print_report_by_user
end
end
def grader_grade_contests(grader_proc, options)
# always use dry run when grading during contest
dry_run = options[:dry_run] = true
contest_name = ARGV.shift
contest = Contest.find_by_name(contest_name)
if contest==nil
puts "cannot find contest: #{contest_name}"
exit(0)
end
if options[:report]
result_collector = ResultCollector.new
else
result_collector = nil
end
if options[:dry_run]
puts "Running in dry mode"
end
prob_reporter = Grader::SubmissionReporter.new(:dry_run => dry_run,
:result_collector => result_collector)
engine = Grader::Engine.new(:reporter => prob_reporter)
runner = Grader::Runner.new(engine, grader_proc)
grader_proc.report_active if grader_proc!=nil
contest.problems.each do |problem|
puts "Grading: #{problem.name}"
runner.grade_problem(problem,
:user_conditions => lambda do |u|
u.contest_finished? and
u.contest_ids.include?(contest.id)
end)
end
if options[:report]
result_collector.print_report_by_user
end
end
def grader_grade_submissions(grader_proc, options)
engine = Grader::Engine.new
runner = Grader::Runner.new(engine, grader_proc)
grader_proc.report_active if grader_proc!=nil
ARGV.each do |sub_id|
puts "Grading #{sub_id}"
begin
submission = Submission.find(sub_id.to_i)
rescue ActiveRecord::RecordNotFound
puts "Submission #{sub_id} not found"
submission = nil
end
if submission!=nil
runner.grade_submission(submission)
end
end
end
#########################################
# main program
#########################################
options = process_options_and_stop_file
GRADER_ENV = options[:environment]
grader_mode = options[:mode]
dry_run = options[:dry_run]
puts "environment: #{GRADER_ENV}"
require File.join(File.dirname(__FILE__),'config/environment')
# add grader_mode to config
# this is needed because method log needs it. TODO: clean this up
class << config
attr_accessor :grader_mode
end
config.grader_mode = grader_mode
# reading rails environment
log 'Reading rails environment'
RAILS_ENV = config.rails_env
require RAILS_ROOT + '/config/environment'
# register grader process
if config.report_grader
grader_proc = GraderProcess.register(config.grader_hostname,
Process.pid,
grader_mode)
else
grader_proc = nil
end
#set loggin environment
ENV['GRADER_LOGGING'] = log_file_name
# register exit handler to report inactive, and terminated
at_exit do
if grader_proc!=nil
grader_proc.report_inactive
grader_proc.terminate
end
end
#
# MAIN LOOP
#
case grader_mode
when "queue"
grader_queue_loop(grader_proc, options)
when "test_request"
grader_test_request_loop(grader_proc, options)
when "prob"
grader_grade_problems(grader_proc, options)
when "contest"
grader_grade_contests(grader_proc, options)
when "sub"
grader_grade_submissions(grader_proc, options)
when "autonew"
grader_autonew_loop(grader_proc, options)
else
display_manual
exit(0)
end