Last active
December 27, 2015 07:39
-
-
Save pstachula-dev/7290370 to your computer and use it in GitHub Desktop.
Sandbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'timeout' | |
class Sandbox | |
def initialize(tar, time) | |
@tar = tar | |
@time = time | |
@path = get_main_folder_name | |
@app_map = { | |
bin: './', | |
exe: './', | |
class: 'java ', | |
pl: 'perl ', | |
php: 'php ', | |
py: 'python ', | |
rb: 'ruby ' | |
} | |
open_tar unless Dir.exists?(@path) | |
check_dir_names | |
get_tests | |
@main = get_main | |
end | |
# Otwiera paczke [tar] i wypakowuje do wskazanego katalogu [path] | |
def open_tar | |
Dir.mkdir(@path) | |
Dir.mkdir("#{@path}/result") | |
system ("tar -xf #{@tar} -C #{@path}") | |
end | |
# Wybiera wszystkie nazwy plikow z katalogu [path] | |
def check_dir_names | |
@dirs = Dir.entries(@path) | |
@tests = @dirs.clone | |
end | |
# Wybiera wszystkie pliki /test[0-9]+/.txt | |
def get_tests | |
@tests.delete_if { |e| /test\d+/ !~ e } | |
end | |
# Wybiera nasz plik wykonywalny | |
def get_main | |
@dirs.map { |e| @name = e if /main\..*/ =~ e } | |
@name | |
end | |
# Wycina nazwe programu bez rozszerzenia | |
def get_main_folder_name | |
@tar[/[a-zA-Z0-9]+/] | |
end | |
# Wycina rozszerzenie pliku wykonywalnego rb, c, py... | |
def get_extname | |
begin | |
@extname = File.extname( @name ).gsub /^\./, '' | |
@extname.to_sym | |
rescue Exception => error | |
puts error | |
puts "Wrong or empty extension main file!" | |
exit | |
end | |
end | |
# Pakowanie wynikow do paczki tar | |
def pack_results | |
system ("cd #{@path} && tar -cf ../result.tar result > nowy") | |
system ("rm -r #{@path}") | |
end | |
# Glowne uruchomienie progamu | |
def tests_program | |
@extname = get_extname | |
if @app_map.has_key? @extname | |
i=0 | |
@tests.map do |e| | |
time1 = Time.now | |
begin | |
Timeout::timeout(@time) do | |
program = "#{@app_map[@extname]}#{@path}/#{@main} " | |
# ./path/main.bin | |
stream = "< #{@path}/#{e} > #{@path}/result/result#{i+=1} \n" | |
# < path/testN > path/result/resultN | |
# system('chroot . ' + program + stream) | |
system "./timeout -t #{@time} -m #{1024*100} --no-info-on-success " + program + stream | |
# pid = Process.fork { exec(program + stream) } | |
# Process.wait | |
# system("./secure #{pid}") | |
end | |
rescue Exception => error | |
puts error | |
puts "Sorry timeout: #{@path}/#{e}" | |
next | |
end | |
time2 = Time.now | |
$stdout = File.open("#{@path}/result/result_#{i}", "a") | |
puts "Finshed in: #{time2 - time1}\n" + $?.to_s | |
$stdout.close | |
$stdout = STDOUT | |
puts "Done: $ #{@app_map[@extname]}#{@path}/#{@main} < #{@path}/#{e}" | |
puts "Finshed in: #{time2 - time1}s" | |
end | |
pack_results | |
end | |
end | |
end | |
#------------------- Sandbox --------------------- | |
config = { | |
archive: ARGV[0] || 'main.tar', | |
time: ARGV[1] || 2 | |
} | |
sandbox = Sandbox.new config[:archive], config[:time] | |
sandbox.tests_program |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment