334 lines
12 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
2024-03-29 18:24:18 -07:00
require "abstract_command"
require "formula"
require "fetch"
2020-11-19 18:12:16 +01:00
require "cask/download"
2024-07-14 11:42:22 -04:00
require "retryable_download"
2011-03-12 09:40:10 -08:00
module Homebrew
2024-03-29 18:24:18 -07:00
module Cmd
class FetchCmd < AbstractCommand
include Fetch
FETCH_MAX_TRIES = 5
cmd_args do
description <<~EOS
Download a bottle (if available) or source packages for <formula>e
and binaries for <cask>s. For files, also print SHA-256 checksums.
EOS
flag "--os=",
description: "Download for the given operating system. " \
"(Pass `all` to download for all operating systems.)"
flag "--arch=",
description: "Download for the given CPU architecture. " \
"(Pass `all` to download for all architectures.)"
flag "--bottle-tag=",
description: "Download a bottle for given tag."
2024-07-14 11:42:22 -04:00
flag "--concurrency=", description: "Number of concurrent downloads.", hidden: true
2024-03-29 18:24:18 -07:00
switch "--HEAD",
description: "Fetch HEAD version instead of stable version."
switch "-f", "--force",
description: "Remove a previously cached version and re-fetch."
switch "-v", "--verbose",
description: "Do a verbose VCS checkout, if the URL represents a VCS. This is useful for " \
"seeing if an existing VCS cache has been updated."
switch "--retry",
description: "Retry if downloading fails or re-download if the checksum of a previously cached " \
"version no longer matches. Tries at most #{FETCH_MAX_TRIES} times with " \
"exponential backoff."
switch "--deps",
description: "Also download dependencies for any listed <formula>."
switch "-s", "--build-from-source",
description: "Download source packages rather than a bottle."
switch "--build-bottle",
description: "Download source packages (for eventual bottling) rather than a bottle."
switch "--force-bottle",
description: "Download a bottle if it exists for the current or newest version of macOS, " \
"even if it would not be used during installation."
switch "--[no-]quarantine",
description: "Disable/enable quarantining of downloads (default: enabled).",
env: :cask_opts_quarantine
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
conflicts "--build-from-source", "--build-bottle", "--force-bottle", "--bottle-tag"
conflicts "--cask", "--HEAD"
conflicts "--cask", "--deps"
conflicts "--cask", "-s"
conflicts "--cask", "--build-bottle"
conflicts "--cask", "--force-bottle"
conflicts "--cask", "--bottle-tag"
conflicts "--formula", "--cask"
conflicts "--os", "--bottle-tag"
conflicts "--arch", "--bottle-tag"
named_args [:formula, :cask], min: 1
end
2018-10-27 23:44:32 +05:30
2024-07-14 11:42:22 -04:00
def concurrency
@concurrency ||= args.concurrency&.to_i || 1
end
def download_queue
@download_queue ||= begin
require "download_queue"
DownloadQueue.new(concurrency)
end
end
class Spinner
FRAMES = [
2024-07-16 00:01:40 -04:00
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
].freeze
sig { void }
def initialize
@start = Time.now
@i = 0
end
sig { returns(String) }
def to_s
now = Time.now
if @start + 0.1 < now
@start = now
@i = (@i + 1) % FRAMES.count
end
FRAMES.fetch(@i)
end
end
2024-03-29 18:24:18 -07:00
sig { override.void }
def run
Formulary.enable_factory_cache!
bucket = if args.deps?
args.named.to_formulae_and_casks.flat_map do |formula_or_cask|
case formula_or_cask
when Formula
formula = formula_or_cask
[formula, *formula.recursive_dependencies.map(&:to_formula)]
else
formula_or_cask
end
end
else
args.named.to_formulae_and_casks
end.uniq
2018-10-27 23:44:32 +05:30
2024-03-29 18:24:18 -07:00
os_arch_combinations = args.os_arch_combinations
2023-03-25 11:56:05 +01:00
2024-03-29 18:24:18 -07:00
puts "Fetching: #{bucket * ", "}" if bucket.size > 1
bucket.each do |formula_or_cask|
case formula_or_cask
when Formula
formula = T.cast(formula_or_cask, Formula)
ref = formula.loaded_from_api? ? formula.full_name : formula.path
2024-03-29 18:24:18 -07:00
os_arch_combinations.each do |os, arch|
SimulateSystem.with(os:, arch:) do
formula = Formulary.factory(ref, args.HEAD? ? :head : :stable)
2024-03-29 18:24:18 -07:00
formula.print_tap_action verb: "Fetching"
2024-03-29 18:24:18 -07:00
fetched_bottle = false
if fetch_bottle?(
formula,
force_bottle: args.force_bottle?,
bottle_tag: args.bottle_tag&.to_sym,
build_from_source_formulae: args.build_from_source_formulae,
os: args.os&.to_sym,
arch: args.arch&.to_sym,
)
begin
formula.clear_cache if args.force?
bottle_tag = if (bottle_tag = args.bottle_tag&.to_sym)
Utils::Bottles::Tag.from_symbol(bottle_tag)
else
Utils::Bottles::Tag.new(system: os, arch:)
end
bottle = formula.bottle_for_tag(bottle_tag)
if bottle.nil?
opoo "Bottle for tag #{bottle_tag.to_sym.inspect} is unavailable."
next
end
2024-07-14 11:42:22 -04:00
if (manifest_resource = bottle.github_packages_manifest_resource)
fetch_downloadable(manifest_resource)
2024-03-29 18:24:18 -07:00
end
2024-07-14 11:42:22 -04:00
fetch_downloadable(bottle)
2024-03-29 18:24:18 -07:00
rescue Interrupt
raise
rescue => e
raise if Homebrew::EnvConfig.developer?
fetched_bottle = false
onoe e.message
opoo "Bottle fetch failed, fetching the source instead."
else
fetched_bottle = true
end
end
2024-03-29 18:24:18 -07:00
next if fetched_bottle
2024-07-14 11:42:22 -04:00
fetch_downloadable(formula.resource)
2024-03-29 18:24:18 -07:00
formula.resources.each do |r|
2024-07-14 11:42:22 -04:00
fetch_downloadable(r)
r.patches.each { |patch| fetch_downloadable(patch.resource) if patch.external? }
2024-03-29 18:24:18 -07:00
end
2024-07-14 11:42:22 -04:00
formula.patchlist.each { |patch| fetch_downloadable(patch.resource) if patch.external? }
2024-03-29 18:24:18 -07:00
end
end
2024-03-29 18:24:18 -07:00
else
cask = formula_or_cask
ref = cask.loaded_from_api? ? cask.full_name : cask.sourcefile_path
2024-03-29 18:24:18 -07:00
os_arch_combinations.each do |os, arch|
next if os == :linux
2020-11-19 18:12:16 +01:00
2024-03-29 18:24:18 -07:00
SimulateSystem.with(os:, arch:) do
cask = Cask::CaskLoader.load(ref)
2020-11-19 18:12:16 +01:00
2024-03-29 18:24:18 -07:00
if cask.url.nil? || cask.sha256.nil?
opoo "Cask #{cask} is not supported on os #{os} and arch #{arch}"
next
end
2024-03-29 18:24:18 -07:00
quarantine = args.quarantine?
quarantine = true if quarantine.nil?
2024-03-29 18:24:18 -07:00
download = Cask::Download.new(cask, quarantine:)
2024-07-14 11:42:22 -04:00
fetch_downloadable(download)
2024-03-29 18:24:18 -07:00
end
end
end
end
if concurrency == 1
2024-07-16 00:01:40 -04:00
downloads.each do |downloadable, promise|
promise.wait!
rescue ChecksumMismatchError => e
opoo "#{downloadable.download_type.capitalize} reports different checksum: #{e.expected}"
Homebrew.failed = true if downloadable.is_a?(Resource::Patch)
2024-07-14 11:42:22 -04:00
end
else
spinner = Spinner.new
remaining_downloads = downloads.dup
previous_pending_line_count = 0
begin
2024-07-16 00:01:40 -04:00
$stdout.print Tty.hide_cursor
$stdout.flush
2024-07-16 10:18:26 -04:00
output_message = lambda do |downloadable, future|
status = case future.state
when :fulfilled
"#{Tty.green}✔︎#{Tty.reset}"
when :rejected
"#{Tty.red}#{Tty.reset}"
2024-07-16 10:18:26 -04:00
when :pending, :processing
2024-07-16 00:01:40 -04:00
"#{Tty.blue}#{spinner}#{Tty.reset}"
else
2024-07-16 10:18:26 -04:00
raise future.state.to_s
end
2024-03-30 16:31:13 -07:00
message = "#{downloadable.download_type.capitalize} #{downloadable.name}"
2024-07-16 00:01:40 -04:00
$stdout.puts "#{status} #{message}"
$stdout.flush
2024-07-16 10:18:26 -04:00
if future.rejected? && (e = future.reason).is_a?(ChecksumMismatchError)
opoo "#{downloadable.download_type.capitalize} reports different checksum: #{e.expected}"
Homebrew.failed = true if downloadable.is_a?(Resource::Patch)
next 2
end
1
end
until remaining_downloads.empty?
begin
finished_states = [:fulfilled, :rejected]
2024-07-16 10:18:26 -04:00
finished_downloads, remaining_downloads = remaining_downloads.partition do |_, future|
finished_states.include?(future.state)
2024-07-15 21:17:45 -04:00
end
2024-07-16 10:18:26 -04:00
finished_downloads.each do |downloadable, future|
previous_pending_line_count -= 1
2024-08-14 21:41:01 +02:00
$stdout.print Tty.clear_to_end
2024-07-16 00:01:40 -04:00
$stdout.flush
2024-07-16 10:18:26 -04:00
output_message.call(downloadable, future)
end
previous_pending_line_count = 0
2024-07-16 10:18:26 -04:00
remaining_downloads.each do |downloadable, future|
2024-09-04 22:40:21 +02:00
# FIXME: Allow printing full terminal height.
2024-07-15 17:00:01 -04:00
break if previous_pending_line_count >= [concurrency, (Tty.height - 1)].min
2024-08-14 21:41:01 +02:00
$stdout.print Tty.clear_to_end
2024-07-16 00:01:40 -04:00
$stdout.flush
2024-07-16 10:18:26 -04:00
previous_pending_line_count += output_message.call(downloadable, future)
end
if previous_pending_line_count.positive?
$stdout.print Tty.move_cursor_up_beginning(previous_pending_line_count)
$stdout.flush
end
2024-07-15 17:00:01 -04:00
sleep 0.05
rescue Interrupt
remaining_downloads.each do |_, future|
# FIXME: Implement cancellation of running downloads.
end
2024-08-14 21:41:01 +02:00
if previous_pending_line_count.positive?
$stdout.print Tty.move_cursor_down(previous_pending_line_count - 1)
$stdout.flush
end
raise
end
end
ensure
2024-07-16 00:01:40 -04:00
$stdout.print Tty.show_cursor
$stdout.flush
end
2024-07-14 11:42:22 -04:00
end
2020-11-19 18:12:16 +01:00
2024-07-14 11:42:22 -04:00
download_queue.shutdown
2024-03-29 18:24:18 -07:00
end
2014-03-13 19:51:23 -05:00
2024-07-14 11:42:22 -04:00
private
2024-07-14 11:42:22 -04:00
def downloads
@downloads ||= {}
2024-03-29 18:24:18 -07:00
end
2024-07-14 11:42:22 -04:00
def fetch_downloadable(downloadable)
2024-09-05 17:14:40 +02:00
tries = args.retry? ? {} : { tries: 1 }
downloads[downloadable] ||= download_queue.enqueue(RetryableDownload.new(downloadable, **tries), force: args.force?)
2024-03-29 18:24:18 -07:00
end
end
2011-03-12 09:40:10 -08:00
end
end