diff --git a/Library/Homebrew/utils/pypi.rb b/Library/Homebrew/utils/pypi.rb index e24fefe09e..38d7e75ea0 100644 --- a/Library/Homebrew/utils/pypi.rb +++ b/Library/Homebrew/utils/pypi.rb @@ -250,7 +250,7 @@ module PyPI extra_packages = (extra_packages || []).map { |p| Package.new p } exclude_packages = (exclude_packages || []).map { |p| Package.new p } - exclude_packages += %W[#{main_package.name} argparse pip setuptools wsgiref].map { |p| Package.new p } + exclude_packages += %w[argparse pip setuptools wsgiref].map { |p| Package.new p } # remove packages from the exclude list if we've explicitly requested them as an extra package exclude_packages.delete_if { |package| extra_packages.include?(package) } @@ -278,20 +278,13 @@ module PyPI ensure_formula_installed!("python") + # Resolve the dependency tree of all input packages ohai "Retrieving PyPI dependencies for \"#{input_packages.join(" ")}\"..." if !print_only && !silent - command = - [Formula["python"].bin/"python3", "-m", "pip", "install", "-q", "--dry-run", "--ignore-installed", "--report", - "/dev/stdout", *input_packages.map(&:to_s)] - pip_output = Utils.popen_read({ "PIP_REQUIRE_VIRTUALENV" => "false" }, *command) - unless $CHILD_STATUS.success? - odie <<~EOS - Unable to determine dependencies for "#{input_packages.join(" ")}" because of a failure when running - `#{command.join(" ")}`. - Please update the resources for "#{formula.name}" manually. - EOS - end - - found_packages = pip_report_to_packages(JSON.parse(pip_output), exclude_packages).uniq + found_packages = pip_report(input_packages) + # Resolve the dependency tree of excluded packages to prune the above + exclude_packages.delete_if { |package| found_packages.exclude? package } + ohai "Retrieving PyPI dependencies for excluded \"#{exclude_packages.join(" ")}\"..." if !print_only && !silent + exclude_packages = pip_report(exclude_packages) + [Package.new(main_package.name)] new_resource_blocks = "" found_packages.sort.each do |package| @@ -355,16 +348,30 @@ module PyPI name.gsub(/[-_.]+/, "-").downcase end - def self.pip_report_to_packages(report, exclude_packages) + def self.pip_report(packages) + return [] if packages.blank? + + command = [Formula["python"].bin/"python3", "-m", "pip", "install", "-q", "--dry-run", + "--ignore-installed", "--report=/dev/stdout", *packages.map(&:to_s)] + pip_output = Utils.popen_read({ "PIP_REQUIRE_VIRTUALENV" => "false" }, *command) + unless $CHILD_STATUS.success? + odie <<~EOS + Unable to determine dependencies for "#{packages.join(" ")}" because of a failure when running + `#{command.join(" ")}`. + Please update the resources manually. + EOS + end + pip_report_to_packages(JSON.parse(pip_output)).uniq + end + + def self.pip_report_to_packages(report) return [] if report.blank? report["install"].map do |package| name = normalize_python_package(package["metadata"]["name"]) version = package["metadata"]["version"] - package = Package.new "#{name}==#{version}" - - package if exclude_packages.exclude? package + Package.new "#{name}==#{version}" end.compact end end