Description:
merge
Commit status:
[Not Reviewed]
References:
merge java
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r243:b409073698a0 - - 1 file changed: 5 inserted, 2 deleted

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