Description:
let testdata importer set the full score
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r345:d3662f88d75c - - 1 file changed: 11 inserted, 0 deleted

@@ -1,172 +1,183
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 import_to_db=false)
14 import_to_db=false)
15
15
16 dirname = extract(tempfile)
16 dirname = extract(tempfile)
17 return false if not dirname
17 return false if not dirname
18 if not import_to_db
18 if not import_to_db
19 @log_msg = GraderScript.call_import_problem(@problem.name,
19 @log_msg = GraderScript.call_import_problem(@problem.name,
20 dirname,
20 dirname,
21 time_limit,
21 time_limit,
22 memory_limit)
22 memory_limit)
23 else
23 else
24 # Import test data to test pairs.
24 # Import test data to test pairs.
25
25
26 @problem.test_pairs.clear
26 @problem.test_pairs.clear
27 if import_test_pairs(dirname)
27 if import_test_pairs(dirname)
28 test_pair_count = TestPair.count :conditions => "problem_id = #{@problem.id}"
28 test_pair_count = TestPair.count :conditions => "problem_id = #{@problem.id}"
29 @log_msg = "Importing test pair successful. (#{test_pair_count} test pairs imported)"
29 @log_msg = "Importing test pair successful. (#{test_pair_count} test pairs imported)"
30 else
30 else
31 @log_msg = "Importing test pair failed. (0 test pairs imported)"
31 @log_msg = "Importing test pair failed. (0 test pairs imported)"
32 end
32 end
33 end
33 end
34
34
35 @log_msg << import_problem_description(dirname)
35 @log_msg << import_problem_description(dirname)
36 @log_msg << import_problem_pdf(dirname)
36 @log_msg << import_problem_pdf(dirname)
37 + @log_msg << import_full_score(dirname)
37
38
38 return true
39 return true
39 end
40 end
40
41
41 protected
42 protected
42
43
43 def self.long_ext(filename)
44 def self.long_ext(filename)
44 i = filename.index('.')
45 i = filename.index('.')
45 len = filename.length
46 len = filename.length
46 return filename.slice(i..len)
47 return filename.slice(i..len)
47 end
48 end
48
49
49 def extract(tempfile)
50 def extract(tempfile)
50 testdata_filename = save_testdata_file(tempfile)
51 testdata_filename = save_testdata_file(tempfile)
51 ext = TestdataImporter.long_ext(tempfile.original_filename)
52 ext = TestdataImporter.long_ext(tempfile.original_filename)
52
53
53 extract_dir = File.join(GraderScript.raw_dir, @problem.name)
54 extract_dir = File.join(GraderScript.raw_dir, @problem.name)
54 if File.exists? extract_dir
55 if File.exists? extract_dir
55 backup_count = 0
56 backup_count = 0
56 begin
57 begin
57 backup_count += 1
58 backup_count += 1
58 backup_dirname = "#{extract_dir}.backup.#{backup_count}"
59 backup_dirname = "#{extract_dir}.backup.#{backup_count}"
59 end while File.exists? backup_dirname
60 end while File.exists? backup_dirname
60 File.rename(extract_dir, backup_dirname)
61 File.rename(extract_dir, backup_dirname)
61 end
62 end
62 Dir.mkdir extract_dir
63 Dir.mkdir extract_dir
63
64
64 if ext=='.tar.gz' or ext=='.tgz'
65 if ext=='.tar.gz' or ext=='.tgz'
65 cmd = "tar -zxvf #{testdata_filename} -C #{extract_dir}"
66 cmd = "tar -zxvf #{testdata_filename} -C #{extract_dir}"
66 elsif ext=='.tar'
67 elsif ext=='.tar'
67 cmd = "tar -xvf #{testdata_filename} -C #{extract_dir}"
68 cmd = "tar -xvf #{testdata_filename} -C #{extract_dir}"
68 elsif ext=='.zip'
69 elsif ext=='.zip'
69 cmd = "unzip -o #{testdata_filename} -d #{extract_dir}"
70 cmd = "unzip -o #{testdata_filename} -d #{extract_dir}"
70 else
71 else
71 return nil
72 return nil
72 end
73 end
73
74
74 system(cmd)
75 system(cmd)
75
76
76 files = Dir["#{extract_dir}/**/*1*.in"]
77 files = Dir["#{extract_dir}/**/*1*.in"]
77 return nil if files.length==0
78 return nil if files.length==0
78
79
79 File.delete(testdata_filename)
80 File.delete(testdata_filename)
80
81
81 return File.dirname(files[0])
82 return File.dirname(files[0])
82 end
83 end
83
84
84 def save_testdata_file(tempfile)
85 def save_testdata_file(tempfile)
85 ext = TestdataImporter.long_ext(tempfile.original_filename)
86 ext = TestdataImporter.long_ext(tempfile.original_filename)
86 testdata_filename = File.join(Dir.tmpdir,"#{@problem.name}#{ext}")
87 testdata_filename = File.join(Dir.tmpdir,"#{@problem.name}#{ext}")
87
88
88 return nil if tempfile==""
89 return nil if tempfile==""
89
90
90 if tempfile.instance_of?(Tempfile)
91 if tempfile.instance_of?(Tempfile)
91 tempfile.close
92 tempfile.close
92 FileUtils.move(tempfile.path,testdata_filename)
93 FileUtils.move(tempfile.path,testdata_filename)
93 else
94 else
94 File.open(testdata_filename, "wb") do |f|
95 File.open(testdata_filename, "wb") do |f|
95 f.write(tempfile.read)
96 f.write(tempfile.read)
96 end
97 end
97 end
98 end
98
99
99 return testdata_filename
100 return testdata_filename
100 end
101 end
101
102
102 def import_test_pairs(dirname)
103 def import_test_pairs(dirname)
103 test_num = 1
104 test_num = 1
104 while FileTest.exists? "#{dirname}/#{test_num}.in"
105 while FileTest.exists? "#{dirname}/#{test_num}.in"
105 in_filename = "#{dirname}/#{test_num}.in"
106 in_filename = "#{dirname}/#{test_num}.in"
106 sol_filename = "#{dirname}/#{test_num}.sol"
107 sol_filename = "#{dirname}/#{test_num}.sol"
107
108
108 break if not FileTest.exists? sol_filename
109 break if not FileTest.exists? sol_filename
109
110
110 test_pair = TestPair.new(:input => open(in_filename).read,
111 test_pair = TestPair.new(:input => open(in_filename).read,
111 :solution => open(sol_filename).read,
112 :solution => open(sol_filename).read,
112 :problem => @problem)
113 :problem => @problem)
113 break if not test_pair.save
114 break if not test_pair.save
114
115
115 test_num += 1
116 test_num += 1
116 end
117 end
117 return test_num > 1
118 return test_num > 1
118 end
119 end
119
120
120 def import_problem_description(dirname)
121 def import_problem_description(dirname)
121 html_files = Dir["#{dirname}/*.html"]
122 html_files = Dir["#{dirname}/*.html"]
122 markdown_files = Dir["#{dirname}/*.md"] + Dir["#{dirname}/*.markdown"]
123 markdown_files = Dir["#{dirname}/*.md"] + Dir["#{dirname}/*.markdown"]
123 if (html_files.length != 0) or (markdown_files.length != 0)
124 if (html_files.length != 0) or (markdown_files.length != 0)
124 description = @problem.description || Description.new
125 description = @problem.description || Description.new
125
126
126 if html_files.length != 0
127 if html_files.length != 0
127 filename = html_files[0]
128 filename = html_files[0]
128 description.markdowned = false
129 description.markdowned = false
129 else
130 else
130 filename = markdown_files[0]
131 filename = markdown_files[0]
131 description.markdowned = true
132 description.markdowned = true
132 end
133 end
133
134
134 description.body = open(filename).read
135 description.body = open(filename).read
135 description.save
136 description.save
136 @problem.description = description
137 @problem.description = description
137 @problem.save
138 @problem.save
138 return "\nProblem description imported from #{filename}."
139 return "\nProblem description imported from #{filename}."
139 else
140 else
140 return ''
141 return ''
141 end
142 end
142 end
143 end
143
144
144 def import_problem_pdf(dirname)
145 def import_problem_pdf(dirname)
145 pdf_files = Dir["#{dirname}/*.pdf"]
146 pdf_files = Dir["#{dirname}/*.pdf"]
146 puts "CHECKING... #{dirname}"
147 puts "CHECKING... #{dirname}"
147 if pdf_files.length != 0
148 if pdf_files.length != 0
148 puts "HAS PDF FILE"
149 puts "HAS PDF FILE"
149 filename = pdf_files[0]
150 filename = pdf_files[0]
150
151
151 @problem.save if not @problem.id
152 @problem.save if not @problem.id
152 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
153 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
153 if not FileTest.exists? out_dirname
154 if not FileTest.exists? out_dirname
154 Dir.mkdir out_dirname
155 Dir.mkdir out_dirname
155 end
156 end
156
157
157 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
158 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
158
159
159 if FileTest.exists? out_filename
160 if FileTest.exists? out_filename
160 File.delete out_filename
161 File.delete out_filename
161 end
162 end
162
163
163 File.rename(filename, out_filename)
164 File.rename(filename, out_filename)
164 @problem.description_filename = "#{@problem.name}.pdf"
165 @problem.description_filename = "#{@problem.name}.pdf"
165 @problem.save
166 @problem.save
166 return "\nProblem pdf imported from #{filename}."
167 return "\nProblem pdf imported from #{filename}."
167 else
168 else
168 return ""
169 return ""
169 end
170 end
170 end
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 end
181 end
182 +
183 + end
You need to be logged in to leave comments. Login now