diff --git a/Library/Homebrew/cask/download.rb b/Library/Homebrew/cask/download.rb index 4c80eb262827e..a5b3d31f7496c 100644 --- a/Library/Homebrew/cask/download.rb +++ b/Library/Homebrew/cask/download.rb @@ -19,10 +19,9 @@ def initialize(cask, quarantine: nil) @quarantine = quarantine end - def fetch(quiet: nil, verify_download_integrity: true, timeout: nil) + def fetch(verify_download_integrity: true) downloaded_path = begin - downloader.shutup! if quiet - downloader.fetch(timeout: timeout) + downloader.fetch downloader.cached_location rescue => e error = CaskError.new("Download failed on Cask '#{cask}' with message: #{e}") @@ -41,8 +40,8 @@ def downloader end end - def time_file_size(timeout: nil) - downloader.resolved_time_file_size(timeout: timeout) + def time_file_size + downloader.resolved_time_file_size end def clear_cache diff --git a/Library/Homebrew/cask/installer.rb b/Library/Homebrew/cask/installer.rb index 4b1395786b533..4b50d16279cf6 100644 --- a/Library/Homebrew/cask/installer.rb +++ b/Library/Homebrew/cask/installer.rb @@ -62,15 +62,13 @@ def self.caveats(cask) EOS end - sig { params(quiet: T.nilable(T::Boolean), timeout: T.nilable(T.any(Integer, Float))).void } - def fetch(quiet: nil, timeout: nil) + def fetch odebug "Cask::Installer#fetch" verify_has_sha if require_sha? && !force? - - download(quiet: quiet, timeout: timeout) - satisfy_dependencies + + download end def stage @@ -164,10 +162,9 @@ def downloader @downloader ||= Download.new(@cask, quarantine: quarantine?) end - sig { params(quiet: T.nilable(T::Boolean), timeout: T.nilable(T.any(Integer, Float))).returns(Pathname) } - def download(quiet: nil, timeout: nil) - @download ||= downloader.fetch(quiet: quiet, verify_download_integrity: @verify_download_integrity, -timeout: timeout) + sig { returns(Pathname) } + def download + @download ||= downloader.fetch(verify_download_integrity: @verify_download_integrity) end def verify_has_sha @@ -182,7 +179,7 @@ def verify_has_sha def primary_container @primary_container ||= begin - downloaded_path = download(quiet: true) + downloaded_path = download UnpackStrategy.detect(downloaded_path, type: @cask.container&.type, merge_xattrs: true) end end diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index fffb0ad86ace3..31289f7bbc455 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -15,9 +15,6 @@ require "github_packages" -require "extend/time" -using TimeRemaining - # @abstract Abstract superclass for all download strategies. # # @api private @@ -55,7 +52,7 @@ def initialize(url, name, version, **meta) # Download and cache the resource at {#cached_location}. # # @api public - def fetch(timeout: nil); end + def fetch; end # Disable any output during downloading. # @@ -179,20 +176,18 @@ def initialize(url, name, version, **meta) # Download and cache the repository at {#cached_location}. # # @api public - def fetch(timeout: nil) - end_time = Time.now + timeout if timeout - + def fetch ohai "Cloning #{url}" if cached_location.exist? && repo_valid? puts "Updating #{cached_location}" - update(timeout: timeout) + update elsif cached_location.exist? puts "Removing invalid repository from cache" clear_cache - clone_repo(timeout: end_time) + clone_repo else - clone_repo(timeout: end_time) + clone_repo end version.update_commit(last_commit) if head? @@ -237,11 +232,9 @@ def repo_valid? raise NotImplementedError end - sig { params(timeout: T.nilable(Time)).void } - def clone_repo(timeout: nil); end + def clone_repo; end - sig { params(timeout: T.nilable(Time)).void } - def update(timeout: nil); end + def update; end def current_revision; end @@ -360,9 +353,7 @@ def initialize(url, name, version, **meta) # Download and cache the file at {#cached_location}. # # @api public - def fetch(timeout: nil) - end_time = Time.now + timeout if timeout - + def fetch download_lock = LockFile.new(temporary_path.basename) download_lock.lock @@ -373,7 +364,7 @@ def fetch(timeout: nil) ohai "Downloading #{url}" - resolved_url, _, url_time, = resolve_url_basename_time_file_size(url, timeout: end_time&.remaining!) + resolved_url, _, url_time, = resolve_url_basename_time_file_size(url) fresh = if cached_location.exist? && url_time url_time <= cached_location.mtime @@ -387,7 +378,7 @@ def fetch(timeout: nil) puts "Already downloaded: #{cached_location}" else begin - _fetch(url: url, resolved_url: resolved_url, timeout: end_time&.remaining!) + _fetch(url: url, resolved_url: resolved_url) rescue ErrorDuringExecution raise CurlDownloadStrategyError, url end @@ -404,8 +395,6 @@ def fetch(timeout: nil) puts "Trying a mirror..." retry - rescue Timeout::Error => e - raise Timeout::Error, "Timed out downloading #{self.url}: #{e}" end ensure download_lock&.unlock @@ -417,19 +406,19 @@ def clear_cache rm_rf(temporary_path) end - def resolved_time_file_size(timeout: nil) - _, _, time, file_size = resolve_url_basename_time_file_size(url, timeout: timeout) + def resolved_time_file_size + _, _, time, file_size = resolve_url_basename_time_file_size(url) [time, file_size] end private - def resolved_url_and_basename(timeout: nil) - resolved_url, basename, = resolve_url_basename_time_file_size(url, timeout: nil) + def resolved_url_and_basename + resolved_url, basename, = resolve_url_basename_time_file_size(url) [resolved_url, basename] end - def resolve_url_basename_time_file_size(url, timeout: nil) + def resolve_url_basename_time_file_size(url) @resolved_info_cache ||= {} return @resolved_info_cache[url] if @resolved_info_cache.include?(url) @@ -437,7 +426,7 @@ def resolve_url_basename_time_file_size(url, timeout: nil) url = url.sub(%r{^((ht|f)tps?://)?}, "#{domain.chomp("/")}/") end - out, _, status= curl_output("--location", "--silent", "--head", "--request", "GET", url.to_s, timeout: timeout) + out, _, status= curl_output("--location", "--silent", "--head", "--request", "GET", url.to_s) lines = status.success? ? out.lines.map(&:chomp) : [] @@ -496,7 +485,7 @@ def resolve_url_basename_time_file_size(url, timeout: nil) @resolved_info_cache[url] = [redirect_url, basename, time, file_size] end - def _fetch(url:, resolved_url:, timeout:) + def _fetch(url:, resolved_url:) ohai "Downloading from #{resolved_url}" if url != resolved_url if Homebrew::EnvConfig.no_insecure_redirect? && @@ -505,7 +494,7 @@ def _fetch(url:, resolved_url:, timeout:) raise CurlDownloadStrategyError, url end - curl_download resolved_url, to: temporary_path, timeout: timeout + curl_download resolved_url, to: temporary_path end # Curl options to be always passed to curl, @@ -578,9 +567,9 @@ def combined_mirrors @combined_mirrors = [*@mirrors, *backup_mirrors] end - def resolve_url_basename_time_file_size(url, timeout: nil) + def resolve_url_basename_time_file_size(url) if url == self.url - super("#{apache_mirrors["preferred"]}#{apache_mirrors["path_info"]}", timeout: timeout) + super("#{apache_mirrors["preferred"]}#{apache_mirrors["path_info"]}") else super end @@ -603,7 +592,7 @@ def apache_mirrors class CurlPostDownloadStrategy < CurlDownloadStrategy private - def _fetch(url:, resolved_url:, timeout:) + def _fetch(url:, resolved_url:) args = if meta.key?(:data) escape_data = ->(d) { ["-d", URI.encode_www_form([d])] } [url, *meta[:data].flat_map(&escape_data)] @@ -612,7 +601,7 @@ def _fetch(url:, resolved_url:, timeout:) query.nil? ? [url, "-X", "POST"] : [url, "-d", query] end - curl_download(*args, to: temporary_path, timeout: timeout) + curl_download(*args, to: temporary_path) end end @@ -652,7 +641,7 @@ def initialize(url, name, version, **meta) # Download and cache the repository at {#cached_location}. # # @api public - def fetch(timeout: nil) + def fetch if @url.chomp("/") != repo_url || !silent_command("svn", args: ["switch", @url, cached_location]).success? clear_cache end @@ -693,11 +682,7 @@ def externals end end - sig { - params(target: Pathname, url: String, revision: T.nilable(String), ignore_externals: T::Boolean, - timeout: T.nilable(Time)).void - } - def fetch_repo(target, url, revision = nil, ignore_externals: false, timeout: nil) + def fetch_repo(target, url, revision = nil, ignore_externals: false) # Use "svn update" when the repository already exists locally. # This saves on bandwidth and will have a similar effect to verifying the # cache as it will make any changes to get the right revision. @@ -717,9 +702,9 @@ def fetch_repo(target, url, revision = nil, ignore_externals: false, timeout: ni end if target.directory? - command! "svn", args: ["update", *args], chdir: target.to_s, timeout: timeout&.remaining + command!("svn", args: ["update", *args], chdir: target.to_s) else - command! "svn", args: ["checkout", url, target, *args], timeout: timeout&.remaining + command!("svn", args: ["checkout", url, target, *args]) end end @@ -732,22 +717,20 @@ def repo_valid? (cached_location/".svn").directory? end - sig { params(timeout: T.nilable(Time)).void } - def clone_repo(timeout: nil) + def clone_repo case @ref_type when :revision - fetch_repo cached_location, @url, @ref, timeout: timeout + fetch_repo cached_location, @url, @ref when :revisions # nil is OK for main_revision, as fetch_repo will then get latest main_revision = @ref[:trunk] - fetch_repo cached_location, @url, main_revision, ignore_externals: true, timeout: timeout + fetch_repo cached_location, @url, main_revision, ignore_externals: true externals do |external_name, external_url| - fetch_repo cached_location/external_name, external_url, @ref[external_name], ignore_externals: true, - timeout: timeout + fetch_repo cached_location/external_name, external_url, @ref[external_name], ignore_externals: true end else - fetch_repo cached_location, @url, timeout: timeout + fetch_repo cached_location, @url end end alias update clone_repo @@ -796,13 +779,12 @@ def cache_version 0 end - sig { params(timeout: T.nilable(Time)).void } - def update(timeout: nil) + def update config_repo - update_repo(timeout: timeout) - checkout(timeout: timeout) + update_repo + checkout reset - update_submodules(timeout: timeout) if submodules? + update_submodules if submodules? end def shallow_clone? @@ -863,7 +845,6 @@ def refspec end end - sig { void } def config_repo command! "git", args: ["config", "remote.origin.url", @url], @@ -876,42 +857,35 @@ def config_repo chdir: cached_location end - sig { params(timeout: T.nilable(Time)).void } - def update_repo(timeout: nil) + def update_repo return if @ref_type != :branch && ref? if !shallow_clone? && shallow_dir? command! "git", - args: ["fetch", "origin", "--unshallow"], - chdir: cached_location, - timeout: timeout&.remaining + args: ["fetch", "origin", "--unshallow"], + chdir: cached_location else command! "git", - args: ["fetch", "origin"], - chdir: cached_location, - timeout: timeout&.remaining + args: ["fetch", "origin"], + chdir: cached_location end end - sig { params(timeout: T.nilable(Time)).void } - def clone_repo(timeout: nil) - command! "git", args: clone_args, timeout: timeout&.remaining + def clone_repo + command! "git", args: clone_args command! "git", - args: ["config", "homebrew.cacheversion", cache_version], - chdir: cached_location, - timeout: timeout&.remaining - checkout(timeout: timeout) - update_submodules(timeout: timeout) if submodules? + args: ["config", "homebrew.cacheversion", cache_version], + chdir: cached_location + checkout + update_submodules if submodules? end - sig { params(timeout: T.nilable(Time)).void } - def checkout(timeout: nil) + def checkout ohai "Checking out #{@ref_type} #{@ref}" if @ref_type && @ref - command! "git", args: ["checkout", "-f", @ref, "--"], chdir: cached_location, timeout: timeout&.remaining + command! "git", args: ["checkout", "-f", @ref, "--"], chdir: cached_location end - sig { void } def reset ref = case @ref_type when :branch @@ -925,16 +899,13 @@ def reset chdir: cached_location end - sig { params(timeout: T.nilable(Time)).void } - def update_submodules(timeout: nil) + def update_submodules command! "git", - args: ["submodule", "foreach", "--recursive", "git submodule sync"], - chdir: cached_location, - timeout: timeout&.remaining + args: ["submodule", "foreach", "--recursive", "git submodule sync"], + chdir: cached_location command! "git", - args: ["submodule", "update", "--init", "--recursive"], - chdir: cached_location, - timeout: timeout&.remaining + args: ["submodule", "update", "--init", "--recursive"], + chdir: cached_location fix_absolute_submodule_gitdir_references! end @@ -1086,23 +1057,19 @@ def quiet_flag "-Q" unless verbose? end - sig { params(timeout: T.nilable(Time)).void } - def clone_repo(timeout: nil) + def clone_repo # Login is only needed (and allowed) with pserver; skip for anoncvs. - command! "cvs", args: [*quiet_flag, "-d", @url, "login"], timeout: timeout&.remaining if @url.include? "pserver" + command! "cvs", args: [*quiet_flag, "-d", @url, "login"] if @url.include? "pserver" command! "cvs", - args: [*quiet_flag, "-d", @url, "checkout", "-d", cached_location.basename, @module], - chdir: cached_location.dirname, - timeout: timeout&.remaining + args: [*quiet_flag, "-d", @url, "checkout", "-d", cached_location.basename, @module], + chdir: cached_location.dirname end - sig { params(timeout: T.nilable(Time)).void } - def update(timeout: nil) + def update command! "cvs", - args: [*quiet_flag, "update"], - chdir: cached_location, - timeout: timeout&.remaining + args: [*quiet_flag, "update"], + chdir: cached_location end def split_url(in_url) @@ -1154,14 +1121,12 @@ def repo_valid? (cached_location/".hg").directory? end - sig { params(timeout: T.nilable(Time)).void } - def clone_repo(timeout: nil) - command! "hg", args: ["clone", @url, cached_location], timeout: timeout&.remaining + def clone_repo + command! "hg", args: ["clone", @url, cached_location] end - sig { params(timeout: T.nilable(Time)).void } - def update(timeout: nil) - command! "hg", args: ["--cwd", cached_location, "pull", "--update"], timeout: timeout&.remaining + def update + command! "hg", args: ["--cwd", cached_location, "pull", "--update"] update_args = if @ref_type && @ref ohai "Checking out #{@ref_type} #{@ref}" @@ -1170,7 +1135,7 @@ def update(timeout: nil) ["--clean"] end - command! "hg", args: ["--cwd", cached_location, "update", *update_args], timeout: timeout&.remaining + command! "hg", args: ["--cwd", cached_location, "update", *update_args] end end @@ -1219,20 +1184,16 @@ def repo_valid? (cached_location/".bzr").directory? end - sig { params(timeout: T.nilable(Time)).void } - def clone_repo(timeout: nil) + def clone_repo # "lightweight" means history-less command! "bzr", - args: ["checkout", "--lightweight", @url, cached_location], - timeout: timeout&.remaining + args: ["checkout", "--lightweight", @url, cached_location] end - sig { params(timeout: T.nilable(Time)).void } - def update(timeout: nil) + def update command! "bzr", - args: ["update"], - chdir: cached_location, - timeout: timeout&.remaining + args: ["update"], + chdir: cached_location end end @@ -1275,14 +1236,12 @@ def cache_tag "fossil" end - sig { params(timeout: T.nilable(Time)).void } - def clone_repo(timeout: nil) - silent_command! "fossil", args: ["clone", @url, cached_location], timeout: timeout&.remaining + def clone_repo + silent_command!("fossil", args: ["clone", @url, cached_location]) end - sig { params(timeout: T.nilable(Time)).void } - def update(timeout: nil) - silent_command! "fossil", args: ["pull", "-R", cached_location], timeout: timeout&.remaining + def update + silent_command!("fossil", args: ["pull", "-R", cached_location]) end end diff --git a/Library/Homebrew/extend/time.rb b/Library/Homebrew/extend/time.rb deleted file mode 100644 index 81779cd8dcd6f..0000000000000 --- a/Library/Homebrew/extend/time.rb +++ /dev/null @@ -1,18 +0,0 @@ -# typed: false -# frozen_string_literal: true - -module TimeRemaining - refine Time do - def remaining - [0, self - Time.now].max - end - - def remaining! - r = remaining - - raise Timeout::Error if r <= 0 - - r - end - end -end diff --git a/Library/Homebrew/sorbet/plugins/using.rb b/Library/Homebrew/sorbet/plugins/using.rb index 288c4c6c4465e..e1bf761b29756 100644 --- a/Library/Homebrew/sorbet/plugins/using.rb +++ b/Library/Homebrew/sorbet/plugins/using.rb @@ -28,16 +28,4 @@ class ::Hash def assert_valid_keys!(*valid_keys); end end RUBY -when "TimeRemaining" - puts <<-RUBY - # typed: strict - - class ::Time - sig { returns(T.any(Integer, Float)) } - def remaining; end - - sig { returns(T.any(Integer, Float)) } - def remaining!; end - end - RUBY end diff --git a/Library/Homebrew/system_command.rb b/Library/Homebrew/system_command.rb index 952564ffb6c85..7d049c0c6270d 100644 --- a/Library/Homebrew/system_command.rb +++ b/Library/Homebrew/system_command.rb @@ -10,15 +10,13 @@ require "extend/predicable" require "extend/hash_validator" -require "extend/time" - # Class for running sub-processes and capturing their output and exit status. # # @api private class SystemCommand extend T::Sig - using TimeRemaining + using HashValidator # Helper functions for calling {SystemCommand.run}. module Mixin @@ -80,24 +78,10 @@ def run! verbose: T.nilable(T::Boolean), secrets: T.any(String, T::Array[String]), chdir: T.any(String, Pathname), - timeout: T.nilable(T.any(Integer, Float)), ).void } - def initialize( - executable, - args: [], - sudo: false, - env: {}, - input: [], - must_succeed: false, - print_stdout: false, - print_stderr: true, - debug: nil, - verbose: false, - secrets: [], - chdir: T.unsafe(nil), - timeout: nil - ) + def initialize(executable, args: [], sudo: false, env: {}, input: [], must_succeed: false, + print_stdout: false, print_stderr: true, debug: nil, verbose: nil, secrets: [], chdir: T.unsafe(nil)) require "extend/ENV" @executable = executable @args = args @@ -116,7 +100,6 @@ def initialize( @verbose = verbose @secrets = (Array(secrets) + ENV.sensitive_environment.values).uniq @chdir = chdir - @timeout = timeout end sig { returns(T::Array[String]) } @@ -214,14 +197,10 @@ def write_input_to(raw_stdin) sig { params(sources: T::Array[IO], _block: T.proc.params(type: Symbol, line: String).void).void } def each_line_from(sources, &_block) - end_time = Time.now + @timeout if @timeout - loop do - select_timeout = end_time&.remaining! - readable_sources, = IO.select(sources, [], [], select_timeout) - raise Timeout::Error if readable_sources.nil? + readable_sources, = IO.select(sources) - readable_sources = readable_sources.reject(&:eof?) + readable_sources = T.must(readable_sources).reject(&:eof?) break if readable_sources.empty? diff --git a/Library/Homebrew/utils/curl.rb b/Library/Homebrew/utils/curl.rb index f6d7cbea755ca..8e2a798ed36bf 100644 --- a/Library/Homebrew/utils/curl.rb +++ b/Library/Homebrew/utils/curl.rb @@ -3,15 +3,11 @@ require "open3" -require "extend/time" - module Utils # Helper function for interacting with `curl`. # # @api private module Curl - using TimeRemaining - module_function def curl_executable @@ -53,20 +49,14 @@ def curl_args(*extra_args, **options) args << "--silent" unless $stdout.tty? end - args << "--connect-timeout" << connect_timeout.round(3) if options[:connect_timeout] - args << "--max-time" << max_time.round(3) if options[:max_time] args << "--retry" << Homebrew::EnvConfig.curl_retries unless options[:retry] == false - args << "--retry-max-time" << retry_max_time.round if options[:retry_max_time] args + extra_args end def curl_with_workarounds( - *args, - secrets: nil, print_stdout: nil, print_stderr: nil, debug: nil, verbose: nil, env: {}, timeout: nil, **options + *args, secrets: nil, print_stdout: nil, print_stderr: nil, debug: nil, verbose: nil, env: {}, **options ) - end_time = Time.now + timeout if timeout - command_options = { secrets: secrets, print_stdout: print_stdout, @@ -78,22 +68,14 @@ def curl_with_workarounds( # SSL_CERT_FILE can be incorrectly set by users or portable-ruby and screw # with SSL downloads so unset it here. result = system_command curl_executable, - args: curl_args(*args, **options), - env: { "SSL_CERT_FILE" => nil }.merge(env), - timeout: end_time&.remaining, + args: curl_args(*args, **options), + env: { "SSL_CERT_FILE" => nil }.merge(env), **command_options return result if result.success? || !args.exclude?("--http1.1") - raise Timeout::Error, result.stderr.chomp if result.status.exitstatus == 28 - # Error in the HTTP2 framing layer - if result.status.exitstatus == 16 - return curl_with_workarounds( - *args, "--http1.1", - timeout: end_time&.remaining, **command_options, **options - ) - end + return curl_with_workarounds(*args, "--http1.1", **command_options, **options) if result.status.exitstatus == 16 # This is a workaround for https://github.com/curl/curl/issues/1618. if result.status.exitstatus == 56 # Unexpected EOF