Class: Homebrew::Bundle::ParallelInstaller Private

Inherits:
Object
  • Object
show all
Defined in:
bundle/parallel_installer.rb

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Instance Method Summary collapse

Constructor Details

#initialize(entries, jobs:, no_upgrade:, verbose:, force:, quiet:) ⇒ 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.

Parameters:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'bundle/parallel_installer.rb', line 25

def initialize(entries, jobs:, no_upgrade:, verbose:, force:, quiet:)
  @entries = entries
  @jobs = jobs
  @no_upgrade = no_upgrade
  @verbose = verbose
  @force = force
  @quiet = quiet
  @pool = T.let(Concurrent::FixedThreadPool.new(jobs), Concurrent::FixedThreadPool)
  @output_mutex = T.let(Monitor.new, Monitor)
  # Cask installs may trigger interactive sudo prompts that write
  # directly to the terminal.  Serialize them so Password: prompts
  # don't interleave with status output from other workers.
  @cask_install_mutex = T.let(Mutex.new, Mutex)
end

Instance Method Details

#build_dependency_map(entries) ⇒ Hash{String => 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.

Parameters:

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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 'bundle/parallel_installer.rb', line 107

def build_dependency_map(entries)
  installed_taps = Homebrew::Bundle::Tap.installed_taps
  attestation_formula = if Homebrew::EnvConfig.verify_attestations?
    entries.find { |entry| entry.cls == Homebrew::Bundle::Brew && entry.name == "gh" }
  end

  # Phase 1: Map both full and short names so dep lookups work either way.
  entry_name_map = entries.each_with_object({}) do |entry, map|
    map[entry.name] = entry.name
    map[normalize_formula_name(entry.name)] = entry.name
  end

  # Phase 2: Direct dependencies declared in the Brewfile. Determines
  # install ordering (entry A must finish before entry B starts).
  brewfile_deps = T.let({}, T::Hash[String, T::Array[String]])
  entries.each do |entry|
    deps = case entry.cls.name
    when "Homebrew::Bundle::Brew"
      Homebrew::Bundle::Brew.formula_dep_names(entry.name)
    when "Homebrew::Bundle::Cask"
      Homebrew::Bundle::Cask.formula_dependencies([entry.full_name])
    else
      []
    end

    # Entries from non-default taps depend on the tap being installed first.
    deps += Homebrew::Bundle::Installer.tap_dependencies(entry, entries:, installed_taps:)
    if attestation_formula && [Homebrew::Bundle::Brew, Homebrew::Bundle::Cask].include?(entry.cls) &&
       entry.name != attestation_formula.name
      deps << attestation_formula.name
    end

    brewfile_deps[entry.name] = deps
  end

  # Phase 3: Recursive dependency sets for lock conflict detection.
  # `FormulaInstaller#lock` locks all recursive dependencies before
  # installing, even when pouring bottles.
  cask_names = T.let(entries.select { |e| e.cls == Homebrew::Bundle::Cask }.to_set(&:name), T::Set[String])
  recursive_deps = T.let({}, T::Hash[String, T::Set[String]])
  entries.each do |entry|
    recursive_deps[entry.name] = case entry.cls.name
    when "Homebrew::Bundle::Brew"
      Homebrew::Bundle::Brew.recursive_dep_names(entry.name)
    when "Homebrew::Bundle::Cask"
      cask_dep_names(entry.name, cask_names)
    else
      Set.new
    end
  end

  # Phase 3.5: formulae racing for an undeclared implicit dependency (e.g. a
  # Linux sandbox executable) wait on just the first one, not on each other.
  implicit_pioneer = T.let(nil, T.nilable(String))
  unless DependencyCollector.new.implicit_dependency_names.empty?
    implicit_pioneer = entries.find { |entry| entry.cls == Homebrew::Bundle::Brew }&.name
  end

  # Phase 4: Merge explicit ordering and implicit lock conflicts.
  entries.each_with_object({}) do |entry, map|
    depends_on = brewfile_deps.fetch(entry.name).each_with_object(Set.new) do |dep, set|
      name = entry_name_map[dep] || entry_name_map[normalize_formula_name(dep)]
      set << name if name.present? && name != entry.name
    end

    # Later entries wait for earlier ones when they share any recursive dep.
    entry_rdeps = recursive_deps.fetch(entry.name)
    entries.each do |earlier|
      break if earlier.name == entry.name
      next if depends_on.include?(earlier.name)

      earlier_rdeps = recursive_deps.fetch(earlier.name)
      depends_on << earlier.name if entry_rdeps.intersect?(earlier_rdeps)
    end

    if implicit_pioneer && entry.name != implicit_pioneer && entry.cls == Homebrew::Bundle::Brew
      depends_on << implicit_pioneer
    end

    map[entry.name] = depends_on
  end
end

#run!Array<(Integer, Integer)>

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.

Returns:



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
# File 'bundle/parallel_installer.rb', line 41

def run!
  success = 0
  failure = 0

  tap_entries, pending_entries = @entries.partition { |entry| entry.cls == Homebrew::Bundle::Tap }
  tap_entries.each_slice(@jobs) do |batch|
    tap_success, tap_failure = install_entries_parallel!(batch)
    success += tap_success
    failure += tap_failure
  end
  ::Tap.clear_cache if tap_entries.present?

  require "tap"
  installed_taps = Homebrew::Bundle::Tap.installed_taps
  pending_entries.each do |entry|
    tap_with_name = if entry.cls == Homebrew::Bundle::Brew
      ::Tap.with_formula_name(entry.full_name)
    elsif entry.cls == Homebrew::Bundle::Cask
      ::Tap.with_cask_token(entry.full_name)
    end
    next unless tap_with_name

    tap = tap_with_name.first
    next if installed_taps.include?(tap.name) || tap_entries.any? { |tap_entry| tap_entry.name == tap.name }

    tap.ensure_installed!
    installed_taps << tap.name
  end

  prepare_attestation_verification!(pending_entries)
  dependency_map = build_dependency_map(pending_entries)
  completed = T.let(Set.new, T::Set[String])
  until pending_entries.empty?
    ready_entries = pending_entries.select do |entry|
      dependency_map.fetch(entry.name, Set.new).all? { |dependency| completed.include?(dependency) }
    end

    if ready_entries.empty?
      pending_entries.each do |entry|
        installed = install_entry!(entry)
        completed << entry.name
        if installed
          success += 1
        else
          failure += 1
        end
      end
      break
    end

    batch = ready_entries.take(@jobs)
    batch_success, batch_failure = install_entries_parallel!(batch)
    success += batch_success
    failure += batch_failure

    pending_entries -= batch
    completed.merge(batch.map(&:name))
  end

  [success, failure]
ensure
  @pool.shutdown
  @pool.wait_for_termination
end

#write_output(message, stream: $stdout) ⇒ 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.

Parameters:

  • message (String)
  • stream (IO) (defaults to: $stdout)


191
192
193
194
195
196
197
198
199
200
201
# File 'bundle/parallel_installer.rb', line 191

def write_output(message, stream: $stdout)
  @output_mutex.synchronize do
    # Interactive installers can leave ONLCR disabled, so use CRLF to
    # ensure terminal status output returns to column 0.
    if stream.tty?
      stream.write(message, "\r\n")
    else
      stream.puts(message)
    end
  end
end