| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2018-06-05 23:19:18 -04:00
										 |  |  | require "fetch" | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  | require "cask/download" | 
					
						
							| 
									
										
										
										
											2011-03-12 09:40:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 02:43:22 +02:00
										 |  |  |   extend Fetch | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-06 01:25:34 +08:00
										 |  |  |   FETCH_MAX_TRIES = 5
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  |   def fetch_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |         Download a bottle (if available) or source packages for <formula>e | 
					
						
							|  |  |  |         and binaries for <cask>s. For files, also print SHA-256 checksums. | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2022-06-28 11:20:22 -04:00
										 |  |  |       flag "--bottle-tag=", | 
					
						
							| 
									
										
										
										
											2021-07-10 21:17:33 +02:00
										 |  |  |            description: "Download a bottle for given tag." | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  |       switch "--HEAD", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Fetch HEAD version instead of stable version." | 
					
						
							| 
									
										
										
										
											2020-07-27 03:59:52 +02:00
										 |  |  |       switch "-f", "--force", | 
					
						
							| 
									
										
										
										
											2019-08-09 14:23:04 -04:00
										 |  |  |              description: "Remove a previously cached version and re-fetch." | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |       switch "-v", "--verbose", | 
					
						
							| 
									
										
										
										
											2022-06-28 10:09:59 +01:00
										 |  |  |              description: "Do a verbose VCS checkout, if the URL represents a VCS. This is useful for " \ | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |                           "seeing if an existing VCS cache has been updated." | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  |       switch "--retry", | 
					
						
							| 
									
										
										
										
											2022-06-28 10:09:59 +01:00
										 |  |  |              description: "Retry if downloading fails or re-download if the checksum of a previously cached " \ | 
					
						
							| 
									
										
										
										
											2022-09-06 01:25:34 +08:00
										 |  |  |                           "version no longer matches. Tries at most #{FETCH_MAX_TRIES} times with " \ | 
					
						
							|  |  |  |                           "exponential backoff." | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  |       switch "--deps", | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |              description: "Also download dependencies for any listed <formula>." | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  |       switch "-s", "--build-from-source", | 
					
						
							| 
									
										
										
										
											2019-08-19 22:44:50 -04:00
										 |  |  |              description: "Download source packages rather than a bottle." | 
					
						
							| 
									
										
										
										
											2019-01-29 11:35:04 +00:00
										 |  |  |       switch "--build-bottle", | 
					
						
							| 
									
										
										
										
											2019-08-19 22:44:50 -04:00
										 |  |  |              description: "Download source packages (for eventual bottling) rather than a bottle." | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  |       switch "--force-bottle", | 
					
						
							| 
									
										
										
										
											2022-06-28 10:09:59 +01:00
										 |  |  |              description: "Download a bottle if it exists for the current or newest version of macOS, " \ | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |                           "even if it would not be used during installation." | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |       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." | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-10 21:17:33 +02:00
										 |  |  |       conflicts "--build-from-source", "--build-bottle", "--force-bottle", "--bottle-tag" | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |       conflicts "--cask", "--HEAD" | 
					
						
							|  |  |  |       conflicts "--cask", "--deps" | 
					
						
							|  |  |  |       conflicts "--cask", "-s" | 
					
						
							|  |  |  |       conflicts "--cask", "--build-bottle" | 
					
						
							|  |  |  |       conflicts "--cask", "--force-bottle" | 
					
						
							| 
									
										
										
										
											2021-07-10 21:17:33 +02:00
										 |  |  |       conflicts "--cask", "--bottle-tag" | 
					
						
							| 
									
										
										
										
											2020-11-27 11:41:08 -05:00
										 |  |  |       conflicts "--formula", "--cask" | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |       named_args [:formula, :cask], min: 1
 | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-12 09:40:10 -08:00
										 |  |  |   def fetch | 
					
						
							| 
									
										
										
										
											2020-07-26 22:00:38 +02:00
										 |  |  |     args = fetch_args.parse | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |     bucket = if args.deps? | 
					
						
							| 
									
										
										
										
											2022-06-15 16:32:14 -04:00
										 |  |  |       args.named.to_formulae_and_casks.flat_map do |formula_or_cask| | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |         case formula_or_cask | 
					
						
							|  |  |  |         when Formula | 
					
						
							|  |  |  |           f = formula_or_cask | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-15 16:32:14 -04:00
										 |  |  |           [f, *f.recursive_dependencies.map(&:to_formula)] | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |         else | 
					
						
							|  |  |  |           formula_or_cask | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2011-04-14 14:57:21 -07:00
										 |  |  |       end | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2022-06-15 16:32:14 -04:00
										 |  |  |       args.named.to_formulae_and_casks | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |     end.uniq | 
					
						
							| 
									
										
										
										
											2011-04-14 14:57:21 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     puts "Fetching: #{bucket * ", "}" if bucket.size > 1
 | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |     bucket.each do |formula_or_cask| | 
					
						
							|  |  |  |       case formula_or_cask | 
					
						
							|  |  |  |       when Formula | 
					
						
							|  |  |  |         f = formula_or_cask | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         f.print_tap_action verb: "Fetching" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         fetched_bottle = false | 
					
						
							|  |  |  |         if fetch_bottle?(f, args: args) | 
					
						
							|  |  |  |           begin | 
					
						
							| 
									
										
										
										
											2021-04-02 12:44:07 +01:00
										 |  |  |             f.clear_cache if args.force? | 
					
						
							| 
									
										
										
										
											2021-03-31 20:55:35 +01:00
										 |  |  |             f.fetch_bottle_tab | 
					
						
							| 
									
										
										
										
											2021-09-20 20:18:19 +01:00
										 |  |  |             fetch_formula(f.bottle_for_tag(args.bottle_tag&.to_sym), args: args) | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |           rescue Interrupt | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |           rescue => e | 
					
						
							|  |  |  |             raise if Homebrew::EnvConfig.developer? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             fetched_bottle = false | 
					
						
							|  |  |  |             onoe e.message | 
					
						
							| 
									
										
										
										
											2021-01-26 15:21:24 -05:00
										 |  |  |             opoo "Bottle fetch failed, fetching the source instead." | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |           else | 
					
						
							|  |  |  |             fetched_bottle = true | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2015-12-03 11:00:39 +02:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |         next if fetched_bottle | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |         fetch_formula(f, args: args) | 
					
						
							| 
									
										
										
										
											2018-01-21 23:41:54 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |         f.resources.each do |r| | 
					
						
							|  |  |  |           fetch_resource(r, args: args) | 
					
						
							|  |  |  |           r.patches.each { |p| fetch_patch(p, args: args) if p.external? } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         f.patchlist.each { |p| fetch_patch(p, args: args) if p.external? } | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         cask = formula_or_cask | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-02 12:25:44 +01:00
										 |  |  |         quarantine = args.quarantine? | 
					
						
							|  |  |  |         quarantine = true if quarantine.nil? | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-02 12:25:44 +01:00
										 |  |  |         download = Cask::Download.new(cask, quarantine: quarantine) | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |         fetch_cask(download, args: args) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-08-06 19:53:43 -07:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-01-11 18:03:51 -06:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-04-14 14:57:21 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 19:14:25 +02:00
										 |  |  |   def fetch_resource(r, args:) | 
					
						
							| 
									
										
										
										
											2013-08-06 19:53:43 -07:00
										 |  |  |     puts "Resource: #{r.name}" | 
					
						
							| 
									
										
										
										
											2020-07-31 19:14:25 +02:00
										 |  |  |     fetch_fetchable r, args: args | 
					
						
							| 
									
										
										
										
											2013-08-06 19:53:43 -07:00
										 |  |  |   rescue ChecksumMismatchError => e | 
					
						
							| 
									
										
										
										
											2020-07-31 19:14:25 +02:00
										 |  |  |     retry if retry_fetch?(r, args: args) | 
					
						
							| 
									
										
										
										
											2021-01-07 08:33:57 +01:00
										 |  |  |     opoo "Resource #{r.name} reports different sha256: #{e.expected}" | 
					
						
							| 
									
										
										
										
											2013-08-06 19:53:43 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 19:14:25 +02:00
										 |  |  |   def fetch_formula(f, args:) | 
					
						
							|  |  |  |     fetch_fetchable f, args: args | 
					
						
							| 
									
										
										
										
											2013-08-06 19:53:43 -07:00
										 |  |  |   rescue ChecksumMismatchError => e | 
					
						
							| 
									
										
										
										
											2020-07-31 19:14:25 +02:00
										 |  |  |     retry if retry_fetch?(f, args: args) | 
					
						
							| 
									
										
										
										
											2021-01-07 08:33:57 +01:00
										 |  |  |     opoo "Formula reports different sha256: #{e.expected}" | 
					
						
							| 
									
										
										
										
											2013-08-06 19:53:43 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |   def fetch_cask(cask_download, args:) | 
					
						
							|  |  |  |     fetch_fetchable cask_download, args: args | 
					
						
							|  |  |  |   rescue ChecksumMismatchError => e | 
					
						
							|  |  |  |     retry if retry_fetch?(cask_download, args: args) | 
					
						
							| 
									
										
										
										
											2021-01-07 08:33:57 +01:00
										 |  |  |     opoo "Cask reports different sha256: #{e.expected}" | 
					
						
							| 
									
										
										
										
											2020-11-19 18:12:16 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 19:14:25 +02:00
										 |  |  |   def fetch_patch(p, args:) | 
					
						
							|  |  |  |     fetch_fetchable p, args: args | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |   rescue ChecksumMismatchError => e | 
					
						
							| 
									
										
										
										
											2021-01-07 08:33:57 +01:00
										 |  |  |     opoo "Patch reports different sha256: #{e.expected}" | 
					
						
							| 
									
										
										
										
											2020-07-29 19:41:39 -04:00
										 |  |  |     Homebrew.failed = true | 
					
						
							| 
									
										
										
										
											2014-03-13 19:51:23 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 19:14:25 +02:00
										 |  |  |   def retry_fetch?(f, args:) | 
					
						
							| 
									
										
										
										
											2022-09-06 18:35:34 +08:00
										 |  |  |     @fetch_tries ||= Hash.new { |h, k| h[k] = 1 } | 
					
						
							|  |  |  |     if args.retry? && (@fetch_tries[f] < FETCH_MAX_TRIES) | 
					
						
							|  |  |  |       wait = 2 ** @fetch_tries[f] | 
					
						
							|  |  |  |       remaining = FETCH_MAX_TRIES - @fetch_tries[f] | 
					
						
							| 
									
										
										
										
											2022-09-06 01:25:34 +08:00
										 |  |  |       what = "try".pluralize(remaining) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       ohai "Retrying download in #{wait}s... (#{remaining} #{what} left)" | 
					
						
							|  |  |  |       sleep wait | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-22 22:55:10 -05:00
										 |  |  |       f.clear_cache | 
					
						
							| 
									
										
										
										
											2022-09-06 18:35:34 +08:00
										 |  |  |       @fetch_tries[f] += 1
 | 
					
						
							| 
									
										
										
										
											2014-08-22 22:55:10 -05:00
										 |  |  |       true | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2014-03-01 12:28:45 +00:00
										 |  |  |       Homebrew.failed = true | 
					
						
							| 
									
										
										
										
											2014-08-22 22:55:10 -05:00
										 |  |  |       false | 
					
						
							| 
									
										
										
										
											2014-03-01 12:28:45 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 19:14:25 +02:00
										 |  |  |   def fetch_fetchable(f, args:) | 
					
						
							| 
									
										
										
										
											2018-10-27 23:44:32 +05:30
										 |  |  |     f.clear_cache if args.force? | 
					
						
							| 
									
										
										
										
											2013-10-31 14:28:49 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     already_fetched = f.cached_download.exist? | 
					
						
							| 
									
										
										
										
											2014-08-16 08:48:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     begin | 
					
						
							| 
									
										
										
										
											2019-06-15 17:22:45 +01:00
										 |  |  |       download = f.fetch(verify_download_integrity: false) | 
					
						
							| 
									
										
										
										
											2014-08-22 22:55:10 -05:00
										 |  |  |     rescue DownloadError | 
					
						
							| 
									
										
										
										
											2020-07-31 19:14:25 +02:00
										 |  |  |       retry if retry_fetch?(f, args: args) | 
					
						
							| 
									
										
										
										
											2014-08-22 22:55:10 -05:00
										 |  |  |       raise | 
					
						
							| 
									
										
										
										
											2014-08-16 08:48:28 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-03-12 09:40:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 14:06:26 -05:00
										 |  |  |     return unless download.file? | 
					
						
							| 
									
										
										
										
											2011-03-12 09:40:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-31 14:28:49 -05:00
										 |  |  |     puts "Downloaded to: #{download}" unless already_fetched | 
					
						
							| 
									
										
										
										
											2021-01-07 08:33:57 +01:00
										 |  |  |     puts "SHA256: #{download.sha256}" | 
					
						
							| 
									
										
										
										
											2013-01-11 18:03:51 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 14:06:26 -05:00
										 |  |  |     f.verify_download_integrity(download) | 
					
						
							| 
									
										
										
										
											2011-03-12 09:40:10 -08:00
										 |  |  |   end | 
					
						
							|  |  |  | end |