Merge pull request #15307 from woodruffw-forks/die-pipgrip

utils/pypi: replace `pipgrip` with `pip`'s built in dependency resolution
This commit is contained in:
Mike McQuaid 2023-04-25 09:36:23 +01:00 committed by GitHub
commit c343e545c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,7 +24,7 @@ module PyPI
end end
raise ArgumentError, "Package should be a valid PyPI URL" if match.blank? raise ArgumentError, "Package should be a valid PyPI URL" if match.blank?
@name = match[1] @name = PyPI.normalize_python_package(match[1])
@version = match[2] @version = match[2]
return return
end end
@ -208,12 +208,13 @@ module PyPI
end end
end end
ensure_formula_installed!("pipgrip") ensure_formula_installed!("python")
ohai "Retrieving PyPI dependencies for \"#{input_packages.join(" ")}\"..." if !print_only && !silent ohai "Retrieving PyPI dependencies for \"#{input_packages.join(" ")}\"..." if !print_only && !silent
command = command =
[Formula["pipgrip"].opt_bin/"pipgrip", "--json", "--tree", "--no-cache-dir", *input_packages.map(&:to_s)] [Formula["python"].bin/"python3", "-m", "pip", "install", "-q", "--dry-run", "--ignore-installed", "--report",
pipgrip_output = Utils.popen_read(*command) "/dev/stdout", *input_packages.map(&:to_s)]
pip_output = Utils.popen_read({ "PIP_REQUIRE_VIRTUALENV" => "false" }, *command)
unless $CHILD_STATUS.success? unless $CHILD_STATUS.success?
odie <<~EOS odie <<~EOS
Unable to determine dependencies for "#{input_packages.join(" ")}" because of a failure when running Unable to determine dependencies for "#{input_packages.join(" ")}" because of a failure when running
@ -222,7 +223,7 @@ module PyPI
EOS EOS
end end
found_packages = json_to_packages(JSON.parse(pipgrip_output), main_package, exclude_packages).uniq found_packages = pip_report_to_packages(JSON.parse(pip_output), exclude_packages).uniq
new_resource_blocks = "" new_resource_blocks = ""
found_packages.sort.each do |package| found_packages.sort.each do |package|
@ -280,17 +281,22 @@ module PyPI
true true
end end
def self.json_to_packages(json_tree, main_package, exclude_packages) def self.normalize_python_package(name)
return [] if json_tree.blank? # This normalization is defined in the PyPA packaging specifications;
# https://packaging.python.org/en/latest/specifications/name-normalization/#name-normalization
name.gsub(/[-_.]+/, "-").downcase
end
json_tree.flat_map do |package_json| def self.pip_report_to_packages(report, exclude_packages)
package = Package.new("#{package_json["name"]}==#{package_json["version"]}") return [] if report.blank?
dependencies = if package == main_package || exclude_packages.exclude?(package)
json_to_packages(package_json["dependencies"], main_package, exclude_packages) report["install"].map do |package|
else name = normalize_python_package(package["metadata"]["name"])
[] version = package["metadata"]["version"]
end
[package] + dependencies package = Package.new "#{name}==#{version}"
end
package if exclude_packages.exclude? package
end.compact
end end
end end