Migrate to new cache directory. (#23265)

This commit is contained in:
Markus Reiter 2016-07-30 02:41:00 +02:00
parent 09582545d3
commit 1381528f07
8 changed files with 56 additions and 104 deletions

View File

@ -1,5 +1,3 @@
HOMEBREW_CACHE_CASKS = HOMEBREW_CACHE.join("Casks")
module Hbc; end
require "hbc/extend"
@ -51,8 +49,33 @@ module Hbc
def self.init
odebug "Creating directories"
HOMEBREW_CACHE.mkpath unless HOMEBREW_CACHE.exist?
HOMEBREW_CACHE_CASKS.mkpath unless HOMEBREW_CACHE_CASKS.exist?
# cache
Hbc.cache.mkpath unless Hbc.cache.exist?
if Hbc.legacy_cache.exist?
ohai "Migrating cached files to #{Hbc.cache}..."
Hbc.legacy_cache.children.select(&:symlink?).each do |symlink|
file = symlink.readlink
new_name = file.basename
.sub(%r{\-((?:(\d|#{Hbc::DSL::Version::DIVIDER_REGEX})*\-\2*)*[^\-]+)$}x,
'--\1')
renamed_file = Hbc.cache.join(new_name)
if file.exist?
puts "#{file} -> #{renamed_file}"
FileUtils.mv(file, renamed_file)
end
FileUtils.rm(symlink)
end
FileUtils.remove_entry_secure(Hbc.legacy_cache)
end
# caskroom
unless caskroom.exist?
ohai "Creating Caskroom at #{caskroom}"
current_user = Hbc::Utils.current_user

View File

@ -19,79 +19,47 @@ class Hbc::CLI::Cleanup < Hbc::CLI::Base
end
def self.default
@default ||= new(HOMEBREW_CACHE_CASKS, Hbc.cleanup_outdated)
@default ||= new(Hbc.cache, Hbc.cleanup_outdated)
end
attr_reader :cache_location, :outdated_only
def initialize(cache_location, outdated_only)
@cache_location = Pathname(cache_location)
@cache_location = Pathname.new(cache_location)
@outdated_only = outdated_only
end
def cleanup!
remove_dead_symlinks
remove_all_cache_files
end
def cache_symlinks
cache_location.children.select(&:symlink?)
def cache_files
return [] unless cache_location.exist?
cache_location.children
.map(&method(:Pathname))
.reject(&method(:outdated?))
end
def dead_symlinks
cache_symlinks.reject(&:exist?)
def outdated?(file)
outdated_only && file && file.stat.mtime > OUTDATED_TIMESTAMP
end
def cache_incompletes
cache_symlinks.collect { |symlink|
incomplete_file = Dir.chdir cache_location 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_only && incomplete_file && incomplete_file.stat.mtime > OUTDATED_TIMESTAMP
incomplete_file
}.compact
cache_files.select { |file| file.extname == ".incomplete" }
end
def cache_completes
completes = cache_symlinks.collect { |symlink|
file = Dir.chdir cache_location do
f = symlink.readlink
f.exist? ? f.realpath : f
end
file = nil unless file.exist?
if outdated_only && file && file.stat.mtime > OUTDATED_TIMESTAMP
file = nil
symlink = nil
end
[symlink, file]
}
completes
.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 all_cache_files
cache_incompletes + cache_completes
cache_files.reject { |file| file.extname == ".incomplete" }
end
def disk_cleanup_size
Hbc::Utils.size_in_bytes(all_cache_files)
end
def remove_dead_symlinks
ohai "Removing dead symlinks"
delete_paths(dead_symlinks)
Hbc::Utils.size_in_bytes(cache_files)
end
def remove_all_cache_files
message = "Removing cached downloads"
message.concat " older than #{OUTDATED_DAYS} days old" if outdated_only
ohai message
delete_paths(all_cache_files)
delete_paths(cache_files)
end
def delete_paths(paths)

View File

@ -210,13 +210,13 @@ class Hbc::CLI::Doctor < Hbc::CLI::Base
def self.render_cached_downloads
cleanup = Hbc::CLI::Cleanup.default
files = cleanup.all_cache_files
files = cleanup.cache_files
count = files.count
size = cleanup.disk_cleanup_size
size_msg = "#{number_readable(count)} files, #{disk_usage_readable(size)}"
warn_msg = error_string('warning: run "brew cask cleanup"')
size_msg << " #{warn_msg}" if count > 0
[HOMEBREW_CACHE, HOMEBREW_CACHE_CASKS, size_msg]
[Hbc.cache, size_msg]
end
def self.help

View File

@ -12,7 +12,6 @@ class Hbc::Download
def perform
clear_cache
fetch
create_cache_symlink
downloaded_path
end
@ -41,10 +40,4 @@ class Hbc::Download
rescue StandardError => e
raise Hbc::CaskError, "Download failed on Cask '#{cask}' with message: #{e}"
end
# this symlink helps track which downloads are ours
def create_cache_symlink
symlink_path = HOMEBREW_CACHE_CASKS.join(downloaded_path.basename)
FileUtils.ln_sf downloaded_path, symlink_path
end
end

View File

@ -35,7 +35,7 @@ class Hbc::HbVCSDownloadStrategy < Hbc::AbstractDownloadStrategy
def initialize(cask, command = Hbc::SystemCommand)
super
@ref_type, @ref = extract_ref
@clone = HOMEBREW_CACHE.join(cache_filename)
@clone = Hbc.cache.join(cache_filename)
end
def extract_ref
@ -69,7 +69,7 @@ class Hbc::CurlDownloadStrategy < Hbc::AbstractDownloadStrategy
end
def tarball_path
@tarball_path ||= HOMEBREW_CACHE.join("#{name}-#{version}#{ext}")
@tarball_path ||= Hbc.cache.join("#{name}-#{version}#{ext}")
end
def temporary_path

View File

@ -1,75 +1,45 @@
require "spec_helper"
describe Hbc::CLI::Cleanup do
let(:homebrew_cache_location) { Pathname(Dir.mktmpdir).realpath }
let(:cache_location) { homebrew_cache_location.join("Casks").tap(&:mkdir) }
let(:cache_location) { Pathname.new(Dir.mktmpdir).realpath }
let(:cleanup_outdated) { false }
subject { described_class.new(cache_location, cleanup_outdated) }
after do
homebrew_cache_location.rmtree
cache_location.rmtree
end
describe "cleanup!" do
it "removes dead symlinks" do
bad_symlink = cache_location.join("bad_symlink")
bad_symlink.make_symlink("../does_not_exist")
expect {
subject.cleanup!
}.to output(<<-OUTPUT.undent).to_stdout
==> Removing dead symlinks
#{bad_symlink}
==> Removing cached downloads
Nothing to do
OUTPUT
expect(bad_symlink.symlink?).to eq(false)
end
it "removes cached downloads" do
cached_download = homebrew_cache_location.join("SomeDownload.dmg")
cached_download = cache_location.join("SomeDownload.dmg")
FileUtils.touch(cached_download)
cached_download_symlink = cache_location.join("SomeDownload.dmg")
cached_download_symlink.make_symlink(cached_download)
expect {
subject.cleanup!
}.to output(<<-OUTPUT.undent).to_stdout
==> Removing dead symlinks
Nothing to do
==> Removing cached downloads
#{cached_download}
#{cached_download_symlink}
OUTPUT
expect(cached_download.exist?).to eq(false)
expect(cached_download_symlink.symlink?).to eq(false)
end
context "when cleanup_outdated is specified" do
let(:cleanup_outdated) { true }
it "does not remove cache files newer than 10 days old" do
cached_download = homebrew_cache_location.join("SomeNewDownload.dmg")
cached_download = cache_location.join("SomeNewDownload.dmg")
FileUtils.touch(cached_download)
cached_download_symlink = cache_location.join("SomeNewDownload.dmg")
cached_download_symlink.make_symlink(cached_download)
expect {
subject.cleanup!
}.to output(<<-OUTPUT.undent).to_stdout
==> Removing dead symlinks
Nothing to do
==> Removing cached downloads older than 10 days old
Nothing to do
OUTPUT
expect(cached_download.exist?).to eq(true)
expect(cached_download_symlink.symlink?).to eq(true)
end
end
end

View File

@ -47,15 +47,14 @@ Hbc.homebrew_tapspath = nil
Hbc.binarydir = Hbc.homebrew_prefix.join("binarydir", "bin")
Hbc.appdir = Pathname.new(TEST_TMPDIR).join("appdir")
# making homebrew's cache dir allows us to actually download Casks in tests
HOMEBREW_CACHE = Pathname.new(TEST_TMPDIR).join("cache")
HOMEBREW_CACHE.mkpath
HOMEBREW_CACHE.join("Casks").mkpath
# Look for Casks in testcasks by default. It is elsewhere required that
# the string "test" appear in the directory name.
Hbc.default_tap = project_root.join("spec", "support")
# create cache directory
HOMEBREW_CACHE = Pathname.new(TEST_TMPDIR).join("cache")
Hbc.cache.mkpath
# our own testy caskroom
Hbc.caskroom = Hbc.homebrew_prefix.join("TestCaskroom")

View File

@ -53,11 +53,6 @@ at_exit do
FileUtils.remove_entry(TEST_TMPDIR)
end
# making homebrew's cache dir allows us to actually download Casks in tests
HOMEBREW_CACHE = Pathname.new(TEST_TMPDIR).join("cache")
HOMEBREW_CACHE.mkpath
HOMEBREW_CACHE.join("Casks").mkpath
# must be called after testing_env so at_exit hooks are in proper order
require "minitest/autorun"
require "minitest/reporters"
@ -80,6 +75,10 @@ Hbc.homebrew_tapspath = nil
# the string "test" appear in the directory name.
Hbc.default_tap = "caskroom/homebrew-testcasks"
# create cache directory
HOMEBREW_CACHE = Pathname.new(TEST_TMPDIR).join("cache")
Hbc.cache.mkpath
# our own testy caskroom
Hbc.caskroom = Hbc.homebrew_prefix.join("TestCaskroom")