-
Notifications
You must be signed in to change notification settings - Fork 12
/
grader_id
executable file
·91 lines (74 loc) · 2.26 KB
/
grader_id
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
#!/usr/bin/env ruby
require 'fileutils'
def talk(str)
if TALKATIVE
puts str
end
end
def save_source(submission,dir,fname)
f = File.open("#{dir}/#{fname}","w")
f.write(submission.source)
f.close
end
def call_judge(problem_home,language,problem_out_dir,fname)
ENV['PROBLEM_HOME'] = problem_home
Dir.chdir problem_out_dir
cmd = "#{problem_home}/script/judge #{language} #{fname}"
# puts "CMD: #{cmd}"
system(cmd)
end
def read_result(test_result_dir)
cmp_msg_fname = "#{test_result_dir}/compiler_message"
cmp_msg = File.open(cmp_msg_fname).read
result_fname = "#{test_result_dir}/result"
comment_fname = "#{test_result_dir}/comment"
if FileTest.exist?(result_fname)
result = File.open(result_fname).readline.to_i
comment = File.open(comment_fname).readline.chomp
return {:points => result,
:comment => comment,
:cmp_msg => cmp_msg}
else
return {:points => 0,
:comment => 'compile error',
:cmp_msg => cmp_msg}
end
end
def save_result(submission,result)
submission.graded_at = Time.now
submission.points = result[:points]
submission.grader_comment = report_comment(result[:comment])
submission.compiler_message = result[:cmp_msg]
submission.save
end
def grade(submission_id)
sub = Submission.find(submission_id)
user = sub.user
problem = sub.problem
language = sub.language.name
lang_ext = sub.language.ext
# FIX THIS
talk 'some hack on language'
if language == 'cpp'
language = 'c++'
end
user_dir = "#{USER_RESULT_DIR}/#{user.login}"
Dir.mkdir(user_dir) if !FileTest.exist?(user_dir)
problem_out_dir = "#{user_dir}/#{problem.name}"
Dir.mkdir(problem_out_dir) if !FileTest.exist?(problem_out_dir)
problem_home = "#{PROBLEMS_DIR}/#{problem.name}"
source_name = "#{problem.name}.#{lang_ext}"
save_source(sub,problem_out_dir,source_name)
call_judge(problem_home,language,problem_out_dir,source_name)
save_result(sub,read_result("#{problem_out_dir}/test-result"))
end
# reading environment and options
GRADER_ENV = 'exam'
puts "environment: #{GRADER_ENV}"
require File.dirname(__FILE__) + "/environment.rb"
#main program
talk 'Reading rails environment'
RAILS_ENV = 'development'
require RAILS_APP_DIR + '/config/environment'
current_dir = FileUtils.pwd
grade(ARGV[0].to_i)