Added flag to flush_cache in LinkageChecker. Format ruby hash as JSON string before storing in dbm.

This commit is contained in:
AndrewMcBurney 2018-02-27 13:05:19 -05:00
parent 4bc6459ed7
commit 14256faa47
4 changed files with 45 additions and 35 deletions

View File

@ -23,8 +23,7 @@ module Homebrew
ARGV.kegs.each do |keg| ARGV.kegs.each do |keg|
ohai "Checking #{keg.name} linkage" if ARGV.kegs.size > 1 ohai "Checking #{keg.name} linkage" if ARGV.kegs.size > 1
result = LinkageChecker.new(keg, database_cache) result = LinkageChecker.new(keg, database_cache, ARGV.include?("--rebuild"))
result.flush_cache_and_check_dylibs if ARGV.include?("--rebuild")
if ARGV.include?("--test") if ARGV.include?("--test")
result.display_test_output result.display_test_output

View File

@ -65,10 +65,7 @@ module FormulaCellarChecks
return unless formula.prefix.directory? return unless formula.prefix.directory?
keg = Keg.new(formula.prefix) keg = Keg.new(formula.prefix)
DatabaseCache.new(:linkage) do |database_cache| DatabaseCache.new(:linkage) { |database_cache| LinkageChecker.new(keg, database_cache, true, formula) }
checker = LinkageChecker.new(keg, database_cache, formula)
checker.flush_cache_and_check_dylibs
end
return unless checker.broken_dylibs? return unless checker.broken_dylibs?
output = <<~EOS output = <<~EOS

View File

