move pypi list to tap formula_lists directory

This commit is contained in:
Rylan Polster 2020-11-20 00:55:21 -05:00
parent b1d907291b
commit 51a1b7c9e1
4 changed files with 76 additions and 56 deletions

View File

@ -27,7 +27,6 @@ module Homebrew
description: "Use the specified <version> when finding resources for <formula>. "\ description: "Use the specified <version> when finding resources for <formula>. "\
"If no version is specified, the current version for <formula> will be used." "If no version is specified, the current version for <formula> will be used."
flag "--package-name=", flag "--package-name=",
depends_on: "--version",
description: "Use the specified <package-name> when finding resources for <formula>. "\ description: "Use the specified <package-name> when finding resources for <formula>. "\
"If no package name is specified, it will be inferred from the formula's stable URL." "If no package name is specified, it will be inferred from the formula's stable URL."
comma_array "--extra-packages=", comma_array "--extra-packages=",

View File

@ -21,11 +21,13 @@ class Tap
HOMEBREW_TAP_FORMULA_RENAMES_FILE = "formula_renames.json" HOMEBREW_TAP_FORMULA_RENAMES_FILE = "formula_renames.json"
HOMEBREW_TAP_MIGRATIONS_FILE = "tap_migrations.json" HOMEBREW_TAP_MIGRATIONS_FILE = "tap_migrations.json"
HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR = "audit_exceptions" HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR = "audit_exceptions"
HOMEBREW_TAP_FORMULA_LISTS_DIR = "formula_lists"
HOMEBREW_TAP_JSON_FILES = %W[ HOMEBREW_TAP_JSON_FILES = %W[
#{HOMEBREW_TAP_FORMULA_RENAMES_FILE} #{HOMEBREW_TAP_FORMULA_RENAMES_FILE}
#{HOMEBREW_TAP_MIGRATIONS_FILE} #{HOMEBREW_TAP_MIGRATIONS_FILE}
#{HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR}/*.json #{HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR}/*.json
#{HOMEBREW_TAP_FORMULA_LISTS_DIR}/*.json
].freeze ].freeze
def self.fetch(*args) def self.fetch(*args)
@ -112,6 +114,7 @@ class Tap
@formula_renames = nil @formula_renames = nil
@tap_migrations = nil @tap_migrations = nil
@audit_exceptions = nil @audit_exceptions = nil
@formula_lists = nil
@config = nil @config = nil
remove_instance_variable(:@private) if instance_variable_defined?(:@private) remove_instance_variable(:@private) if instance_variable_defined?(:@private)
end end
@ -560,22 +563,12 @@ class Tap
# Hash with audit exceptions # Hash with audit exceptions
def audit_exceptions def audit_exceptions
@audit_exceptions = {} @audit_exceptions = read_formula_list_directory HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR
Pathname.glob(path/HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR/"*").each do |exception_file|
list_name = exception_file.basename.to_s.chomp(".json").to_sym
list_contents = begin
JSON.parse exception_file.read
rescue JSON::ParserError
opoo "#{exception_file} contains invalid JSON"
end end
next if list_contents.nil? # Hash with formula lists
def formula_lists
@audit_exceptions[list_name] = list_contents @formula_lists = read_formula_list_directory HOMEBREW_TAP_FORMULA_LISTS_DIR
end
@audit_exceptions
end end
def ==(other) def ==(other)
@ -636,6 +629,25 @@ class Tap
end end
end end
end end
def read_formula_list_directory(directory)
list = {}
Pathname.glob(path/directory/"*").each do |exception_file|
list_name = exception_file.basename.to_s.chomp(".json").to_sym
list_contents = begin
JSON.parse exception_file.read
rescue JSON::ParserError
opoo "#{exception_file} contains invalid JSON"
end
next if list_contents.nil?
list[list_name] = list_contents
end
list
end
end end
# A specialized {Tap} class for the core formulae. # A specialized {Tap} class for the core formulae.
@ -739,6 +751,13 @@ class CoreTap < Tap
end end
end end
def formula_lists
@formula_lists ||= begin
self.class.ensure_installed!
super
end
end
# @private # @private
def formula_file_to_name(file) def formula_file_to_name(file)
file.basename(".rb").to_s file.basename(".rb").to_s

View File

