Class: Homebrew::Cleanup Private

Inherits:
Object
  • Object
show all
Extended by:
Predicable
Defined in:
cleanup.rb

Overview

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.

Helper class for cleaning up the Homebrew cache.

Defined Under Namespace

Modules: CleanupRefinement

Constant Summary collapse

PERIODIC_CLEAN_FILE =

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

(HOMEBREW_CACHE/".cleaned").freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Predicable

attr_predicate

Constructor Details

#initialize(*args, dry_run: false, scrub: false, days: nil, cache: HOMEBREW_CACHE) ⇒ Cleanup

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 a new instance of Cleanup.



146
147
148
149
150
151
152
153
154
155
# File 'cleanup.rb', line 146

def initialize(*args, dry_run: false, scrub: false, days: nil, cache: HOMEBREW_CACHE)
  @disk_cleanup_size = 0
  @args = args
  @dry_run = dry_run
  @scrub = scrub
  @prune = days.present?
  @days = days || Homebrew::EnvConfig.cleanup_max_age_days.to_i
  @cache = cache
  @cleaned_up_paths = Set.new
end

Instance Attribute Details

#argsObject (readonly)

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.



144
145
146
# File 'cleanup.rb', line 144

def args
  @args
end

#cacheObject (readonly)

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.



144
145
146
# File 'cleanup.rb', line 144

def cache
  @cache
end

#daysObject (readonly)

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.



144
145
146
# File 'cleanup.rb', line 144

def days
  @days
end

#disk_cleanup_sizeObject (readonly)

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.



144
145
146
# File 'cleanup.rb', line 144

def disk_cleanup_size
  @disk_cleanup_size
end

Class Method Details

.install_formula_clean!(f) ⇒ 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.



157
158
159
160
161
162
163
164
165
166
# File 'cleanup.rb', line 157

def self.install_formula_clean!(f)
  return if Homebrew::EnvConfig.no_install_cleanup?

  cleanup = Cleanup.new
  if cleanup.periodic_clean_due?
    cleanup.periodic_clean!
  elsif f.latest_version_installed?
    cleanup.cleanup_formula(f)
  end
end

Instance Method Details

#clean!(quiet: false, periodic: 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.



187
188
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
222
223
224
225
226
227
228
229
230
# File 'cleanup.rb', line 187

def clean!(quiet: false, periodic: false)
  if args.empty?
    Formula.installed.sort_by(&:name).each do |formula|
      cleanup_formula(formula, quiet: quiet, ds_store: false, cache_db: false)
    end
    cleanup_cache
    cleanup_logs
    cleanup_lockfiles
    prune_prefix_symlinks_and_directories

    unless dry_run?
      cleanup_cache_db
      rm_ds_store
      HOMEBREW_CACHE.mkpath
      FileUtils.touch PERIODIC_CLEAN_FILE
    end

    # Cleaning up Ruby needs to be done last to avoid requiring additional
    # files afterwards. Additionally, don't allow it on periodic cleans to
    # avoid having to try to do a `brew install` when we've just deleted
    # the running Ruby process...
    return if periodic

    cleanup_portable_ruby
    cleanup_bootsnap
  else
    args.each do |arg|
      formula = begin
        Formulary.resolve(arg)
      rescue FormulaUnavailableError, TapFormulaAmbiguityError, TapFormulaWithOldnameAmbiguityError
        nil
      end

      cask = begin
        Cask::CaskLoader.load(arg)
      rescue Cask::CaskError
        nil
      end

      cleanup_formula(formula) if formula
      cleanup_cask(cask) if cask
    end
  end
end

#cleanup_bootsnapObject

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.



402
403
404
405
406
407
408
409
410
411
# File 'cleanup.rb', line 402

def cleanup_bootsnap
  bootsnap = cache/"bootsnap"
  return unless bootsnap.exist?

  if dry_run?
    puts "Would remove: #{bootsnap} (#{bootsnap.abv})"
  else
    FileUtils.rm_rf bootsnap
  end
end

#cleanup_cache(entries = nil) ⇒ 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.



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
# File 'cleanup.rb', line 301

def cleanup_cache(entries = nil)
  entries ||= [cache, cache/"Cask"].select(&:directory?).flat_map(&:children)

  entries.each do |path|
    next if path == PERIODIC_CLEAN_FILE

    FileUtils.chmod_R 0755, path if path.go_cache_directory? && !dry_run?
    next cleanup_path(path) { path.unlink } if path.incomplete?
    next cleanup_path(path) { FileUtils.rm_rf path } if path.nested_cache?

    if path.prune?(days)
      if path.file? || path.symlink?
        cleanup_path(path) { path.unlink }
      elsif path.directory? && path.to_s.include?("--")
        cleanup_path(path) { FileUtils.rm_rf path }
      end
      next
    end

    # If we've specified --prune don't do the (expensive) .stale? check.
    cleanup_path(path) { path.unlink } if !prune? && path.stale?(scrub: scrub?)
  end

  cleanup_unreferenced_downloads
