Description:
merge to java
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r404:e5811929f1d4 - - 1 file changed: 7 inserted, 2 deleted
@@ -1,185 +1,190 | |||||
|
1 | require 'tmpdir' |
|
1 | require 'tmpdir' |
|
2 |
|
2 | ||
|
3 | class TestdataImporter |
|
3 | class TestdataImporter |
|
4 |
|
4 | ||
|
5 | attr :log_msg |
|
5 | attr :log_msg |
|
6 |
|
6 | ||
|
7 | def initialize(problem) |
|
7 | def initialize(problem) |
|
8 | @problem = problem |
|
8 | @problem = problem |
|
9 | end |
|
9 | end |
|
10 |
|
10 | ||
|
11 | def import_from_file(tempfile, |
|
11 | def import_from_file(tempfile, |
|
12 | time_limit, |
|
12 | time_limit, |
|
13 | memory_limit, |
|
13 | memory_limit, |
|
14 | checker_name='text', |
|
14 | checker_name='text', |
|
15 | import_to_db=false) |
|
15 | import_to_db=false) |
|
16 |
|
16 | ||
|
17 | dirname = extract(tempfile) |
|
17 | dirname = extract(tempfile) |
|
18 | return false if not dirname |
|
18 | return false if not dirname |
|
19 | if not import_to_db |
|
19 | if not import_to_db |
|
20 | @log_msg = GraderScript.call_import_problem(@problem.name, |
|
20 | @log_msg = GraderScript.call_import_problem(@problem.name, |
|
21 | dirname, |
|
21 | dirname, |
|
22 | time_limit, |
|
22 | time_limit, |
|
23 | memory_limit, |
|
23 | memory_limit, |
|
24 | checker_name) |
|
24 | checker_name) |
|
25 | else |
|
25 | else |
|
26 | # Import test data to test pairs. |
|
26 | # Import test data to test pairs. |
|
27 |
|
27 | ||
|
28 | @problem.test_pairs.clear |
|
28 | @problem.test_pairs.clear |
|
29 | if import_test_pairs(dirname) |
|
29 | if import_test_pairs(dirname) |
|
30 | test_pair_count = TestPair.count :conditions => "problem_id = #{@problem.id}" |
|
30 | test_pair_count = TestPair.count :conditions => "problem_id = #{@problem.id}" |
|
31 | @log_msg = "Importing test pair successful. (#{test_pair_count} test pairs imported)" |
|
31 | @log_msg = "Importing test pair successful. (#{test_pair_count} test pairs imported)" |
|
32 | else |
|
32 | else |
|
33 | @log_msg = "Importing test pair failed. (0 test pairs imported)" |
|
33 | @log_msg = "Importing test pair failed. (0 test pairs imported)" |
|
34 | end |
|
34 | end |
|
35 | end |
|
35 | end |
|
36 |
|
36 | ||
|
37 | @log_msg << import_problem_description(dirname) |
|
37 | @log_msg << import_problem_description(dirname) |
|
38 | @log_msg << import_problem_pdf(dirname) |
|
38 | @log_msg << import_problem_pdf(dirname) |
|
39 | @log_msg << import_full_score(dirname) |
|
39 | @log_msg << import_full_score(dirname) |
|
40 |
|
40 | ||
|
41 | return true |
|
41 | return true |
|
42 | end |
|
42 | end |
|
43 |
|
43 | ||
|
44 | protected |
|
44 | protected |
|
45 |
|
45 | ||
|
46 | def self.long_ext(filename) |
|
46 | def self.long_ext(filename) |
|
47 | i = filename.index('.') |
|
47 | i = filename.index('.') |
|
48 | len = filename.length |
|
48 | len = filename.length |
|
49 | return filename.slice(i..len) |
|
49 | return filename.slice(i..len) |
|
50 | end |
|
50 | end |
|
51 |
|
51 | ||
|
52 | def extract(tempfile) |
|
52 | def extract(tempfile) |
|
53 | testdata_filename = save_testdata_file(tempfile) |
|
53 | testdata_filename = save_testdata_file(tempfile) |
|
54 | ext = TestdataImporter.long_ext(tempfile.original_filename) |
|
54 | ext = TestdataImporter.long_ext(tempfile.original_filename) |
|
55 |
|
55 | ||
|
56 | extract_dir = File.join(GraderScript.raw_dir, @problem.name) |
|
56 | extract_dir = File.join(GraderScript.raw_dir, @problem.name) |
|
57 | if File.exists? extract_dir |
|
57 | if File.exists? extract_dir |
|
58 | backup_count = 0 |
|
58 | backup_count = 0 |
|
59 | begin |
|
59 | begin |
|
60 | backup_count += 1 |
|
60 | backup_count += 1 |
|
61 | backup_dirname = "#{extract_dir}.backup.#{backup_count}" |
|
61 | backup_dirname = "#{extract_dir}.backup.#{backup_count}" |
|
62 | end while File.exists? backup_dirname |
|
62 | end while File.exists? backup_dirname |
|
63 | File.rename(extract_dir, backup_dirname) |
|
63 | File.rename(extract_dir, backup_dirname) |
|
64 | end |
|
64 | end |
|
65 | Dir.mkdir extract_dir |
|
65 | Dir.mkdir extract_dir |
|
66 |
|
66 | ||
|
67 | if ext=='.tar.gz' or ext=='.tgz' |
|
67 | if ext=='.tar.gz' or ext=='.tgz' |
|
68 | cmd = "tar -zxvf #{testdata_filename} -C #{extract_dir}" |
|
68 | cmd = "tar -zxvf #{testdata_filename} -C #{extract_dir}" |
|
69 | elsif ext=='.tar' |
|
69 | elsif ext=='.tar' |
|
70 | cmd = "tar -xvf #{testdata_filename} -C #{extract_dir}" |
|
70 | cmd = "tar -xvf #{testdata_filename} -C #{extract_dir}" |
|
71 | elsif ext=='.zip' |
|
71 | elsif ext=='.zip' |
|
72 | cmd = "unzip -o #{testdata_filename} -d #{extract_dir}" |
|
72 | cmd = "unzip -o #{testdata_filename} -d #{extract_dir}" |
|
73 | else |
|
73 | else |
|
74 | return nil |
|
74 | return nil |
|
75 | end |
|
75 | end |
|
76 |
|
76 | ||
|
77 | system(cmd) |
|
77 | system(cmd) |
|
78 |
|
78 | ||
|
79 | files = Dir["#{extract_dir}/**/*1*.in"] |
|
79 | files = Dir["#{extract_dir}/**/*1*.in"] |
|
80 | return nil if files.length==0 |
|
80 | return nil if files.length==0 |
|
81 |
|
81 | ||
|
82 | File.delete(testdata_filename) |
|
82 | File.delete(testdata_filename) |
|
83 |
|
83 | ||
|
84 | return File.dirname(files[0]) |
|
84 | return File.dirname(files[0]) |
|
85 | end |
|
85 | end |
|
86 |
|
86 | ||
|
87 | def save_testdata_file(tempfile) |
|
87 | def save_testdata_file(tempfile) |
|
88 | ext = TestdataImporter.long_ext(tempfile.original_filename) |
|
88 | ext = TestdataImporter.long_ext(tempfile.original_filename) |
|
89 | testdata_filename = File.join(Dir.tmpdir,"#{@problem.name}#{ext}") |
|
89 | testdata_filename = File.join(Dir.tmpdir,"#{@problem.name}#{ext}") |
|
90 |
|
90 | ||
|
91 | return nil if tempfile=="" |
|
91 | return nil if tempfile=="" |
|
92 |
|
92 | ||
|
93 | if tempfile.instance_of?(Tempfile) |
|
93 | if tempfile.instance_of?(Tempfile) |
|
94 | tempfile.close |
|
94 | tempfile.close |
|
95 | FileUtils.move(tempfile.path,testdata_filename) |
|
95 | FileUtils.move(tempfile.path,testdata_filename) |
|
96 | else |
|
96 | else |
|
97 | File.open(testdata_filename, "wb") do |f| |
|
97 | File.open(testdata_filename, "wb") do |f| |
|
98 | f.write(tempfile.read) |
|
98 | f.write(tempfile.read) |
|
99 | end |
|
99 | end |
|
100 | end |
|
100 | end |
|
101 |
|
101 | ||
|
102 | return testdata_filename |
|
102 | return testdata_filename |
|
103 | end |
|
103 | end |
|
104 |
|
104 | ||
|
105 | def import_test_pairs(dirname) |
|
105 | def import_test_pairs(dirname) |
|
106 | test_num = 1 |
|
106 | test_num = 1 |
|
107 | while FileTest.exists? "#{dirname}/#{test_num}.in" |
|
107 | while FileTest.exists? "#{dirname}/#{test_num}.in" |
|
108 | in_filename = "#{dirname}/#{test_num}.in" |
|
108 | in_filename = "#{dirname}/#{test_num}.in" |
|
109 | sol_filename = "#{dirname}/#{test_num}.sol" |
|
109 | sol_filename = "#{dirname}/#{test_num}.sol" |
|
110 |
|
110 | ||
|
111 | break if not FileTest.exists? sol_filename |
|
111 | break if not FileTest.exists? sol_filename |
|
112 |
|
112 | ||
|
113 | test_pair = TestPair.new(:input => open(in_filename).read, |
|
113 | test_pair = TestPair.new(:input => open(in_filename).read, |
|
114 | :solution => open(sol_filename).read, |
|
114 | :solution => open(sol_filename).read, |
|
115 | :problem => @problem) |
|
115 | :problem => @problem) |
|
116 | break if not test_pair.save |
|
116 | break if not test_pair.save |
|
117 |
|
117 | ||
|
118 | test_num += 1 |
|
118 | test_num += 1 |
|
119 | end |
|
119 | end |
|
120 | return test_num > 1 |
|
120 | return test_num > 1 |
|
121 | end |
|
121 | end |
|
122 |
|
122 | ||
|
123 | def import_problem_description(dirname) |
|
123 | def import_problem_description(dirname) |
|
124 | html_files = Dir["#{dirname}/*.html"] |
|
124 | html_files = Dir["#{dirname}/*.html"] |
|
125 | markdown_files = Dir["#{dirname}/*.md"] + Dir["#{dirname}/*.markdown"] |
|
125 | markdown_files = Dir["#{dirname}/*.md"] + Dir["#{dirname}/*.markdown"] |
|
126 | if (html_files.length != 0) or (markdown_files.length != 0) |
|
126 | if (html_files.length != 0) or (markdown_files.length != 0) |
|
127 | description = @problem.description || Description.new |
|
127 | description = @problem.description || Description.new |
|
128 |
|
128 | ||
|
129 | if html_files.length != 0 |
|
129 | if html_files.length != 0 |
|
130 | filename = html_files[0] |
|
130 | filename = html_files[0] |
|
131 | description.markdowned = false |
|
131 | description.markdowned = false |
|
132 | else |
|
132 | else |
|
133 | filename = markdown_files[0] |
|
133 | filename = markdown_files[0] |
|
134 | description.markdowned = true |
|
134 | description.markdowned = true |
|
135 | end |
|
135 | end |
|
136 |
|
136 | ||
|
137 | description.body = open(filename).read |
|
137 | description.body = open(filename).read |
|
138 | description.save |
|
138 | description.save |
|
139 | @problem.description = description |
|
139 | @problem.description = description |
|
140 | @problem.save |
|
140 | @problem.save |
|
141 | return "\nProblem description imported from #{filename}." |
|
141 | return "\nProblem description imported from #{filename}." |
|
142 | else |
|
142 | else |
|
143 | return '' |
|
143 | return '' |
|
144 | end |
|
144 | end |
|
145 | end |
|
145 | end |
|
146 |
|
146 | ||
|
147 | def import_problem_pdf(dirname) |
|
147 | def import_problem_pdf(dirname) |
|
148 | pdf_files = Dir["#{dirname}/*.pdf"] |
|
148 | pdf_files = Dir["#{dirname}/*.pdf"] |
|
149 | puts "CHECKING... #{dirname}" |
|
149 | puts "CHECKING... #{dirname}" |
|
150 | if pdf_files.length != 0 |
|
150 | if pdf_files.length != 0 |
|
151 | puts "HAS PDF FILE" |
|
151 | puts "HAS PDF FILE" |
|
152 | filename = pdf_files[0] |
|
152 | filename = pdf_files[0] |
|
153 |
|
153 | ||
|
154 | @problem.save if not @problem.id |
|
154 | @problem.save if not @problem.id |
|
155 | out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}" |
|
155 | out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}" |
|
156 | if not FileTest.exists? out_dirname |
|
156 | if not FileTest.exists? out_dirname |
|
157 | Dir.mkdir out_dirname |
|
157 | Dir.mkdir out_dirname |
|
158 | end |
|
158 | end |
|
159 |
|
159 | ||
|
160 | out_filename = "#{out_dirname}/#{@problem.name}.pdf" |
|
160 | out_filename = "#{out_dirname}/#{@problem.name}.pdf" |
|
161 |
|
161 | ||
|
162 | if FileTest.exists? out_filename |
|
162 | if FileTest.exists? out_filename |
|
163 | File.delete out_filename |
|
163 | File.delete out_filename |
|
164 | end |
|
164 | end |
|
165 |
|
165 | ||
|
166 | File.rename(filename, out_filename) |
|
166 | File.rename(filename, out_filename) |
|
167 | @problem.description_filename = "#{@problem.name}.pdf" |
|
167 | @problem.description_filename = "#{@problem.name}.pdf" |
|
168 | @problem.save |
|
168 | @problem.save |
|
169 | return "\nProblem pdf imported from #{filename}." |
|
169 | return "\nProblem pdf imported from #{filename}." |
|
170 | else |
|
170 | else |
|
171 | return "" |
|
171 | return "" |
|
172 | end |
|
172 | end |
|
173 | end |
|
173 | end |
|
174 |
|
174 | ||
|
175 | #just set the full score to the total number of test case |
|
175 | #just set the full score to the total number of test case |
|
176 | #it is not perfect but works on most normal use case |
|
176 | #it is not perfect but works on most normal use case |
|
177 | def import_full_score(dirname) |
|
177 | def import_full_score(dirname) |
|
178 | - in_file = Dir["#{dirname}/*.in"] |
|
178 | + num = 0 |
|
179 | - full_score =in_file.length * 10 |
|
179 | + loop do |
|
|
180 | + num += 1 | ||
|
|
181 | + in_file = Dir["#{dirname}/#{num}*.in"] | ||
|
|
182 | + break if in_file.length == 0 | ||
|
|
183 | + end | ||
|
|
184 | + full_score = (num - 1) * 10 | ||
|
180 | @problem.full_score = full_score |
|
185 | @problem.full_score = full_score |
|
181 | @problem.save |
|
186 | @problem.save |
|
182 | return "\nFull score is set to #{full_score}." |
|
187 | return "\nFull score is set to #{full_score}." |
|
183 | end |
|
188 | end |
|
184 |
|
189 | ||
|
185 | end |
|
190 | end |
You need to be logged in to leave comments.
Login now