2016-07-02 17:01:04 -04:00
|
|
|
# Used by configure and make to download or update mirrored Ruby and GCC
|
|
|
|
# files. This will use HTTPS if possible, falling back to HTTP.
|
|
|
|
|
2013-06-14 00:01:54 -04:00
|
|
|
require 'open-uri'
|
2014-12-25 11:25:57 -05:00
|
|
|
begin
|
|
|
|
require 'net/https'
|
2015-11-26 04:09:49 -05:00
|
|
|
rescue LoadError
|
|
|
|
https = 'http'
|
|
|
|
else
|
|
|
|
https = 'https'
|
2014-12-25 11:25:57 -05:00
|
|
|
|
|
|
|
# open-uri of ruby 2.2.0 accept an array of PEMs as ssl_ca_cert, but old
|
|
|
|
# versions are not. so, patching OpenSSL::X509::Store#add_file instead.
|
|
|
|
class OpenSSL::X509::Store
|
|
|
|
alias orig_add_file add_file
|
|
|
|
def add_file(pems)
|
|
|
|
Array(pems).each do |pem|
|
|
|
|
if File.directory?(pem)
|
|
|
|
add_path pem
|
|
|
|
else
|
|
|
|
orig_add_file pem
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-01-23 02:30:32 -05:00
|
|
|
# since open-uri internally checks ssl_ca_cert using File.directory?,
|
|
|
|
# allow to accept an array.
|
2014-12-25 11:25:57 -05:00
|
|
|
class <<File
|
|
|
|
alias orig_directory? directory?
|
|
|
|
def File.directory? files
|
|
|
|
files.is_a?(Array) ? false : orig_directory?(files)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-06-14 00:01:54 -04:00
|
|
|
|
2014-06-23 21:26:21 -04:00
|
|
|
class Downloader
|
2016-02-02 02:08:13 -05:00
|
|
|
def self.https=(https)
|
|
|
|
@@https = https
|
|
|
|
end
|
|
|
|
|
2016-02-18 02:53:13 -05:00
|
|
|
def self.https?
|
|
|
|
@@https == 'https'
|
|
|
|
end
|
|
|
|
|
2015-11-26 04:09:49 -05:00
|
|
|
def self.https
|
|
|
|
@@https
|
|
|
|
end
|
|
|
|
|
2014-09-27 22:54:59 -04:00
|
|
|
class GNU < self
|
|
|
|
def self.download(name, *rest)
|
2016-02-18 02:53:13 -05:00
|
|
|
if https?
|
2015-11-26 10:40:58 -05:00
|
|
|
super("https://raw.githubusercontent.com/gcc-mirror/gcc/master/#{name}", name, *rest)
|
|
|
|
else
|
2016-02-18 02:53:13 -05:00
|
|
|
super("https://repo.or.cz/official-gcc.git/blob_plain/HEAD:/#{name}", name, *rest)
|
2015-11-26 10:40:58 -05:00
|
|
|
end
|
2014-09-27 22:54:59 -04:00
|
|
|
end
|
2014-09-23 07:56:30 -04:00
|
|
|
end
|
|
|
|
|
2014-09-27 22:54:59 -04:00
|
|
|
class RubyGems < self
|
2016-01-23 02:30:32 -05:00
|
|
|
def self.download(name, dir = nil, since = true, options = {})
|
2015-01-14 02:45:28 -05:00
|
|
|
require 'rubygems'
|
2017-03-21 03:19:23 -04:00
|
|
|
options = options.dup
|
2016-05-18 03:04:55 -04:00
|
|
|
verify = options.delete(:verify) {Gem::VERSION >= "2.4."}
|
2016-04-06 04:40:02 -04:00
|
|
|
options[:ssl_ca_cert] = Dir.glob(File.expand_path("../lib/rubygems/ssl_certs/**/*.pem", File.dirname(__FILE__)))
|
2015-01-14 02:45:28 -05:00
|
|
|
file = under(dir, name)
|
2016-02-18 02:53:13 -05:00
|
|
|
super("https://rubygems.org/downloads/#{name}", file, nil, since, options) or
|
2015-01-14 02:45:28 -05:00
|
|
|
return false
|
2016-05-18 03:04:55 -04:00
|
|
|
return true unless verify
|
2015-01-14 02:45:28 -05:00
|
|
|
end
|
2014-09-23 07:56:30 -04:00
|
|
|
end
|
|
|
|
|
2014-09-27 22:54:59 -04:00
|
|
|
Gems = RubyGems
|
2014-09-24 04:55:09 -04:00
|
|
|
|
2014-09-27 22:54:59 -04:00
|
|
|
class Unicode < self
|
|
|
|
def self.download(name, *rest)
|
2014-10-30 03:39:51 -04:00
|
|
|
super("http://www.unicode.org/Public/#{name}", name, *rest)
|
2014-09-23 07:56:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-23 07:52:23 -04:00
|
|
|
def self.mode_for(data)
|
2014-09-28 17:47:59 -04:00
|
|
|
/\A#!/ =~ data ? 0755 : 0644
|
2014-09-23 07:52:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.http_options(file, since)
|
|
|
|
options = {}
|
|
|
|
if since
|
|
|
|
case since
|
|
|
|
when true
|
|
|
|
since = (File.mtime(file).httpdate rescue nil)
|
|
|
|
when Time
|
|
|
|
since = since.httpdate
|
|
|
|
end
|
|
|
|
if since
|
|
|
|
options['If-Modified-Since'] = since
|
|
|
|
end
|
|
|
|
end
|
2015-04-06 11:00:00 -04:00
|
|
|
options['Accept-Encoding'] = '*' # to disable Net::HTTP::GenericRequest#decode_content
|
2014-09-23 07:52:23 -04:00
|
|
|
options
|
2014-06-23 21:26:21 -04:00
|
|
|
end
|
2014-09-23 03:08:16 -04:00
|
|
|
|
2016-01-23 02:30:32 -05:00
|
|
|
# Downloader.download(url, name, [dir, [since]])
|
2014-09-23 07:52:23 -04:00
|
|
|
#
|
2014-09-23 03:08:03 -04:00
|
|
|
# Update a file from url if newer version is available.
|
|
|
|
# Creates the file if the file doesn't yet exist; however, the
|
|
|
|
# directory where the file is being created has to exist already.
|
2016-01-23 02:30:32 -05:00
|
|
|
# The +since+ parameter can take the following values, with associated meanings:
|
|
|
|
# true ::
|
|
|
|
# Take the last-modified time of the current file on disk, and only download
|
|
|
|
# if the server has a file that was modified later. Download unconditionally
|
|
|
|
# if we don't have the file yet. Default.
|
|
|
|
# +some time value+ ::
|
|
|
|
# Use this time value instead of the time of modification of the file on disk.
|
|
|
|
# nil ::
|
|
|
|
# Only download the file if it doesn't exist yet.
|
|
|
|
# false ::
|
|
|
|
# always download url regardless of whether we already have a file,
|
|
|
|
# and regardless of modification times. (This is essentially just a waste of
|
|
|
|
# network resources, except in the case that the file we have is somehow damaged.
|
|
|
|
# Please note that using this recurringly might create or be seen as a
|
|
|
|
# denial of service attack.)
|
2014-09-23 07:52:23 -04:00
|
|
|
#
|
2014-09-23 03:08:03 -04:00
|
|
|
# Example usage:
|
2014-10-05 21:15:23 -04:00
|
|
|
# download 'http://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt',
|
|
|
|
# 'UnicodeData.txt', 'enc/unicode/data'
|
2016-01-23 02:30:32 -05:00
|
|
|
def self.download(url, name, dir = nil, since = true, options = {})
|
2017-03-21 03:19:23 -04:00
|
|
|
options = options.dup
|
2016-05-18 03:04:55 -04:00
|
|
|
options.delete(:verify)
|
2015-01-14 02:45:28 -05:00
|
|
|
file = under(dir, name)
|
2017-03-21 03:19:23 -04:00
|
|
|
dryrun = options.delete(:dryrun)
|
2016-01-23 02:30:32 -05:00
|
|
|
if since.nil? and File.exist?(file)
|
2014-11-19 10:06:04 -05:00
|
|
|
if $VERBOSE
|
2017-03-23 11:49:10 -04:00
|
|
|
$stdout.puts "#{file} already exists"
|
2014-11-19 10:06:04 -05:00
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2017-03-23 11:49:10 -04:00
|
|
|
if dryrun
|
|
|
|
puts "Download #{url} into #{file}"
|
|
|
|
return false
|
|
|
|
end
|
2016-02-18 02:53:13 -05:00
|
|
|
if !https? and url.start_with?("https:")
|
|
|
|
warn "*** using http instead of https ***"
|
|
|
|
url = url.sub(/\Ahttps/, 'http')
|
|
|
|
end
|
2014-09-27 22:54:59 -04:00
|
|
|
url = URI(url)
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.print "downloading #{name} ... "
|
|
|
|
$stdout.flush
|
|
|
|
end
|
2014-09-23 07:52:23 -04:00
|
|
|
begin
|
2016-01-23 02:30:32 -05:00
|
|
|
data = url.read(options.merge(http_options(file, since.nil? ? true : since)))
|
2014-09-23 07:52:23 -04:00
|
|
|
rescue OpenURI::HTTPError => http_error
|
2014-09-27 22:54:59 -04:00
|
|
|
if http_error.message =~ /^304 / # 304 Not Modified
|
|
|
|
if $VERBOSE
|
2016-01-23 02:30:32 -05:00
|
|
|
$stdout.puts "#{name} not modified"
|
2014-09-27 22:54:59 -04:00
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2014-09-23 07:52:23 -04:00
|
|
|
raise
|
2014-10-20 06:48:52 -04:00
|
|
|
rescue Timeout::Error
|
2016-01-23 02:30:32 -05:00
|
|
|
if since.nil? and File.exist?(file)
|
2014-10-20 06:48:52 -04:00
|
|
|
puts "Request for #{url} timed out, using old version."
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
raise
|
|
|
|
rescue SocketError
|
2016-01-23 02:30:32 -05:00
|
|
|
if since.nil? and File.exist?(file)
|
2014-10-20 06:48:52 -04:00
|
|
|
puts "No network connection, unable to download #{url}, using old version."
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
raise
|
2014-09-23 03:08:03 -04:00
|
|
|
end
|
2014-09-23 07:52:23 -04:00
|
|
|
mtime = nil
|
|
|
|
open(file, "wb", 0600) do |f|
|
|
|
|
f.write(data)
|
|
|
|
f.chmod(mode_for(data))
|
|
|
|
mtime = data.meta["last-modified"]
|
2014-09-23 03:08:03 -04:00
|
|
|
end
|
2014-09-23 07:52:23 -04:00
|
|
|
if mtime
|
|
|
|
mtime = Time.httpdate(mtime)
|
|
|
|
File.utime(mtime, mtime, file)
|
|
|
|
end
|
2014-09-27 22:54:59 -04:00
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "done"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
2014-09-23 07:52:23 -04:00
|
|
|
true
|
|
|
|
rescue => e
|
2014-12-23 23:00:27 -05:00
|
|
|
raise "failed to download #{name}\n#{e.message}: #{url}"
|
2014-09-23 07:52:23 -04:00
|
|
|
end
|
2015-01-14 02:45:28 -05:00
|
|
|
|
2016-05-18 03:04:55 -04:00
|
|
|
def self.verify(file)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-01-14 02:45:28 -05:00
|
|
|
def self.under(dir, name)
|
|
|
|
dir ? File.join(dir, File.basename(name)) : name
|
|
|
|
end
|
2013-06-14 00:01:54 -04:00
|
|
|
end
|
2014-09-28 07:27:22 -04:00
|
|
|
|
2016-02-02 02:08:13 -05:00
|
|
|
Downloader.https = https.freeze
|
2015-11-26 04:09:49 -05:00
|
|
|
|
2014-09-28 07:27:22 -04:00
|
|
|
if $0 == __FILE__
|
2016-01-23 02:30:32 -05:00
|
|
|
since = true
|
2016-05-18 03:04:55 -04:00
|
|
|
options = {}
|
2014-09-28 07:27:22 -04:00
|
|
|
until ARGV.empty?
|
|
|
|
case ARGV[0]
|
|
|
|
when '-d'
|
|
|
|
destdir = ARGV[1]
|
2014-09-28 17:47:59 -04:00
|
|
|
ARGV.shift
|
2016-01-18 05:21:03 -05:00
|
|
|
when '-p'
|
|
|
|
# strip directory names from the name to download, and add the
|
|
|
|
# prefix instead.
|
|
|
|
prefix = ARGV[1]
|
|
|
|
ARGV.shift
|
2014-09-28 07:27:22 -04:00
|
|
|
when '-e'
|
2016-01-23 02:30:32 -05:00
|
|
|
since = nil
|
2014-10-20 10:58:06 -04:00
|
|
|
when '-a'
|
2016-01-23 02:30:32 -05:00
|
|
|
since = false
|
2016-05-18 03:04:55 -04:00
|
|
|
when '-V'
|
|
|
|
options[:verify] = true
|
2017-03-21 03:19:23 -04:00
|
|
|
when '-n', '--dryrun'
|
|
|
|
options[:dryrun] = true
|
2014-09-28 07:27:22 -04:00
|
|
|
when /\A-/
|
|
|
|
abort "#{$0}: unknown option #{ARGV[0]}"
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
2014-09-28 17:47:59 -04:00
|
|
|
ARGV.shift
|
2014-09-28 07:27:22 -04:00
|
|
|
end
|
|
|
|
dl = Downloader.constants.find do |name|
|
|
|
|
ARGV[0].casecmp(name.to_s) == 0
|
2014-10-30 03:39:51 -04:00
|
|
|
end unless ARGV.empty?
|
2014-09-28 07:27:22 -04:00
|
|
|
$VERBOSE = true
|
|
|
|
if dl
|
|
|
|
dl = Downloader.const_get(dl)
|
|
|
|
ARGV.shift
|
|
|
|
ARGV.each do |name|
|
2017-03-23 11:49:10 -04:00
|
|
|
dir = destdir
|
2016-11-30 12:29:19 -05:00
|
|
|
if prefix
|
2017-03-23 11:49:10 -04:00
|
|
|
name = name.sub(/\A\.\//, '')
|
|
|
|
if name.start_with?(destdir+"/")
|
|
|
|
name = name[(destdir.size+1)..-1]
|
|
|
|
if (dir = File.dirname(name)) == '.'
|
|
|
|
dir = destdir
|
|
|
|
else
|
|
|
|
dir = File.join(destdir, dir)
|
|
|
|
end
|
2016-11-30 12:29:19 -05:00
|
|
|
else
|
2017-03-23 11:49:10 -04:00
|
|
|
name = File.basename(name)
|
2016-11-30 12:29:19 -05:00
|
|
|
end
|
2017-03-23 11:49:10 -04:00
|
|
|
name = "#{prefix}/#{name}"
|
2016-11-30 12:29:19 -05:00
|
|
|
end
|
2017-03-23 11:49:10 -04:00
|
|
|
dl.download(name, dir, since, options)
|
2014-09-28 07:27:22 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
abort "usage: #{$0} url name" unless ARGV.size == 2
|
2016-05-18 03:04:55 -04:00
|
|
|
Downloader.download(ARGV[0], ARGV[1], destdir, since, options)
|
2014-09-28 07:27:22 -04:00
|
|
|
end
|
|
|
|
end
|