Description:
(previous) new way for default script files git-svn-id: http://theory.cpe.ku.ac.th/grader/cli/trunk/scripts@26 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r13:f103b0ea2e2a - - 1 file changed: 1 inserted, 1 deleted

@@ -1,221 +1,221
1 #!/usr/bin/ruby
1 #!/usr/bin/ruby
2
2
3 def talk(str)
3 def talk(str)
4 if TALKATIVE
4 if TALKATIVE
5 puts str
5 puts str
6 end
6 end
7 end
7 end
8
8
9 def execute(command, error_message="")
9 def execute(command, error_message="")
10 if not system(command)
10 if not system(command)
11 puts "ERROR: #{error_message}"
11 puts "ERROR: #{error_message}"
12 exit(127)
12 exit(127)
13 end
13 end
14 end
14 end
15
15
16 def save_source(submission,dir,fname)
16 def save_source(submission,dir,fname)
17 f = File.open("#{dir}/#{fname}","w")
17 f = File.open("#{dir}/#{fname}","w")
18 f.write(submission.source)
18 f.write(submission.source)
19 f.close
19 f.close
20 end
20 end
21
21
22 def call_judge(problem_home,language,problem_out_dir,fname)
22 def call_judge(problem_home,language,problem_out_dir,fname)
23 ENV['PROBLEM_HOME'] = problem_home
23 ENV['PROBLEM_HOME'] = problem_home
24
24
25 puts problem_out_dir
25 puts problem_out_dir
26 Dir.chdir problem_out_dir
26 Dir.chdir problem_out_dir
27 cmd = "#{problem_home}/script/judge #{language} #{fname}"
27 cmd = "#{problem_home}/script/judge #{language} #{fname}"
28 # puts "CMD: #{cmd}"
28 # puts "CMD: #{cmd}"
29 system(cmd)
29 system(cmd)
30 end
30 end
31
31
32 def read_result(test_result_dir)
32 def read_result(test_result_dir)
33 cmp_msg_fname = "#{test_result_dir}/compiler_message"
33 cmp_msg_fname = "#{test_result_dir}/compiler_message"
34 cmp_file = File.open(cmp_msg_fname)
34 cmp_file = File.open(cmp_msg_fname)
35 cmp_msg = cmp_file.read
35 cmp_msg = cmp_file.read
36 cmp_file.close
36 cmp_file.close
37
37
38 result_fname = "#{test_result_dir}/result"
38 result_fname = "#{test_result_dir}/result"
39 comment_fname = "#{test_result_dir}/comment"
39 comment_fname = "#{test_result_dir}/comment"
40 if FileTest.exist?(result_fname)
40 if FileTest.exist?(result_fname)
41 result_file = File.open(result_fname)
41 result_file = File.open(result_fname)
42 result = result_file.readline.to_i
42 result = result_file.readline.to_i
43 result_file.close
43 result_file.close
44
44
45 comment_file = File.open(comment_fname)
45 comment_file = File.open(comment_fname)
46 comment = comment_file.readline.chomp
46 comment = comment_file.readline.chomp
47 comment_file.close
47 comment_file.close
48
48
49 return {:points => result,
49 return {:points => result,
50 :comment => comment,
50 :comment => comment,
51 :cmp_msg => cmp_msg}
51 :cmp_msg => cmp_msg}
52 else
52 else
53 return {:points => 0,
53 return {:points => 0,
54 :comment => 'compile error',
54 :comment => 'compile error',
55 :cmp_msg => cmp_msg}
55 :cmp_msg => cmp_msg}
56 end
56 end
57 end
57 end
58
58
59 def save_result(submission,result)
59 def save_result(submission,result)
60 problem = Problem.find(submission.problem_id)
60 problem = Problem.find(submission.problem_id)
61 submission.graded_at = Time.now
61 submission.graded_at = Time.now
62 submission.points = result[:points]
62 submission.points = result[:points]
63 if submission.points == problem.full_score
63 if submission.points == problem.full_score
64 submission.grader_comment = 'PASSED: ' + report_comment(result[:comment])
64 submission.grader_comment = 'PASSED: ' + report_comment(result[:comment])
65 else
65 else
66 submission.grader_comment = 'FAILED: ' + report_comment(result[:comment])
66 submission.grader_comment = 'FAILED: ' + report_comment(result[:comment])
67 end
67 end
68 submission.compiler_message = result[:cmp_msg]
68 submission.compiler_message = result[:cmp_msg]
69 submission.save
69 submission.save
70 end
70 end
71
71
72 def copy_script(problem_home)
72 def copy_script(problem_home)
73 script_dir = "#{problem_home}/script"
73 script_dir = "#{problem_home}/script"
74 std_script_dir = File.dirname(__FILE__) + '/std-script'
74 std_script_dir = File.dirname(__FILE__) + '/std-script'
75 scripts = Dir[std_script_dir + '/*']
75 scripts = Dir[std_script_dir + '/*']
76
76
77 copied = []
77 copied = []
78
78
79 scripts.each do |s|
79 scripts.each do |s|
80 fname = File.basename(s)
80 fname = File.basename(s)
81 if !FileTest.exist?("#{script_dir}/#{fname}")
81 if !FileTest.exist?("#{script_dir}/#{fname}")
82 copied << fname
82 copied << fname
83 system("cp #{s} #{script_dir}")
83 system("cp #{s} #{script_dir}")
84 end
84 end
85 end
85 end
86
86
87 return copied
87 return copied
88 end
88 end
89
89
90 def clear_script(log,problem_home)
90 def clear_script(log,problem_home)
91 log.each do |s|
91 log.each do |s|
92 system("rm #{problem_home}/script/#{s}")
92 system("rm #{problem_home}/script/#{s}")
93 end
93 end
94 end
94 end
95
95
96 def grade(submission_id)
96 def grade(submission_id)
97 current_dir = `pwd`.chomp
97 current_dir = `pwd`.chomp
98
98
99 sub = Submission.find(submission_id)
99 sub = Submission.find(submission_id)
100 user = sub.user
100 user = sub.user
101 problem = sub.problem
101 problem = sub.problem
102
102
103 language = sub.language.name
103 language = sub.language.name
104 lang_ext = sub.language.ext
104 lang_ext = sub.language.ext
105 # FIX THIS
105 # FIX THIS
106 talk 'some hack on language'
106 talk 'some hack on language'
107 if language == 'cpp'
107 if language == 'cpp'
108 language = 'c++'
108 language = 'c++'
109 end
109 end
110
110
111 user_dir = "#{USER_RESULT_DIR}/#{user.login}"
111 user_dir = "#{USER_RESULT_DIR}/#{user.login}"
112 Dir.mkdir(user_dir) if !FileTest.exist?(user_dir)
112 Dir.mkdir(user_dir) if !FileTest.exist?(user_dir)
113
113
114 problem_out_dir = "#{user_dir}/#{problem.name}"
114 problem_out_dir = "#{user_dir}/#{problem.name}"
115 Dir.mkdir(problem_out_dir) if !FileTest.exist?(problem_out_dir)
115 Dir.mkdir(problem_out_dir) if !FileTest.exist?(problem_out_dir)
116
116
117 problem_home = "#{PROBLEMS_DIR}/#{problem.name}"
117 problem_home = "#{PROBLEMS_DIR}/#{problem.name}"
118 source_name = "#{problem.name}.#{lang_ext}"
118 source_name = "#{problem.name}.#{lang_ext}"
119
119
120 save_source(sub,problem_out_dir,source_name)
120 save_source(sub,problem_out_dir,source_name)
121
121
122 copy_log = copy_script(problem_home)
122 copy_log = copy_script(problem_home)
123
123
124 call_judge(problem_home,language,problem_out_dir,source_name)
124 call_judge(problem_home,language,problem_out_dir,source_name)
125 save_result(sub,read_result("#{problem_out_dir}/test-result"))
125 save_result(sub,read_result("#{problem_out_dir}/test-result"))
126
126
127 clear_script(copy_log,problem_home)
127 clear_script(copy_log,problem_home)
128
128
129 Dir.chdir(current_dir)
129 Dir.chdir(current_dir)
130 end
130 end
131
131
132 def stop_grader
132 def stop_grader
133 File.open(File.dirname(__FILE__) + '/stop','w').close
133 File.open(File.dirname(__FILE__) + '/stop','w').close
134 end
134 end
135
135
136 def check_stopfile
136 def check_stopfile
137 FileTest.exist?(File.dirname(__FILE__) + '/stop')
137 FileTest.exist?(File.dirname(__FILE__) + '/stop')
138 end
138 end
139
139
140 def clear_stopfile
140 def clear_stopfile
141 system("rm " + File.dirname(__FILE__) + '/stop')
141 system("rm " + File.dirname(__FILE__) + '/stop')
142 end
142 end
143
143
144 #########################################
144 #########################################
145 # main program
145 # main program
146 #########################################
146 #########################################
147
147
148 # reading environment and options
148 # reading environment and options
149 if (ARGV.length >= 1) and (ARGV[0]=='stop')
149 if (ARGV.length >= 1) and (ARGV[0]=='stop')
150 stop_grader
150 stop_grader
151 puts "stop file created"
151 puts "stop file created"
152 exit(0)
152 exit(0)
153 end
153 end
154
154
155 if check_stopfile
155 if check_stopfile
156 puts "stop file exists"
156 puts "stop file exists"
157 clear_stopfile
157 clear_stopfile
158 exit(0)
158 exit(0)
159 end
159 end
160
160
161 grader_mode = 'queue'
161 grader_mode = 'queue'
162 if ARGV.length >= 1
162 if ARGV.length >= 1
163 GRADER_ENV = ARGV[0]
163 GRADER_ENV = ARGV[0]
164 if ARGV.length >=2
164 if ARGV.length >=2
165 grader_mode = ARGV[1]
165 grader_mode = ARGV[1]
166 end
166 end
167 else
167 else
168 GRADER_ENV = 'exam'
168 GRADER_ENV = 'exam'
169 end
169 end
170
170
171 puts "environment: #{GRADER_ENV}"
171 puts "environment: #{GRADER_ENV}"
172 require File.dirname(__FILE__) + "/environment.rb"
172 require File.dirname(__FILE__) + "/environment.rb"
173
173
174 #main program
174 #main program
175 talk 'Reading rails environment'
175 talk 'Reading rails environment'
176
176
177 RAILS_ENV = 'development'
177 RAILS_ENV = 'development'
178 require RAILS_APP_DIR + '/config/environment'
178 require RAILS_APP_DIR + '/config/environment'
179
179
180 case grader_mode
180 case grader_mode
181 when "queue"
181 when "queue"
182 talk 'Grader queue'
182 talk 'Grader queue'
183 while true
183 while true
184 if check_stopfile # created by calling grader stop
184 if check_stopfile # created by calling grader stop
185 clear_stopfile
185 clear_stopfile
186 puts "stopped"
186 puts "stopped"
187 exit(0)
187 exit(0)
188 end
188 end
189
189
190 task = Task.find(:first, :order => 'created_at')
190 task = Task.find(:first, :order => 'created_at')
191 if task!=nil
191 if task!=nil
192 grade(task.submission_id)
192 grade(task.submission_id)
193 task.destroy
193 task.destroy
194 else
194 else
195 sleep(1)
195 sleep(1)
196 end
196 end
197 end
197 end
198
198
199 when "prob"
199 when "prob"
200 prob = Problem.find_by_name(ARGV[2])
200 prob = Problem.find_by_name(ARGV[2])
201 if prob==nil
201 if prob==nil
202 puts "cannot find problem: #{ARGV[2]}"
202 puts "cannot find problem: #{ARGV[2]}"
203 exit(0)
203 exit(0)
204 end
204 end
205 users = User.find(:all)
205 users = User.find(:all)
206 users.each do |u|
206 users.each do |u|
207 puts "user: #{u.login}"
207 puts "user: #{u.login}"
208 last_sub = Submission.find(:first,
208 last_sub = Submission.find(:first,
209 :conditions => "user_id = #{u.id} and " +
209 :conditions => "user_id = #{u.id} and " +
210 - "problem_id = #{prob.id}",
210 + "problem_id = #{prob.id}",
211 :order => 'submitted_at DESC')
211 :order => 'submitted_at DESC')
212 if last_sub!=nil
212 if last_sub!=nil
213 grade(last_sub.id)
213 grade(last_sub.id)
214 end
214 end
215 end
215 end
216 end
216 end
217
217
218
218
219
219
220
220
221
221
You need to be logged in to leave comments. Login now