Skip to content

Commit

Permalink
env_config: use only one pip index URL
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Sep 15, 2022
1 parent 800fdcd commit a371682
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
17 changes: 10 additions & 7 deletions Library/Homebrew/env_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ module EnvConfig
"outdated.",
boolean: true,
},
HOMEBREW_PIP_INDEX_URLS: {
description: "A space-separated list of URLs. If set, `brew install <formula>` will use these URLs to " \
"download PyPI package resources.",
default_text: '`"${PIP_INDEX_URL} ${PIP_EXTRA_INDEX_URL}"`',
HOMEBREW_PIP_INDEX_URL: {
description: "A URL string. If set, `brew install <formula>` will use this URL to download PyPI package " \
"resources.",
default_text: "`$PIP_INDEX_URL` or `$PIP_EXTRA_INDEX_URL`, only the first non-empty URL will be used.",
},
HOMEBREW_PRY: {
description: "If set, use Pry for the `brew irb` command.",
Expand Down Expand Up @@ -480,9 +480,12 @@ def cask_opts_require_sha?
cask_opts.include?("--require-sha")
end

sig { returns(T::Array[String]) }
def pip_index_urls
ENV["HOMEBREW_PIP_INDEX_URLS"].to_s.split.map { |url| url.chomp("/") }.uniq
sig { returns(T.nilable(String)) }
def pip_index_url
pip_index_urls = ENV["HOMEBREW_PIP_INDEX_URL"].to_s.split.map { |url| url.chomp("/") }
return if pip_index_urls.empty?

pip_index_urls.first
end
end
end
8 changes: 3 additions & 5 deletions Library/Homebrew/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,9 @@ def determine_url_mirrors
end
end

# PyPI (PEP 503): https://peps.python.org/pep-0503
if url.start_with?("https://files.pythonhosted.org/packages")
Homebrew::EnvConfig.pip_index_urls.each do |pip_index_url|
extra_urls << url.sub("https://files.pythonhosted.org", pip_index_url.chomp("/simple"))
end
# PyPI packages (PEP 503): https://peps.python.org/pep-0503
if url.start_with?("https://files.pythonhosted.org/packages") && Homebrew::EnvConfig.pip_index_url.present?
extra_urls << url.sub("https://files.pythonhosted.org", Homebrew::EnvConfig.pip_index_url.chomp("/simple"))
end

[*extra_urls, url, *mirrors].uniq
Expand Down
29 changes: 11 additions & 18 deletions Library/Homebrew/test/env_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,19 @@
end
end

describe ".pip_index_urls" do
it "returns empty array if unset or empty" do
ENV["HOMEBREW_PIP_INDEX_URLS"] = nil
expect(env_config.pip_index_urls).to be_empty
ENV["HOMEBREW_PIP_INDEX_URLS"] = ""
expect(env_config.pip_index_urls).to be_empty
describe ".pip_index_url" do
it "returns empty string if unset or empty" do
ENV["HOMEBREW_PIP_INDEX_URL"] = nil
expect(env_config.pip_index_url).to be_empty
ENV["HOMEBREW_PIP_INDEX_URL"] = ""
expect(env_config.pip_index_url).to be_empty
ENV["HOMEBREW_PIP_INDEX_URL"] = " "
expect(env_config.pip_index_url).to be_empty
end

it "returns all elements in `HOMEBREW_PIP_INDEX_URLS`" do
ENV["HOMEBREW_PIP_INDEX_URLS"] = "https://foo.com/simple"
expect(env_config.pip_index_urls).to eq(["https://foo.com/simple"])

ENV["HOMEBREW_PIP_INDEX_URLS"] = "https://bar.com/simple https://baz.com/simple"
expect(env_config.pip_index_urls).to eq(["https://bar.com/simple", "https://baz.com/simple"])
end

it "removes duplicate index urls" do
ENV["HOMEBREW_PIP_INDEX_URLS"] = "https://foo.com/simple https://foo.com/simple https://bar.com/simple " \
"https://baz.com/simple https://bar.com/simple/"
expect(env_config.pip_index_urls).to eq(["https://foo.com/simple", "https://bar.com/simple", "https://baz.com/simple"])
it "returns the first index url present" do
ENV["HOMEBREW_PIP_INDEX_URL"] = "https://foo.com/simple/ https://bar.com/simple http://baz.com/simple"
expect(env_config.pip_index_url).to eq("https://foo.com/simple")
end
end
end
9 changes: 4 additions & 5 deletions bin/brew
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ fi
# set from user environment
# shellcheck disable=SC2154
# Use `PIP_INDEX_URL` and `PIP_EXTRA_INDEX_URL` to speed up PyPI resource
# downloading. `HOMEBREW_PIP_INDEX_URLS` will be set to the concatenation of
# these two values with an extra space in between.
if [[ -z "${HOMEBREW_PIP_INDEX_URLS}" ]]
# downloading. Only the first non-empty URL is used.
if [[ -z "${HOMEBREW_PIP_INDEX_URL}" ]]
then
export HOMEBREW_PIP_INDEX_URLS="${PIP_INDEX_URL:-}"
export HOMEBREW_PIP_INDEX_URL="${PIP_INDEX_URL:-}"
if [[ -n "${PIP_EXTRA_INDEX_URL}" ]]
then
export HOMEBREW_PIP_INDEX_URLS="${HOMEBREW_PIP_INDEX_URLS:+"${HOMEBREW_PIP_INDEX_URLS} "}${PIP_EXTRA_INDEX_URL}"
export HOMEBREW_PIP_INDEX_URL="${HOMEBREW_PIP_INDEX_URL:+"${HOMEBREW_PIP_INDEX_URL} "}${PIP_EXTRA_INDEX_URL}"
fi
fi

Expand Down

0 comments on commit a371682

Please sign in to comment.