@ -15,13 +15,14 @@ module Homebrew
@name = tap.name @name = tap.name
@path = tap.path @path = tap.path
@tap_audit_exceptions = tap.audit_exceptions @tap_audit_exceptions = tap.audit_exceptions
@tap_formula_lists = tap.formula_lists
@problems = [] @problems = []
end end
sig { void } sig { void }
def audit def audit
audit_json_files audit_json_files
audit_tap_audit_exceptions audit_tap_formula_lists
end end
sig { void } sig { void }
@ -35,11 +36,16 @@ module Homebrew
end end
sig { void } sig { void }
def audit_tap_audit_exceptions def audit_tap_formula_lists
@tap_audit_exceptions.each do |list_name, formula_names| tap_lists = {
audit_exceptions: @tap_audit_exceptions,
formula_lists: @tap_formula_lists,
}
tap_lists.each do |list_directory, list|
list.each do |list_name, formula_names|
unless [Hash, Array].include? formula_names.class unless [Hash, Array].include? formula_names.class
problem <<~EOS problem <<~EOS
audit_exceptions/#{list_name}.json should contain a JSON array #{list_directory}/#{list_name}.json should contain a JSON array
of formula names or a JSON object mapping formula names to values of formula names or a JSON object mapping formula names to values
EOS EOS
next next
@ -57,12 +63,13 @@ module Homebrew
next if invalid_formulae.empty? next if invalid_formulae.empty?
problem <<~EOS problem <<~EOS
audit_exceptions/#{list_name}.json references #{list_directory}/#{list_name}.json references
formulae that are not found in the #{@name} tap. formulae that are not found in the #{@name} tap.
Invalid formulae: #{invalid_formulae.join(", ")} Invalid formulae: #{invalid_formulae.join(", ")}
EOS EOS
end end
end end
end
sig { params(message: String).void } sig { params(message: String).void }
def problem(message) def problem(message)

View File

@ -26,8 +26,9 @@ module PyPI
url url
end end
# Get name, URL and SHA-256 checksum for a given PyPI package. # Get name, URL, SHA-256 checksum, and latest version for a given PyPI package.
def get_pypi_info(package, version) def get_pypi_info(package, version = nil)
package = package.split("[").first
metadata_url = if version.present? metadata_url = if version.present?
"https://pypi.org/pypi/#{package}/#{version}/json" "https://pypi.org/pypi/#{package}/#{version}/json"
else else
@ -46,15 +47,15 @@ module PyPI
sdist = json["urls"].find { |url| url["packagetype"] == "sdist" } sdist = json["urls"].find { |url| url["packagetype"] == "sdist" }
return json["info"]["name"] if sdist.nil? return json["info"]["name"] if sdist.nil?
[json["info"]["name"], sdist["url"], sdist["digests"]["sha256"]] [json["info"]["name"], sdist["url"], sdist["digests"]["sha256"], json["info"]["version"]]
end end
# Return true if resources were checked (even if no change). # Return true if resources were checked (even if no change).
def update_python_resources!(formula, version: nil, package_name: nil, extra_packages: nil, exclude_packages: nil, def update_python_resources!(formula, version: nil, package_name: nil, extra_packages: nil, exclude_packages: nil,
print_only: false, silent: false, ignore_non_pypi_packages: false) print_only: false, silent: false, ignore_non_pypi_packages: false)
auto_update_list = formula.tap.audit_exceptions[:automatic_resource_update_list] auto_update_list = formula.tap.formula_lists[:pypi_automatic_resource_update_list]
if package_name.blank? && extra_packages.blank? && !print_only && if package_name.blank? && extra_packages.blank? && exclude_packages.blank? && !print_only &&
auto_update_list.present? && auto_update_list.key?(formula.full_name) auto_update_list.present? && auto_update_list.key?(formula.full_name)
list_entry = auto_update_list[formula.full_name] list_entry = auto_update_list[formula.full_name]
@ -70,18 +71,12 @@ module PyPI
end end
end end
version ||= formula.version if package_name.blank?
package_name ||= url_to_pypi_package_name formula.stable.url package_name ||= url_to_pypi_package_name formula.stable.url
version ||= formula.version
extra_packages ||= [] extra_packages ||= []
exclude_packages ||= [] exclude_packages ||= []
# opoo "package_name: #{package_name}" if package_name.blank?
# opoo "version: #{version}"
# opoo "extra_packages: #{extra_packages}"
# opoo "exclude_packages: #{exclude_packages}"
# odie ""
if package_name.nil?
return if ignore_non_pypi_packages return if ignore_non_pypi_packages
odie <<~EOS odie <<~EOS
@ -140,7 +135,7 @@ module PyPI
EOS EOS
end end
found_packages.merge!(JSON.parse(pipgrip_output).sort.to_h) do |conflicting_package, old_version, new_version| found_packages.merge!(JSON.parse(pipgrip_output).to_h) do |conflicting_package, old_version, new_version|
next old_version if old_version == new_version next old_version if old_version == new_version
odie "Conflicting versions found for the `#{conflicting_package}` resource: #{old_version}, #{new_version}" odie "Conflicting versions found for the `#{conflicting_package}` resource: #{old_version}, #{new_version}"
@ -148,11 +143,11 @@ module PyPI
end end
# Remove extra packages that may be included in pipgrip output # Remove extra packages that may be included in pipgrip output
exclude_list = %W[#{package_name.downcase} argparse pip setuptools wheel wsgiref] exclude_list = %W[#{package_name.split("[").first.downcase} argparse pip setuptools wheel wsgiref]
found_packages.delete_if { |package| exclude_list.include? package } found_packages.delete_if { |package| exclude_list.include? package }
new_resource_blocks = "" new_resource_blocks = ""
found_packages.each do |package, package_version| found_packages.sort.each do |package, package_version|
if exclude_packages.include? package if exclude_packages.include? package
ohai "Excluding \"#{package}==#{package_version}\"" if !print_only && !silent ohai "Excluding \"#{package}==#{package_version}\"" if !print_only && !silent
next next