Show More
Commit Description:
imports task description as pdf
Commit Description:
imports task description as pdf
File last commit:
Show/Diff file:
Action:
lib/testdata_importer.rb | 151 lines | 4.0 KiB | text/x-ruby | RubyLexer |
jittat
added problem import...
r204 require 'tmpdir'
class TestdataImporter
Jittat Fakcharoenphol
imports task description as pdf
r271
jittat
added problem import...
r204 attr :log_msg
Jittat Fakcharoenphol
imports test pairs
r210 def initialize(problem)
@problem = problem
end
def import_from_file(tempfile,
jittat
added problem import...
r204 time_limit,
Jittat Fakcharoenphol
imports test pairs
r210 memory_limit,
import_to_db=false)
jittat
added problem import...
r204
Jittat Fakcharoenphol
imports test pairs
r210 dirname = extract(tempfile)
jittat
added problem import...
r204 return false if not dirname
Jittat Fakcharoenphol
imports test pairs
r210 if not import_to_db
@log_msg = GraderScript.call_import_problem(@problem.name,
dirname,
time_limit,
memory_limit)
else
# Import test data to test pairs.
@problem.test_pairs.clear
if import_test_pairs(dirname)
test_pair_count = TestPair.count :conditions => "problem_id = #{@problem.id}"
@log_msg = "Importing test pair successful. (#{test_pair_count} test pairs imported)"
else
@log_msg = "Importing test pair failed. (0 test pairs imported)"
end
end
Jittat Fakcharoenphol
added problem description import
r211
@log_msg << import_problem_description(dirname)
Jittat Fakcharoenphol
imports task description as pdf
r271 @log_msg << import_problem_pdf(dirname)
Jittat Fakcharoenphol
added problem description import
r211
jittat
added problem import...
r204 return true
end
protected
def self.long_ext(filename)
i = filename.index('.')
len = filename.length
return filename.slice(i..len)
end
Jittat Fakcharoenphol
imports test pairs
r210 def extract(tempfile)
testdata_filename = save_testdata_file(tempfile)
jittat
added problem import...
r204 ext = TestdataImporter.long_ext(tempfile.original_filename)
Jittat Fakcharoenphol
imports test pairs
r210 extract_dir = File.join(GraderScript.raw_dir, @problem.name)
jittat
added problem import...
r204 begin
Dir.mkdir extract_dir
rescue Errno::EEXIST
end
if ext=='.tar.gz' or ext=='.tgz'
cmd = "tar -zxvf #{testdata_filename} -C #{extract_dir}"
elsif ext=='.tar'
cmd = "tar -xvf #{testdata_filename} -C #{extract_dir}"
elsif ext=='.zip'
jittat
import problem replaced old one, fixed small problems...
r205 cmd = "unzip -o #{testdata_filename} -d #{extract_dir}"
jittat
added problem import...
r204 else
return nil
end
system(cmd)
jittat
testdata import now looks for *1*.in when finding raw dir...
r206 files = Dir["#{extract_dir}/**/*1*.in"]
jittat
added problem import...
r204 return nil if files.length==0
Jittat Fakcharoenphol
removes imported testdata file after extracted
r270 File.delete(testdata_filename)
jittat
added problem import...
r204 return File.dirname(files[0])
end
Jittat Fakcharoenphol
imports test pairs
r210 def save_testdata_file(tempfile)
jittat
added problem import...
r204 ext = TestdataImporter.long_ext(tempfile.original_filename)
Jittat Fakcharoenphol
imports test pairs
r210 testdata_filename = File.join(Dir.tmpdir,"#{@problem.name}#{ext}")
jittat
added problem import...
r204
return nil if tempfile==""
if tempfile.instance_of?(Tempfile)
tempfile.close
FileUtils.move(tempfile.path,testdata_filename)
else
File.open(testdata_filename, "wb") do |f|
f.write(tempfile.read)
end
end
return testdata_filename
end
Jittat Fakcharoenphol
imports test pairs
r210 def import_test_pairs(dirname)
test_num = 1
while FileTest.exists? "#{dirname}/#{test_num}.in"
in_filename = "#{dirname}/#{test_num}.in"
sol_filename = "#{dirname}/#{test_num}.sol"
break if not FileTest.exists? sol_filename
test_pair = TestPair.new(:input => open(in_filename).read,
:solution => open(sol_filename).read,
:problem => @problem)
break if not test_pair.save
test_num += 1
end
return test_num > 1
end
Jittat Fakcharoenphol
added problem description import
r211 def import_problem_description(dirname)
html_files = Dir["#{dirname}/*.html"]
markdown_files = Dir["#{dirname}/*.md"] + Dir["#{dirname}/*.markdown"]
if (html_files.length != 0) or (markdown_files.length != 0)
description = @problem.description || Description.new
if html_files.length != 0
filename = html_files[0]
description.markdowned = false
else
filename = markdown_files[0]
description.markdowned = true
end
description.body = open(filename).read
description.save
@problem.description = description
@problem.save
return "\nProblem description imported from #{filename}."
Jittat Fakcharoenphol
fixed import error when no problem descriptions provided
r235 else
return ''
Jittat Fakcharoenphol
added problem description import
r211 end
end
Jittat Fakcharoenphol
imports task description as pdf
r271 def import_problem_pdf(dirname)
pdf_files = Dir["#{dirname}/*.pdf"]
if pdf_files.length != 0
filename = pdf_files[0]
out_filename = "#{Problem.download_file_basedir}/#{@problem.name}.pdf"
File.rename(filename, out_filename)
@problem.description_filename = "#{@problem.name}.pdf"
@problem.save
return "\nProblem pdf imported from #{filename}."
end
end
jittat
added problem import...
r204 end