end

#cleanup_cache_db(rack = nil) ⇒ 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.



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'cleanup.rb', line 413

def cleanup_cache_db(rack = nil)
  FileUtils.rm_rf [
    cache/"desc_cache.json",
    cache/"linkage.db",
    cache/"linkage.db.db",
  ]

  CacheStoreDatabase.use(:linkage) do |db|
    break unless db.created?

    db.each_key do |keg|
      next if rack.present? && !keg.start_with?("#{rack}/")
      next if File.directory?(keg)

      LinkageCacheStore.new(keg, db).delete!
    end
  end
end

#cleanup_cask(cask, ds_store: true) ⇒ 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.



245
246
247
248
249
# File 'cleanup.rb', line 245

def cleanup_cask(cask, ds_store: true)
  cleanup_cache(Pathname.glob(cache/"Cask/#{cask.token}--*"))
  rm_ds_store([cask.caskroom_path]) if ds_store
  cleanup_lockfiles(CaskLock.new(cask.token).path)
end

#cleanup_formula(formula, quiet: false, ds_store: true, cache_db: true) ⇒ 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.



236
237
238
239
240
241
242
243
# File 'cleanup.rb', line 236

def cleanup_formula(formula, quiet: false, ds_store: true, cache_db: true)
  formula.eligible_kegs_for_cleanup(quiet: quiet)
         .each(&method(:cleanup_keg))
  cleanup_cache(Pathname.glob(cache/"#{formula.name}--*"))
  rm_ds_store([formula.rack]) if ds_store
  cleanup_cache_db(formula.rack) if cache_db
  cleanup_lockfiles(FormulaLock.new(formula.name).path)
end

#cleanup_keg(keg) ⇒ 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.



251
252
253
254
255
256
# File 'cleanup.rb', line 251

def cleanup_keg(keg)
  cleanup_path(keg) { keg.uninstall(raise_failures: true) }
rescue Errno::EACCES => e
  opoo e.message
  unremovable_kegs << keg
end

#cleanup_lockfiles(*lockfiles) ⇒ 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.



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'cleanup.rb', line 343

def cleanup_lockfiles(*lockfiles)
  return if dry_run?

  lockfiles = HOMEBREW_LOCKS.children.select(&:file?) if lockfiles.empty? && HOMEBREW_LOCKS.directory?

  lockfiles.each do |file|
    next unless file.readable?
    next unless file.open(File::RDWR).flock(File::LOCK_EX | File::LOCK_NB)

    begin
      file.unlink
    ensure
      file.open(File::RDWR).flock(File::LOCK_UN) if file.exist?
    end
  end
end

#cleanup_logsObject

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.



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'cleanup.rb', line 258

def cleanup_logs
  return unless HOMEBREW_LOGS.directory?

  logs_days = if days > CLEANUP_DEFAULT_DAYS
    CLEANUP_DEFAULT_DAYS
  else
    days
  end

  HOMEBREW_LOGS.subdirs.each do |dir|
    cleanup_path(dir) { dir.rmtree } if dir.prune?(logs_days)
  end
end

#cleanup_path(path) ⇒ 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.



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'cleanup.rb', line 327

def cleanup_path(path)
  return unless path.exist?
  return unless @cleaned_up_paths.add?(path)

  disk_usage = path.disk_usage

  if dry_run?
    puts "Would remove: #{path} (#{path.abv})"
    @disk_cleanup_size += disk_usage
  else
    puts "Removing: #{path}... (#{path.abv})"
    yield
    @disk_cleanup_size += disk_usage - path.disk_usage
  end
end

#cleanup_portable_rubyObject

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.



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
389
390
391
392
393
394
395
396
397
398
399
400
# File 'cleanup.rb', line 360

