2014-12-23 08:24:33 -05:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
require 'fileutils'
|
|
|
|
require 'digest'
|
|
|
|
require_relative 'downloader'
|
|
|
|
|
|
|
|
def do_download(url, base, cache_dir)
|
|
|
|
Downloader.download(url, base, cache_dir, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_checksum(cache, chksums)
|
|
|
|
chksums.each do |sum|
|
|
|
|
name, sum = sum.split(/:/)
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.print "checking #{name} of #{cache} ..."
|
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
hd = Digest(name.upcase).file(cache).hexdigest
|
|
|
|
if hd == sum
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts " OK"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts " NG"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
raise "checksum mismatch: #{cache}, #{name}:#{hd}, expected #{sum}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_extract(cache, dir)
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "extracting #{cache} into #{dir}"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
2014-12-23 09:14:27 -05:00
|
|
|
ext = File.extname(cache)
|
|
|
|
case ext
|
|
|
|
when '.gz', '.tgz'
|
2014-12-23 10:32:37 -05:00
|
|
|
f = IO.popen(["gzip", "-dc", cache])
|
2014-12-23 09:14:27 -05:00
|
|
|
cache = cache.chomp('.gz')
|
|
|
|
when '.bz2', '.tbz'
|
2014-12-23 10:32:37 -05:00
|
|
|
f = IO.popen(["bzip2", "-dc", cache])
|
2014-12-23 09:14:27 -05:00
|
|
|
cache = cache.chomp('.bz2')
|
|
|
|
when '.xz', '.txz'
|
2014-12-23 10:32:37 -05:00
|
|
|
f = IO.popen(["xz", "-dc", cache])
|
2014-12-23 09:14:27 -05:00
|
|
|
cache = cache.chomp('.xz')
|
|
|
|
else
|
|
|
|
inp = cache
|
|
|
|
end
|
2014-12-23 10:24:07 -05:00
|
|
|
inp ||= f.binmode
|
2014-12-23 09:14:27 -05:00
|
|
|
ext = File.extname(cache)
|
|
|
|
case ext
|
|
|
|
when '.tar', /\A\.t[gbx]z\z/
|
|
|
|
pid = Process.spawn("tar", "xpf", "-", in: inp, chdir: dir)
|
|
|
|
when '.zip'
|
|
|
|
pid = Process.spawn("unzip", inp, "-d", dir)
|
|
|
|
end
|
|
|
|
f.close if f
|
|
|
|
Process.wait(pid)
|
2014-12-23 08:24:33 -05:00
|
|
|
$?.success? or raise "failed to extract #{cache}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_patch(dest, patch, args)
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "applying #{patch} under #{dest}"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
2015-09-09 00:38:58 -04:00
|
|
|
Process.wait(Process.spawn("patch", "-d", dest, "-i", patch, *args))
|
2014-12-23 08:24:33 -05:00
|
|
|
$?.success? or raise "failed to patch #{patch}"
|
|
|
|
end
|
|
|
|
|
2016-01-20 03:14:24 -05:00
|
|
|
cache_dir = ENV['CACHE_DIR'] || ".downloaded-cache"
|
|
|
|
mode = :all
|
|
|
|
until ARGV.empty?
|
|
|
|
case ARGV[0]
|
|
|
|
when '--download'
|
|
|
|
mode = :download
|
|
|
|
when '--extract'
|
|
|
|
mode = :extract
|
|
|
|
when '--patch'
|
|
|
|
mode = :patch
|
|
|
|
when '--all'
|
|
|
|
mode = :all
|
|
|
|
when '--cache'
|
|
|
|
ARGV.shift
|
|
|
|
cache_dir = ARGV[0]
|
|
|
|
when /\A--cache=/
|
|
|
|
cache_dir = $'
|
|
|
|
when '--'
|
|
|
|
ARGV.shift
|
|
|
|
break
|
|
|
|
when /\A-/
|
|
|
|
abort "unknown option: #{ARGV[0]}"
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
2014-12-23 08:24:33 -05:00
|
|
|
ARGV.shift
|
|
|
|
end
|
|
|
|
|
2016-01-20 03:14:24 -05:00
|
|
|
FileUtils.mkdir_p(cache_dir)
|
|
|
|
|
2014-12-23 08:24:33 -05:00
|
|
|
success = true
|
|
|
|
ARGV.each do |dir|
|
|
|
|
Dir.glob("#{dir}/**/extlibs") do |list|
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "downloading for #{list}"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
extracted = false
|
|
|
|
dest = File.dirname(list)
|
|
|
|
IO.foreach(list) do |line|
|
|
|
|
line.sub!(/\s*#.*/, '')
|
|
|
|
if /^\t/ =~ line
|
|
|
|
if extracted and (mode == :all or mode == :patch)
|
|
|
|
patch, *args = line.split
|
|
|
|
do_patch(dest, patch, args)
|
|
|
|
end
|
|
|
|
next
|
|
|
|
end
|
|
|
|
url, *chksums = line.split(' ')
|
|
|
|
next unless url
|
|
|
|
extracted = false
|
|
|
|
base = File.basename(url)
|
|
|
|
cache = File.join(cache_dir, base)
|
|
|
|
target = File.join(dest, base[/.*(?=\.tar(?:\.\w+)?\z)/])
|
|
|
|
begin
|
|
|
|
case mode
|
|
|
|
when :download
|
|
|
|
do_download(url, base, cache_dir)
|
|
|
|
do_checksum(cache, chksums)
|
|
|
|
when :extract
|
|
|
|
unless File.directory?(target)
|
|
|
|
do_checksum(cache, chksums)
|
|
|
|
extracted = do_extract(cache, dest)
|
|
|
|
end
|
|
|
|
when :all
|
|
|
|
do_download(url, base, cache_dir)
|
|
|
|
unless File.directory?(target)
|
|
|
|
do_checksum(cache, chksums)
|
|
|
|
extracted = do_extract(cache, dest)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
warn e.inspect
|
|
|
|
success = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
exit(success)
|