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

r232:da875364b006 - - 2 files changed: 101 inserted, 16 deleted

@@ -0,0 +1,85
1 + #!/usr/bin/env ruby
2 +
3 + def config
4 + Grader::Configuration.get_instance
5 + end
6 +
7 + def display_manual
8 + puts <<USAGE
9 + load_testcases
10 + using: load_testcases [problem_name ...]
11 + problem_name are list of "short name" of the problems
12 +
13 + options:
14 + --dry-run do nothing, just simulate the run
15 + --all import all problem. This might take several minutes
16 +
17 + USAGE
18 + end
19 +
20 + def process_options_and_stop_file
21 +
22 + # Process 'help' option
23 + if (ARGV.length==1) and (/help/.match(ARGV[0]))
24 + display_manual
25 + exit(0)
26 + end
27 +
28 + #default options
29 + options = {
30 + :dry_run => false,
31 + }
32 +
33 + options[:dry_run] = (ARGV.delete('--dry') != nil)
34 + options[:all] = (ARGV.delete('--all') != nil)
35 +
36 + return options
37 + end
38 +
39 + def process_problem(prob,dry_run = false)
40 + prob.testcases.destroy_all
41 + testcases_root = File.expand_path(GRADER_ROOT+"/../ev/#{prob.name}/test_cases/")
42 + num = 1
43 + puts "Processing problem #{prob.name}"
44 + loop do
45 + file_root = testcases_root + "/#{num}/"
46 + puts " checking file #{file_root}"
47 + break unless File.exists? file_root
48 + input = File.read(file_root + "/input-#{num}.txt")
49 + answer = File.read(file_root + "/answer-#{num}.txt")
50 + puts " got test case ##{num} of size #{input.size} and #{answer.size}"
51 +
52 + #THIS IS JUST A PLACE HOLDER
53 + group = num #this is wrong!!! fix it!!
54 + score = 10
55 + #BEWARE
56 +
57 + prob.testcases.create(input: input,sol: answer, num: num, score:score,group: group) unless dry_run
58 + num += 1
59 + end
60 + end
61 +
62 + #########################################
63 + # main program
64 + #########################################
65 +
66 + options = process_options_and_stop_file
67 +
68 + # load grader environment
69 + GRADER_ENV = 'grading'
70 + require File.join(File.dirname(__FILE__),'config/environment')
71 +
72 + # boot rails, to be able to use the active record
73 + RAILS_ENV = config.rails_env
74 + require RAILS_ROOT + '/config/environment'
75 +
76 + if options[:all]
77 + Problem.all.each { |prob| process_problem(prob,options[:all]) }
78 + else
79 + ARGV.each do |name|
80 + prob = Problem.find_by(name: name)
81 + process_problem(prob,options[:dry_run]) if prob
82 + puts "Cannot find the problem #{name}" unless prob
83 + end
84 + end
85 +
@@ -39,198 +39,198
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)
104 if ! FileTest.exists? dir
104 if ! FileTest.exists? dir
105 FileUtils.mkdir(dir)
105 FileUtils.mkdir(dir)
106 end
106 end
107 end
107 end
108
108
109 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)
110 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)
111
111
112 if !(FileTest.exists? ev_dir)
112 if !(FileTest.exists? ev_dir)
113 puts "Testdata dir (#{ev_dir}) not found."
113 puts "Testdata dir (#{ev_dir}) not found."
114 return
114 return
115 end
115 end
116
116
117 problem_dir = "#{ev_dir}/#{problem}"
117 problem_dir = "#{ev_dir}/#{problem}"
118
118
119 # start working
119 # start working
120 puts "creating directories"
120 puts "creating directories"
121
121
122 create_dir_if_not_exists("#{problem_dir}")
122 create_dir_if_not_exists("#{problem_dir}")
123 create_dir_if_not_exists("#{problem_dir}/script")
123 create_dir_if_not_exists("#{problem_dir}/script")
124 create_dir_if_not_exists("#{problem_dir}/test_cases")
124 create_dir_if_not_exists("#{problem_dir}/test_cases")
125
125
126 puts "copying testcases"
126 puts "copying testcases"
127
127
128 tr_num = 0
128 tr_num = 0
129
129
130 num_testcases = 0
130 num_testcases = 0
131
131
132 testrun_info.each do |testrun|
132 testrun_info.each do |testrun|
133 tr_num += 1
133 tr_num += 1
134 puts "testrun: #{tr_num}"
134 puts "testrun: #{tr_num}"
135 -
135 +
136 testrun.each do |testcase_info|
136 testrun.each do |testcase_info|
137 testcase_num, testcase_fname = testcase_info
137 testcase_num, testcase_fname = testcase_info
138 -
138 +
139 puts "copy #{testcase_fname} to #{testcase_num}"
139 puts "copy #{testcase_fname} to #{testcase_num}"
140 -
140 +
141 create_dir_if_not_exists("#{problem_dir}/test_cases/#{testcase_num}")
141 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)
142 copy_testcase("#{testcase_dir}",testcase_fname,"#{problem_dir}/test_cases/#{testcase_num}",testcase_num)
143 -
143 +
144 num_testcases += 1
144 num_testcases += 1
145 end
145 end
146 end
146 end
147 -
147 +
148 # generating all_tests.cfg
148 # generating all_tests.cfg
149 puts "generating testcase config file"
149 puts "generating testcase config file"
150 -
150 +
151 template = File.open(SCRIPT_DIR + "/templates/all_tests.cfg.erb").read
151 template = File.open(SCRIPT_DIR + "/templates/all_tests.cfg.erb").read
152 all_test_cfg = ERB.new(template)
152 all_test_cfg = ERB.new(template)
153 -
153 +
154 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")
155 cfg_file.puts all_test_cfg.result binding
155 cfg_file.puts all_test_cfg.result binding
156 cfg_file.close
156 cfg_file.close
157 -
157 +
158 # copy check script
158 # copy check script
159 if res = /^wrapper:(.*)$/.match(check_script)
159 if res = /^wrapper:(.*)$/.match(check_script)
160 # wrapper script
160 # wrapper script
161 check_script_fname = res[1]
161 check_script_fname = res[1]
162 script_name = File.basename(check_script_fname)
162 script_name = File.basename(check_script_fname)
163 check_wrapper_template = File.open(SCRIPT_DIR + "/templates/check_wrapper").read
163 check_wrapper_template = File.open(SCRIPT_DIR + "/templates/check_wrapper").read
164 check_wrapper = ERB.new(check_wrapper_template)
164 check_wrapper = ERB.new(check_wrapper_template)
165 -
165 +
166 check_file = File.open("#{problem_dir}/script/check","w")
166 check_file = File.open("#{problem_dir}/script/check","w")
167 check_file.puts check_wrapper.result binding
167 check_file.puts check_wrapper.result binding
168 check_file.close
168 check_file.close
169 -
169 +
170 File.chmod(0755,"#{problem_dir}/script/check")
170 File.chmod(0755,"#{problem_dir}/script/check")
171 -
171 +
172 FileUtils.cp("#{check_script_fname}", "#{problem_dir}/script/#{script_name}")
172 FileUtils.cp("#{check_script_fname}", "#{problem_dir}/script/#{script_name}")
173 else
173 else
174 if File.exists?(SCRIPT_DIR + "/templates/check.#{check_script}")
174 if File.exists?(SCRIPT_DIR + "/templates/check.#{check_script}")
175 check_script_fname = SCRIPT_DIR + "/templates/check.#{check_script}"
175 check_script_fname = SCRIPT_DIR + "/templates/check.#{check_script}"
176 else
176 else
177 check_script_fname = check_script
177 check_script_fname = check_script
178 end
178 end
179 FileUtils.cp("#{check_script_fname}", "#{problem_dir}/script/check", :preserve => true)
179 FileUtils.cp("#{check_script_fname}", "#{problem_dir}/script/check", :preserve => true)
180 end
180 end
181 -
181 +
182 # generating test_request directory
182 # generating test_request directory
183 puts "generating test_request template"
183 puts "generating test_request template"
184 FileUtils.mkdir_p("#{ev_dir}/test_request/#{problem}/script")
184 FileUtils.mkdir_p("#{ev_dir}/test_request/#{problem}/script")
185 FileUtils.mkdir_p("#{ev_dir}/test_request/#{problem}/test_cases/1")
185 FileUtils.mkdir_p("#{ev_dir}/test_request/#{problem}/test_cases/1")
186 -
186 +
187 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
188 test_request_all_test_cfg = ERB.new(template)
188 test_request_all_test_cfg = ERB.new(template)
189 -
189 +
190 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")
191 cfg_file.puts test_request_all_test_cfg.result
191 cfg_file.puts test_request_all_test_cfg.result
192 cfg_file.close
192 cfg_file.close
193 -
193 +
194 FileUtils.cp("#{SCRIPT_DIR}/templates/check_empty",
194 FileUtils.cp("#{SCRIPT_DIR}/templates/check_empty",
195 "#{ev_dir}/test_request/#{problem}/script/check")
195 "#{ev_dir}/test_request/#{problem}/script/check")
196 FileUtils.cp("#{SCRIPT_DIR}/templates/answer-1.txt",
196 FileUtils.cp("#{SCRIPT_DIR}/templates/answer-1.txt",
197 "#{ev_dir}/test_request/#{problem}/test_cases/1")
197 "#{ev_dir}/test_request/#{problem}/test_cases/1")
198 -
198 +
199 puts "done"
199 puts "done"
200 end
200 end
201
201
202
202
203 SCRIPT_DIR = File.dirname(__FILE__)
203 SCRIPT_DIR = File.dirname(__FILE__)
204
204
205 # print usage
205 # print usage
206 if (ARGV.length < 3) or (ARGV[2][0,1]=="-")
206 if (ARGV.length < 3) or (ARGV[2][0,1]=="-")
207 print_usage
207 print_usage
208 exit(127)
208 exit(127)
209 end
209 end
210
210
211 # processing arguments
211 # processing arguments
212 problem = ARGV[0]
212 problem = ARGV[0]
213 testcase_dir = ARGV[1]
213 testcase_dir = ARGV[1]
214 problem = File.basename(testcase_dir) if problem=="-"
214 problem = File.basename(testcase_dir) if problem=="-"
215 check_script = ARGV[2]
215 check_script = ARGV[2]
216 options = {:time_limit => 1, :mem_limit => 16}
216 options = {:time_limit => 1, :mem_limit => 16}
217 process_options(options)
217 process_options(options)
218
218
219 JUDGE_ENVIRONMENTS.each do |env|
219 JUDGE_ENVIRONMENTS.each do |env|
220 ev_dir = ENV_INFO[env][:ev_dir]
220 ev_dir = ENV_INFO[env][:ev_dir]
221 raw_prefix = ENV_INFO[env][:raw_prefix]
221 raw_prefix = ENV_INFO[env][:raw_prefix]
222
222
223 num_testruns = count_testruns(testcase_dir,raw_prefix)
223 num_testruns = count_testruns(testcase_dir,raw_prefix)
224
224
225 puts ""
225 puts ""
226 puts "*** Environment: #{env} (#{num_testruns} test runs) ***"
226 puts "*** Environment: #{env} (#{num_testruns} test runs) ***"
227 puts ""
227 puts ""
228
228
229 import_problem(ev_dir,
229 import_problem(ev_dir,
230 problem,
230 problem,
231 testcase_dir,
231 testcase_dir,
232 num_testruns,
232 num_testruns,
233 raw_prefix,
233 raw_prefix,
234 check_script,
234 check_script,
235 options)
235 options)
236 end
236 end
You need to be logged in to leave comments. Login now