2014-12-23 08:24:33 -05:00
|
|
|
#!/usr/bin/ruby
|
2016-07-02 17:01:04 -04:00
|
|
|
|
|
|
|
# Used to download, extract and patch extension libraries (extlibs)
|
|
|
|
# for Ruby. See common.mk for Ruby's usage.
|
|
|
|
|
2014-12-23 08:24:33 -05:00
|
|
|
require 'digest'
|
|
|
|
require_relative 'downloader'
|
2022-03-19 09:44:28 -04:00
|
|
|
begin
|
|
|
|
require_relative 'lib/colorize'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2014-12-23 08:24:33 -05:00
|
|
|
|
2022-03-19 09:42:54 -04:00
|
|
|
class ExtLibs
|
2022-03-19 09:44:28 -04:00
|
|
|
unless defined?(Colorize)
|
|
|
|
class Colorize
|
|
|
|
def pass(str) str; end
|
|
|
|
def fail(str) str; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-19 09:42:54 -04:00
|
|
|
class Vars < Hash
|
|
|
|
def pattern
|
|
|
|
/\$\((#{Regexp.union(keys)})\)/
|
|
|
|
end
|
2020-05-10 01:58:55 -04:00
|
|
|
|
2022-03-19 09:42:54 -04:00
|
|
|
def expand(str)
|
|
|
|
if empty?
|
|
|
|
str
|
|
|
|
else
|
|
|
|
str.gsub(pattern) {self[$1]}
|
|
|
|
end
|
2020-05-10 01:58:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-18 09:11:49 -04:00
|
|
|
def initialize(mode = :all, cache_dir: nil)
|
|
|
|
@mode = mode
|
|
|
|
@cache_dir = cache_dir
|
2019-06-04 04:09:16 -04:00
|
|
|
@colorize = Colorize.new
|
|
|
|
end
|
|
|
|
|
2017-05-21 12:45:35 -04:00
|
|
|
def cache_file(url, cache_dir)
|
2020-05-14 04:19:39 -04:00
|
|
|
Downloader.cache_file(url, nil, cache_dir).to_path
|
2017-05-21 12:45:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def do_download(url, cache_dir)
|
|
|
|
Downloader.download(url, nil, nil, nil, :cache_dir => cache_dir)
|
2017-04-07 08:56:35 -04:00
|
|
|
end
|
2014-12-23 08:24:33 -05:00
|
|
|
|
2017-04-07 08:56:35 -04:00
|
|
|
def do_checksum(cache, chksums)
|
|
|
|
chksums.each do |sum|
|
|
|
|
name, sum = sum.split(/:/)
|
2014-12-23 08:24:33 -05:00
|
|
|
if $VERBOSE
|
2017-04-07 08:56:35 -04:00
|
|
|
$stdout.print "checking #{name} of #{cache} ..."
|
2014-12-23 08:24:33 -05:00
|
|
|
$stdout.flush
|
|
|
|
end
|
2017-04-07 08:56:35 -04:00
|
|
|
hd = Digest(name.upcase).file(cache).hexdigest
|
2019-06-04 04:09:16 -04:00
|
|
|
if $VERBOSE
|
|
|
|
$stdout.print " "
|
|
|
|
$stdout.puts hd == sum ? @colorize.pass("OK") : @colorize.fail("NG")
|
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
unless hd == sum
|
2017-04-07 08:56:35 -04:00
|
|
|
raise "checksum mismatch: #{cache}, #{name}:#{hd}, expected #{sum}"
|
2014-12-23 08:24:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-07 08:56:35 -04:00
|
|
|
def do_extract(cache, dir)
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "extracting #{cache} into #{dir}"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
ext = File.extname(cache)
|
|
|
|
case ext
|
|
|
|
when '.gz', '.tgz'
|
|
|
|
f = IO.popen(["gzip", "-dc", cache])
|
|
|
|
cache = cache.chomp('.gz')
|
|
|
|
when '.bz2', '.tbz'
|
|
|
|
f = IO.popen(["bzip2", "-dc", cache])
|
|
|
|
cache = cache.chomp('.bz2')
|
|
|
|
when '.xz', '.txz'
|
|
|
|
f = IO.popen(["xz", "-dc", cache])
|
|
|
|
cache = cache.chomp('.xz')
|
|
|
|
else
|
|
|
|
inp = cache
|
|
|
|
end
|
|
|
|
inp ||= f.binmode
|
|
|
|
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
|
2018-06-09 12:05:38 -04:00
|
|
|
f.close if f
|
2017-04-07 08:56:35 -04:00
|
|
|
Process.wait(pid)
|
|
|
|
$?.success? or raise "failed to extract #{cache}"
|
2014-12-23 09:14:27 -05:00
|
|
|
end
|
2017-04-07 08:56:35 -04:00
|
|
|
|
|
|
|
def do_patch(dest, patch, args)
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "applying #{patch} under #{dest}"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
2021-01-14 19:32:35 -05:00
|
|
|
Process.wait(Process.spawn(ENV.fetch("PATCH", "patch"), "-d", dest, "-i", patch, *args))
|
2017-04-07 08:56:35 -04:00
|
|
|
$?.success? or raise "failed to patch #{patch}"
|
2014-12-23 09:14:27 -05:00
|
|
|
end
|
2014-12-23 08:24:33 -05:00
|
|
|
|
2020-05-10 02:00:36 -04:00
|
|
|
def do_link(file, src, dest)
|
|
|
|
file = File.join(dest, file)
|
|
|
|
if (target = src).start_with?("/")
|
|
|
|
target = File.join([".."] * file.count("/"), src)
|
|
|
|
end
|
2020-05-23 01:56:13 -04:00
|
|
|
return unless File.exist?(File.expand_path(target, File.dirname(file)))
|
2020-05-10 02:00:36 -04:00
|
|
|
File.unlink(file) rescue nil
|
|
|
|
begin
|
|
|
|
File.symlink(target, file)
|
|
|
|
rescue
|
|
|
|
else
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "linked #{target} to #{file}"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
src = src.sub(/\A\//, '')
|
|
|
|
File.copy_stream(src, file)
|
|
|
|
rescue
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "failed to link #{src} to #{file}: #{$!.message}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "copied #{src} to #{file}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-26 11:30:18 -04:00
|
|
|
def do_exec(command, dir, dest)
|
|
|
|
dir = dir ? File.join(dest, dir) : dest
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "running #{command.dump} under #{dir}"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
system(command, chdir: dir) or raise "failed #{command.dump}"
|
|
|
|
end
|
|
|
|
|
2017-04-07 08:56:35 -04:00
|
|
|
def do_command(mode, dest, url, cache_dir, chksums)
|
|
|
|
extracted = false
|
2017-05-21 12:45:35 -04:00
|
|
|
base = /.*(?=\.tar(?:\.\w+)?\z)/
|
|
|
|
|
2017-04-07 08:56:35 -04:00
|
|
|
case mode
|
|
|
|
when :download
|
2017-05-21 12:45:35 -04:00
|
|
|
cache = do_download(url, cache_dir)
|
2017-04-07 08:56:35 -04:00
|
|
|
do_checksum(cache, chksums)
|
|
|
|
when :extract
|
2017-05-21 12:45:35 -04:00
|
|
|
cache = cache_file(url, cache_dir)
|
|
|
|
target = File.join(dest, File.basename(cache)[base])
|
2017-04-07 08:56:35 -04:00
|
|
|
unless File.directory?(target)
|
|
|
|
do_checksum(cache, chksums)
|
|
|
|
extracted = do_extract(cache, dest)
|
|
|
|
end
|
|
|
|
when :all
|
2017-05-21 12:45:35 -04:00
|
|
|
cache = do_download(url, cache_dir)
|
|
|
|
target = File.join(dest, File.basename(cache)[base])
|
2017-04-07 08:56:35 -04:00
|
|
|
unless File.directory?(target)
|
|
|
|
do_checksum(cache, chksums)
|
|
|
|
extracted = do_extract(cache, dest)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
extracted
|
2014-12-23 08:24:33 -05:00
|
|
|
end
|
|
|
|
|
2022-03-18 09:11:49 -04:00
|
|
|
def process(list)
|
|
|
|
mode = @mode
|
|
|
|
cache_dir = @cache_dir
|
|
|
|
after_extract = (mode == :all or mode == :patch)
|
|
|
|
success = true
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "downloading for #{list}"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
vars = Vars.new
|
|
|
|
extracted = false
|
|
|
|
dest = File.dirname(list)
|
|
|
|
url = chksums = nil
|
|
|
|
IO.foreach(list) do |line|
|
|
|
|
line.sub!(/\s*#.*/, '')
|
|
|
|
if /^(\w+)\s*=\s*(.*)/ =~ line
|
|
|
|
vars[$1] = vars.expand($2)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if chksums
|
|
|
|
chksums.concat(line.split)
|
|
|
|
elsif /^\t/ =~ line
|
|
|
|
if extracted and after_extract
|
|
|
|
patch, *args = line.split.map {|s| vars.expand(s)}
|
|
|
|
do_patch(dest, patch, args)
|
|
|
|
end
|
|
|
|
next
|
|
|
|
elsif /^!\s*(?:chdir:\s*([^|\s]+)\|\s*)?(.*)/ =~ line
|
|
|
|
if extracted and after_extract
|
|
|
|
command = vars.expand($2.strip)
|
|
|
|
chdir = $1 and chdir = vars.expand(chdir)
|
|
|
|
do_exec(command, chdir, dest)
|
|
|
|
end
|
|
|
|
next
|
|
|
|
elsif /->/ =~ line
|
|
|
|
if extracted and after_extract
|
|
|
|
link, file = $`.strip, $'.strip
|
|
|
|
do_link(vars.expand(link), vars.expand(file), dest)
|
|
|
|
end
|
|
|
|
next
|
|
|
|
else
|
|
|
|
url, *chksums = line.split(' ')
|
|
|
|
end
|
|
|
|
if chksums.last == '\\'
|
|
|
|
chksums.pop
|
|
|
|
next
|
|
|
|
end
|
|
|
|
unless url
|
|
|
|
chksums = nil
|
|
|
|
next
|
|
|
|
end
|
|
|
|
url = vars.expand(url)
|
|
|
|
begin
|
|
|
|
extracted = do_command(mode, dest, url, cache_dir, chksums)
|
|
|
|
rescue => e
|
2022-06-17 18:13:19 -04:00
|
|
|
warn defined?(e.full_message) ? e.full_message : e.message
|
2022-03-18 09:11:49 -04:00
|
|
|
success = false
|
|
|
|
end
|
|
|
|
url = chksums = nil
|
|
|
|
end
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_under(dir)
|
|
|
|
success = true
|
|
|
|
Dir.glob("#{dir}/**/extlibs") do |list|
|
|
|
|
success &= process(list)
|
|
|
|
end
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.run(argv)
|
2017-05-21 12:45:35 -04:00
|
|
|
cache_dir = nil
|
2016-01-20 03:14:24 -05:00
|
|
|
mode = :all
|
2017-04-07 08:56:35 -04:00
|
|
|
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-/
|
|
|
|
warn "unknown option: #{argv[0]}"
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
argv.shift
|
|
|
|
end
|
2014-12-23 08:24:33 -05:00
|
|
|
|
2022-03-18 09:11:49 -04:00
|
|
|
extlibs = new(mode, cache_dir: cache_dir)
|
|
|
|
argv.inject(true) do |success, dir|
|
|
|
|
success & extlibs.process_under(dir)
|
2014-12-23 08:24:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-07 08:56:35 -04:00
|
|
|
if $0 == __FILE__
|
|
|
|
exit ExtLibs.run(ARGV)
|
|
|
|
end
|