Port Homebrew::Cmd::FetchCmd

This commit is contained in:
Douglas Eichelberger 2024-03-29 18:24:18 -07:00
parent f83ba58f8d
commit 90cd9d2e0a
2 changed files with 224 additions and 224 deletions

View File

@ -1,19 +1,18 @@
# typed: true # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command"
require "formula" require "formula"
require "fetch" require "fetch"
require "cli/parser"
require "cask/download" require "cask/download"
module Homebrew module Homebrew
extend Fetch module Cmd
class FetchCmd < AbstractCommand
include Fetch
FETCH_MAX_TRIES = 5 FETCH_MAX_TRIES = 5
sig { returns(CLI::Parser) } cmd_args do
def self.fetch_args
Homebrew::CLI::Parser.new do
description <<~EOS description <<~EOS
Download a bottle (if available) or source packages for <formula>e Download a bottle (if available) or source packages for <formula>e
and binaries for <cask>s. For files, also print SHA-256 checksums. and binaries for <cask>s. For files, also print SHA-256 checksums.
@ -67,11 +66,9 @@ module Homebrew
named_args [:formula, :cask], min: 1 named_args [:formula, :cask], min: 1
end end
end
def self.fetch
args = fetch_args.parse
sig { override.void }
def run
Formulary.enable_factory_cache! Formulary.enable_factory_cache!
bucket = if args.deps? bucket = if args.deps?
@ -131,10 +128,10 @@ module Homebrew
begin begin
bottle.fetch_tab bottle.fetch_tab
rescue DownloadError rescue DownloadError
retry if retry_fetch?(bottle, args:) retry if retry_fetch?(bottle)
raise raise
end end
fetch_formula(bottle, args:) fetch_formula(bottle)
rescue Interrupt rescue Interrupt
raise raise
rescue => e rescue => e
@ -150,14 +147,14 @@ module Homebrew
next if fetched_bottle next if fetched_bottle
fetch_formula(formula, args:) fetch_formula(formula)
formula.resources.each do |r| formula.resources.each do |r|
fetch_resource(r, args:) fetch_resource(r)
r.patches.each { |p| fetch_patch(p, args:) if p.external? } r.patches.each { |p| fetch_patch(p) if p.external? }
end end
formula.patchlist.each { |p| fetch_patch(p, args:) if p.external? } formula.patchlist.each { |p| fetch_patch(p) if p.external? }
end end
end end
else else
@ -179,43 +176,43 @@ module Homebrew
quarantine = true if quarantine.nil? quarantine = true if quarantine.nil?
download = Cask::Download.new(cask, quarantine:) download = Cask::Download.new(cask, quarantine:)
fetch_cask(download, args:) fetch_cask(download)
end end
end end
end end
end end
end end
def self.fetch_resource(resource, args:) def fetch_resource(resource)
puts "Resource: #{resource.name}" puts "Resource: #{resource.name}"
fetch_fetchable resource, args: fetch_fetchable resource
rescue ChecksumMismatchError => e rescue ChecksumMismatchError => e
retry if retry_fetch?(resource, args:) retry if retry_fetch?(resource)
opoo "Resource #{resource.name} reports different sha256: #{e.expected}" opoo "Resource #{resource.name} reports different sha256: #{e.expected}"
end end
def self.fetch_formula(formula, args:) def fetch_formula(formula)
fetch_fetchable(formula, args:) fetch_fetchable(formula)
rescue ChecksumMismatchError => e rescue ChecksumMismatchError => e
retry if retry_fetch?(formula, args:) retry if retry_fetch?(formula)
opoo "Formula reports different sha256: #{e.expected}" opoo "Formula reports different sha256: #{e.expected}"
end end
def self.fetch_cask(cask_download, args:) def fetch_cask(cask_download)
fetch_fetchable(cask_download, args:) fetch_fetchable(cask_download)
rescue ChecksumMismatchError => e rescue ChecksumMismatchError => e
retry if retry_fetch?(cask_download, args:) retry if retry_fetch?(cask_download)
opoo "Cask reports different sha256: #{e.expected}" opoo "Cask reports different sha256: #{e.expected}"
end end
def self.fetch_patch(patch, args:) def fetch_patch(patch)
fetch_fetchable(patch, args:) fetch_fetchable(patch)
rescue ChecksumMismatchError => e rescue ChecksumMismatchError => e
opoo "Patch reports different sha256: #{e.expected}" opoo "Patch reports different sha256: #{e.expected}"
Homebrew.failed = true Homebrew.failed = true
end end
def self.retry_fetch?(formula, args:) def retry_fetch?(formula)
@fetch_tries ||= Hash.new { |h, k| h[k] = 1 } @fetch_tries ||= Hash.new { |h, k| h[k] = 1 }
if args.retry? && (@fetch_tries[formula] < FETCH_MAX_TRIES) if args.retry? && (@fetch_tries[formula] < FETCH_MAX_TRIES)
wait = 2 ** @fetch_tries[formula] wait = 2 ** @fetch_tries[formula]
@ -234,7 +231,7 @@ module Homebrew
end end
end end
def self.fetch_fetchable(formula, args:) def fetch_fetchable(formula)
formula.clear_cache if args.force? formula.clear_cache if args.force?
already_fetched = formula.cached_download.exist? already_fetched = formula.cached_download.exist?
@ -242,7 +239,7 @@ module Homebrew
begin begin
download = formula.fetch(verify_download_integrity: false) download = formula.fetch(verify_download_integrity: false)
rescue DownloadError rescue DownloadError
retry if retry_fetch?(formula, args:) retry if retry_fetch?(formula)
raise raise
end end
@ -254,3 +251,5 @@ module Homebrew
formula.verify_download_integrity(download) formula.verify_download_integrity(download)
end end
end end
end
end

View File

@ -1,8 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cmd/fetch"
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
RSpec.describe "brew fetch" do RSpec.describe Homebrew::Cmd::FetchCmd do
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments"
it "downloads the Formula's URL", :integration_test do it "downloads the Formula's URL", :integration_test do