172 lines
5.0 KiB
Ruby
Raw Normal View History

2016-05-09 15:37:01 +02:00
#: * `fetch` [`--force`] [`--retry`] [`-v`] [`--devel`|`--HEAD`] [`--deps`] [`--build-from-source`|`--force-bottle`] <formulae>:
2016-04-08 16:28:43 +02:00
#: Download the source packages for the given <formulae>.
#: For tarballs, also print SHA-256 checksums.
2016-04-08 16:28:43 +02:00
#:
#: If `--HEAD` or `--devel` is passed, fetch that version instead of the
#: stable version.
#:
2016-05-06 11:03:38 -04:00
#: If `-v` is passed, do a verbose VCS checkout, if the URL represents a VCS.
2016-04-08 16:28:43 +02:00
#: This is useful for seeing if an existing VCS cache has been updated.
#:
2017-04-02 10:14:21 +01:00
#: If `--force` (or `-f`) is passed, remove a previously cached version and re-fetch.
2016-04-08 16:28:43 +02:00
#:
2016-05-09 15:37:01 +02:00
#: If `--retry` is passed, retry if a download fails or re-download if the
#: checksum of a previously cached version no longer matches.
#:
2016-04-08 16:28:43 +02:00
#: If `--deps` is passed, also download dependencies for any listed <formulae>.
#:
#: If `--build-from-source` (or `-s`) or `--build-bottle` is passed, download the
#: source rather than a bottle.
2016-04-08 16:28:43 +02:00
#:
#: If `--force-bottle` is passed, download a bottle if it exists for the
#: current or newest version of macOS, even if it would not be used during
#: installation.
2016-04-08 16:28:43 +02:00
require "formula"
require "fetch"
2018-10-27 23:44:32 +05:30
require "cli_parser"
2011-03-12 09:40:10 -08:00
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
2018-10-27 23:44:32 +05:30
def fetch_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`fetch` [<options>] <formulae>
Download the source packages for the given <formulae>.
For tarballs, also print SHA-256 checksums.
EOS
switch "--HEAD",
description: "Fetch HEAD version instead of stable version."
switch "--devel",
description: "Fetch devel version instead of stable version."
switch :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 :force,
description: "Remove a previously cached version and re-fetch."
switch "--retry",
description: "Retry if a download fails or re-download if the checksum of a previously cached "\
"version no longer matches."
switch "--deps",
description: "Download dependencies for any listed <formulae>."
switch "-s", "--build-from-source",
description: "Download the source for rather than a bottle."
switch "--build-bottle",
description: "Download the source (for eventual bottling) rather than a bottle."
2018-10-27 23:44:32 +05:30
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 :verbose
switch :debug
conflicts "--devel", "--HEAD"
conflicts "--build-from-source", "--build-bottle", "--force-bottle"
2018-10-27 23:44:32 +05:30
end
end
2011-03-12 09:40:10 -08:00
def fetch
2018-10-27 23:44:32 +05:30
fetch_args.parse
raise FormulaUnspecifiedError if ARGV.named.empty?
2018-10-27 23:44:32 +05:30
if args.deps?
bucket = []
ARGV.formulae.each do |f|
bucket << f
bucket.concat f.recursive_dependencies.map(&:to_formula)
end
bucket.uniq!
else
bucket = ARGV.formulae
end
puts "Fetching: #{bucket * ", "}" if bucket.size > 1
bucket.each do |f|
f.print_tap_action verb: "Fetching"
fetched_bottle = false
if Fetch.fetch_bottle?(f)
begin
fetch_formula(f.bottle)
rescue Interrupt
raise
rescue => e
raise if ARGV.homebrew_developer?
2018-09-17 02:45:00 +02:00
fetched_bottle = false
onoe e.message
opoo "Bottle fetch failed: fetching the source."
else
fetched_bottle = true
end
end
2016-09-10 10:24:56 +01:00
next if fetched_bottle
2018-09-17 02:45:00 +02:00
2016-09-10 10:24:56 +01:00
fetch_formula(f)
2018-01-21 23:41:54 -08:00
f.resources.each do |r|
fetch_resource(r)
r.patches.each { |p| fetch_patch(p) if p.external? }
end
2016-09-10 10:24:56 +01:00
f.patchlist.each { |p| fetch_patch(p) if p.external? }
end
end
def fetch_resource(r)
puts "Resource: #{r.name}"
fetch_fetchable r
rescue ChecksumMismatchError => e
2014-03-08 17:21:37 +00:00
retry if retry_fetch? r
opoo "Resource #{r.name} reports different #{e.hash_type}: #{e.expected}"
end
def fetch_formula(f)
fetch_fetchable f
rescue ChecksumMismatchError => e
retry if retry_fetch? f
opoo "Formula reports different #{e.hash_type}: #{e.expected}"
end
def fetch_patch(p)
2014-03-13 19:51:23 -05:00
fetch_fetchable p
rescue ChecksumMismatchError => e
Homebrew.failed = true
opoo "Patch reports different #{e.hash_type}: #{e.expected}"
end
def retry_fetch?(f)
2014-08-22 22:55:10 -05:00
@fetch_failed ||= Set.new
2018-10-27 23:44:32 +05:30
if args.retry? && @fetch_failed.add?(f)
2014-08-22 22:55:10 -05:00
ohai "Retrying download"
f.clear_cache
true
else
Homebrew.failed = true
2014-08-22 22:55:10 -05:00
false
end
end
def fetch_fetchable(f)
2018-10-27 23:44:32 +05:30
f.clear_cache if args.force?
already_fetched = f.cached_download.exist?
begin
download = f.fetch
2014-08-22 22:55:10 -05:00
rescue DownloadError
retry if retry_fetch? f
2014-08-22 22:55:10 -05:00
raise
end
2011-03-12 09:40:10 -08:00
return unless download.file?
2011-03-12 09:40:10 -08:00
puts "Downloaded to: #{download}" unless already_fetched
puts Checksum::TYPES.map { |t| "#{t.to_s.upcase}: #{download.send(t)}" }
2013-05-16 14:06:26 -05:00
f.verify_download_integrity(download)
2011-03-12 09:40:10 -08:00
end
end