Module: Homebrew::Upgrade Private

Defined in:
upgrade.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 upgrading formulae.

Class Method Summary collapse

Class Method Details

.check_broken_dependents(installed_formulae) ⇒ Object

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.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'upgrade.rb', line 223

def check_broken_dependents(installed_formulae)
  CacheStoreDatabase.use(:linkage) do |db|
    installed_formulae.flat_map(&:runtime_installed_formula_dependents)
                      .uniq
                      .select do |f|
      keg = f.any_installed_keg
      next unless keg
      next unless keg.directory?

      LinkageChecker.new(keg, cache_db: db)
                    .broken_library_linkage?
    end.compact
  end
end

.check_installed_dependents(formulae, flags:, dry_run: false, installed_on_request: false, force_bottle: false, build_from_source_formulae: [], interactive: false, keep_tmp: false, force: false, debug: false, quiet: false, verbose: false) ⇒ Object

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.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'upgrade.rb', line 238

def check_installed_dependents(
  formulae,
  flags:,
  dry_run: false,
  installed_on_request: false,
  force_bottle: false,
  build_from_source_formulae: [],
  interactive: false,
  keep_tmp: false,
  force: false,
  debug: false,
  quiet: false,
  verbose: false
)
  return if Homebrew::EnvConfig.no_installed_dependents_check?

  installed_formulae = dry_run ? formulae : FormulaInstaller.installed.to_a
  return if installed_formulae.empty?

  already_broken_dependents = check_broken_dependents(installed_formulae)

  outdated_dependents =
    installed_formulae.flat_map(&:runtime_installed_formula_dependents)
                      .uniq
                      .select(&:outdated?)
  return if outdated_dependents.blank? && already_broken_dependents.blank?

  outdated_dependents -= installed_formulae if dry_run

  upgradeable_dependents =
    outdated_dependents.reject(&:pinned?)
                       .sort { |a, b| depends_on(a, b) }
  pinned_dependents =
    outdated_dependents.select(&:pinned?)
                       .sort { |a, b| depends_on(a, b) }

  if pinned_dependents.present?
    plural = "dependent".pluralize(pinned_dependents.count)
    ohai "Not upgrading #{pinned_dependents.count} pinned #{plural}:"
    puts(pinned_dependents.map do |f|
      "#{f.full_specified_name} #{f.pkg_version}"
    end.join(", "))
  end

  # Print the upgradable dependents.
  if upgradeable_dependents.blank?
    ohai "No outdated dependents to upgrade!" unless dry_run
  else
    plural = "dependent".pluralize(upgradeable_dependents.count)
    verb = dry_run ? "Would upgrade" : "Upgrading"
    ohai "#{verb} #{upgradeable_dependents.count} #{plural}:"
    formulae_upgrades = upgradeable_dependents.map do |f|
      name = f.full_specified_name
      if f.optlinked?
        "#{name} #{Keg.new(f.opt_prefix).version} -> #{f.pkg_version}"
      else
        "#{name} #{f.pkg_version}"
      end
    end
    puts formulae_upgrades.join(", ")
  end

  unless dry_run
    upgrade_formulae(
      upgradeable_dependents,
      flags:                      flags,
      installed_on_request:       installed_on_request,
      force_bottle:               force_bottle,
      build_from_source_formulae: build_from_source_formulae,
      interactive:                interactive,
      keep_tmp:                   keep_tmp,
      force:                      force,
      debug:                      debug,
      quiet:                      quiet,
      verbose:                    verbose,
    )
  end

  # Update installed formulae after upgrading
  installed_formulae = FormulaInstaller.installed.to_a

  # Assess the dependents tree again now we've upgraded.
  oh1 "Checking for dependents of upgraded formulae..." unless dry_run
  broken_dependents = check_broken_dependents(installed_formulae)
  if broken_dependents.blank?
    if dry_run
      ohai "No currently broken dependents found!"
      opoo "If they are broken by the upgrade they will also be upgraded or reinstalled."
    else
      ohai "No broken dependents found!"
    end
    return
  end

  reinstallable_broken_dependents =
    broken_dependents.reject(&:outdated?)
                     .reject(&:pinned?)
                     .sort { |a, b| depends_on(a, b) }
  outdated_pinned_broken_dependents =
    broken_dependents.select(&:outdated?)
                     .select(&:pinned?)
                     .sort { |a, b| depends_on(a, b) }

  # Print the pinned dependents.
  if outdated_pinned_broken_dependents.present?
    count = outdated_pinned_broken_dependents.count
    plural = "dependent".pluralize(outdated_pinned_broken_dependents.count)
    onoe "Not reinstalling #{count} broken and outdated, but pinned #{plural}:"
    $stderr.puts(outdated_pinned_broken_dependents.map do |f|
      "#{f.full_specified_name} #{f.pkg_version}"
    end.join(", "))
  end

  # Print the broken dependents.
  if reinstallable_broken_dependents.blank?
    ohai "No broken dependents to reinstall!"
  else
    count = reinstallable_broken_dependents.count
    plural = "dependent".pluralize(reinstallable_broken_dependents.count)
    ohai "Reinstalling #{count} broken #{plural} from source:"
    puts reinstallable_broken_dependents.map(&:full_specified_name)
                                        .join(", ")
  end

  return if dry_run

  reinstallable_broken_dependents.each do |formula|
    Homebrew.reinstall_formula(
      formula,
      flags:                      flags,
      force_bottle:               force_bottle,
      build_from_source_formulae: build_from_source_formulae + [formula.full_name],
      interactive:                interactive,
      keep_tmp:                   keep_tmp,
      force:                      force,
      debug:                      debug,
      quiet:                      quiet,
      verbose:                    verbose,
    )
  rescue FormulaInstallationAlreadyAttemptedError
    # We already attempted to reinstall f as part of the dependency tree of
    # another formula. In that case, don't generate an error, just move on.
    nil
  rescue CannotInstallFormulaError, DownloadError => e
    ofail e
  rescue BuildError => e
    e.dump(verbose: verbose)
    puts
    Homebrew.failed = true
  end
