From e6e730f49d7a46e021bed2e7a0342fdfac0efbfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?fn=20=E2=8C=83=20=E2=8C=A5?= <70830482+FnControlOption@users.noreply.github.com> Date: Sun, 14 Nov 2021 03:26:40 -0800 Subject: [PATCH 1/2] update-python-resources: skip dependencies of excluded packages --- Library/Homebrew/utils/pypi.rb | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/utils/pypi.rb b/Library/Homebrew/utils/pypi.rb index e7782048b2..7dc925003d 100644 --- a/Library/Homebrew/utils/pypi.rb +++ b/Library/Homebrew/utils/pypi.rb @@ -94,11 +94,17 @@ module PyPI @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) @@ -212,7 +218,8 @@ module PyPI 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 @@ -222,9 +229,7 @@ module PyPI 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| @@ -281,4 +286,17 @@ module PyPI true end + + def json_to_packages(json_tree, main_package, exclude_packages) + return [] if json_tree.nil? + + json_tree.flat_map do |package_json| + package = Package.new("#{package_json["name"]}==#{package_json["version"]}") + [package] + if package == main_package || exclude_packages.exclude?(package) + json_to_packages(package_json["dependencies"], main_package, exclude_packages) + else + [] + end + end + end end From dddce0de74354d4a22ac5335b0ef570e3e5bd16a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?fn=20=E2=8C=83=20=E2=8C=A5?= <70830482+FnControlOption@users.noreply.github.com> Date: Mon, 15 Nov 2021 10:49:07 -0800 Subject: [PATCH 2/2] Style fixes --- Library/Homebrew/utils/pypi.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/utils/pypi.rb b/Library/Homebrew/utils/pypi.rb index 7dc925003d..c90c369255 100644 --- a/Library/Homebrew/utils/pypi.rb +++ b/Library/Homebrew/utils/pypi.rb @@ -288,15 +288,16 @@ module PyPI end def json_to_packages(json_tree, main_package, exclude_packages) - return [] if json_tree.nil? + return [] if json_tree.blank? json_tree.flat_map do |package_json| package = Package.new("#{package_json["name"]}==#{package_json["version"]}") - [package] + if package == main_package || exclude_packages.exclude?(package) + 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