Classify extlibs tool

* tool/extlibs.rb (ExtLibs): classify the tool to make it able to reuse it.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58270 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
usa 2017-04-07 12:56:35 +00:00
parent a0035eb620
commit 873d62b1e2

View File

@ -7,11 +7,12 @@ require 'fileutils'
require 'digest'
require_relative 'downloader'
def do_download(url, base, cache_dir)
class ExtLibs
def do_download(url, base, cache_dir)
Downloader.download(url, base, cache_dir, nil)
end
end
def do_checksum(cache, chksums)
def do_checksum(cache, chksums)
chksums.each do |sum|
name, sum = sum.split(/:/)
if $VERBOSE
@ -32,9 +33,9 @@ def do_checksum(cache, chksums)
raise "checksum mismatch: #{cache}, #{name}:#{hd}, expected #{sum}"
end
end
end
end
def do_extract(cache, dir)
def do_extract(cache, dir)
if $VERBOSE
$stdout.puts "extracting #{cache} into #{dir}"
$stdout.flush
@ -64,21 +65,47 @@ def do_extract(cache, dir)
f.close if f
Process.wait(pid)
$?.success? or raise "failed to extract #{cache}"
end
end
def do_patch(dest, patch, args)
def do_patch(dest, patch, args)
if $VERBOSE
$stdout.puts "applying #{patch} under #{dest}"
$stdout.flush
end
Process.wait(Process.spawn("patch", "-d", dest, "-i", patch, *args))
$?.success? or raise "failed to patch #{patch}"
end
end
cache_dir = ENV['CACHE_DIR'] || ".downloaded-cache"
mode = :all
until ARGV.empty?
case ARGV[0]
def do_command(mode, dest, url, cache_dir, chksums)
base = File.basename(url)
cache = File.join(cache_dir, base)
target = File.join(dest, base[/.*(?=\.tar(?:\.\w+)?\z)/])
extracted = false
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
extracted
end
def run(argv)
cache_dir = ENV['CACHE_DIR'] || ".downloaded-cache"
mode = :all
until argv.empty?
case argv[0]
when '--download'
mode = :download
when '--extract'
@ -88,25 +115,26 @@ until ARGV.empty?
when '--all'
mode = :all
when '--cache'
ARGV.shift
cache_dir = ARGV[0]
argv.shift
cache_dir = argv[0]
when /\A--cache=/
cache_dir = $'
when '--'
ARGV.shift
argv.shift
break
when /\A-/
abort "unknown option: #{ARGV[0]}"
warn "unknown option: #{argv[0]}"
return false
else
break
end
ARGV.shift
end
argv.shift
end
FileUtils.mkdir_p(cache_dir)
FileUtils.mkdir_p(cache_dir)
success = true
ARGV.each do |dir|
success = true
argv.each do |dir|
Dir.glob("#{dir}/**/extlibs") do |list|
if $VERBOSE
$stdout.puts "downloading for #{list}"
@ -125,33 +153,23 @@ ARGV.each do |dir|
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
extracted = do_command(mode, dest, url, cache_dir, chksums)
rescue => e
warn e.inspect
success = false
end
end
end
end
success
end
def self.run(argv)
self.new.run(argv)
end
end
exit(success)
if $0 == __FILE__
exit ExtLibs.run(ARGV)
end