def cleanup_portable_ruby
  rubies = [which("ruby"), which("ruby", ENV["HOMEBREW_PATH"])].compact
  system_ruby = Pathname.new("/usr/bin/ruby")
  rubies << system_ruby if system_ruby.exist?

  use_system_ruby = if Homebrew::EnvConfig.force_vendor_ruby?
    false
  elsif OS.mac?
    ENV["HOMEBREW_MACOS_SYSTEM_RUBY_NEW_ENOUGH"].present?
  else
    check_ruby_version = HOMEBREW_LIBRARY_PATH/"utils/ruby_check_version_script.rb"
    rubies.uniq.any? do |ruby|
      quiet_system ruby, "--enable-frozen-string-literal", "--disable=gems,did_you_mean,rubyopt",
                   check_ruby_version, HOMEBREW_REQUIRED_RUBY_VERSION
    end
  end

  vendor_dir = HOMEBREW_LIBRARY/"Homebrew/vendor"
  portable_ruby_latest_version = (vendor_dir/"portable-ruby-version").read.chomp

  portable_rubies_to_remove = []
  Pathname.glob(vendor_dir/"portable-ruby/*.*").select(&:directory?).each do |path|
    next if !use_system_ruby && portable_ruby_latest_version == path.basename.to_s

    portable_rubies_to_remove << path
    puts "Would remove: #{path} (#{path.abv})" if dry_run?
  end

  return if portable_rubies_to_remove.empty?

  bundler_path = vendor_dir/"bundle/ruby"
  if dry_run?
    puts Utils.popen_read("git", "-C", HOMEBREW_REPOSITORY, "clean", "-nx", bundler_path).chomp
  else
    puts Utils.popen_read("git", "-C", HOMEBREW_REPOSITORY, "clean", "-ffqx", bundler_path).chomp
  end

  return if dry_run?

  FileUtils.rm_rf portable_rubies_to_remove
end

#cleanup_unreferenced_downloadsObject

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.



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
# File 'cleanup.rb', line 272

def cleanup_unreferenced_downloads
  return if dry_run?
  return unless (cache/"downloads").directory?

  downloads = (cache/"downloads").children

  referenced_downloads = [cache, cache/"Cask"].select(&:directory?)
                                              .flat_map(&:children)
                                              .select(&:symlink?)
                                              .map(&:resolved_path)

  (downloads - referenced_downloads).each do |download|
    if download.incomplete?
      begin
        LockFile.new(download.basename).with_lock do
          download.unlink
        end
      rescue OperationInProgressError
        # Skip incomplete downloads which are still in progress.
        next
      end
    elsif download.directory?
      FileUtils.rm_rf download
    else
      download.unlink
    end
  end
end

#periodic_clean!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.



180
181
182
183
184
185
# File 'cleanup.rb', line 180

def periodic_clean!
  return false unless periodic_clean_due?

  ohai "`brew cleanup` has not been run in #{CLEANUP_DEFAULT_DAYS} days, running now..."
  clean!(quiet: true, periodic: true)
end

#periodic_clean_due?Boolean

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:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
# File 'cleanup.rb', line 168

def periodic_clean_due?
  return false if Homebrew::EnvConfig.no_install_cleanup?

  unless PERIODIC_CLEAN_FILE.exist?
    HOMEBREW_CACHE.mkpath
    FileUtils.touch PERIODIC_CLEAN_FILE
    return false
  end

  PERIODIC_CLEAN_FILE.mtime < CLEANUP_DEFAULT_DAYS.days.ago
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.



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'cleanup.rb', line 446

def prune_prefix_symlinks_and_directories
  ObserverPathnameExtension.reset_counts!

  dirs = []

  Keg::MUST_EXIST_SUBDIRECTORIES.each do |dir|
    next unless dir.directory?

    dir.find do |path|
      path.extend(ObserverPathnameExtension)
      if path.symlink?
        unless path.resolved_path_exists?
          path.uninstall_info if path.to_s.match?(Keg::INFOFILE_RX) && !dry_run?

          if dry_run?
            puts "Would remove (broken link): #{path}"
          else
            path.unlink
          end
        end
      elsif path.directory? && Keg::MUST_EXIST_SUBDIRECTORIES.exclude?(path)
        dirs << path
      end
    end
  end

  dirs.reverse_each do |d|
    if dry_run? && d.children.empty?
      puts "Would remove (empty directory): #{d}"
    else
      d.rmdir_if_possible
    end
  end

  return if dry_run?

  return if ObserverPathnameExtension.total.zero?

  n, d = ObserverPathnameExtension.counts
  print "Pruned #{n} symbolic links "
  print "and #{d} directories " if d.positive?
  puts "from #{HOMEBREW_PREFIX}"
end

#rm_ds_store(dirs = nil) ⇒ 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.



432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'cleanup.rb', line 432

def rm_ds_store(dirs = nil)
  dirs ||= Keg::MUST_EXIST_DIRECTORIES + [
    HOMEBREW_PREFIX/"Caskroom",
  ]
  dirs.select(&:directory?)
      .flat_map { |d| Pathname.glob("#{d}/**/.DS_Store") }
      .each do |dir|
        dir.unlink
      rescue Errno::EACCES
        # don't care if we can't delete a .DS_Store
        nil
      end
end

#unremovable_kegsObject

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.



232
233
234
# File 'cleanup.rb', line 232

def unremovable_kegs
  @unremovable_kegs ||= []
end