2013-06-14 00:01:54 -04:00
|
|
|
require 'open-uri'
|
|
|
|
|
2014-06-23 21:26:21 -04:00
|
|
|
class Downloader
|
2014-09-27 22:54:59 -04:00
|
|
|
class GNU < self
|
|
|
|
def self.download(name, *rest)
|
|
|
|
super("http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=#{name};hb=HEAD", name, *rest)
|
|
|
|
end
|
2014-09-23 07:56:30 -04:00
|
|
|
end
|
|
|
|
|
2014-09-27 22:54:59 -04:00
|
|
|
class RubyGems < self
|
|
|
|
def self.download(name, *rest)
|
|
|
|
super("https://rubygems.org/downloads/#{name}", name, *rest)
|
|
|
|
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
|
|
|
|
options
|
2014-06-23 21:26:21 -04:00
|
|
|
end
|
2014-09-23 03:08:16 -04:00
|
|
|
|
2014-09-23 07:52:23 -04:00
|
|
|
# Downloader.download(url, name, [dir, [ims]])
|
|
|
|
#
|
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.
|
2014-09-23 21:43:44 -04:00
|
|
|
# If +ims+ is false, always download url regardless of its last
|
2014-09-23 07:52:23 -04:00
|
|
|
# modified time.
|
|
|
|
#
|
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'
|
2014-09-23 07:52:23 -04:00
|
|
|
def self.download(url, name, dir = nil, ims = true)
|
2014-10-30 03:39:51 -04:00
|
|
|
file = dir ? File.join(dir, File.basename(name)) : name
|
2014-11-19 10:06:04 -05:00
|
|
|
if ims.nil? and File.exist?(file)
|
|
|
|
if $VERBOSE
|
|
|
|
$stdout.puts "#{name} already exists"
|
|
|
|
$stdout.flush
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
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
|
2014-10-20 06:48:52 -04:00
|
|
|
data = url.read(http_options(file, ims.nil? ? true : ims))
|
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
|
|
|
|
$stdout.puts "not modified"
|
|
|
|
$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
|
|
|
|
if ims.nil? and File.exist?(file)
|
|
|
|
puts "Request for #{url} timed out, using old version."
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
raise
|
|
|
|
rescue SocketError
|
|
|
|
if ims.nil? and File.exist?(file)
|
|
|
|
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-09-23 07:56:30 -04:00
|
|
|
raise e.class, "failed to download #{name}\n#{e.message}: #{url}", e.backtrace
|
2014-09-23 07:52:23 -04:00
|
|
|
end
|
2013-06-14 00:01:54 -04:00
|
|
|
end
|
2014-09-28 07:27:22 -04:00
|
|
|
|
|
|
|
if $0 == __FILE__
|
|
|
|
ims = true
|
|
|
|
until ARGV.empty?
|
|
|
|
case ARGV[0]
|
|
|
|
when '-d'
|
|
|
|
destdir = ARGV[1]
|
2014-09-28 17:47:59 -04:00
|
|
|
ARGV.shift
|
2014-09-28 07:27:22 -04:00
|
|
|
when '-e'
|
|
|
|
ims = nil
|
2014-10-20 10:58:06 -04:00
|
|
|
when '-a'
|
|
|
|
ims = 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|
|
|
|
|
dl.download(name, destdir, ims)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
abort "usage: #{$0} url name" unless ARGV.size == 2
|
2014-09-28 11:26:21 -04:00
|
|
|
Downloader.download(ARGV[0], ARGV[1], destdir, ims)
|
2014-09-28 07:27:22 -04:00
|
|
|
end
|
|
|
|
end
|