Module: InstalledDependents Private
- Defined in:
- installed_dependents.rb
Overview
This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Helper functions for installed dependents.
Class Method Summary collapse
-
.build_dependents_by_name(formulae, installed, removed_kegs: []) ⇒ Hash{String => Array<String>}
private
The names of the build dependencies to keep, each with the names of the
formulaethat need it. -
.commands_to_drop_dependencies(dependency_names, formulae = Formula.installed, required_by: nil) ⇒ Array<String>
private
Return upgrade commands that free dependencies.
-
.find_installed_build_dependents(kegs) ⇒ Array<(Array<Keg>, Array<String>)>?
private
Given an array of kegs, this method finds the installed formulae that
brew autoremovekeeps them for as build dependencies. -
.find_some_installed_dependents(kegs, casks: [], formulae: Formula.installed) ⇒ Array<(Array<Keg>, Array<String>)>?
private
Given an array of kegs, this method will try to find some other kegs or casks that depend on them.
-
.formulae_safe_to_remove(removable, installed) ⇒ Array<Formula>
private
Filters out the dependencies of everything that stays installed, including earlier removal candidates.
-
.names_of_dependencies_to_keep(formulae, installed) ⇒ Set<String>
private
The names of the runtime dependencies of
formulaeand of the build dependencies in InstalledDependents.build_dependents_by_name. -
.prune_stale_dependencies(formulae, dry_run: false, removed_names: []) ⇒ void
private
Remove installed dependencies that a fresh receipt for the same keg would omit.
Class Method Details
.build_dependents_by_name(formulae, installed, removed_kegs: []) ⇒ Hash{String => Array<String>}
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
The names of the build dependencies to keep, each with the names of the
formulae that need it. Build dependencies are kept for Formula built
from source or without a usable bottle. Those built from source are
always kept, as a bottle may not be usable by the next upgrade and they
take a while to install again.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'installed_dependents.rb', line 157 def build_dependents_by_name(formulae, installed, removed_kegs: []) dependents = T.let({}, T::Hash[String, T::Array[String]]) names_by_dependency = T.let({}, T::Hash[String, T::Array[String]]) built_from_source_names = installed.reject { |f| f.any_installed_keg&.tab&.poured_from_bottle } .flat_map(&:possible_names) .to_set formulae.each do |formula| keg = formula.any_installed_keg # Read the install receipt that is left once `removed_kegs` are gone. keg = (formula.installed_kegs - removed_kegs).last if keg && removed_kegs.include?(keg) tab = keg&.tab # Pouring a bottle doesn't use build dependencies pours_bottle = tab&.poured_from_bottle && formula.bottled? && formula.pour_bottle? formula.deps.select(&:build?).each do |dep| next if pours_bottle && built_from_source_names.exclude?(Utils.name_from_full_name(dep.name)) # Many formulae share a build dependency, so look each one up once. dependency_names = names_by_dependency[dep.name] ||= begin [dep.to_formula.name] rescue TapFormulaAmbiguityError => e e.loaders.map(&:name) rescue Homebrew::UntrustedTapError, FormulaSpecificationError, FormulaUnavailableError dep.formula_name? ? dep.formula_loaders.map(&:name) : [] end dependency_names.each do |dependency_name| dependents[dependency_name] = dependents.fetch(dependency_name, []) + [formula.name] end end end dependents end |
.commands_to_drop_dependencies(dependency_names, formulae = Formula.installed, required_by: nil) ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Return upgrade commands that free dependencies. dependency_names maps possible names to full names.
For uninstall, every dependent in required_by must drop all its targets.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'installed_dependents.rb', line 212 def commands_to_drop_dependencies(dependency_names, formulae = Formula.installed, required_by: nil) kept_dependencies = T.let([], T::Array[String]) droppers = T.let([], T::Array[[Formula, T::Array[String]]]) full_names = ->(deps) { deps.filter_map { |dep| dependency_names[Utils.name_from_full_name(dep.name)] } } formulae.each do |dependent| next if required_by&.exclude?(dependent.name) installed_dependency_names = full_names.call(dependent.runtime_dependencies(stop_at_untrusted: true)) next if installed_dependency_names.empty? # Unlike `runtime_dependencies`, this raises when a dependency cannot be loaded. declared_dependencies = dependent.recursive_dependencies do |formula, dep| next Dependable::PRUNE if dep.build? || dep.test? next if dep.required? || !formula.is_a?(Formula) # Upgrades carry both inherited and installed options into the current formula. = formula.build. | Tab.for_formula(formula). Dependable::PRUNE if BuildOptions.new(, formula.).without?(dep) end declared_dependency_names = full_names.call(declared_dependencies) dropped = installed_dependency_names - declared_dependency_names if dropped.any? && dependent.outdated? && !dependent.disabled? droppers << [dependent, dropped] else kept_dependencies.concat(dropped) end kept_dependencies.concat(declared_dependency_names) rescue FormulaUnavailableError, TapFormulaAmbiguityError, FormulaSpecificationError, Homebrew::UntrustedTapError # Its latest dependencies cannot be read, so this dependent keeps what its receipt lists. kept_dependencies.concat(installed_dependency_names.to_a) end # Casks and dependents that list a target under an alias can also block uninstall. return [] if required_by && (kept_dependencies.any? || droppers.size != required_by.size) # A command only helps when no other dependent keeps requiring what it drops. outdated = droppers.filter_map { |dependent, dropped| dependent if (dropped - kept_dependencies).any? } { "unpin" => outdated.select(&:pinned?), "upgrade" => outdated, }.filter_map do |command, dependents| ["brew", command, *dependents.map(&:full_name).uniq.sort].join(" ") if dependents.any? end end |
.find_installed_build_dependents(kegs) ⇒ Array<(Array<Keg>, Array<String>)>?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Given an array of kegs, this method finds the installed formulae that
brew autoremove keeps them for as build dependencies. If there are any,
it returns:
- the kegs in the passed array that are kept for them
- the names of those formulae.
If there are none, it returns nil.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'installed_dependents.rb', line 116 def find_installed_build_dependents(kegs) # An older keg can go while another one stays, unless `opt` points to it. last_or_linked_kegs = kegs.select do |keg| keg.optlinked? || keg.rack.subdirs.all? { |dir| kegs.include?(Keg.new(dir)) } end names_by_keg = last_or_linked_kegs.to_h do |keg| [keg, keg.to_formula.possible_names] rescue FormulaUnavailableError, TapFormulaAmbiguityError, Homebrew::UntrustedTapError, FormulaSpecificationError [keg, [keg.name]] end keg_names = kegs.map(&:name) # Don't include build dependencies of formulae that keep none of their kegs. remaining_formulae = Formula.installed.reject do |f| keg_names.intersect?(f.possible_names) && (f.installed_kegs - kegs).empty? end build_dependents = build_dependents_by_name(remaining_formulae, Formula.installed, removed_kegs: kegs) dependents_by_keg = names_by_keg.transform_values { |names| build_dependents.values_at(*names).flatten.compact } .reject { |_, dependents| dependents.empty? } return if dependents_by_keg.empty? [dependents_by_keg.keys, dependents_by_keg.values.flatten.uniq.sort] end |
.find_some_installed_dependents(kegs, casks: [], formulae: Formula.installed) ⇒ Array<(Array<Keg>, Array<String>)>?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Given an array of kegs, this method will try to find some other kegs or casks that depend on them. If it does, it returns:
- some kegs in the passed array that have installed dependents
- some installed dependents of those kegs.
If it doesn't, it returns nil.
Note that nil will be returned if the only installed dependents of the passed kegs are other kegs in the array or casks present in the casks parameter, or formulae excluded from the formulae parameter.
For efficiency, we don't bother trying to get complete data.
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 |
# File 'installed_dependents.rb', line 30 def find_some_installed_dependents(kegs, casks: [], formulae: Formula.installed) keg_names = kegs.select(&:optlinked?).map(&:name) keg_formulae = [] kegs_by_source = kegs.group_by do |keg| # First, attempt to resolve the keg to a formula # to get up-to-date name and tap information. f = keg.to_formula keg_formulae << f [f.name, f.tap] rescue # If the formula for the keg can't be found, # fall back to the information in the tab. [keg.name, keg.tab.tap] end all_required_kegs = Set.new all_dependents = [] # Don't include dependencies of kegs that were in the given array. dependents_to_check = (formulae - keg_formulae) + (Cask::Caskroom.casks - casks) dependents_to_check.each do |dependent| required_kegs = case dependent when Formula dependent.missing_dependencies(hide: keg_names).flat_map do |d| formula = begin d.to_installed_formula rescue FormulaUnavailableError, Homebrew::UntrustedTapError, TapFormulaAmbiguityError => e next [] unless d.formula_name? loaders = e.is_a?(TapFormulaAmbiguityError) ? e.loaders : d.formula_loaders matching_kegs = loaders.filter_map do |loader| kegs_by_source[[loader.name, loader.tap]]&.max_by(&:scheme_and_version) end if matching_kegs.empty? fallback_names = [Utils.name_from_full_name(d.name)] fallback_names.concat(loaders.map { |loader| Utils.name_from_full_name(loader.name) }) known_taps = loaders.filter_map(&:tap) dependency_tap = d.tap known_taps << dependency_tap if dependency_tap matching_kegs = kegs_by_source.filter_map do |(name, tap), source_kegs| next unless fallback_names.include?(name) next if tap && known_taps.any? && known_taps.exclude?(tap) source_kegs.max_by(&:scheme_and_version) end end next matching_kegs end f_kegs = kegs_by_source[[formula.name, formula.tap]] [f_kegs&.max_by(&:scheme_and_version)].compact end when Cask::Cask # When checking for cask dependents, we don't care about missing or non-runtime dependencies CaskDependent.new(dependent).runtime_dependencies.filter_map do |d| source = begin f = d.to_installed_formula [f.name, f.tap] rescue FormulaUnavailableError, Homebrew::UntrustedTapError [Utils.name_from_full_name(d.name), d.tap] end kegs_by_source[source]&.max_by(&:scheme_and_version) end end next if required_kegs.empty? all_required_kegs += required_kegs all_dependents << dependent.to_s end return if all_required_kegs.empty? return if all_dependents.empty? [all_required_kegs.to_a, all_dependents.sort] end |
.formulae_safe_to_remove(removable, installed) ⇒ Array<Formula>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Filters out the dependencies of everything that stays installed, including earlier removal candidates.
193 194 195 196 197 198 199 200 201 202 203 |
# File 'installed_dependents.rb', line 193 def formulae_safe_to_remove(removable, installed) # Each pass keeps at least one more formula or is the last one. removable.length.times do names_to_keep = names_of_dependencies_to_keep(installed - removable, installed) still_removable = removable.reject { |f| names_to_keep.intersect?(f.possible_names) } break if still_removable.length == removable.length removable = still_removable end removable end |
.names_of_dependencies_to_keep(formulae, installed) ⇒ Set<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
The names of the runtime dependencies of formulae and of the build
dependencies in build_dependents_by_name.
142 143 144 145 146 |
# File 'installed_dependents.rb', line 142 def names_of_dependencies_to_keep(formulae, installed) build_dependents_by_name(formulae, installed).keys .to_set .merge(formulae.flat_map(&:installed_runtime_dependency_names)) end |
.prune_stale_dependencies(formulae, dry_run: false, removed_names: []) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
Remove installed dependencies that a fresh receipt for the same keg would omit. Check explicit uninstall targets even when another formula still requires them.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'installed_dependents.rb', line 261 def prune_stale_dependencies(formulae, dry_run: false, removed_names: []) declared_names = formulae.to_h { |f| [f.name, f.deps.map { |dep| Utils.name_from_full_name(dep.name) }] } declared_somewhere = Set.new receipt_name = ->(dep) { Utils.name_from_full_name(dep["full_name"]) if dep.is_a?(Hash) && dep["full_name"] } candidates = formulae.filter_map do |dependent| next unless (keg = dependent.any_installed_keg) next unless (tab_dependencies = keg.runtime_dependencies) tab_names = tab_dependencies.filter_map(&receipt_name) # The dependent or one of its dependencies declares nearly every dependency. declared = declared_names.values_at(dependent.name, *tab_names).flatten declared_somewhere.merge(tab_names & declared) undeclared = tab_names - declared [dependent, keg, tab_dependencies, undeclared] if undeclared.any? end return if candidates.empty? declared_somewhere.subtract(removed_names) # A receipt can list a formula under the name it had before it was renamed. possible_names = T.let({}, T::Hash[String, T::Array[String]]) current_names = ->(deps) { deps.flat_map { |dep| possible_names[dep.name] ||= dep.to_formula.possible_names } } pruned = T.let(false, T::Boolean) # Share the linkage cache across kegs that need checking. CacheStoreDatabase.use(:linkage) do candidates.each do |dependent, keg, tab_dependencies, undeclared| stale_names = undeclared.reject { |name| declared_somewhere.include?(name) } .select { |name| (HOMEBREW_PREFIX/"opt"/name).directory? } next if stale_names.empty? || keg.version != dependent.pkg_version # An alias may have moved on to another formula while the keg still uses the one in the receipt. next if dependent.deps.any? { |dep| dep.to_formula.name != Utils.name_from_full_name(dep.name) } stale_names -= current_names.call(dependent.recursive_dependencies) next if stale_names.empty? # A receipt also lists what the keg links to, whether or not a formula declares it. stale_names -= current_names.call(dependent.runtime_dependencies(read_from_tab: false)) next if stale_names.empty? tab = keg.tab tab.runtime_dependencies = tab_dependencies.delete_if { |dep| stale_names.include?(receipt_name.call(dep)) } tab.write unless dry_run pruned = true rescue FormulaUnavailableError, TapFormulaAmbiguityError, FormulaSpecificationError, Homebrew::UntrustedTapError # The dependency tree cannot be read, so the receipt stays as it is. next end end Formula.clear_cache if pruned end |