Skip to content

Commit

Permalink
Merge pull request #4980 from rolandwalker/cleanup_refactor
Browse files Browse the repository at this point in the history
detect cached downloads in doctor
  • Loading branch information
rolandwalker committed Jun 19, 2014
2 parents e6cca46 + bfc0756 commit f5f25ad
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 28 deletions.
90 changes: 63 additions & 27 deletions lib/cask/cli/cleanup.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,78 @@
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.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"
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

Expand Down
13 changes: 13 additions & 0 deletions lib/cask/cli/doctor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.run
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 )
Expand Down Expand Up @@ -160,6 +161,18 @@ def self.render_load_path(paths)
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
Expand Down
4 changes: 3 additions & 1 deletion test/cask/cli/cleanup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
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

Expand All @@ -19,6 +21,6 @@
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

0 comments on commit f5f25ad

Please sign in to comment.