Description:
MERGED change set (407:408) from web/judge in branch combined-with-judge (added import_problem_new)
git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@409 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
r72:af356efa3b31 - - 2 files changed: 227 inserted, 4 deleted
@@ -0,0 +1,223 | |||||
|
|
1 | + #!/usr/bin/ruby | ||
|
|
2 | + | ||
|
|
3 | + require 'erb' | ||
|
|
4 | + require 'fileutils' | ||
|
|
5 | + require File.join(File.dirname(__FILE__),'lib/import_helper') | ||
|
|
6 | + | ||
|
|
7 | + JUDGE_ENVIRONMENTS = [:grading, :exam] | ||
|
|
8 | + ENV_INFO = { | ||
|
|
9 | + :grading => { | ||
|
|
10 | + :ev_dir => 'ev', | ||
|
|
11 | + :raw_prefix => '', | ||
|
|
12 | + }, | ||
|
|
13 | + :exam => { | ||
|
|
14 | + :ev_dir => 'ev-exam', | ||
|
|
15 | + :raw_prefix => 'ex.', | ||
|
|
16 | + } | ||
|
|
17 | + } | ||
|
|
18 | + | ||
|
|
19 | + def input_filename(dir,i) | ||
|
|
20 | + "#{dir}/input-#{i}.txt" | ||
|
|
21 | + end | ||
|
|
22 | + | ||
|
|
23 | + def answer_filename(dir,i) | ||
|
|
24 | + "#{dir}/answer-#{i}.txt" | ||
|
|
25 | + end | ||
|
|
26 | + | ||
|
|
27 | + def build_testrun_info_from_dir(num_testruns, importing_test_dir, raw_prefix='') | ||
|
|
28 | + filenames = Dir["#{importing_test_dir}/#{raw_prefix}*.in"].collect do |filename| | ||
|
|
29 | + File.basename((/(.*)\.in/.match(filename))[1]) | ||
|
|
30 | + end | ||
|
|
31 | + build_testrun_info(num_testruns,filenames,raw_prefix) | ||
|
|
32 | + end | ||
|
|
33 | + | ||
|
|
34 | + def copy_testcase(importing_test_dir,fname,dir,i) | ||
|
|
35 | + system("cp #{importing_test_dir}/#{fname}.in #{input_filename(dir,i)}") | ||
|
|
36 | + system("cp #{importing_test_dir}/#{fname}.sol #{answer_filename(dir,i)}") | ||
|
|
37 | + end | ||
|
|
38 | + | ||
|
|
39 | + def process_options(options) | ||
|
|
40 | + i = 4 | ||
|
|
41 | + while i<ARGV.length | ||
|
|
42 | + if ARGV[i]=='-t' | ||
|
|
43 | + options[:time_limit] = ARGV[i+1].to_f if ARGV.length>i+1 | ||
|
|
44 | + i += 1 | ||
|
|
45 | + end | ||
|
|
46 | + if ARGV[i]=='-m' | ||
|
|
47 | + options[:mem_limit] = ARGV[i+1].to_i if ARGV.length>i+1 | ||
|
|
48 | + i += 1 | ||
|
|
49 | + end | ||
|
|
50 | + i += 1 | ||
|
|
51 | + end | ||
|
|
52 | + end | ||
|
|
53 | + | ||
|
|
54 | + def print_usage | ||
|
|
55 | + puts "using: import_problem_new name dir check [options] | ||
|
|
56 | + | ||
|
|
57 | + where: name = problem_name (put '-' (dash) to use dir name) | ||
|
|
58 | + dir = importing testcase directory | ||
|
|
59 | + check = check script, which can be | ||
|
|
60 | + 'integer', 'text' (for standard script), | ||
|
|
61 | + path_to_your_script, or | ||
|
|
62 | + 'wrapper:(path_to_your_wrapped_script)' | ||
|
|
63 | + options: -t time-limit (in seconds) | ||
|
|
64 | + -m memory-limit (in megabytes) | ||
|
|
65 | + | ||
|
|
66 | + The script looks at test data files in the dir of the forms: *.in and | ||
|
|
67 | + *.sol and import them to the evaluation dir for their environment, | ||
|
|
68 | + based on their prefixes. | ||
|
|
69 | + | ||
|
|
70 | + Currently supporting environments are:" | ||
|
|
71 | + | ||
|
|
72 | + JUDGE_ENVIRONMENTS.each do |env| | ||
|
|
73 | + prefix = ENV_INFO[env][:raw_prefix] | ||
|
|
74 | + prefix = 'no prefix' if prefix=='' | ||
|
|
75 | + puts " * #{env}" | ||
|
|
76 | + puts " import to: #{ENV_INFO[env][:ev_dir]}" | ||
|
|
77 | + puts " prefix with: #{prefix} (e.g., #{ENV_INFO[env][:raw_prefix]}1.in, #{ENV_INFO[env][:raw_prefix]}5a.sol)" | ||
|
|
78 | + end | ||
|
|
79 | + | ||
|
|
80 | + puts" | ||
|
|
81 | + For each environment, the script | ||
|
|
82 | + * creates a directory for a problem in ev dir of that environment, | ||
|
|
83 | + * copies testdata in the old format and create standard testcase config file | ||
|
|
84 | + * copies a check script for grading | ||
|
|
85 | + * creates a test_request template in the ev dir + '/test_request' | ||
|
|
86 | + | ||
|
|
87 | + For wrapped checked script see comment in templates/check_wrapper for | ||
|
|
88 | + information." | ||
|
|
89 | + end | ||
|
|
90 | + | ||
|
|
91 | + def count_testruns(testcase_dir, raw_prefix) | ||
|
|
92 | + n = 0 | ||
|
|
93 | + begin | ||
|
|
94 | + # check for test case n+1 | ||
|
|
95 | + if ((Dir["#{testcase_dir}/#{raw_prefix}#{n+1}.in"].length==0) and | ||
|
|
96 | + (Dir["#{testcase_dir}/#{raw_prefix}#{n+1}[a-z].in"].length==0)) | ||
|
|
97 | + return n | ||
|
|
98 | + end | ||
|
|
99 | + n += 1 | ||
|
|
100 | + end while true | ||
|
|
101 | + end | ||
|
|
102 | + | ||
|
|
103 | + def import_problem(ev_dir, problem, testcase_dir, num_testruns, raw_prefix, check_script, options) | ||
|
|
104 | + testrun_info = build_testrun_info_from_dir(num_testruns, testcase_dir, raw_prefix) | ||
|
|
105 | + | ||
|
|
106 | + problem_dir = "#{ev_dir}/#{problem}" | ||
|
|
107 | + | ||
|
|
108 | + # start working | ||
|
|
109 | + puts "creating directories" | ||
|
|
110 | + | ||
|
|
111 | + system("mkdir #{problem_dir}") | ||
|
|
112 | + system("mkdir #{problem_dir}/script") | ||
|
|
113 | + system("mkdir #{problem_dir}/test_cases") | ||
|
|
114 | + | ||
|
|
115 | + puts "copying testcases" | ||
|
|
116 | + | ||
|
|
117 | + tr_num = 0 | ||
|
|
118 | + | ||
|
|
119 | + num_testcases = 0 | ||
|
|
120 | + | ||
|
|
121 | + testrun_info.each do |testrun| | ||
|
|
122 | + tr_num += 1 | ||
|
|
123 | + puts "testrun: #{tr_num}" | ||
|
|
124 | + | ||
|
|
125 | + testrun.each do |testcase_info| | ||
|
|
126 | + testcase_num, testcase_fname = testcase_info | ||
|
|
127 | + | ||
|
|
128 | + puts "copy #{testcase_fname} to #{testcase_num}" | ||
|
|
129 | + | ||
|
|
130 | + system("mkdir #{problem_dir}/test_cases/#{testcase_num}") | ||
|
|
131 | + copy_testcase("#{testcase_dir}",testcase_fname,"#{problem_dir}/test_cases/#{testcase_num}",testcase_num) | ||
|
|
132 | + | ||
|
|
133 | + num_testcases += 1 | ||
|
|
134 | + end | ||
|
|
135 | + end | ||
|
|
136 | + | ||
|
|
137 | + # generating all_tests.cfg | ||
|
|
138 | + puts "generating testcase config file" | ||
|
|
139 | + | ||
|
|
140 | + template = File.open(SCRIPT_DIR + "/templates/all_tests.cfg.erb").read | ||
|
|
141 | + all_test_cfg = ERB.new(template) | ||
|
|
142 | + | ||
|
|
143 | + cfg_file = File.open("#{problem_dir}/test_cases/all_tests.cfg","w") | ||
|
|
144 | + cfg_file.puts all_test_cfg.result binding | ||
|
|
145 | + cfg_file.close | ||
|
|
146 | + | ||
|
|
147 | + # copy check script | ||
|
|
148 | + if res = /^wrapper:(.*)$/.match(check_script) | ||
|
|
149 | + # wrapper script | ||
|
|
150 | + check_script_fname = res[1] | ||
|
|
151 | + script_name = File.basename(check_script_fname) | ||
|
|
152 | + check_wrapper_template = File.open(SCRIPT_DIR + "/templates/check_wrapper").read | ||
|
|
153 | + check_wrapper = ERB.new(check_wrapper_template) | ||
|
|
154 | + | ||
|
|
155 | + check_file = File.open("#{problem_dir}/script/check","w") | ||
|
|
156 | + check_file.puts check_wrapper.result | ||
|
|
157 | + check_file.close | ||
|
|
158 | + | ||
|
|
159 | + File.chmod(0755,"#{problem_dir}/script/check") | ||
|
|
160 | + | ||
|
|
161 | + system("cp #{check_script_fname} #{problem_dir}/script/#{script_name}") | ||
|
|
162 | + else | ||
|
|
163 | + if File.exists?(SCRIPT_DIR + "/templates/check.#{check_script}") | ||
|
|
164 | + check_script_fname = SCRIPT_DIR + "/templates/check.#{check_script}" | ||
|
|
165 | + else | ||
|
|
166 | + check_script_fname = check_script | ||
|
|
167 | + end | ||
|
|
168 | + system("cp #{check_script_fname} #{problem_dir}/script/check") | ||
|
|
169 | + end | ||
|
|
170 | + | ||
|
|
171 | + # generating test_request directory | ||
|
|
172 | + puts "generating test_request template" | ||
|
|
173 | + FileUtils.mkdir_p("#{ev_dir}/test_request/#{problem}/script") | ||
|
|
174 | + FileUtils.mkdir_p("#{ev_dir}/test_request/#{problem}/test_cases/1") | ||
|
|
175 | + | ||
|
|
176 | + template = File.open(SCRIPT_DIR + "/templates/test_request_all_tests.cfg.erb").read | ||
|
|
177 | + test_request_all_test_cfg = ERB.new(template) | ||
|
|
178 | + | ||
|
|
179 | + cfg_file = File.open("#{ev_dir}/test_request/#{problem}/test_cases/all_tests.cfg","w") | ||
|
|
180 | + cfg_file.puts test_request_all_test_cfg.result | ||
|
|
181 | + cfg_file.close | ||
|
|
182 | + | ||
|
|
183 | + system("cp #{SCRIPT_DIR}/templates/check_empty #{ev_dir}/test_request/#{problem}/script/check") | ||
|
|
184 | + system("cp #{SCRIPT_DIR}/templates/answer-1.txt #{ev_dir}/test_request/#{problem}/test_cases/1") | ||
|
|
185 | + | ||
|
|
186 | + puts "done" | ||
|
|
187 | + end | ||
|
|
188 | + | ||
|
|
189 | + | ||
|
|
190 | + SCRIPT_DIR = File.dirname(__FILE__) | ||
|
|
191 | + | ||
|
|
192 | + # print usage | ||
|
|
193 | + if (ARGV.length < 3) or (ARGV[2][0,1]=="-") | ||
|
|
194 | + print_usage | ||
|
|
195 | + exit(127) | ||
|
|
196 | + end | ||
|
|
197 | + | ||
|
|
198 | + # processing arguments | ||
|
|
199 | + problem = ARGV[0] | ||
|
|
200 | + testcase_dir = ARGV[1] | ||
|
|
201 | + problem = File.basename(testcase_dir) if problem=="-" | ||
|
|
202 | + check_script = ARGV[2] | ||
|
|
203 | + options = {:time_limit => 1, :mem_limit => 16} | ||
|
|
204 | + process_options(options) | ||
|
|
205 | + | ||
|
|
206 | + JUDGE_ENVIRONMENTS.each do |env| | ||
|
|
207 | + ev_dir = ENV_INFO[env][:ev_dir] | ||
|
|
208 | + raw_prefix = ENV_INFO[env][:raw_prefix] | ||
|
|
209 | + | ||
|
|
210 | + num_testruns = count_testruns(testcase_dir,raw_prefix) | ||
|
|
211 | + | ||
|
|
212 | + puts "" | ||
|
|
213 | + puts "*** Environment: #{env} (#{num_testruns} test runs) ***" | ||
|
|
214 | + puts "" | ||
|
|
215 | + | ||
|
|
216 | + import_problem(ev_dir, | ||
|
|
217 | + problem, | ||
|
|
218 | + testcase_dir, | ||
|
|
219 | + num_testruns, | ||
|
|
220 | + raw_prefix, | ||
|
|
221 | + check_script, | ||
|
|
222 | + options) | ||
|
|
223 | + end |
@@ -1,7 +1,7 | |||||
|
1 |
|
1 | ||
|
2 | - def filter_filename_for_testrun(testrun, filename_list) |
|
2 | + def filter_filename_for_testrun(testrun, filename_list, raw_prefix='') |
|
3 | l = [] |
|
3 | l = [] |
|
4 | - regex = Regexp.new("^(#{testrun}[a-z]*|#{testrun}-.*)$") |
|
4 | + regex = Regexp.new("^(#{Regexp.escape(raw_prefix)}#{testrun}[a-z]*|#{testrun}-.*)$") |
|
5 | filename_list.each do |filename| |
|
5 | filename_list.each do |filename| |
|
6 | if regex.match(filename) |
|
6 | if regex.match(filename) |
|
7 | l << filename |
|
7 | l << filename |
@@ -10,13 +10,13 | |||||
|
10 | l |
|
10 | l |
|
11 | end |
|
11 | end |
|
12 |
|
12 | ||
|
13 | - def build_testrun_info(num_testruns, input_filename_list) |
|
13 | + def build_testrun_info(num_testruns, input_filename_list, raw_prefix='') |
|
14 | info = [] |
|
14 | info = [] |
|
15 | num_testcases = 0 |
|
15 | num_testcases = 0 |
|
16 | num_testruns.times do |i| |
|
16 | num_testruns.times do |i| |
|
17 | r = i+1 |
|
17 | r = i+1 |
|
18 | testrun_info = [] |
|
18 | testrun_info = [] |
|
19 |
- filenames = filter_filename_for_testrun(r, |
|
19 | + filenames = filter_filename_for_testrun(r,input_filename_list,raw_prefix) |
|
20 | filenames.each do |fname| |
|
20 | filenames.each do |fname| |
|
21 | num_testcases += 1 |
|
21 | num_testcases += 1 |
|
22 | testrun_info << [num_testcases,fname] |
|
22 | testrun_info << [num_testcases,fname] |
You need to be logged in to leave comments.
Login now