Description:
imports test pairs
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r210:0c551aa1f64a - - 4 files changed: 77 inserted, 35 deleted
@@ -147,27 +147,23 | |||
|
147 | 147 | end |
|
148 | 148 | |
|
149 | 149 | def import |
|
150 | 150 | end |
|
151 | 151 | |
|
152 | 152 | def do_import |
|
153 | - @problem, import_log = Problem.new_from_import_form_params(params) | |
|
153 | + old_problem = Problem.find_by_name(params[:name]) | |
|
154 | + @problem, import_log = Problem.create_from_import_form_params(params, | |
|
155 | + old_problem) | |
|
154 | 156 | |
|
155 | 157 | if @problem.errors.length != 0 |
|
156 | 158 | render :action => 'import' and return |
|
157 | 159 | end |
|
158 | 160 | |
|
159 | - old_problem = Problem.find_by_name(@problem.name) | |
|
160 | 161 | if old_problem!=nil |
|
161 | - old_problem.full_name = @problem.full_name | |
|
162 | - @problem = old_problem | |
|
163 | - | |
|
164 | 162 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
165 | 163 | end |
|
166 | - | |
|
167 | - @problem.save | |
|
168 | 164 | @log = import_log |
|
169 | 165 | end |
|
170 | 166 | |
|
171 | 167 | ################################## |
|
172 | 168 | protected |
|
173 | 169 |
@@ -1,44 +1,51 | |||
|
1 | 1 | class Problem < ActiveRecord::Base |
|
2 | 2 | |
|
3 | 3 | belongs_to :description |
|
4 | - has_many :test_pairs | |
|
4 | + has_many :test_pairs, :dependent => :delete_all | |
|
5 | 5 | |
|
6 | 6 | validates_presence_of :name |
|
7 | 7 | validates_format_of :name, :with => /^\w+$/ |
|
8 | 8 | validates_presence_of :full_name |
|
9 | 9 | |
|
10 | 10 | DEFAULT_TIME_LIMIT = 1 |
|
11 | 11 | DEFAULT_MEMORY_LIMIT = 32 |
|
12 | 12 | |
|
13 | 13 | def self.find_available_problems |
|
14 | 14 | find(:all, :conditions => {:available => true}, :order => "date_added DESC") |
|
15 | 15 | end |
|
16 | 16 | |
|
17 |
- def self. |
|
|
18 | - problem = Problem.new | |
|
17 | + def self.create_from_import_form_params(params, old_problem=nil) | |
|
18 | + problem = old_problem || Problem.new | |
|
19 | 19 | import_params = Problem.extract_params_and_check(params, problem) |
|
20 | 20 | |
|
21 | 21 | if not problem.valid? |
|
22 | - return problem | |
|
23 | - end | |
|
24 | - | |
|
25 | - importer = TestdataImporter.new | |
|
26 | - | |
|
27 | - if not importer.import_from_file(problem.name, | |
|
28 | - import_params[:file], | |
|
29 | - import_params[:time_limit], | |
|
30 | - import_params[:memory_limit]) | |
|
31 | - problem.errors.add_to_base('Import error.') | |
|
22 | + return problem, 'Error importing' | |
|
32 | 23 | end |
|
33 | 24 | |
|
34 | 25 | problem.full_score = 100 |
|
35 | 26 | problem.date_added = Time.new |
|
36 | 27 | problem.test_allowed = true |
|
37 | 28 | problem.output_only = false |
|
38 | 29 | problem.available = false |
|
30 | + | |
|
31 | + if not problem.save | |
|
32 | + return problem, 'Error importing' | |
|
33 | + end | |
|
34 | + | |
|
35 | + import_to_db = params.has_key? :import_to_db | |
|
36 | + | |
|
37 | + importer = TestdataImporter.new(problem) | |
|
38 | + | |
|
39 | + if not importer.import_from_file(import_params[:file], | |
|
40 | + import_params[:time_limit], | |
|
41 | + import_params[:memory_limit], | |
|
42 | + import_to_db) | |
|
43 | + problem.errors.add_to_base('Import error.') | |
|
44 | + end | |
|
45 | + | |
|
39 | 46 | return problem, importer.log_msg |
|
40 | 47 | end |
|
41 | 48 | |
|
42 | 49 | protected |
|
43 | 50 | |
|
44 | 51 | def self.to_i_or_default(st, default) |
@@ -19,13 +19,19 | |||
|
19 | 19 | %td Full name: |
|
20 | 20 | %td |
|
21 | 21 | = text_field_tag 'full_name' |
|
22 | 22 | %span{:class => 'help'} Leave blank to use the same value as the name above. |
|
23 | 23 | %tr |
|
24 | 24 | %td Testdata file: |
|
25 | - %td= file_field_tag 'file' | |
|
25 | + %td | |
|
26 | + = file_field_tag 'file' | |
|
27 | + %tr | |
|
28 | + %td | |
|
29 | + %td | |
|
30 | + = check_box_tag 'import_to_db' | |
|
31 | + Import test data to database (for a test-pair task) | |
|
26 | 32 | %tr |
|
27 | 33 | %td Time limit: |
|
28 | 34 | %td |
|
29 | 35 | = text_field_tag 'time_limit' |
|
30 | 36 | %span{:class => 'help'} In seconds. Leave blank to use 1 sec. |
|
31 | 37 | %tr |
@@ -1,40 +1,55 | |||
|
1 | 1 | require 'tmpdir' |
|
2 | 2 | |
|
3 | 3 | class TestdataImporter |
|
4 | 4 | |
|
5 | 5 | attr :log_msg |
|
6 | 6 | |
|
7 |
- def i |
|
|
8 | - tempfile, | |
|
7 | + def initialize(problem) | |
|
8 | + @problem = problem | |
|
9 | + end | |
|
10 | + | |
|
11 | + def import_from_file(tempfile, | |
|
9 | 12 | time_limit, |
|
10 |
- memory_limit |
|
|
13 | + memory_limit, | |
|
14 | + import_to_db=false) | |
|
11 | 15 | |
|
12 |
- dirname = |
|
|
16 | + dirname = extract(tempfile) | |
|
13 | 17 | return false if not dirname |
|
14 | - @log_msg = GraderScript.call_import_problem(problem_name, | |
|
15 | - dirname, | |
|
16 |
- |
|
|
17 |
- me |
|
|
18 | + if not import_to_db | |
|
19 | + @log_msg = GraderScript.call_import_problem(@problem.name, | |
|
20 | + dirname, | |
|
21 | + time_limit, | |
|
22 | + memory_limit) | |
|
23 | + else | |
|
24 | + # Import test data to test pairs. | |
|
25 | + | |
|
26 | + @problem.test_pairs.clear | |
|
27 | + if import_test_pairs(dirname) | |
|
28 | + test_pair_count = TestPair.count :conditions => "problem_id = #{@problem.id}" | |
|
29 | + @log_msg = "Importing test pair successful. (#{test_pair_count} test pairs imported)" | |
|
30 | + else | |
|
31 | + @log_msg = "Importing test pair failed. (0 test pairs imported)" | |
|
32 | + end | |
|
33 | + end | |
|
18 | 34 | return true |
|
19 | 35 | end |
|
20 | 36 | |
|
21 | 37 | protected |
|
22 | 38 | |
|
23 | 39 | def self.long_ext(filename) |
|
24 | 40 | i = filename.index('.') |
|
25 | 41 | len = filename.length |
|
26 | 42 | return filename.slice(i..len) |
|
27 | 43 | end |
|
28 | 44 | |
|
29 |
- def |
|
|
30 |
- testdata_filename = |
|
|
31 | - tempfile) | |
|
45 | + def extract(tempfile) | |
|
46 | + testdata_filename = save_testdata_file(tempfile) | |
|
32 | 47 | ext = TestdataImporter.long_ext(tempfile.original_filename) |
|
33 | 48 | |
|
34 |
- extract_dir = File.join(GraderScript.raw_dir, problem |
|
|
49 | + extract_dir = File.join(GraderScript.raw_dir, @problem.name) | |
|
35 | 50 | begin |
|
36 | 51 | Dir.mkdir extract_dir |
|
37 | 52 | rescue Errno::EEXIST |
|
38 | 53 | end |
|
39 | 54 | |
|
40 | 55 | if ext=='.tar.gz' or ext=='.tgz' |
@@ -52,15 +67,15 | |||
|
52 | 67 | files = Dir["#{extract_dir}/**/*1*.in"] |
|
53 | 68 | return nil if files.length==0 |
|
54 | 69 | |
|
55 | 70 | return File.dirname(files[0]) |
|
56 | 71 | end |
|
57 | 72 | |
|
58 |
- def |
|
|
73 | + def save_testdata_file(tempfile) | |
|
59 | 74 | ext = TestdataImporter.long_ext(tempfile.original_filename) |
|
60 |
- testdata_filename = File.join(Dir.tmpdir,"#{problem |
|
|
75 | + testdata_filename = File.join(Dir.tmpdir,"#{@problem.name}#{ext}") | |
|
61 | 76 | |
|
62 | 77 | return nil if tempfile=="" |
|
63 | 78 | |
|
64 | 79 | if tempfile.instance_of?(Tempfile) |
|
65 | 80 | tempfile.close |
|
66 | 81 | FileUtils.move(tempfile.path,testdata_filename) |
@@ -70,7 +85,25 | |||
|
70 | 85 | end |
|
71 | 86 | end |
|
72 | 87 | |
|
73 | 88 | return testdata_filename |
|
74 | 89 | end |
|
75 | 90 | |
|
91 | + def import_test_pairs(dirname) | |
|
92 | + test_num = 1 | |
|
93 | + while FileTest.exists? "#{dirname}/#{test_num}.in" | |
|
94 | + in_filename = "#{dirname}/#{test_num}.in" | |
|
95 | + sol_filename = "#{dirname}/#{test_num}.sol" | |
|
96 | + | |
|
97 | + break if not FileTest.exists? sol_filename | |
|
98 | + | |
|
99 | + test_pair = TestPair.new(:input => open(in_filename).read, | |
|
100 | + :solution => open(sol_filename).read, | |
|
101 | + :problem => @problem) | |
|
102 | + break if not test_pair.save | |
|
103 | + | |
|
104 | + test_num += 1 | |
|
105 | + end | |
|
106 | + return test_num > 1 | |
|
107 | + end | |
|
108 | + | |
|
76 | 109 | end |
You need to be logged in to leave comments.
Login now