Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update-python-resources: skip dependencies of excluded packages #12427

Merged
merged 2 commits into from
Nov 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions Library/Homebrew/utils/pypi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,17 @@ def same_package?(other)
@name.tr("_", "-").casecmp(other.name.tr("_", "-")).zero?
end

# Compare only names so we can use .include? on a Package array
# Compare only names so we can use .include? and .uniq on a Package array
sig { params(other: Package).returns(T::Boolean) }
def ==(other)
same_package?(other)
end
alias eql? ==

sig { returns(Integer) }
def hash
@name.tr("_", "-").downcase.hash
end

sig { params(other: Package).returns(T.nilable(Integer)) }
def <=>(other)
Expand Down Expand Up @@ -212,7 +218,8 @@ def update_python_resources!(formula, version: nil, package_name: nil, extra_pac
odie '"pipgrip" must be installed (`brew install pipgrip`)' unless @pipgrip_installed

ohai "Retrieving PyPI dependencies for \"#{input_packages.join(" ")}\"..." if !print_only && !silent
command = [Formula["pipgrip"].opt_bin/"pipgrip", "--json", "--no-cache-dir", *input_packages.map(&:to_s)]
command =
[Formula["pipgrip"].opt_bin/"pipgrip", "--json", "--tree", "--no-cache-dir", *input_packages.map(&:to_s)]
pipgrip_output = Utils.popen_read(*command)
unless $CHILD_STATUS.success?
odie <<~EOS
Expand All @@ -222,9 +229,7 @@ def update_python_resources!(formula, version: nil, package_name: nil, extra_pac
EOS
end

found_packages = JSON.parse(pipgrip_output).to_h.map do |new_name, new_version|
Package.new("#{new_name}==#{new_version}")
end
found_packages = json_to_packages(JSON.parse(pipgrip_output), main_package, exclude_packages).uniq

new_resource_blocks = ""
found_packages.sort.each do |package|
Expand Down Expand Up @@ -281,4 +286,18 @@ def update_python_resources!(formula, version: nil, package_name: nil, extra_pac

true
end

def json_to_packages(json_tree, main_package, exclude_packages)
return [] if json_tree.blank?

json_tree.flat_map do |package_json|
package = Package.new("#{package_json["name"]}==#{package_json["version"]}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
package = Package.new("#{package_json["name"]}==#{package_json["version"]}")
package = [Package.new("#{package_json["name"]}==#{package_json["version"]}")]

dependencies = if package == main_package || exclude_packages.exclude?(package)
json_to_packages(package_json["dependencies"], main_package, exclude_packages)
else
[]
end
[package] + dependencies
end
end
end