From 4eda9831bd3ebd8ae15fdfe662c4dbd9eab6df66 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Wed, 15 Sep 2021 17:49:35 -0700 Subject: [PATCH] Cache dir and method name changes in R10K::Tarball For consistency, clarity --- lib/r10k/tarball.rb | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/r10k/tarball.rb b/lib/r10k/tarball.rb index 014232335..d4654438e 100644 --- a/lib/r10k/tarball.rb +++ b/lib/r10k/tarball.rb @@ -18,7 +18,7 @@ class Tarball include R10K::Util::Cacheable def_setting_attr :proxy # Defaults to global proxy setting - def_setting_attr :cache_root # Defaults to global cachedir setting + def_setting_attr :cache_root, R10K::Util::Cacheable.default_cachedir # @!attribute [rw] name # @return [String] The tarball's name @@ -49,19 +49,28 @@ def initialize(name, source, checksum: nil) @checksum_algorithm = :SHA256 end - # @return [String] Directory. Where the cache will be created. - def cache_root - File.join(settings[:cache_root], sanitized_dirname(source)) + # @return [String] Directory. Where the cache_basename file will be created. + def cache_dirname + File.join(settings[:cache_root], 'tarball-' + sanitized_dirname(name)) end - # @return [String] File. The path the tarball will be cached to. + # The final cache_path should match one of the templates: + # + # - {cachedir}/tarball-{name}/{checksum}.tar.gz + # - {cachedir}/tarball-{name}/{source}.tar.gz + # + # @return [String] File. The full file path the tarball will be cached to. def cache_path - File.join(cache_root, tarball_basename) + File.join(cache_dirname, cache_basename) end # @return [String] The basename of the tarball cache file. - def tarball_basename - name + '.tar.gz' + def cache_basename + if checksum.nil? + sanitized_dirname(source) + '.tar.gz' + else + checksum + '.tar.gz' + end end # Extract the cached tarball to the target directory. @@ -105,7 +114,7 @@ def insync?(target_dir, ignore_untracked_files: false) # Download the tarball from @source to @cache_path def download - Tempfile.open(tarball_basename) do |tempfile| + Tempfile.open(cache_basename) do |tempfile| tempfile.binmode src_uri = URI.parse(source) @@ -126,7 +135,7 @@ def download end # Move the download to cache_path - FileUtils::mkdir_p(cache_root) + FileUtils::mkdir_p(cache_dirname) begin FileUtils.mv(tempfile.path, cache_path) rescue Errno::EACCES