move mapping from formula_lists to pypi_formula_mappings

This commit is contained in:
Rylan Polster 2020-11-20 18:14:45 -05:00
parent 20de58b5ae
commit ee47b863c4
3 changed files with 69 additions and 55 deletions

View File

@ -21,13 +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_PYPI_FORMULA_MAPPINGS = "pypi_formula_mappings.json"
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 #{HOMEBREW_TAP_PYPI_FORMULA_MAPPINGS}
].freeze ].freeze
def self.fetch(*args) def self.fetch(*args)
@ -114,7 +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 @pypi_formula_mappings = 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
@ -562,13 +562,15 @@ class Tap
end end
# Hash with audit exceptions # Hash with audit exceptions
sig { returns(Hash) }
def audit_exceptions def audit_exceptions
@audit_exceptions = read_formula_list_directory HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR @audit_exceptions = read_formula_list_directory "#{HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR}/*"
end end
# Hash with formula lists # Hash with pypi formula mappings
def formula_lists sig { returns(Hash) }
@formula_lists = read_formula_list_directory HOMEBREW_TAP_FORMULA_LISTS_DIR def pypi_formula_mappings
@pypi_formula_mappings = read_formula_list path/HOMEBREW_TAP_PYPI_FORMULA_MAPPINGS
end end
def ==(other) def ==(other)
@ -630,18 +632,25 @@ class Tap
end end
end end
sig { params(file: Pathname).returns(Hash) }
def read_formula_list(file)
JSON.parse file.read
rescue JSON::ParserError
opoo "#{file} contains invalid JSON"
{}
rescue Errno::ENOENT
{}
end
sig { params(directory: String).returns(Hash) }
def read_formula_list_directory(directory) def read_formula_list_directory(directory)
list = {} list = {}
Pathname.glob(path/directory/"*").each do |exception_file| Pathname.glob(path/directory).each do |exception_file|
list_name = exception_file.basename.to_s.chomp(".json").to_sym list_name = exception_file.basename.to_s.chomp(".json").to_sym
list_contents = begin list_contents = read_formula_list exception_file
JSON.parse exception_file.read
rescue JSON::ParserError
opoo "#{exception_file} contains invalid JSON"
end
next if list_contents.nil? next if list_contents.blank?
list[list_name] = list_contents list[list_name] = list_contents
end end
@ -751,8 +760,8 @@ class CoreTap < Tap
end end
end end
def formula_lists def pypi_formula_mappings
@formula_lists ||= begin @pypi_formula_mappings ||= begin
self.class.ensure_installed! self.class.ensure_installed!
super super
end end

View File

@ -8,15 +8,15 @@ module Homebrew
class TapAuditor class TapAuditor
extend T::Sig extend T::Sig
attr_reader :name, :path, :tap_audit_exceptions, :problems attr_reader :name, :path, :tap_audit_exceptions, :tap_pypi_formula_mappings, :problems
sig { params(tap: Tap, strict: T.nilable(T::Boolean)).void } sig { params(tap: Tap, strict: T.nilable(T::Boolean)).void }
def initialize(tap, strict:) def initialize(tap, strict:)
@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 @tap_pypi_formula_mappings = tap.pypi_formula_mappings
@problems = [] @problems = []
end end
sig { void } sig { void }
@ -37,43 +37,48 @@ module Homebrew
sig { void } sig { void }
def audit_tap_formula_lists def audit_tap_formula_lists
tap_lists = { check_formula_list_directory "audit_exceptions", @tap_audit_exceptions
audit_exceptions: @tap_audit_exceptions, check_formula_list "pypi_formula_mappings", @tap_pypi_formula_mappings
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
problem <<~EOS
#{list_directory}/#{list_name}.json should contain a JSON array
of formula names or a JSON object mapping formula names to values
EOS
next
end
formula_names = formula_names.keys if formula_names.is_a? Hash
invalid_formulae = []
formula_names.each do |name|
invalid_formulae << name if Formula[name].tap != @name
rescue FormulaUnavailableError
invalid_formulae << name
end
next if invalid_formulae.empty?
problem <<~EOS
#{list_directory}/#{list_name}.json references
formulae that are not found in the #{@name} tap.
Invalid formulae: #{invalid_formulae.join(", ")}
EOS
end
end
end end
sig { params(message: String).void } sig { params(message: String).void }
def problem(message) def problem(message)
@problems << ({ message: message, location: nil }) @problems << ({ message: message, location: nil })
end end
private
sig { params(list_file: String, list: T.untyped).void }
def check_formula_list(list_file, list)
unless [Hash, Array].include? list.class
problem <<~EOS
#{list_file}.json should contain a JSON array
of formula names or a JSON object mapping formula names to values
EOS
return
end
invalid_formulae = []
list.each do |name, _|
invalid_formulae << name if Formula[name].tap != @name
rescue FormulaUnavailableError
invalid_formulae << name
end
return if invalid_formulae.empty?
problem <<~EOS
#{list_file}.json references
formulae that are not found in the #{@name} tap.
Invalid formulae: #{invalid_formulae.join(", ")}
EOS
end
sig { params(directory_name: String, lists: Hash).void }
def check_formula_list_directory(directory_name, lists)
lists.each do |list_name, list|
check_formula_list "#{directory_name}/#{list_name}", list
end
end
end end
end end

View File

@ -54,7 +54,7 @@ module PyPI
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.formula_lists[:pypi_automatic_resource_update_list] auto_update_list = formula.tap.pypi_formula_mappings
if auto_update_list.present? && auto_update_list.key?(formula.full_name) && if auto_update_list.present? && auto_update_list.key?(formula.full_name) &&
package_name.blank? && extra_packages.blank? && exclude_packages.blank? package_name.blank? && extra_packages.blank? && exclude_packages.blank?