Description:
fix reimport bug when creating existing dirs
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r100:384e1befe70e - - 1 file changed: 16 inserted, 11 deleted
@@ -1,61 +1,60 | |||||
|
1 | #!/usr/bin/ruby |
|
1 | #!/usr/bin/ruby |
|
2 |
|
2 | ||
|
3 | require 'erb' |
|
3 | require 'erb' |
|
4 | - require 'ftools' |
|
||
|
5 | require 'fileutils' |
|
4 | require 'fileutils' |
|
6 | require File.join(File.dirname(__FILE__),'lib/import_helper') |
|
5 | require File.join(File.dirname(__FILE__),'lib/import_helper') |
|
7 |
|
6 | ||
|
8 | JUDGE_ENVIRONMENTS = [:grading, :exam] |
|
7 | JUDGE_ENVIRONMENTS = [:grading, :exam] |
|
9 | ENV_INFO = { |
|
8 | ENV_INFO = { |
|
10 | :grading => { |
|
9 | :grading => { |
|
11 | :ev_dir => 'ev', |
|
10 | :ev_dir => 'ev', |
|
12 | :raw_prefix => '', |
|
11 | :raw_prefix => '', |
|
13 | }, |
|
12 | }, |
|
14 | :exam => { |
|
13 | :exam => { |
|
15 | :ev_dir => 'ev-exam', |
|
14 | :ev_dir => 'ev-exam', |
|
16 | :raw_prefix => 'ex.', |
|
15 | :raw_prefix => 'ex.', |
|
17 | } |
|
16 | } |
|
18 | } |
|
17 | } |
|
19 |
|
18 | ||
|
20 | def input_filename(dir,i) |
|
19 | def input_filename(dir,i) |
|
21 | "#{dir}/input-#{i}.txt" |
|
20 | "#{dir}/input-#{i}.txt" |
|
22 | end |
|
21 | end |
|
23 |
|
22 | ||
|
24 | def answer_filename(dir,i) |
|
23 | def answer_filename(dir,i) |
|
25 | "#{dir}/answer-#{i}.txt" |
|
24 | "#{dir}/answer-#{i}.txt" |
|
26 | end |
|
25 | end |
|
27 |
|
26 | ||
|
28 | 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='') |
|
29 | filenames = Dir["#{importing_test_dir}/#{raw_prefix}*.in"].collect do |filename| |
|
28 | filenames = Dir["#{importing_test_dir}/#{raw_prefix}*.in"].collect do |filename| |
|
30 | File.basename((/(.*)\.in/.match(filename))[1]) |
|
29 | File.basename((/(.*)\.in/.match(filename))[1]) |
|
31 | end |
|
30 | end |
|
32 | build_testrun_info(num_testruns,filenames,raw_prefix) |
|
31 | build_testrun_info(num_testruns,filenames,raw_prefix) |
|
33 | end |
|
32 | end |
|
34 |
|
33 | ||
|
35 | def copy_testcase(importing_test_dir,fname,dir,i) |
|
34 | def copy_testcase(importing_test_dir,fname,dir,i) |
|
36 |
- File.c |
|
35 | + FileUtils.cp("#{importing_test_dir}/#{fname}.in", "#{input_filename(dir,i)}") |
|
37 |
- File.c |
|
36 | + FileUtils.cp("#{importing_test_dir}/#{fname}.sol", "#{answer_filename(dir,i)}") |
|
38 | end |
|
37 | end |
|
39 |
|
38 | ||
|
40 | def process_options(options) |
|
39 | def process_options(options) |
|
41 | i = 3 |
|
40 | i = 3 |
|
42 | while i<ARGV.length |
|
41 | while i<ARGV.length |
|
43 | if ARGV[i]=='-t' |
|
42 | if ARGV[i]=='-t' |
|
44 | 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 |
|
45 | i += 1 |
|
44 | i += 1 |
|
46 | end |
|
45 | end |
|
47 | if ARGV[i]=='-m' |
|
46 | if ARGV[i]=='-m' |
|
48 | 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 |
|
49 | i += 1 |
|
48 | i += 1 |
|
50 | end |
|
49 | end |
|
51 | i += 1 |
|
50 | i += 1 |
|
52 | end |
|
51 | end |
|
53 | end |
|
52 | end |
|
54 |
|
53 | ||
|
55 | def print_usage |
|
54 | def print_usage |
|
56 | puts "using: import_problem_new name dir check [options] |
|
55 | puts "using: import_problem_new name dir check [options] |
|
57 |
|
56 | ||
|
58 | where: name = problem_name (put '-' (dash) to use dir name) |
|
57 | where: name = problem_name (put '-' (dash) to use dir name) |
|
59 | dir = importing testcase directory |
|
58 | dir = importing testcase directory |
|
60 | check = check script, which can be |
|
59 | check = check script, which can be |
|
61 | 'integer', 'text' (for standard script), |
|
60 | 'integer', 'text' (for standard script), |
@@ -80,136 +79,142 | |||||
|
80 |
|
79 | ||
|
81 | puts" |
|
80 | puts" |
|
82 | For each environment, the script |
|
81 | For each environment, the script |
|
83 | * 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, |
|
84 | * 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 |
|
85 | * copies a check script for grading |
|
84 | * copies a check script for grading |
|
86 | * creates a test_request template in the ev dir + '/test_request' |
|
85 | * creates a test_request template in the ev dir + '/test_request' |
|
87 |
|
86 | ||
|
88 | For wrapped checked script see comment in templates/check_wrapper for |
|
87 | For wrapped checked script see comment in templates/check_wrapper for |
|
89 | information." |
|
88 | information." |
|
90 | end |
|
89 | end |
|
91 |
|
90 | ||
|
92 | def count_testruns(testcase_dir, raw_prefix) |
|
91 | def count_testruns(testcase_dir, raw_prefix) |
|
93 | n = 0 |
|
92 | n = 0 |
|
94 | begin |
|
93 | begin |
|
95 | # check for test case n+1 |
|
94 | # check for test case n+1 |
|
96 | 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 |
|
97 | (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)) |
|
98 | return n |
|
97 | return n |
|
99 | end |
|
98 | end |
|
100 | n += 1 |
|
99 | n += 1 |
|
101 | end while true |
|
100 | end while true |
|
102 | end |
|
101 | end |
|
103 |
|
102 | ||
|
|
103 | + def create_dir_if_not_exists(dir) | ||
|
|
104 | + if ! FileTest.exists? dir | ||
|
|
105 | + FileUtils.mkdir(dir) | ||
|
|
106 | + end | ||
|
|
107 | + end | ||
|
|
108 | + | ||
|
104 | def import_problem(ev_dir, problem, testcase_dir, num_testruns, raw_prefix, check_script, options) |
|
109 | def import_problem(ev_dir, problem, testcase_dir, num_testruns, raw_prefix, check_script, options) |
|
105 | testrun_info = build_testrun_info_from_dir(num_testruns, testcase_dir, raw_prefix) |
|
110 | testrun_info = build_testrun_info_from_dir(num_testruns, testcase_dir, raw_prefix) |
|
106 |
|
111 | ||
|
107 | if !(FileTest.exists? ev_dir) |
|
112 | if !(FileTest.exists? ev_dir) |
|
108 | puts "Testdata dir (#{ev_dir}) not found." |
|
113 | puts "Testdata dir (#{ev_dir}) not found." |
|
109 | return |
|
114 | return |
|
110 | end |
|
115 | end |
|
111 |
|
116 | ||
|
112 | problem_dir = "#{ev_dir}/#{problem}" |
|
117 | problem_dir = "#{ev_dir}/#{problem}" |
|
113 |
|
118 | ||
|
114 | # start working |
|
119 | # start working |
|
115 | puts "creating directories" |
|
120 | puts "creating directories" |
|
116 |
|
121 | ||
|
117 |
- |
|
122 | + create_dir_if_not_exists("#{problem_dir}") |
|
118 |
- |
|
123 | + create_dir_if_not_exists("#{problem_dir}/script") |
|
119 |
- |
|
124 | + create_dir_if_not_exists("#{problem_dir}/test_cases") |
|
120 |
|
125 | ||
|
121 | puts "copying testcases" |
|
126 | puts "copying testcases" |
|
122 |
|
127 | ||
|
123 | tr_num = 0 |
|
128 | tr_num = 0 |
|
124 |
|
129 | ||
|
125 | num_testcases = 0 |
|
130 | num_testcases = 0 |
|
126 |
|
131 | ||
|
127 | testrun_info.each do |testrun| |
|
132 | testrun_info.each do |testrun| |
|
128 | tr_num += 1 |
|
133 | tr_num += 1 |
|
129 | puts "testrun: #{tr_num}" |
|
134 | puts "testrun: #{tr_num}" |
|
130 |
|
135 | ||
|
131 | testrun.each do |testcase_info| |
|
136 | testrun.each do |testcase_info| |
|
132 | testcase_num, testcase_fname = testcase_info |
|
137 | testcase_num, testcase_fname = testcase_info |
|
133 |
|
138 | ||
|
134 | puts "copy #{testcase_fname} to #{testcase_num}" |
|
139 | puts "copy #{testcase_fname} to #{testcase_num}" |
|
135 |
|
140 | ||
|
136 |
- |
|
141 | + create_dir_if_not_exists("#{problem_dir}/test_cases/#{testcase_num}") |
|
137 | copy_testcase("#{testcase_dir}",testcase_fname,"#{problem_dir}/test_cases/#{testcase_num}",testcase_num) |
|
142 | copy_testcase("#{testcase_dir}",testcase_fname,"#{problem_dir}/test_cases/#{testcase_num}",testcase_num) |
|
138 |
|
143 | ||
|
139 | num_testcases += 1 |
|
144 | num_testcases += 1 |
|
140 | end |
|
145 | end |
|
141 | end |
|
146 | end |
|
142 |
|
147 | ||
|
143 | # generating all_tests.cfg |
|
148 | # generating all_tests.cfg |
|
144 | puts "generating testcase config file" |
|
149 | puts "generating testcase config file" |
|
145 |
|
150 | ||
|
146 | template = File.open(SCRIPT_DIR + "/templates/all_tests.cfg.erb").read |
|
151 | template = File.open(SCRIPT_DIR + "/templates/all_tests.cfg.erb").read |
|
147 | all_test_cfg = ERB.new(template) |
|
152 | all_test_cfg = ERB.new(template) |
|
148 |
|
153 | ||
|
149 | cfg_file = File.open("#{problem_dir}/test_cases/all_tests.cfg","w") |
|
154 | cfg_file = File.open("#{problem_dir}/test_cases/all_tests.cfg","w") |
|
150 | cfg_file.puts all_test_cfg.result binding |
|
155 | cfg_file.puts all_test_cfg.result binding |
|
151 | cfg_file.close |
|
156 | cfg_file.close |
|
152 |
|
157 | ||
|
153 | # copy check script |
|
158 | # copy check script |
|
154 | if res = /^wrapper:(.*)$/.match(check_script) |
|
159 | if res = /^wrapper:(.*)$/.match(check_script) |
|
155 | # wrapper script |
|
160 | # wrapper script |
|
156 | check_script_fname = res[1] |
|
161 | check_script_fname = res[1] |
|
157 | script_name = File.basename(check_script_fname) |
|
162 | script_name = File.basename(check_script_fname) |
|
158 | check_wrapper_template = File.open(SCRIPT_DIR + "/templates/check_wrapper").read |
|
163 | check_wrapper_template = File.open(SCRIPT_DIR + "/templates/check_wrapper").read |
|
159 | check_wrapper = ERB.new(check_wrapper_template) |
|
164 | check_wrapper = ERB.new(check_wrapper_template) |
|
160 |
|
165 | ||
|
161 | check_file = File.open("#{problem_dir}/script/check","w") |
|
166 | check_file = File.open("#{problem_dir}/script/check","w") |
|
162 | check_file.puts check_wrapper.result binding |
|
167 | check_file.puts check_wrapper.result binding |
|
163 | check_file.close |
|
168 | check_file.close |
|
164 |
|
169 | ||
|
165 | File.chmod(0755,"#{problem_dir}/script/check") |
|
170 | File.chmod(0755,"#{problem_dir}/script/check") |
|
166 |
|
171 | ||
|
167 |
- File.c |
|
172 | + FileUtils.cp("#{check_script_fname}", "#{problem_dir}/script/#{script_name}") |
|
168 | else |
|
173 | else |
|
169 | if File.exists?(SCRIPT_DIR + "/templates/check.#{check_script}") |
|
174 | if File.exists?(SCRIPT_DIR + "/templates/check.#{check_script}") |
|
170 | check_script_fname = SCRIPT_DIR + "/templates/check.#{check_script}" |
|
175 | check_script_fname = SCRIPT_DIR + "/templates/check.#{check_script}" |
|
171 | else |
|
176 | else |
|
172 | check_script_fname = check_script |
|
177 | check_script_fname = check_script |
|
173 | end |
|
178 | end |
|
174 |
- File.c |
|
179 | + FileUtils.cp("#{check_script_fname}", "#{problem_dir}/script/check") |
|
175 | end |
|
180 | end |
|
176 |
|
181 | ||
|
177 | # generating test_request directory |
|
182 | # generating test_request directory |
|
178 | puts "generating test_request template" |
|
183 | puts "generating test_request template" |
|
179 | FileUtils.mkdir_p("#{ev_dir}/test_request/#{problem}/script") |
|
184 | FileUtils.mkdir_p("#{ev_dir}/test_request/#{problem}/script") |
|
180 | FileUtils.mkdir_p("#{ev_dir}/test_request/#{problem}/test_cases/1") |
|
185 | FileUtils.mkdir_p("#{ev_dir}/test_request/#{problem}/test_cases/1") |
|
181 |
|
186 | ||
|
182 | template = File.open(SCRIPT_DIR + "/templates/test_request_all_tests.cfg.erb").read |
|
187 | template = File.open(SCRIPT_DIR + "/templates/test_request_all_tests.cfg.erb").read |
|
183 | test_request_all_test_cfg = ERB.new(template) |
|
188 | test_request_all_test_cfg = ERB.new(template) |
|
184 |
|
189 | ||
|
185 | cfg_file = File.open("#{ev_dir}/test_request/#{problem}/test_cases/all_tests.cfg","w") |
|
190 | cfg_file = File.open("#{ev_dir}/test_request/#{problem}/test_cases/all_tests.cfg","w") |
|
186 | cfg_file.puts test_request_all_test_cfg.result |
|
191 | cfg_file.puts test_request_all_test_cfg.result |
|
187 | cfg_file.close |
|
192 | cfg_file.close |
|
188 |
|
193 | ||
|
189 |
- File.c |
|
194 | + FileUtils.cp("#{SCRIPT_DIR}/templates/check_empty", |
|
190 | "#{ev_dir}/test_request/#{problem}/script/check") |
|
195 | "#{ev_dir}/test_request/#{problem}/script/check") |
|
191 |
- File.c |
|
196 | + FileUtils.cp("#{SCRIPT_DIR}/templates/answer-1.txt", |
|
192 | "#{ev_dir}/test_request/#{problem}/test_cases/1") |
|
197 | "#{ev_dir}/test_request/#{problem}/test_cases/1") |
|
193 |
|
198 | ||
|
194 | puts "done" |
|
199 | puts "done" |
|
195 | end |
|
200 | end |
|
196 |
|
201 | ||
|
197 |
|
202 | ||
|
198 | SCRIPT_DIR = File.dirname(__FILE__) |
|
203 | SCRIPT_DIR = File.dirname(__FILE__) |
|
199 |
|
204 | ||
|
200 | # print usage |
|
205 | # print usage |
|
201 | if (ARGV.length < 3) or (ARGV[2][0,1]=="-") |
|
206 | if (ARGV.length < 3) or (ARGV[2][0,1]=="-") |
|
202 | print_usage |
|
207 | print_usage |
|
203 | exit(127) |
|
208 | exit(127) |
|
204 | end |
|
209 | end |
|
205 |
|
210 | ||
|
206 | # processing arguments |
|
211 | # processing arguments |
|
207 | problem = ARGV[0] |
|
212 | problem = ARGV[0] |
|
208 | testcase_dir = ARGV[1] |
|
213 | testcase_dir = ARGV[1] |
|
209 | problem = File.basename(testcase_dir) if problem=="-" |
|
214 | problem = File.basename(testcase_dir) if problem=="-" |
|
210 | check_script = ARGV[2] |
|
215 | check_script = ARGV[2] |
|
211 | options = {:time_limit => 1, :mem_limit => 16} |
|
216 | options = {:time_limit => 1, :mem_limit => 16} |
|
212 | process_options(options) |
|
217 | process_options(options) |
|
213 |
|
218 | ||
|
214 | JUDGE_ENVIRONMENTS.each do |env| |
|
219 | JUDGE_ENVIRONMENTS.each do |env| |
|
215 | ev_dir = ENV_INFO[env][:ev_dir] |
|
220 | ev_dir = ENV_INFO[env][:ev_dir] |
You need to be logged in to leave comments.
Login now