@ -6,13 +6,13 @@ require "json"
# residing in the `HOMEBREW_CACHE` # residing in the `HOMEBREW_CACHE`
# #
class DatabaseCache class DatabaseCache
# Users have read and write, but not execute permissions # The mode of any created files will be 0664 (that is, readable and writable
DATABASE_MODE = 0666 # by the owner and the group, and readable by everyone else)
DATABASE_MODE = 0664
# Opens and yields a database in read/write mode # Opens and yields a database in read/write mode
#
# DBM::WRCREAT: Creates the database if it does not already exist
def initialize(name) def initialize(name)
# DBM::WRCREAT: Creates the database if it does not already exist
@db = DBM.open("#{HOMEBREW_CACHE}/#{name}.db", DATABASE_MODE, DBM::WRCREAT) @db = DBM.open("#{HOMEBREW_CACHE}/#{name}.db", DATABASE_MODE, DBM::WRCREAT)
yield(@db) yield(@db)
@db.close @db.close
@ -47,16 +47,24 @@ class CacheStore
protected protected
# @return [DBM]
attr_reader :database_cache attr_reader :database_cache
# Parses `DBM` stored `String` into ruby `Hash`
#
# DBM stores ruby objects as a ruby `String`. Hence, when fetching the data, # DBM stores ruby objects as a ruby `String`. Hence, when fetching the data,
# to convert the ruby string back into a ruby `Hash`, the string is converted # to convert the ruby string back into a ruby `Hash`, the string is converted
# into a JSON compatible string, where it may be parsed by the JSON.parse # into a JSON compatible string in `ruby_hash_to_json_string`, where it may
# function # later be parsed by `JSON.parse` in the `json_string_to_ruby_hash` method
def string_to_hash(string) #
JSON.parse(string.gsub("=>", ":")) # @param [Hash]
# @return [String]
def ruby_hash_to_json_string(hash)
hash.to_json
end
# @param [String]
# @return [Hash]
def json_string_to_ruby_hash(string)
JSON.parse(string)
end end
end end
@ -73,7 +81,7 @@ class LinkageStore < CacheStore
end end
def update!( def update!(
path_values: { array_values: {
system_dylibs: %w[], system_dylibs: %w[],
variable_dylibs: %w[], variable_dylibs: %w[],
broken_dylibs: %w[], broken_dylibs: %w[],
@ -86,17 +94,17 @@ class LinkageStore < CacheStore
reverse_links: {}, reverse_links: {},
} }
) )
database_cache[keg_name] = { database_cache[keg_name] = ruby_hash_to_json_string(
"path_values" => format_path_values(path_values), array_values: format_array_values(array_values),
"hash_values" => format_hash_values(hash_values), hash_values: format_hash_values(hash_values),
} )
end end
def fetch_type(type) def fetch_type(type)
if HASH_LINKAGE_TYPES.include?(type) if HASH_LINKAGE_TYPES.include?(type)
fetch_hash_values(type: type) fetch_hash_values(type: type)
else else
fetch_path_values(type: type) fetch_array_values(type: type)
end end
end end
@ -108,28 +116,33 @@ class LinkageStore < CacheStore
attr_reader :keg_name attr_reader :keg_name
def fetch_path_values(type:) def fetch_array_values(type:)
return [] if !database_cache.key?(keg_name) || database_cache[keg_name].nil? return [] unless database_cache.key?(keg_name)
string_to_hash(database_cache[keg_name])["path_values"][type.to_s] json_string_to_ruby_hash(database_cache[keg_name])["array_values"][type.to_s]
end end
def fetch_hash_values(type:) def fetch_hash_values(type:)
return {} if !database_cache.key?(keg_name) || database_cache[keg_name].nil? return {} unless database_cache.key?(keg_name)
string_to_hash(database_cache[keg_name])["hash_values"][type.to_s] json_string_to_ruby_hash(database_cache[keg_name])["hash_values"][type.to_s]
end end
# Formats the linkage data for `path_values` into a kind which can be parsed # Formats the linkage data for `array_values` into a kind which can be parsed
# by the `string_to_hash` method. Converts ruby `Set`s to `Array`s # by the `json_string_to_ruby_hash` method. Internally converts ruby `Set`s to
def format_path_values(hash) # `Array`s
hash.each_with_object({}) { |(k, v), h| h[k.to_s] = v.to_a } #
# @return [String]
def format_array_values(hash)
hash.each_with_object({}) { |(k, v), h| h[k] = v.to_a }
end end
# Formats the linkage data for `hash_values` into a kind which can be parsed # Formats the linkage data for `hash_values` into a kind which can be parsed
# by the `string_to_hash` method. Converts ruby `Set`s to `Array`s, and # by the `json_string_to_ruby_hash` method. Converts ruby `Set`s to `Array`s, and
# converts ruby `Pathname`s to `String`s # converts ruby `Pathname`s to `String`s
#
# @return [String]
def format_hash_values(hash) def format_hash_values(hash)
hash.each_with_object({}) do |(outer_key, outer_values), outer_hash| hash.each_with_object({}) do |(outer_key, outer_values), outer_hash|
outer_hash[outer_key.to_s] = outer_values.each_with_object({}) do |(k, v), h| outer_hash[outer_key] = outer_values.each_with_object({}) do |(k, v), h|
h[k] = v.to_a.map(&:to_s) h[k] = v.to_a.map(&:to_s)
end end
end end

View File

@ -6,10 +6,11 @@ require "os/mac/cache_store"
class LinkageChecker class LinkageChecker
attr_reader :keg, :formula, :store attr_reader :keg, :formula, :store
def initialize(keg, db, formula = nil) def initialize(keg, db, rebuild_cache = false, formula = nil)
@keg = keg @keg = keg
@formula = formula || resolve_formula(keg) @formula = formula || resolve_formula(keg)
@store = LinkageStore.new(keg.name, db) @store = LinkageStore.new(keg.name, db)
flush_cache_and_check_dylibs if rebuild_cache
end end
# 'Hash-type' cache values # 'Hash-type' cache values
@ -222,7 +223,7 @@ class LinkageChecker
# Updates data store with package path values # Updates data store with package path values
def store_dylibs! def store_dylibs!
store.update!( store.update!(
path_values: { array_values: {
system_dylibs: @system_dylibs, system_dylibs: @system_dylibs,
variable_dylibs: @variable_dylibs, variable_dylibs: @variable_dylibs,
broken_dylibs: @broken_dylibs, broken_dylibs: @broken_dylibs,