Skip to content

Commit

Permalink
downloader.rb: Use keyword arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Jan 16, 2025
1 parent f2c9eac commit 73f8c62
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
64 changes: 31 additions & 33 deletions tool/downloader.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Used by configure and make to download or update mirrored Ruby and GCC
# files. This will use HTTPS if possible, falling back to HTTP.

# -*- frozen-string-literal: true -*-

require 'fileutils'
require 'open-uri'
require 'pathname'
Expand Down Expand Up @@ -75,14 +77,13 @@ def self.download(name, *rest)
end

class RubyGems < self
def self.download(name, dir = nil, since = true, options = {})
def self.download(name, dir = nil, since = true, **options)
require 'rubygems'
options = options.dup
options[:ssl_ca_cert] = Dir.glob(File.expand_path("../lib/rubygems/ssl_certs/**/*.pem", File.dirname(__FILE__)))
if Gem::Version.new(name[/-\K[^-]*(?=\.gem\z)/]).prerelease?
options[:ignore_http_client_errors] = true
end
super("https://rubygems.org/downloads/#{name}", name, dir, since, options)
super("https://rubygems.org/downloads/#{name}", name, dir, since, **options)
end
end

Expand All @@ -107,16 +108,13 @@ def self.get_option(argv, options)
end
end

def self.download(name, dir = nil, since = true, options = {})
options = options.dup
unicode_beta = options.delete(:unicode_beta)
def self.download(name, dir = nil, since = true, unicode_beta: nil, **options)
name_dir_part = name.sub(/[^\/]+$/, '')
if unicode_beta == 'YES'
if INDEX.size == 0
index_options = options.dup
index_options[:cache_save] = false # TODO: make sure caching really doesn't work for index file
cache_save = false # TODO: make sure caching really doesn't work for index file
index_data = File.read(under(dir, "index.html")) rescue nil
index_file = super(UNICODE_PUBLIC+name_dir_part, "#{name_dir_part}index.html", dir, true, index_options)
index_file = super(UNICODE_PUBLIC+name_dir_part, "#{name_dir_part}index.html", dir, true, cache_save: cache_save, **options)
INDEX[:index] = File.read(index_file)
since = true unless INDEX[:index] == index_data
end
Expand All @@ -125,15 +123,15 @@ def self.download(name, dir = nil, since = true, options = {})
beta_name = INDEX[:index][/#{Regexp.quote(file_base)}(-[0-9.]+d\d+)?\.txt/]
# make sure we always check for new versions of files,
# because they can easily change in the beta period
super(UNICODE_PUBLIC+name_dir_part+beta_name, name, dir, since, options)
super(UNICODE_PUBLIC+name_dir_part+beta_name, name, dir, since, **options)
else
index_file = Pathname.new(under(dir, name_dir_part+'index.html'))
if index_file.exist? and name_dir_part !~ /^(12\.1\.0|emoji\/12\.0)/
raise "Although Unicode is not in beta, file #{index_file} exists. " +
"Remove all files in this directory and in .downloaded-cache/ " +
"because they may be leftovers from the beta period."
end
super(UNICODE_PUBLIC+name, name, dir, since, options)
super(UNICODE_PUBLIC+name, name, dir, since, **options)
end
end
end
Expand Down Expand Up @@ -198,23 +196,20 @@ def self.httpdate(date)
# Example usage:
# download 'http://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt',
# 'UnicodeData.txt', 'enc/unicode/data'
def self.download(url, name, dir = nil, since = true, options = {})
options = options.dup
def self.download(url, name, dir = nil, since = true,
cache_save: ENV["CACHE_SAVE"] != "no", cache_dir: nil,
ignore_http_client_errors: nil,
dryrun: nil, verbose: false, **options)
url = URI(url)
dryrun = options.delete(:dryrun)

if name
file = Pathname.new(under(dir, name))
else
name = File.basename(url.path)
end
cache_save = options.delete(:cache_save) {
ENV["CACHE_SAVE"] != "no"
}
cache = cache_file(url, name, options.delete(:cache_dir))
cache = cache_file(url, name, cache_dir)
file ||= cache
if since.nil? and file.exist?
if $VERBOSE
if verbose
$stdout.puts "#{file} already exists"
$stdout.flush
end
Expand All @@ -227,20 +222,19 @@ def self.download(url, name, dir = nil, since = true, options = {})
puts "Download #{url} into #{file}"
return
end
if link_cache(cache, file, name, $VERBOSE)
if link_cache(cache, file, name, verbose: verbose)
return file.to_path
end
if !https? and URI::HTTPS === url
warn "*** using http instead of https ***"
url.scheme = 'http'
url = URI(url.to_s)
end
if $VERBOSE
if verbose
$stdout.print "downloading #{name} ... "
$stdout.flush
end
mtime = nil
ignore_http_client_errors = options.delete(:ignore_http_client_errors)
options = options.merge(http_options(file, since.nil? ? true : since))
begin
data = with_retry(10) do
Expand All @@ -253,7 +247,7 @@ def self.download(url, name, dir = nil, since = true, options = {})
rescue OpenURI::HTTPError => http_error
case http_error.message
when /^304 / # 304 Not Modified
if $VERBOSE
if verbose
$stdout.puts "#{name} not modified"
$stdout.flush
end
Expand Down Expand Up @@ -288,7 +282,7 @@ def self.download(url, name, dir = nil, since = true, options = {})
if mtime
dest.utime(mtime, mtime)
end
if $VERBOSE
if verbose
$stdout.puts "done"
$stdout.flush
end
Expand All @@ -306,20 +300,24 @@ def self.under(dir, name)
dir ? File.join(dir, File.basename(name)) : name
end

def self.default_cache_dir
if cache_dir = ENV['CACHE_DIR']
return cache_dir unless cache_dir.empty?
end
".downloaded-cache"
end

def self.cache_file(url, name, cache_dir = nil)
case cache_dir
when false
return nil
when nil
cache_dir = ENV['CACHE_DIR']
if !cache_dir or cache_dir.empty?
cache_dir = ".downloaded-cache"
end
cache_dir = default_cache_dir
end
Pathname.new(cache_dir) + (name || File.basename(URI(url).path))
end

def self.link_cache(cache, file, name, verbose = false)
def self.link_cache(cache, file, name, verbose: false)
return false unless cache and cache.exist?
return true if cache.eql?(file)
if /cygwin/ !~ RUBY_PLATFORM or /winsymlink:nativestrict/ =~ ENV['CYGWIN']
Expand Down Expand Up @@ -460,7 +458,7 @@ def self.with_retry(max_times, &block)
end
ARGV.shift
end
$VERBOSE = true
options[:verbose] = true
if dl
args.each do |name|
dir = destdir
Expand All @@ -479,10 +477,10 @@ def self.with_retry(max_times, &block)
end
name = "#{prefix}/#{name}"
end
dl.download(name, dir, since, options)
dl.download(name, dir, since, **options)
end
else
abort "usage: #{$0} url name" unless args.size == 2
Downloader.download(args[0], args[1], destdir, since, options)
Downloader.download(args[0], args[1], destdir, since, **options)
end
end
2 changes: 1 addition & 1 deletion tool/extlibs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def cache_file(url, cache_dir)
end

def do_download(url, cache_dir)
Downloader.download(url, nil, nil, nil, :cache_dir => cache_dir)
Downloader.download(url, nil, nil, nil, cache_dir: cache_dir)
end

def do_checksum(cache, chksums)
Expand Down

0 comments on commit 73f8c62

Please sign in to comment.