Description:
git-svn-id: http://theory.cpe.ku.ac.th/grader/cli/trunk/scripts@20 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

r7:d40f897fb488 - - 3 files changed: 33 inserted, 8 deleted

@@ -1,143 +1,160
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 +
25 + puts problem_out_dir
24 Dir.chdir problem_out_dir
26 Dir.chdir problem_out_dir
25 cmd = "#{problem_home}/script/judge #{language} #{fname}"
27 cmd = "#{problem_home}/script/judge #{language} #{fname}"
26 # puts "CMD: #{cmd}"
28 # puts "CMD: #{cmd}"
27 system(cmd)
29 system(cmd)
28 end
30 end
29
31
30 def read_result(test_result_dir)
32 def read_result(test_result_dir)
31 cmp_msg_fname = "#{test_result_dir}/compiler_message"
33 cmp_msg_fname = "#{test_result_dir}/compiler_message"
32 - cmp_msg = File.open(cmp_msg_fname).read
34 + cmp_file = File.open(cmp_msg_fname)
35 + cmp_msg = cmp_file.read
36 + cmp_file.close
33
37
34 result_fname = "#{test_result_dir}/result"
38 result_fname = "#{test_result_dir}/result"
35 comment_fname = "#{test_result_dir}/comment"
39 comment_fname = "#{test_result_dir}/comment"
36 if FileTest.exist?(result_fname)
40 if FileTest.exist?(result_fname)
37 - result = File.open(result_fname).readline.to_i
41 + result_file = File.open(result_fname)
38 - comment = File.open(comment_fname).readline.chomp
42 + result = result_file.readline.to_i
43 + result_file.close
44 +
45 + comment_file = File.open(comment_fname)
46 + comment = comment_file.readline.chomp
47 + comment_file.close
48 +
39 return {:points => result,
49 return {:points => result,
40 :comment => comment,
50 :comment => comment,
41 :cmp_msg => cmp_msg}
51 :cmp_msg => cmp_msg}
42 else
52 else
43 return {:points => 0,
53 return {:points => 0,
44 :comment => 'compile error',
54 :comment => 'compile error',
45 :cmp_msg => cmp_msg}
55 :cmp_msg => cmp_msg}
46 end
56 end
47 end
57 end
48
58
49 def save_result(submission,result)
59 def save_result(submission,result)
60 + problem = Problem.find(submission.problem_id)
50 submission.graded_at = Time.now
61 submission.graded_at = Time.now
51 submission.points = result[:points]
62 submission.points = result[:points]
52 - submission.grader_comment = report_comment(result[:comment])
63 + if submission.points == problem.full_score
64 + submission.grader_comment = 'PASSED: ' + report_comment(result[:comment])
65 + else
66 + submission.grader_comment = 'FAILED: ' + report_comment(result[:comment])
67 + end
53 submission.compiler_message = result[:cmp_msg]
68 submission.compiler_message = result[:cmp_msg]
54 submission.save
69 submission.save
55 end
70 end
56
71
57 def grade(submission_id)
72 def grade(submission_id)
73 + current_dir = `pwd`.chomp
74 +
58 sub = Submission.find(submission_id)
75 sub = Submission.find(submission_id)
59 user = sub.user
76 user = sub.user
60 problem = sub.problem
77 problem = sub.problem
61
78
62 language = sub.language.name
79 language = sub.language.name
63 lang_ext = sub.language.ext
80 lang_ext = sub.language.ext
64 # FIX THIS
81 # FIX THIS
65 talk 'some hack on language'
82 talk 'some hack on language'
66 if language == 'cpp'
83 if language == 'cpp'
67 language = 'c++'
84 language = 'c++'
68 end
85 end
69
86
70 user_dir = "#{USER_RESULT_DIR}/#{user.login}"
87 user_dir = "#{USER_RESULT_DIR}/#{user.login}"
71 Dir.mkdir(user_dir) if !FileTest.exist?(user_dir)
88 Dir.mkdir(user_dir) if !FileTest.exist?(user_dir)
72
89
73 problem_out_dir = "#{user_dir}/#{problem.name}"
90 problem_out_dir = "#{user_dir}/#{problem.name}"
74 Dir.mkdir(problem_out_dir) if !FileTest.exist?(problem_out_dir)
91 Dir.mkdir(problem_out_dir) if !FileTest.exist?(problem_out_dir)
75
92
76 problem_home = "#{PROBLEMS_DIR}/#{problem.name}"
93 problem_home = "#{PROBLEMS_DIR}/#{problem.name}"
77 source_name = "#{problem.name}.#{lang_ext}"
94 source_name = "#{problem.name}.#{lang_ext}"
78
95
79 save_source(sub,problem_out_dir,source_name)
96 save_source(sub,problem_out_dir,source_name)
80 call_judge(problem_home,language,problem_out_dir,source_name)
97 call_judge(problem_home,language,problem_out_dir,source_name)
81 save_result(sub,read_result("#{problem_out_dir}/test-result"))
98 save_result(sub,read_result("#{problem_out_dir}/test-result"))
99 +
100 + Dir.chdir(current_dir)
82 end
101 end
83
102
84 def stop_grader
103 def stop_grader
85 - File.open(File.dirname(__FILE__) + '/stop','w')
104 + File.open(File.dirname(__FILE__) + '/stop','w').close
86 end
105 end
87
106
88 def check_stopfile
107 def check_stopfile
89 FileTest.exist?(File.dirname(__FILE__) + '/stop')
108 FileTest.exist?(File.dirname(__FILE__) + '/stop')
90 end
109 end
91
110
92 def clear_stopfile
111 def clear_stopfile
93 system("rm " + File.dirname(__FILE__) + '/stop')
112 system("rm " + File.dirname(__FILE__) + '/stop')
94 end
113 end
95
114
96 # reading environment and options
115 # reading environment and options
97 if (ARGV.length >= 1) and (ARGV[0]=='stop')
116 if (ARGV.length >= 1) and (ARGV[0]=='stop')
98 stop_grader
117 stop_grader
99 puts "stop file created"
118 puts "stop file created"
100 exit(0)
119 exit(0)
101 end
120 end
102
121
103 if check_stopfile
122 if check_stopfile
104 puts "stop file exists"
123 puts "stop file exists"
105 clear_stopfile
124 clear_stopfile
106 exit(0)
125 exit(0)
107 end
126 end
108
127
109 if ARGV.length >= 1
128 if ARGV.length >= 1
110 GRADER_ENV = ARGV[0]
129 GRADER_ENV = ARGV[0]
111 else
130 else
112 GRADER_ENV = 'exam'
131 GRADER_ENV = 'exam'
113 end
132 end
114
133
115 puts "environment: #{GRADER_ENV}"
134 puts "environment: #{GRADER_ENV}"
116 require File.dirname(__FILE__) + "/environment.rb"
135 require File.dirname(__FILE__) + "/environment.rb"
117
136
118 #main program
137 #main program
119 talk 'Reading rails environment'
138 talk 'Reading rails environment'
120
139
121 RAILS_ENV = 'development'
140 RAILS_ENV = 'development'
122 require RAILS_APP_DIR + '/config/environment'
141 require RAILS_APP_DIR + '/config/environment'
123
142
124 - current_dir = `pwd`
125 -
126 talk 'Grader queue'
143 talk 'Grader queue'
127 while true
144 while true
128 if check_stopfile # created by calling grader stop
145 if check_stopfile # created by calling grader stop
129 clear_stopfile
146 clear_stopfile
130 puts "stopped"
147 puts "stopped"
131 exit(0)
148 exit(0)
132 end
149 end
133
150
134 task = Task.find(:first, :order => 'created_at')
151 task = Task.find(:first, :order => 'created_at')
135 if task!=nil
152 if task!=nil
136 grade(task.submission_id)
153 grade(task.submission_id)
137 task.destroy
154 task.destroy
138 else
155 else
139 sleep(1)
156 sleep(1)
140 end
157 end
141 end
158 end
142
159
143
160
@@ -17,49 +17,53
17 load "#{problem_home}/test_cases/all_tests.cfg"
17 load "#{problem_home}/test_cases/all_tests.cfg"
18 problem = Problem.get_instance
18 problem = Problem.get_instance
19
19
20 if problem.well_formed? == false
20 if problem.well_formed? == false
21 puts "The problem specification is not well formed."
21 puts "The problem specification is not well formed."
22 exit(127)
22 exit(127)
23 end
23 end
24
24
25 all_score = 0
25 all_score = 0
26 all_comment = ''
26 all_comment = ''
27 (1..(problem.runs.length-1)).each do |k|
27 (1..(problem.runs.length-1)).each do |k|
28 run = problem.runs[k]
28 run = problem.runs[k]
29 run_score = 0
29 run_score = 0
30 run_comment = ''
30 run_comment = ''
31 run.tests.each do |test_num|
31 run.tests.each do |test_num|
32 result_file_name = "#{test_num}/result"
32 result_file_name = "#{test_num}/result"
33 if not File.exists?(result_file_name)
33 if not File.exists?(result_file_name)
34 puts "Cannot find the file #{test_num}/result!"
34 puts "Cannot find the file #{test_num}/result!"
35 exit(127)
35 exit(127)
36 end
36 end
37
37
38 result_file = File.new(result_file_name, "r")
38 result_file = File.new(result_file_name, "r")
39 result_file_lines = result_file.readlines
39 result_file_lines = result_file.readlines
40 run_score = run_score + result_file_lines[1].to_i
40 run_score = run_score + result_file_lines[1].to_i
41 - run_comment += char_comment(result_file_lines[0])
41 + # run_comment += char_comment(result_file_lines[0])
42 + result_file_lines.each do |l|
43 + run_comment += l
44 + end
45 + run_comment += "----\n"
42 result_file.close
46 result_file.close
43 end
47 end
44
48
45 run_result_file = File.new("result-#{k}", "w")
49 run_result_file = File.new("result-#{k}", "w")
46 run_result_file.write run_score
50 run_result_file.write run_score
47 run_result_file.write "\n"
51 run_result_file.write "\n"
48 run_result_file.close
52 run_result_file.close
49
53
50 run_comment_file = File.new("comment-#{k}", "w")
54 run_comment_file = File.new("comment-#{k}", "w")
51 run_comment_file.write "#{run_comment}\n"
55 run_comment_file.write "#{run_comment}\n"
52 run_comment_file.close
56 run_comment_file.close
53
57
54 all_score = all_score + run_score
58 all_score = all_score + run_score
55 all_comment += run_comment
59 all_comment += run_comment
56 end
60 end
57
61
58 result_file = File.new("result", "w")
62 result_file = File.new("result", "w")
59 result_file.write all_score
63 result_file.write all_score
60 result_file.write "\n"
64 result_file.write "\n"
61 result_file.close
65 result_file.close
62
66
63 comment_file = File.new("comment", "w")
67 comment_file = File.new("comment", "w")
64 comment_file.write "#{all_comment}\n"
68 comment_file.write "#{all_comment}\n"
65 comment_file.close
69 comment_file.close
@@ -51,48 +51,52
51 system(run_command)
51 system(run_command)
52
52
53 # Create the result file.
53 # Create the result file.
54 result_file = File.new("result", "w")
54 result_file = File.new("result", "w")
55 comment_file = File.new("comment", "w")
55 comment_file = File.new("comment", "w")
56
56
57 # Check if the program actually produced any output.
57 # Check if the program actually produced any output.
58 run_result_file = File.new("run_result", "r")
58 run_result_file = File.new("run_result", "r")
59 run_result = run_result_file.readlines
59 run_result = run_result_file.readlines
60 run_result_file.close
60 run_result_file.close
61 time_elapsed = run_result[run_result.length-1]
61 time_elapsed = run_result[run_result.length-1]
62
62
63 report = lambda{ |status, points, comment|
63 report = lambda{ |status, points, comment|
64 result_file.write status.strip
64 result_file.write status.strip
65 result_file.write "\n"
65 result_file.write "\n"
66 result_file.write points.to_s.strip
66 result_file.write points.to_s.strip
67 result_file.write "\n"
67 result_file.write "\n"
68 result_file.write time_elapsed.strip
68 result_file.write time_elapsed.strip
69 result_file.write "\n"
69 result_file.write "\n"
70 result_file.close
70 result_file.close
71 `rm run_result`
71 `rm run_result`
72 `rm output.txt`
72 `rm output.txt`
73
73
74 comment_file.write comment
74 comment_file.write comment
75 + comment_file.write "--run-result--\n"
76 + run_result.each do |l|
77 + comment_file.write l
78 + end
75 comment_file.close
79 comment_file.close
76
80
77 puts
81 puts
78 puts "Done!"
82 puts "Done!"
79 exit(0)
83 exit(0)
80 }
84 }
81
85
82 if run_result[0][0,2] != "OK"
86 if run_result[0][0,2] != "OK"
83 puts "There was a runtime error."
87 puts "There was a runtime error."
84 report.call(run_result[0], 0, "No comment.\n")
88 report.call(run_result[0], 0, "No comment.\n")
85 end
89 end
86
90
87 # Run 'check' to evaluate the output.
91 # Run 'check' to evaluate the output.
88 #puts "There was no runtime error. Proceed to checking the output."
92 #puts "There was no runtime error. Proceed to checking the output."
89 check_command = "#{problem_home}/script/check #{language} #{test_num}"
93 check_command = "#{problem_home}/script/check #{language} #{test_num}"
90 puts "Checking the output..."
94 puts "Checking the output..."
91 puts check_command
95 puts check_command
92 if not system(check_command)
96 if not system(check_command)
93 exit(127)
97 exit(127)
94 end
98 end
95
99
96 check_file = File.new("check_result", "r")
100 check_file = File.new("check_result", "r")
97 check_file_lines = check_file.readlines
101 check_file_lines = check_file.readlines
98
102
You need to be logged in to leave comments. Login now