Show More
Commit Description:
add new branch for class 2110-101 Introduction to Computer Programming (year 2014) (java language)
Commit Description:
add new branch for class 2110-101 Introduction to Computer Programming (year 2014) (java language)
References:
File last commit:
Show/Diff file:
Action:
lib/testdata_importer.rb | 183 lines | 5.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)
let testdata importer set the full score
r345 @log_msg << import_full_score(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 Fakcharoenphol
added contest problem access control
r282 if File.exists? extract_dir
backup_count = 0
begin
backup_count += 1
backup_dirname = "#{extract_dir}.backup.#{backup_count}"
end while File.exists? backup_dirname
File.rename(extract_dir, backup_dirname)
jittat
added problem import...
r204 end
Jittat Fakcharoenphol
added contest problem access control
r282 Dir.mkdir extract_dir
jittat
added problem import...
r204
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"]
Jittat Fakcharoenphol
added contest problem access control
r282 puts "CHECKING... #{dirname}"
Jittat Fakcharoenphol
imports task description as pdf
r271 if pdf_files.length != 0
Jittat Fakcharoenphol
added contest problem access control
r282 puts "HAS PDF FILE"
Jittat Fakcharoenphol
imports task description as pdf
r271 filename = pdf_files[0]
Jittat Fakcharoenphol
added contest problem access control
r282
@problem.save if not @problem.id
out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
if not FileTest.exists? out_dirname
Dir.mkdir out_dirname
end
out_filename = "#{out_dirname}/#{@problem.name}.pdf"
if FileTest.exists? out_filename
File.delete out_filename
end
Jittat Fakcharoenphol
imports task description as pdf
r271 File.rename(filename, out_filename)
@problem.description_filename = "#{@problem.name}.pdf"
@problem.save
return "\nProblem pdf imported from #{filename}."
Jittat Fakcharoenphol
fixed import error (thanks K.Siththa)
r274 else
return ""
Jittat Fakcharoenphol
imports task description as pdf
r271 end
end
let testdata importer set the full score
r345 #just set the full score to the total number of test case
#it is not perfect but works on most normal use case
def import_full_score(dirname)
in_file = Dir["#{dirname}/*.in"]
full_score =in_file.length * 10
@problem.full_score = full_score
@problem.save
return "\nFull score is set to #{full_score}."
end
jittat
added problem import...
r204 end