-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
tap_auditor.rb
121 lines (102 loc) 路 4.06 KB
/
tap_auditor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# typed: true
# frozen_string_literal: true
module Homebrew
# Auditor for checking common violations in {Tap}s.
#
# @api private
class TapAuditor
attr_reader :name, :path, :formula_names, :formula_aliases, :formula_renames, :cask_tokens,
:tap_audit_exceptions, :tap_style_exceptions, :tap_pypi_formula_mappings, :problems
sig { params(tap: Tap, strict: T.nilable(T::Boolean)).void }
def initialize(tap, strict:)
Homebrew.with_no_api_env do
@name = tap.name
@path = tap.path
@cask_tokens = tap.cask_tokens
@tap_audit_exceptions = tap.audit_exceptions
@tap_style_exceptions = tap.style_exceptions
@tap_pypi_formula_mappings = tap.pypi_formula_mappings
@problems = []
@formula_aliases = tap.aliases.map do |formula_alias|
formula_alias.split("/").last
end
@formula_renames = tap.formula_renames
@formula_names = tap.formula_names.map do |formula_name|
formula_name.split("/").last
end
end
end
sig { void }
def audit
audit_json_files
audit_tap_formula_lists
audit_aliases_renames_duplicates
end
sig { void }
def audit_json_files
json_patterns = Tap::HOMEBREW_TAP_JSON_FILES.map { |pattern| @path/pattern }
Pathname.glob(json_patterns).each do |file|
JSON.parse file.read
rescue JSON::ParserError
problem "#{file.to_s.delete_prefix("#{@path}/")} contains invalid JSON"
end
require "json_schemer"
schemer = JSONSchemer.schema(HOMEBREW_DATA_PATH/"schemas/pypi_formula_mappings.schema.json")
return if schemer.valid?(@tap_pypi_formula_mappings)
problem <<~EOS
pypi_formula_mappings.json schema validation failed with following errors:
* #{schemer.validate(@tap_pypi_formula_mappings).map { _1.fetch("error") }.join("\n* ")}
EOS
end
sig { void }
def audit_tap_formula_lists
check_formula_list_directory "audit_exceptions", @tap_audit_exceptions
check_formula_list_directory "style_exceptions", @tap_style_exceptions
check_formula_list "pypi_formula_mappings", @tap_pypi_formula_mappings
@tap_pypi_formula_mappings.each_value do |formula_mapping|
next unless formula_mapping.is_a?(Hash)
next unless formula_mapping.key?("dependencies")
check_formula_list "pypi_formula_mappings", formula_mapping["dependencies"]
end
end
sig { void }
def audit_aliases_renames_duplicates
duplicates = formula_aliases & formula_renames.keys
return if duplicates.none?
problem "The following should either be an alias or a rename, not both: #{duplicates.to_sentence}"
end
sig { params(message: String).void }
def problem(message)
@problems << ({ message: message, location: nil, corrected: false })
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
list = list.keys if list.is_a? Hash
invalid_formulae_casks = list.select do |formula_or_cask_name|
formula_names.exclude?(formula_or_cask_name) &&
formula_aliases.exclude?(formula_or_cask_name) &&
cask_tokens.exclude?("#{@name}/#{formula_or_cask_name}")
end
return if invalid_formulae_casks.empty?
problem <<~EOS
#{list_file}.json references
formulae or casks that are not found in the #{@name} tap.
Invalid formulae or casks: #{invalid_formulae_casks.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