audit: add additional (in)valid empty files, refactor empty file check

This commit is contained in:
Kenneth Chew 2021-03-15 14:34:05 -04:00
parent 844e865363
commit 5adf8cbe7b
No known key found for this signature in database
GPG Key ID: BED0BEA07E06CA6C

View File

@ -275,42 +275,43 @@ module FormulaCellarChecks
"Python formulae that are keg-only should not create `pip3` and `wheel3` symlinks." "Python formulae that are keg-only should not create `pip3` and `wheel3` symlinks."
end end
INVALID_EMPTY_FILE_NAMES = %w[AUTHORS ChangeLog changes COPYING NEWS README TODO].freeze INVALID_EMPTY_FILE_NAMES = %w[AUTHORS ChangeLog changes COPYING LICENSE NEWS README TODO].freeze
INVALID_EMPTY_FILE_EXTENSIONS = %w[.html .md .rst .txt].freeze INVALID_EMPTY_FILE_EXTENSIONS = %w[.html .md .rst .txt].freeze
INVALID_EMPTY_FILE_LOCATIONS = %w[bin libexec share/man share/doc].freeze INVALID_EMPTY_FILE_DIRECTORIES = %w[bin libexec share/man share/doc].freeze
# Names and extensions to ignore, even if they are in a location within prefix given above # Names and extensions to ignore, even if they are in a location within prefix given above
VALID_EMPTY_FILE_NAMES = %w[__init__.py REQUESTED].freeze VALID_EMPTY_FILE_NAMES = %w[__init__ empty REQUESTED].freeze
VALID_EMPTY_FILE_EXTENSIONS = %w[.stamp].freeze VALID_EMPTY_FILE_EXTENSIONS = %w[.gitkeep .keep .keepme .stamp .typed].freeze
def check_empty_files(prefix) def check_empty_files(prefix)
return unless prefix.directory? return unless prefix.directory?
empty_files = [] empty_files = prefix.children.select do |f|
prefix.children.each do |f| next if INVALID_EMPTY_FILE_NAMES.exclude?(f.basename(".*").to_s) &&
next if INVALID_EMPTY_FILE_NAMES.none?(File.basename(f)) && INVALID_EMPTY_FILE_EXTENSIONS.exclude?(f.extname)
INVALID_EMPTY_FILE_EXTENSIONS.none?(File.extname(f))
next unless File.zero?(f) next unless File.zero?(f)
empty_files << f true
end end
INVALID_EMPTY_FILE_LOCATIONS.each do |loc| INVALID_EMPTY_FILE_DIRECTORIES.each do |dir|
next unless (prefix/loc).directory? path = prefix/dir
next unless path.directory?
# search all directories in INVALID_EMPTY_FILE_LOCATIONS and their subdirectories # search all directories in INVALID_EMPTY_FILE_LOCATIONS and their subdirectories
Dir.glob(prefix/loc/"**/*").each do |f| Pathname.glob(path/"**/*").each do |p|
next if VALID_EMPTY_FILE_NAMES.any?(File.basename(f)) || # Any files in the "valid" list should be skipped because they are expected to be empty
VALID_EMPTY_FILE_EXTENSIONS.any?(File.extname(f)) next if VALID_EMPTY_FILE_NAMES.include?(p.basename(".*").to_s)
next unless File.zero?(f) next if VALID_EMPTY_FILE_EXTENSIONS.include?(p.extname)
next unless File.zero?(p)
empty_files << f empty_files << p
end end
end end
return if empty_files.empty? return if empty_files.empty?
<<~EOS <<~EOS
Suspicious empty files were installed. Unexpected empty files were installed.
The offending files are: The offending files are:
#{empty_files * "\n "} #{empty_files * "\n "}
EOS EOS