Skip to content

Commit

Permalink
utils/pypi: use python from formula for non-pypi url metadata
Browse files Browse the repository at this point in the history
This fixes the resource detection when formula has Python packages that
are not compatible with current aliased python formula, e.g. `awscli`

Also switch to `opt_libexec` path to ignore Python formula patch version
and revision bump differences.
  • Loading branch information
cho-m committed Feb 22, 2024
1 parent 678bfec commit 53bb725
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions Library/Homebrew/utils/pypi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ module PyPI
# or it can be a non-PyPI URL.
# @api private
class Package
sig { params(package_string: String, is_url: T::Boolean).void }
def initialize(package_string, is_url: false)
sig { params(package_string: String, is_url: T::Boolean, python_name: String).void }
def initialize(package_string, is_url: false, python_name: "python")
@pypi_info = nil
@package_string = package_string
@is_url = is_url
@is_pypi_url = package_string.start_with? PYTHONHOSTED_URL_PREFIX
@python_name = python_name
end

sig { returns(String) }
Expand Down Expand Up @@ -131,7 +132,7 @@ def basic_metadata
@extras ||= []
@version ||= match[2]
elsif @is_url
ensure_formula_installed!("python")
ensure_formula_installed!(@python_name)

# The URL might be a source distribution hosted somewhere;
# try and use `pip install -q --no-deps --dry-run --report ...` to get its
Expand All @@ -140,7 +141,7 @@ def basic_metadata
# do below, in that it uses `--no-deps` because we only care about resolving
# this specific URL's project metadata.
command =
[Formula["python"].bin/"python3", "-m", "pip", "install", "-q", "--no-deps",
[Formula[@python_name].opt_libexec/"bin/python", "-m", "pip", "install", "-q", "--no-deps",
"--dry-run", "--ignore-installed", "--report", "/dev/stdout", @package_string]
pip_output = Utils.popen_read({ "PIP_REQUIRE_VIRTUALENV" => "false" }, *command)
unless $CHILD_STATUS.success?
Expand Down Expand Up @@ -226,16 +227,27 @@ def self.update_python_resources!(formula, version: nil, package_name: nil, extr
end
end

python_deps = formula.deps
.select { |d| d.name.match?(/^python(@.+)?$/) }
.map(&:to_formula)
.sort_by(&:version)
.reverse
python_name = if python_deps.empty?
"python"
else
(python_deps.find(&:any_version_installed?) || python_deps.first).name
end

main_package = if package_name.present?
Package.new(package_name)
Package.new(package_name, python_name: python_name)
else
stable = T.must(formula.stable)
url = if stable.specs[:tag].present?
url = "git+#{stable.url}@#{stable.specs[:tag]}"
else
stable.url
end
Package.new(url, is_url: true)
Package.new(url, is_url: true, python_name: python_name)
end

if version.present?
Expand All @@ -249,12 +261,6 @@ def self.update_python_resources!(formula, version: nil, package_name: nil, extr
end
end

python_deps = formula.deps
.select { |d| d.name.match?(/^python(@.+)?$/) }
.map(&:to_formula)
.sort_by(&:version)
.reverse

extra_packages = (extra_packages || []).map { |p| Package.new p }
exclude_packages = (exclude_packages || []).map { |p| Package.new p }
exclude_packages += %w[argparse pip wsgiref].map { |p| Package.new p }
Expand Down Expand Up @@ -286,11 +292,6 @@ def self.update_python_resources!(formula, version: nil, package_name: nil, extr
end
end

python_name = if python_deps.empty?
"python"
else
(python_deps.find(&:any_version_installed?) || python_deps.first).name
end
ensure_formula_installed!(python_name)

# Resolve the dependency tree of all input packages
Expand Down Expand Up @@ -372,7 +373,7 @@ def self.pip_report(packages, python_name: "python", print_stderr: false)
return [] if packages.blank?

command = [
Formula[python_name].libexec/"bin/python", "-m", "pip", "install", "-q", "--disable-pip-version-check",
Formula[python_name].opt_libexec/"bin/python", "-m", "pip", "install", "-q", "--disable-pip-version-check",
"--dry-run", "--ignore-installed", "--report=/dev/stdout", *packages.map(&:to_s)
]
options = {}
Expand Down

0 comments on commit 53bb725

Please sign in to comment.