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: 74 inserted, 32 deleted
@@ -150,21 +150,17 | |||
|
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 |
@@ -1,7 +1,7 | |||
|
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+$/ |
@@ -14,21 +14,12 | |||
|
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 |
@@ -36,6 +27,22 | |||
|
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 |
@@ -22,7 +22,13 | |||
|
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 |
@@ -4,17 +4,33 | |||
|
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, | |
|
18 | + if not import_to_db | |
|
19 | + @log_msg = GraderScript.call_import_problem(@problem.name, | |
|
15 | 20 | dirname, |
|
16 | 21 | time_limit, |
|
17 | 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 | |
@@ -26,12 +42,11 | |||
|
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 |
@@ -55,9 +70,9 | |||
|
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 | |
@@ -73,4 +88,22 | |||
|
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 | |
|
76 | 105 | end |
|
106 | + return test_num > 1 | |
|
107 | + end | |
|
108 | + | |
|
109 | + end |
You need to be logged in to leave comments.
Login now