aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/utils
diff options
context:
space:
mode:
authorGautham Goli2017-10-12 00:29:19 +0530
committerGautham Goli2017-10-13 19:50:46 +0530
commit7fa51f71f1a8a21b905bafc1fb4106f0222d654f (patch)
tree098d2477a8262a5770310fed4693da31c4392e1c /Library/Homebrew/utils
parentafdd0e2437426ec85ff86e5b7562d3a6a69ba3e5 (diff)
parent56458f03fcc68ef6d8ee3ee4a7c1d16021aa5800 (diff)
downloadbrew-7fa51f71f1a8a21b905bafc1fb4106f0222d654f.tar.bz2
Merge branch 'master' into audit_line_rubocop_part_4_rebase_attempt_1
Diffstat (limited to 'Library/Homebrew/utils')
-rw-r--r--Library/Homebrew/utils/analytics.rb5
-rw-r--r--Library/Homebrew/utils/bottles.rb13
-rw-r--r--Library/Homebrew/utils/curl.rb51
-rw-r--r--Library/Homebrew/utils/fork.rb9
-rw-r--r--Library/Homebrew/utils/git.rb23
-rw-r--r--Library/Homebrew/utils/github.rb92
-rw-r--r--Library/Homebrew/utils/popen.rb14
-rw-r--r--Library/Homebrew/utils/ruby.sh13
-rw-r--r--Library/Homebrew/utils/shell.rb4
-rw-r--r--Library/Homebrew/utils/svn.rb4
10 files changed, 120 insertions, 108 deletions
diff --git a/Library/Homebrew/utils/analytics.rb b/Library/Homebrew/utils/analytics.rb
index a89995ba9..9766c14db 100644
--- a/Library/Homebrew/utils/analytics.rb
+++ b/Library/Homebrew/utils/analytics.rb
@@ -3,6 +3,11 @@ require "erb"
module Utils
module Analytics
class << self
+ def clear_anonymous_os_prefix_ci_cache
+ return unless instance_variable_defined?(:@anonymous_os_prefix_ci)
+ remove_instance_variable(:@anonymous_os_prefix_ci)
+ end
+
def os_prefix_ci
@anonymous_os_prefix_ci ||= begin
os = OS_VERSION
diff --git a/Library/Homebrew/utils/bottles.rb b/Library/Homebrew/utils/bottles.rb
index 927963bc1..66b5fb640 100644
--- a/Library/Homebrew/utils/bottles.rb
+++ b/Library/Homebrew/utils/bottles.rb
@@ -29,9 +29,11 @@ module Utils
end
def receipt_path(bottle_file)
- Utils.popen_read("tar", "-tzf", bottle_file).lines.map(&:chomp).find do |line|
+ path = Utils.popen_read("tar", "-tzf", bottle_file).lines.map(&:chomp).find do |line|
line =~ %r{.+/.+/INSTALL_RECEIPT.json}
end
+ raise "This bottle does not contain the file INSTALL_RECEIPT.json: #{bottle_file}" unless path
+ path
end
def resolve_formula_names(bottle_file)
@@ -52,6 +54,15 @@ module Utils
def resolve_version(bottle_file)
PkgVersion.parse receipt_path(bottle_file).split("/")[1]
end
+
+ def formula_contents(bottle_file,
+ name: resolve_formula_names(bottle_file)[0])
+ bottle_version = resolve_version bottle_file
+ formula_path = "#{name}/#{bottle_version}/.brew/#{name}.rb"
+ contents = Utils.popen_read "tar", "-xOzf", bottle_file, formula_path
+ raise BottleFormulaUnavailableError.new(bottle_file, formula_path) unless $CHILD_STATUS.success?
+ contents
+ end
end
class Bintray
diff --git a/Library/Homebrew/utils/curl.rb b/Library/Homebrew/utils/curl.rb
index 5a40ae846..7807d2034 100644
--- a/Library/Homebrew/utils/curl.rb
+++ b/Library/Homebrew/utils/curl.rb
@@ -1,42 +1,57 @@
require "pathname"
require "open3"
-def curl_args(options = {})
+def curl_executable
curl = Pathname.new ENV["HOMEBREW_CURL"]
curl = Pathname.new "/usr/bin/curl" unless curl.exist?
- raise "#{curl} is not executable" unless curl.exist? && curl.executable?
+ return curl if curl.executable?
+ raise "#{curl} is not executable"
+end
+def curl_args(*extra_args, show_output: false, user_agent: :default)
args = [
- curl.to_s,
- "--remote-time",
- "--location",
+ curl_executable.to_s,
+ "--show-error",
]
- case options[:user_agent]
- when :browser
- args << "--user-agent" << HOMEBREW_USER_AGENT_FAKE_SAFARI
+ args << "--user-agent" << case user_agent
+ when :browser, :fake
+ HOMEBREW_USER_AGENT_FAKE_SAFARI
+ when :default
+ HOMEBREW_USER_AGENT_CURL
else
- args << "--user-agent" << HOMEBREW_USER_AGENT_CURL
+ user_agent
end
- unless options[:show_output]
+ unless show_output
+ args << "--fail"
args << "--progress-bar" unless ARGV.verbose?
args << "--verbose" if ENV["HOMEBREW_CURL_VERBOSE"]
- args << "--fail"
args << "--silent" if !$stdout.tty? || ENV["TRAVIS"]
end
- args += options[:extra_args] if options[:extra_args]
- args
+ args + extra_args
end
def curl(*args)
- safe_system(*curl_args(extra_args: args))
+ safe_system(*curl_args(*args))
end
-def curl_output(*args)
- curl_args = curl_args(extra_args: args, show_output: true)
- Open3.popen3(*curl_args) do |_, stdout, stderr, wait_thread|
- [stdout.read, stderr.read, wait_thread.value]
+def curl_download(*args, to: nil, continue_at: "-", **options)
+ had_incomplete_download ||= File.exist?(to)
+ 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 == "-"
+ continue_at = 0
+ had_incomplete_download = false
+ retry
end
+
+ raise
+end
+
+def curl_output(*args, **options)
+ Open3.capture3(*curl_args(*args, show_output: true, **options))
end
diff --git a/Library/Homebrew/utils/fork.rb b/Library/Homebrew/utils/fork.rb
index 92f5bf899..57ddbfae2 100644
--- a/Library/Homebrew/utils/fork.rb
+++ b/Library/Homebrew/utils/fork.rb
@@ -14,7 +14,7 @@ module Utils
read.close
write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
yield
- rescue Exception => e
+ rescue Exception => e # rubocop:disable Lint/RescueException
Marshal.dump(e, write)
write.close
exit!
@@ -26,8 +26,11 @@ module Utils
ignore_interrupts(:quietly) do # the child will receive the interrupt and marshal it back
begin
socket = server.accept_nonblock
+ # rubocop:disable Lint/ShadowedException
+ # FIXME: https://github.com/bbatsov/rubocop/issues/4843
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
retry unless Process.waitpid(pid, Process::WNOHANG)
+ # rubocop:enable Lint/ShadowedException
else
socket.send_io(write)
socket.close
@@ -36,9 +39,9 @@ module Utils
data = read.read
read.close
Process.wait(pid) unless socket.nil?
- raise Marshal.load(data) unless data.nil? || data.empty?
+ raise Marshal.load(data) unless data.nil? || data.empty? # rubocop:disable Security/MarshalLoad
raise Interrupt if $CHILD_STATUS.exitstatus == 130
- raise "Suspicious failure" unless $CHILD_STATUS.success?
+ raise "Forked child process failed: #{$CHILD_STATUS}" unless $CHILD_STATUS.success?
end
end
end
diff --git a/Library/Homebrew/utils/git.rb b/Library/Homebrew/utils/git.rb
index 43d93b64e..f1113af66 100644
--- a/Library/Homebrew/utils/git.rb
+++ b/Library/Homebrew/utils/git.rb
@@ -16,8 +16,7 @@ module Git
def last_revision_of_file(repo, file, before_commit: nil)
relative_file = Pathname(file).relative_path_from(repo)
- commit_hash = last_revision_commit_of_file(repo, file, before_commit: before_commit)
-
+ commit_hash = last_revision_commit_of_file(repo, relative_file, before_commit: before_commit)
out, = Open3.capture3(
HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo,
"show", "#{commit_hash}:#{relative_file}"
@@ -28,8 +27,7 @@ end
module Utils
def self.git_available?
- return @git if instance_variable_defined?(:@git)
- @git = quiet_system HOMEBREW_SHIMS_PATH/"scm/git", "--version"
+ @git ||= quiet_system HOMEBREW_SHIMS_PATH/"scm/git", "--version"
end
def self.git_path
@@ -50,21 +48,20 @@ module Utils
return if git_available?
# we cannot install brewed git if homebrew/core is unavailable.
- raise "Git is unavailable" unless CoreTap.instance.installed?
-
- begin
- oh1 "Installing git"
- safe_system HOMEBREW_BREW_FILE, "install", "git"
- rescue
- raise "Git is unavailable"
+ if CoreTap.instance.installed?
+ begin
+ oh1 "Installing git"
+ safe_system HOMEBREW_BREW_FILE, "install", "git"
+ rescue
+ raise "Git is unavailable"
+ end
end
- clear_git_available_cache
raise "Git is unavailable" unless git_available?
end
def self.clear_git_available_cache
- remove_instance_variable(:@git) if instance_variable_defined?(:@git)
+ @git = nil
@git_path = nil
@git_version = nil
end
diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb
index 1a781cee6..df0811e95 100644
--- a/Library/Homebrew/utils/github.rb
+++ b/Library/Homebrew/utils/github.rb
@@ -86,15 +86,9 @@ module GitHub
def api_credentials_type
token, username = api_credentials
- if token && !token.empty?
- if username && !username.empty?
- :keychain
- else
- :environment
- end
- else
- :none
- end
+ return :none if !token || token.empty?
+ return :keychain if !username || username.empty?
+ :environment
end
def api_credentials_error_message(response_headers, needed_scopes)
@@ -133,7 +127,7 @@ module GitHub
def open(url, data: nil, scopes: [].freeze)
# This is a no-op if the user is opting out of using the GitHub API.
- return if ENV["HOMEBREW_NO_GITHUB_API"]
+ return block_given? ? yield({}) : {} if ENV["HOMEBREW_NO_GITHUB_API"]
args = %W[--header application/vnd.github.v3+json --write-out \n%{http_code}]
args += curl_args
@@ -166,7 +160,7 @@ module GitHub
args += ["--dump-header", headers_tmpfile.path]
- output, errors, status = curl_output(url.to_s, *args)
+ output, errors, status = curl_output(url.to_s, "--location", *args)
output, _, http_code = output.rpartition("\n")
output, _, http_code = output.rpartition("\n") if http_code == "000"
headers = headers_tmpfile.read
@@ -227,72 +221,60 @@ module GitHub
end
end
- def issues_matching(query, qualifiers = {})
- uri = URI.parse("#{API_URL}/search/issues")
- uri.query = build_query_string(query, qualifiers)
- open(uri) { |json| json["items"] }
+ def search_issues(query, **qualifiers)
+ search("issues", query, **qualifiers)
end
def repository(user, repo)
- open(URI.parse("#{API_URL}/repos/#{user}/#{repo}"))
- end
-
- def search_code(*params)
- uri = URI.parse("#{API_URL}/search/code")
- uri.query = "q=#{uri_escape(params.join(" "))}"
- open(uri) { |json| json["items"] }
+ open(url_to("repos", user, repo))
end
- def build_query_string(query, qualifiers)
- s = "q=#{uri_escape(query)}+"
- s << build_search_qualifier_string(qualifiers)
- s << "&per_page=100"
- end
-
- def build_search_qualifier_string(qualifiers)
- {
- repo: "Homebrew/homebrew-core",
- in: "title",
- }.update(qualifiers).map do |qualifier, value|
- "#{qualifier}:#{value}"
- end.join("+")
- end
-
- def uri_escape(query)
- if URI.respond_to?(:encode_www_form_component)
- URI.encode_www_form_component(query)
- else
- require "erb"
- ERB::Util.url_encode(query)
- end
+ def search_code(**qualifiers)
+ search("code", **qualifiers)
end
def issues_for_formula(name, options = {})
tap = options[:tap] || CoreTap.instance
- issues_matching(name, state: "open", repo: "#{tap.user}/homebrew-#{tap.repo}")
+ search_issues(name, state: "open", repo: "#{tap.user}/homebrew-#{tap.repo}")
end
def print_pull_requests_matching(query)
- return [] if ENV["HOMEBREW_NO_GITHUB_API"]
-
- open_or_closed_prs = issues_matching(query, type: "pr")
+ open_or_closed_prs = search_issues(query, type: "pr", user: "Homebrew")
open_prs = open_or_closed_prs.select { |i| i["state"] == "open" }
- if !open_prs.empty?
+ prs = if !open_prs.empty?
puts "Open pull requests:"
- prs = open_prs
- elsif !open_or_closed_prs.empty?
- puts "Closed pull requests:"
- prs = open_or_closed_prs
+ open_prs
else
- return
+ puts "Closed pull requests:" unless open_or_closed_prs.empty?
+ open_or_closed_prs
end
prs.each { |i| puts "#{i["title"]} (#{i["html_url"]})" }
end
def private_repo?(full_name)
- uri = URI.parse("#{API_URL}/repos/#{full_name}")
+ uri = url_to "repos", full_name
open(uri) { |json| json["private"] }
end
+
+ def query_string(*main_params, **qualifiers)
+ params = main_params
+
+ params += qualifiers.flat_map do |key, value|
+ Array(value).map { |v| "#{key}:#{v}" }
+ end
+
+ "q=#{URI.encode_www_form_component(params.join(" "))}&per_page=100"
+ end
+
+ def url_to(*subroutes)
+ URI.parse([API_URL, *subroutes].join("/"))
+ end
+
+ def search(entity, *queries, **qualifiers)
+ uri = url_to "search", entity
+ uri.query = query_string(*queries, **qualifiers)
+ open(uri) { |json| json.fetch("items", []) }
+ end
end
diff --git a/Library/Homebrew/utils/popen.rb b/Library/Homebrew/utils/popen.rb
index 4e03711a1..2fa3ade46 100644
--- a/Library/Homebrew/utils/popen.rb
+++ b/Library/Homebrew/utils/popen.rb
@@ -1,20 +1,20 @@
module Utils
- def self.popen_read(*args, &block)
- popen(args, "rb", &block)
+ def self.popen_read(*args, **options, &block)
+ popen(args, "rb", options, &block)
end
- def self.popen_write(*args, &block)
- popen(args, "wb", &block)
+ def self.popen_write(*args, **options, &block)
+ popen(args, "wb", options, &block)
end
- def self.popen(args, mode)
+ def self.popen(args, mode, options = {})
IO.popen("-", mode) do |pipe|
if pipe
return pipe.read unless block_given?
yield pipe
else
- $stderr.reopen("/dev/null", "w")
- exec(*args)
+ options[:err] ||= :close unless ENV["HOMEBREW_STDERR"]
+ exec(*args, options)
end
end
end
diff --git a/Library/Homebrew/utils/ruby.sh b/Library/Homebrew/utils/ruby.sh
index 6945c068b..9a3ab2e81 100644
--- a/Library/Homebrew/utils/ruby.sh
+++ b/Library/Homebrew/utils/ruby.sh
@@ -2,7 +2,8 @@ setup-ruby-path() {
local vendor_dir
local vendor_ruby_current_version
local vendor_ruby_path
- local ruby_version_major
+ local ruby_old_version
+ local minimum_ruby_version="2.3.3"
vendor_dir="$HOMEBREW_LIBRARY/Homebrew/vendor"
vendor_ruby_current_version="$vendor_dir/portable-ruby/current"
@@ -21,7 +22,7 @@ setup-ruby-path() {
if [[ $(readlink "$vendor_ruby_current_version") != "$(<"$vendor_dir/portable-ruby-version")" ]]
then
- if ! brew vendor-install ruby --quiet
+ if ! brew vendor-install ruby
then
onoe "Failed to upgrade vendor Ruby."
fi
@@ -36,14 +37,12 @@ setup-ruby-path() {
if [[ -n "$HOMEBREW_RUBY_PATH" ]]
then
- ruby_version_major="$("$HOMEBREW_RUBY_PATH" --version)"
- ruby_version_major="${ruby_version_major#ruby }"
- ruby_version_major="${ruby_version_major%%.*}"
+ ruby_old_version="$("$HOMEBREW_RUBY_PATH" -rrubygems -e "puts Gem::Version.new('$minimum_ruby_version') > Gem::Version.new(RUBY_VERSION)")"
fi
- if [[ "$ruby_version_major" != "2" || -n "$HOMEBREW_FORCE_VENDOR_RUBY" ]]
+ if [[ "$ruby_old_version" == "true" || -n "$HOMEBREW_FORCE_VENDOR_RUBY" ]]
then
- brew vendor-install ruby --quiet
+ brew vendor-install ruby
if [[ ! -x "$vendor_ruby_path" ]]
then
odie "Failed to install vendor Ruby."
diff --git a/Library/Homebrew/utils/shell.rb b/Library/Homebrew/utils/shell.rb
index 5327f6ecf..8c1c5f984 100644
--- a/Library/Homebrew/utils/shell.rb
+++ b/Library/Homebrew/utils/shell.rb
@@ -51,8 +51,6 @@ module Utils
end
end
- private
-
SHELL_PROFILE_MAP = {
bash: "~/.bash_profile",
csh: "~/.cshrc",
@@ -65,8 +63,6 @@ module Utils
UNSAFE_SHELL_CHAR = %r{([^A-Za-z0-9_\-.,:/@\n])}
- module_function
-
def csh_quote(str)
# ruby's implementation of shell_escape
str = str.to_s
diff --git a/Library/Homebrew/utils/svn.rb b/Library/Homebrew/utils/svn.rb
index fb49ac2e9..150b7eee7 100644
--- a/Library/Homebrew/utils/svn.rb
+++ b/Library/Homebrew/utils/svn.rb
@@ -1,4 +1,8 @@
module Utils
+ def self.clear_svn_version_cache
+ remove_instance_variable(:@svn) if instance_variable_defined?(:@svn)
+ end
+
def self.svn_available?
return @svn if instance_variable_defined?(:@svn)
@svn = quiet_system HOMEBREW_SHIMS_PATH/"scm/svn", "--version"