From f65d422b829b81055261345d96f5c37f78b23138 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Thu, 19 Jun 2014 07:24:10 -0400 Subject: [PATCH 1/2] refactor `brew cask cleanup` Divide into more tractable methods. No functional change. Change apparent typo in test regular expression --- lib/cask/cli/cleanup.rb | 85 ++++++++++++++++++++++++----------- test/cask/cli/cleanup_test.rb | 4 +- 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/lib/cask/cli/cleanup.rb b/lib/cask/cli/cleanup.rb index 99c857bd45..4d2ac5ce30 100644 --- a/lib/cask/cli/cleanup.rb +++ b/lib/cask/cli/cleanup.rb @@ -1,42 +1,73 @@ class Cask::CLI::Cleanup + + OUTDATED_DAYS = 10 + OUTDATED_TIMESTAMP = Time.now - (60 * 60 * 24 * OUTDATED_DAYS) + def self.run(*_ignored) remove_dead_symlinks - remove_cached_downloads + remove_all_cache_files + end + + def self.cache_symlinks + HOMEBREW_CACHE_CASKS.children.select(&:symlink?) + end + + def self.dead_symlinks + cache_symlinks.reject(&:exist?) + end + + def self.cache_incompletes(outdated=nil) + cache_symlinks.collect do |symlink| + incomplete_file = Dir.chdir HOMEBREW_CACHE_CASKS do + f = symlink.readlink + f = f.realpath if f.exist? + Pathname.new(f.to_s.concat('.incomplete')) + end + incomplete_file = nil unless incomplete_file.exist? + incomplete_file = nil if outdated and incomplete_file and incomplete_file.stat.mtime > OUTDATED_TIMESTAMP + incomplete_file + end.compact + end + + def self.cache_completes(outdated=nil) + cache_symlinks.collect do |symlink| + file = Dir.chdir HOMEBREW_CACHE_CASKS do + f = symlink.readlink + f.exist? ? f.realpath : f + end + file = nil unless file.exist? + if outdated and file and file.stat.mtime > OUTDATED_TIMESTAMP + file = nil + symlink = nil + end + [ symlink, file ] + end.flatten.compact.sort { |x,y| x.to_s.count(File::SEPARATOR) <=> y.to_s.count(File::SEPARATOR) } + end + + # will include dead symlinks if they aren't handled separately + def self.all_cache_files(outdated=nil) + cache_incompletes(outdated) + cache_completes(outdated) end def self.remove_dead_symlinks ohai "Removing dead symlinks" - HOMEBREW_CACHE_CASKS.children.select(&:symlink?).each do |symlink| - unless symlink.exist? - puts symlink - symlink.unlink - end + to_delete = dead_symlinks + puts "Nothing to do" unless to_delete.count > 0 + to_delete.each do |item| + puts item + item.unlink end end - def self.remove_cached_downloads + def self.remove_all_cache_files message = "Removing cached downloads" - days = 10 - outdated_timestamp = Time.now - (60 * 60 * 24 * days) - if Cask.outdated - message.concat(" older than #{days} days old") - end + message.concat " older than #{OUTDATED_DAYS} days old" if Cask.outdated ohai message - HOMEBREW_CACHE_CASKS.children.select(&:symlink?).each do |symlink| - file = Dir.chdir HOMEBREW_CACHE_CASKS do - symlink.readlink.realpath - end - if !Cask.outdated or file.stat.mtime < outdated_timestamp - puts file - file.unlink - symlink.unlink - end - incomplete_file = Pathname.new(file.to_s.concat('.incomplete')) - if incomplete_file.exist? and - (!Cask.outdated or incomplete_file.stat.mtime < outdated_timestamp) - puts incomplete_file - incomplete_file.unlink - end + to_delete = all_cache_files(Cask.outdated) + puts "Nothing to do" unless to_delete.count > 0 + to_delete.each do |item| + puts item + item.unlink end end diff --git a/test/cask/cli/cleanup_test.rb b/test/cask/cli/cleanup_test.rb index 82904bdae5..7f893cd3ed 100644 --- a/test/cask/cli/cleanup_test.rb +++ b/test/cask/cli/cleanup_test.rb @@ -8,7 +8,9 @@ describe Cask::CLI::Cleanup do end out.must_equal <<-OUTPUT.undent ==> Removing dead symlinks + Nothing to do ==> Removing cached downloads older than 10 days old + Nothing to do OUTPUT end @@ -19,6 +21,6 @@ describe Cask::CLI::Cleanup do out, err = capture_io do Cask::CLI::Cleanup.run end - out.must_match(/^==> Removing dead symlinks\n==> Removing cached downloads\n\//) + out.must_match(/^==> Removing dead symlinks\nNothing to do\n==> Removing cached downloads\n/) end end From bfc07561e5ff8bbe7582db1d961c5a15a50a0746 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Thu, 19 Jun 2014 09:12:22 -0400 Subject: [PATCH 2/2] detect cached downloads in doctor and recommend "brew cask cleanup" if present. Closes #4857 --- lib/cask/cli/cleanup.rb | 5 +++++ lib/cask/cli/doctor.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/cask/cli/cleanup.rb b/lib/cask/cli/cleanup.rb index 4d2ac5ce30..3de2664328 100644 --- a/lib/cask/cli/cleanup.rb +++ b/lib/cask/cli/cleanup.rb @@ -49,6 +49,11 @@ class Cask::CLI::Cleanup cache_incompletes(outdated) + cache_completes(outdated) end + def self.space_in_megs(files) + bytes = files.map { |f| begin File.size(f); rescue; 0; end }.reduce(&:+) || 0 + sprintf '%0.2f', bytes / (1024.0 * 1024.0) + end + def self.remove_dead_symlinks ohai "Removing dead symlinks" to_delete = dead_symlinks diff --git a/lib/cask/cli/doctor.rb b/lib/cask/cli/doctor.rb index 483d5cc35e..3c106774ac 100644 --- a/lib/cask/cli/doctor.rb +++ b/lib/cask/cli/doctor.rb @@ -11,6 +11,7 @@ class Cask::CLI::Doctor ohai 'Homebrew Origin:', render_with_none_as_error( homebrew_origin ) ohai 'Homebrew-cask Version:', render_with_none_as_error( HOMEBREW_CASK_VERSION ) ohai 'Homebrew-cask Install Location:', render_install_location( HOMEBREW_CASK_VERSION ) + ohai 'Homebrew-cask Cached Downloads:', render_cached_downloads ohai 'Homebrew-cask Default Tap Path:', render_tap_paths( fq_default_tap ) ohai 'Homebrew-cask Alternate Cask Taps:', render_tap_paths( alt_taps ) ohai 'Homebrew-cask Default Tap Cask Count:', render_with_none_as_error( default_cask_count ) @@ -160,6 +161,18 @@ class Cask::CLI::Doctor copy end + def self.render_cached_downloads + files = Cask::CLI::Cleanup.all_cache_files + count = files.count + space = Cask::CLI::Cleanup.space_in_megs files + [ + HOMEBREW_CACHE, + HOMEBREW_CACHE_CASKS, + count.to_s.concat(" files").concat(count == 0 ? '' : %Q{ #{error_string %Q{warning: run "brew cask cleanup"}}}), + space.to_s.concat(" megs").concat(count == 0 ? '' : %Q{ #{error_string %Q{warning: run "brew cask cleanup"}}}), + ] + end + def self.help "checks for configuration issues" end