end

.install_formula(formula_installer, upgrade:) ⇒ Object

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.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'upgrade.rb', line 189

def install_formula(formula_installer, upgrade:)
  formula = formula_installer.formula

  formula_installer.check_installation_already_attempted

  if upgrade
    print_upgrade_message(formula, formula_installer.options)

    kegs = outdated_kegs(formula)
    linked_kegs = kegs.select(&:linked?)
  else
    formula.print_tap_action
  end

  # first we unlink the currently active keg for this formula otherwise it is
  # possible for the existing build to interfere with the build we are about to
  # do! Seriously, it happens!
  kegs.each(&:unlink) if kegs.present?

  formula_installer.install
  formula_installer.finish
rescue FormulaInstallationAlreadyAttemptedError
  # We already attempted to upgrade f as part of the dependency tree of
  # another formula. In that case, don't generate an error, just move on.
  nil
ensure
  # restore previous installation state if build failed
  begin
    linked_kegs.each(&:link) if linked_kegs.present? && !f.latest_version_installed?
  rescue
    nil
  end
end

.outdated_kegs(formula) ⇒ Object

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.



89
90
91
92
93
# File 'upgrade.rb', line 89

def outdated_kegs(formula)
  [formula, *formula.old_installed_formulae].map(&:linked_keg)
                                            .select(&:directory?)
                                            .map { |k| Keg.new(k.resolved_path) }
end

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.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'upgrade.rb', line 95

def print_dry_run_dependencies(formula, fi_deps)
  return if fi_deps.empty?

  plural = "dependency".pluralize(fi_deps.count)
  ohai "Would upgrade #{fi_deps.count} #{plural} for #{formula.full_specified_name}:"
  formulae_upgrades = fi_deps.map(&:first).map(&:to_formula).map do |f|
    name = f.full_specified_name
    if f.optlinked?
      "#{name} #{Keg.new(f.opt_prefix).version} -> #{f.pkg_version}"
    else
      "#{name} #{f.pkg_version}"
    end
  end
  puts formulae_upgrades.join(", ")
end

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.



111
112
113
114
115
116
117
118
119
120
121
# File 'upgrade.rb', line 111

def print_upgrade_message(formula, fi_options)
  version_upgrade = if formula.optlinked?
    "#{Keg.new(formula.opt_prefix).version} -> #{formula.pkg_version}"
  else
    "-> #{formula.pkg_version}"
  end
  oh1 <<~EOS
    Upgrading #{Formatter.identifier(formula.full_specified_name)}
      #{version_upgrade} #{fi_options.to_a.join(" ")}
  EOS
end

.upgrade_formulae(formulae_to_install, flags:, dry_run: false, installed_on_request: false, force_bottle: false, build_from_source_formulae: [], interactive: false, keep_tmp: false, force: false, debug: false, quiet: false, verbose: false) ⇒ Object

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.



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
# File 'upgrade.rb', line 18

def upgrade_formulae(
  formulae_to_install,
  flags:,
  dry_run: false,
  installed_on_request: false,
  force_bottle: false,
  build_from_source_formulae: [],
  interactive: false,
  keep_tmp: false,
  force: false,
  debug: false,
  quiet: false,
  verbose: false
)
  return if formulae_to_install.empty?

  # Sort keg-only before non-keg-only formulae to avoid any needless conflicts
  # with outdated, non-keg-only versions of formulae being upgraded.
  formulae_to_install.sort! do |a, b|
    if !a.keg_only? && b.keg_only?
      1
    elsif a.keg_only? && !b.keg_only?
      -1
    else
      0
    end
  end

  dependency_graph = Utils::TopologicalHash.graph_package_dependencies(formulae_to_install)
  begin
    formulae_to_install = dependency_graph.tsort & formulae_to_install
  rescue TSort::Cyclic
    raise CyclicDependencyError, dependency_graph.strongly_connected_components if Homebrew::EnvConfig.developer?
  end

  formula_installers = formulae_to_install.map do |formula|
    Migrator.migrate_if_needed(formula, force: force, dry_run: dry_run)
    begin
      fi = create_formula_installer(
        formula,
        flags:                      flags,
        installed_on_request:       installed_on_request,
        force_bottle:               force_bottle,
        build_from_source_formulae: build_from_source_formulae,
        interactive:                interactive,
        keep_tmp:                   keep_tmp,
        force:                      force,
        debug:                      debug,
        quiet:                      quiet,
        verbose:                    verbose,
      )
      unless dry_run
        fi.prelude
        fi.fetch
      end
      fi
    rescue CannotInstallFormulaError => e
      ofail e
      nil
    rescue UnsatisfiedRequirements, DownloadError => e
      ofail "#{formula}: #{e}"
      nil
    end
  end.compact

  formula_installers.each do |fi|
    upgrade_formula(fi, dry_run: dry_run, verbose: verbose)
    Cleanup.install_formula_clean!(fi.formula, dry_run: dry_run)
  end
end