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_MIGRATIONS_FILE = "tap_migrations.json"
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_FORMULA_RENAMES_FILE}
#{HOMEBREW_TAP_MIGRATIONS_FILE}
#{HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR}/*.json
#{HOMEBREW_TAP_FORMULA_LISTS_DIR}/*.json
#{HOMEBREW_TAP_PYPI_FORMULA_MAPPINGS}
].freeze
def self.fetch(*args)
@ -114,7 +114,7 @@ class Tap
@formula_renames = nil
@tap_migrations = nil
@audit_exceptions = nil
@formula_lists = nil
@pypi_formula_mappings = nil
@config = nil
remove_instance_variable(:@private) if instance_variable_defined?(:@private)
end
@ -562,13 +562,15 @@ class Tap
end
# Hash with audit exceptions
sig { returns(Hash) }
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
# Hash with formula lists
def formula_lists
@formula_lists = read_formula_list_directory HOMEBREW_TAP_FORMULA_LISTS_DIR
# Hash with pypi formula mappings
sig { returns(Hash) }
def pypi_formula_mappings
@pypi_formula_mappings = read_formula_list path/HOMEBREW_TAP_PYPI_FORMULA_MAPPINGS
end
def ==(other)
@ -630,18 +632,25 @@ class Tap
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)
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_contents = begin
JSON.parse exception_file.read
rescue JSON::ParserError
opoo "#{exception_file} contains invalid JSON"
end
list_contents = read_formula_list exception_file
next if list_contents.nil?
next if list_contents.blank?
list[list_name] = list_contents
end
@ -751,8 +760,8 @@ class CoreTap < Tap
end
end
def formula_lists
@formula_lists ||= begin
def pypi_formula_mappings
@pypi_formula_mappings ||= begin
self.class.ensure_installed!
super
end

View File

@ -8,15 +8,15 @@ module Homebrew
class TapAuditor
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 }
def initialize(tap, strict:)
@name = tap.name
@path = tap.path
@tap_audit_exceptions = tap.audit_exceptions
@tap_formula_lists = tap.formula_lists
@problems = []
@name = tap.name
@path = tap.path
@tap_audit_exceptions = tap.audit_exceptions
@tap_pypi_formula_mappings = tap.pypi_formula_mappings
@problems = []
end
sig { void }
@ -37,43 +37,48 @@ module Homebrew
sig { void }
def audit_tap_formula_lists
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
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
check_formula_list_directory "audit_exceptions", @tap_audit_exceptions
check_formula_list "pypi_formula_mappings", @tap_pypi_formula_mappings
end
sig { params(message: String).void }
def problem(message)
@problems << ({ message: message, location: nil })
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

View File

@ -54,7 +54,7 @@ module PyPI
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)
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) &&
package_name.blank? && extra_packages.blank? && exclude_packages.blank?