58 lines
1.5 KiB
Ruby
Raw Normal View History

require "pathname"
require "open3"
2017-08-08 18:10:13 +02:00
def curl_executable
curl = Pathname.new ENV["HOMEBREW_CURL"]
curl = Pathname.new "/usr/bin/curl" unless curl.exist?
2017-08-08 18:10:13 +02:00
return curl if curl.executable?
raise "#{curl} is not executable"
end
2017-08-08 18:10:13 +02:00
def curl_args(*extra_args, show_output: false, user_agent: :default)
args = [
2017-08-08 18:10:13 +02:00
curl_executable.to_s,
"--show-error",
]
2017-08-08 18:10:13 +02:00
args << "--user-agent" << case user_agent
when :browser, :fake
HOMEBREW_USER_AGENT_FAKE_SAFARI
when :default
HOMEBREW_USER_AGENT_CURL
else
2017-08-08 18:10:13 +02:00
user_agent
end
2017-08-08 18:10:13 +02:00
unless show_output
args << "--fail"
args << "--progress-bar" unless ARGV.verbose?
args << "--verbose" if ENV["HOMEBREW_CURL_VERBOSE"]
args << "--silent" if !$stdout.tty? || ENV["TRAVIS"]
end
2017-08-08 18:10:13 +02:00
args + extra_args
end
def curl(*args)
2017-08-08 18:10:13 +02:00
safe_system(*curl_args(*args))
2017-08-04 16:24:29 +02:00
end
2017-08-21 19:32:16 +02:00
def curl_download(*args, to: nil, continue_at: "-", **options)
had_incomplete_download ||= File.exist?(to)
2017-08-21 19:32:16 +02:00
curl("--location", "--remote-time", "--continue-at", continue_at.to_s, "--output", to, *args, **options)
rescue ErrorDuringExecution
# `curl` error 33: HTTP server doesn't seem to support byte ranges. Cannot resume.
# HTTP status 416: Requested range not satisfiable
if ($CHILD_STATUS.exitstatus == 33 || had_incomplete_download) && continue_at == "-"
2017-08-21 19:32:16 +02:00
continue_at = 0
had_incomplete_download = false
retry
end
raise
2017-08-08 18:10:13 +02:00
end
def curl_output(*args, **options)
Open3.capture3(*curl_args(*args, show_output: true, **options))
end