Description:
add warning for check
git-svn-id: http://theory.cpe.ku.ac.th/grader/cli/trunk/scripts@22 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r9:8595e5379ca6 - - 2 files changed: 44 inserted, 12 deleted
@@ -1,160 +1,188 | |||
|
1 | 1 | #!/usr/bin/ruby |
|
2 | 2 | |
|
3 | 3 | def talk(str) |
|
4 | 4 | if TALKATIVE |
|
5 | 5 | puts str |
|
6 | 6 | end |
|
7 | 7 | end |
|
8 | 8 | |
|
9 | 9 | def execute(command, error_message="") |
|
10 | 10 | if not system(command) |
|
11 | 11 | puts "ERROR: #{error_message}" |
|
12 | 12 | exit(127) |
|
13 | 13 | end |
|
14 | 14 | end |
|
15 | 15 | |
|
16 | 16 | def save_source(submission,dir,fname) |
|
17 | 17 | f = File.open("#{dir}/#{fname}","w") |
|
18 | 18 | f.write(submission.source) |
|
19 | 19 | f.close |
|
20 | 20 | end |
|
21 | 21 | |
|
22 | 22 | def call_judge(problem_home,language,problem_out_dir,fname) |
|
23 | 23 | ENV['PROBLEM_HOME'] = problem_home |
|
24 | 24 | |
|
25 | 25 | puts problem_out_dir |
|
26 | 26 | Dir.chdir problem_out_dir |
|
27 | 27 | cmd = "#{problem_home}/script/judge #{language} #{fname}" |
|
28 | 28 | # puts "CMD: #{cmd}" |
|
29 | 29 | system(cmd) |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | def read_result(test_result_dir) |
|
33 | 33 | cmp_msg_fname = "#{test_result_dir}/compiler_message" |
|
34 | 34 | cmp_file = File.open(cmp_msg_fname) |
|
35 | 35 | cmp_msg = cmp_file.read |
|
36 | 36 | cmp_file.close |
|
37 | 37 | |
|
38 | 38 | result_fname = "#{test_result_dir}/result" |
|
39 | 39 | comment_fname = "#{test_result_dir}/comment" |
|
40 | 40 | if FileTest.exist?(result_fname) |
|
41 | 41 | result_file = File.open(result_fname) |
|
42 | 42 | result = result_file.readline.to_i |
|
43 | 43 | result_file.close |
|
44 | 44 | |
|
45 | 45 | comment_file = File.open(comment_fname) |
|
46 | 46 | comment = comment_file.readline.chomp |
|
47 | 47 | comment_file.close |
|
48 | 48 | |
|
49 | 49 | return {:points => result, |
|
50 | 50 | :comment => comment, |
|
51 | 51 | :cmp_msg => cmp_msg} |
|
52 | 52 | else |
|
53 | 53 | return {:points => 0, |
|
54 | 54 | :comment => 'compile error', |
|
55 | 55 | :cmp_msg => cmp_msg} |
|
56 | 56 | end |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | def save_result(submission,result) |
|
60 | 60 | problem = Problem.find(submission.problem_id) |
|
61 | 61 | submission.graded_at = Time.now |
|
62 | 62 | submission.points = result[:points] |
|
63 | 63 | if submission.points == problem.full_score |
|
64 | 64 | submission.grader_comment = 'PASSED: ' + report_comment(result[:comment]) |
|
65 | 65 | else |
|
66 | 66 | submission.grader_comment = 'FAILED: ' + report_comment(result[:comment]) |
|
67 | 67 | end |
|
68 | 68 | submission.compiler_message = result[:cmp_msg] |
|
69 | 69 | submission.save |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | def grade(submission_id) |
|
73 | 73 | current_dir = `pwd`.chomp |
|
74 | 74 | |
|
75 | 75 | sub = Submission.find(submission_id) |
|
76 | 76 | user = sub.user |
|
77 | 77 | problem = sub.problem |
|
78 | 78 | |
|
79 | 79 | language = sub.language.name |
|
80 | 80 | lang_ext = sub.language.ext |
|
81 | 81 | # FIX THIS |
|
82 | 82 | talk 'some hack on language' |
|
83 | 83 | if language == 'cpp' |
|
84 | 84 | language = 'c++' |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | user_dir = "#{USER_RESULT_DIR}/#{user.login}" |
|
88 | 88 | Dir.mkdir(user_dir) if !FileTest.exist?(user_dir) |
|
89 | 89 | |
|
90 | 90 | problem_out_dir = "#{user_dir}/#{problem.name}" |
|
91 | 91 | Dir.mkdir(problem_out_dir) if !FileTest.exist?(problem_out_dir) |
|
92 | 92 | |
|
93 | 93 | problem_home = "#{PROBLEMS_DIR}/#{problem.name}" |
|
94 | 94 | source_name = "#{problem.name}.#{lang_ext}" |
|
95 | 95 | |
|
96 | 96 | save_source(sub,problem_out_dir,source_name) |
|
97 | 97 | call_judge(problem_home,language,problem_out_dir,source_name) |
|
98 | 98 | save_result(sub,read_result("#{problem_out_dir}/test-result")) |
|
99 | 99 | |
|
100 | 100 | Dir.chdir(current_dir) |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def stop_grader |
|
104 | 104 | File.open(File.dirname(__FILE__) + '/stop','w').close |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | def check_stopfile |
|
108 | 108 | FileTest.exist?(File.dirname(__FILE__) + '/stop') |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | def clear_stopfile |
|
112 | 112 | system("rm " + File.dirname(__FILE__) + '/stop') |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | # reading environment and options |
|
116 | 116 | if (ARGV.length >= 1) and (ARGV[0]=='stop') |
|
117 | 117 | stop_grader |
|
118 | 118 | puts "stop file created" |
|
119 | 119 | exit(0) |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | if check_stopfile |
|
123 | 123 | puts "stop file exists" |
|
124 | 124 | clear_stopfile |
|
125 | 125 | exit(0) |
|
126 | 126 | end |
|
127 | 127 | |
|
128 | + grader_mode = 'queue' | |
|
128 | 129 | if ARGV.length >= 1 |
|
129 | 130 | GRADER_ENV = ARGV[0] |
|
131 | + if ARGV.length >=2 | |
|
132 | + grader_mode = ARGV[1] | |
|
133 | + end | |
|
130 | 134 | else |
|
131 | 135 | GRADER_ENV = 'exam' |
|
132 | 136 | end |
|
133 | 137 | |
|
134 | 138 | puts "environment: #{GRADER_ENV}" |
|
135 | 139 | require File.dirname(__FILE__) + "/environment.rb" |
|
136 | 140 | |
|
137 | 141 | #main program |
|
138 | 142 | talk 'Reading rails environment' |
|
139 | 143 | |
|
140 | 144 | RAILS_ENV = 'development' |
|
141 | 145 | require RAILS_APP_DIR + '/config/environment' |
|
142 | 146 | |
|
143 | - talk 'Grader queue' | |
|
144 | - while true | |
|
145 | - if check_stopfile # created by calling grader stop | |
|
146 | - clear_stopfile | |
|
147 | - puts "stopped" | |
|
147 | + case grader_mode | |
|
148 | + when "queue" | |
|
149 | + talk 'Grader queue' | |
|
150 | + while true | |
|
151 | + if check_stopfile # created by calling grader stop | |
|
152 | + clear_stopfile | |
|
153 | + puts "stopped" | |
|
154 | + exit(0) | |
|
155 | + end | |
|
156 | + | |
|
157 | + task = Task.find(:first, :order => 'created_at') | |
|
158 | + if task!=nil | |
|
159 | + grade(task.submission_id) | |
|
160 | + task.destroy | |
|
161 | + else | |
|
162 | + sleep(1) | |
|
163 | + end | |
|
164 | + end | |
|
165 | + | |
|
166 | + when "prob" | |
|
167 | + prob = Problem.find_by_name(ARGV[2]) | |
|
168 | + if prob==nil | |
|
169 | + puts "cannot find problem: #{ARGV[2]}" | |
|
148 | 170 | exit(0) |
|
149 | 171 | end |
|
150 | - | |
|
151 | - task = Task.find(:first, :order => 'created_at') | |
|
152 | - if task!=nil | |
|
153 | - grade(task.submission_id) | |
|
154 | - task.destroy | |
|
155 | - else | |
|
156 | - sleep(1) | |
|
172 | + users = User.find(:all) | |
|
173 | + users.each do |u| | |
|
174 | + puts "user: #{u.login}" | |
|
175 | + last_sub = Submission.find(:first, | |
|
176 | + :conditions => "user_id = #{u.id} and " + | |
|
177 | + "problem_id = #{prob.id}", | |
|
178 | + :order => 'submitted_at DESC') | |
|
179 | + if last_sub!=nil | |
|
180 | + grade(last_sub.id) | |
|
181 | + end | |
|
157 | 182 | end |
|
158 | 183 | end |
|
159 | 184 | |
|
160 | 185 | |
|
186 | + | |
|
187 | + | |
|
188 | + |
@@ -1,59 +1,63 | |||
|
1 | 1 | #!/usr/bin/ruby |
|
2 | 2 | |
|
3 | 3 | problem_home = ENV['PROBLEM_HOME'] |
|
4 | 4 | require "#{problem_home}/script/test_dsl.rb" |
|
5 | 5 | |
|
6 | 6 | if ARGV.length < 2 |
|
7 | 7 | puts "Usage: check <language> <test-number> [<output-file>]" |
|
8 | 8 | exit(0) |
|
9 | 9 | end |
|
10 | 10 | |
|
11 | 11 | language = ARGV[0] |
|
12 | 12 | test_num = ARGV[1].to_i |
|
13 | 13 | if ARGV.length >= 3 |
|
14 | 14 | output_file_name = ARGV[2] |
|
15 | 15 | else |
|
16 | 16 | output_file_name = "output.txt" |
|
17 | 17 | end |
|
18 | 18 | |
|
19 | 19 | load "#{problem_home}/test_cases/all_tests.cfg" |
|
20 | 20 | problem = Problem.get_instance |
|
21 | 21 | |
|
22 | 22 | output_file = File.new(output_file_name, "r") |
|
23 | 23 | answer_file = File.new("#{problem_home}/test_cases/#{test_num}/answer-#{test_num}.txt") |
|
24 | 24 | result_file = File.new("check_result", "w") |
|
25 | 25 | |
|
26 | 26 | output_file_content = output_file.read |
|
27 | 27 | answer_file_content = answer_file.read |
|
28 | 28 | |
|
29 | 29 | report_correct = lambda { |
|
30 | 30 | result_file.write "Correct\n" |
|
31 | 31 | result_file.write problem.get_score(test_num) |
|
32 | 32 | result_file.write "\n" |
|
33 | 33 | result_file.close |
|
34 | 34 | exit(0) |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | report_wrong = lambda { |
|
38 | 38 | result_file.write "Incorrect\n" |
|
39 | 39 | result_file.write "0\n" |
|
40 | 40 | result_file.close |
|
41 | 41 | exit(0) |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | ################## |
|
45 | 45 | # Your code here # |
|
46 | 46 | ################## |
|
47 | + | |
|
48 | + puts "YOU HAVE TO EDIT THE CHECKING CODE HERE: #{__FILE__}" | |
|
49 | + exit(127) | |
|
50 | + | |
|
47 | 51 | num_pattern = /^[0-9]*/ |
|
48 | 52 | if (output_file_content =~ num_pattern) == nil |
|
49 | 53 | report_wrong.call |
|
50 | 54 | end |
|
51 | 55 | |
|
52 | 56 | output_i = output_file_content.to_i |
|
53 | 57 | answer_i = answer_file_content.to_i |
|
54 | 58 | |
|
55 | 59 | if output_i == answer_i |
|
56 | 60 | report_correct.call |
|
57 | 61 | else |
|
58 | 62 | report_wrong.call |
|
59 | 63 | end |
You need to be logged in to leave comments.
Login now