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