From 749f6a202e3d61a8a57c6bf6f1e29cf1a4103768 Mon Sep 17 00:00:00 2001 From: satmandu Date: Thu, 17 Dec 2020 12:31:47 -0500 Subject: [PATCH] Add crew cache option (#4763) * Add option to cache downloads * Add CREW_CACHE_DIR const * fewer quotes * Adjust cache dir location * clean up spacing * remove extraneous option * add slashes to CREW_CACHE_DIR path --- crew | 23 ++++++++++++++++++++++- lib/const.rb | 10 ++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crew b/crew index 6d1b99052..ec3025ebb 100755 --- a/crew +++ b/crew @@ -497,15 +497,36 @@ def download sha256sum = @pkg.binary_sha256[@device[:architecture]] end Dir.chdir CREW_BREW_DIR do + unless ENV["CREW_CACHE_OPT"].to_s == '' + unless File.directory?(CREW_CACHE_DIR) + FileUtils.mkdir_p(CREW_CACHE_DIR) + end + if File.file?("#{CREW_CACHE_DIR}#{filename}") + if Digest::SHA256.hexdigest( File.read("#{CREW_CACHE_DIR}#{filename}") ) == sha256sum then + FileUtils.cp "#{CREW_CACHE_DIR}#{filename}", "#{CREW_BREW_DIR}" + puts "Archive found in cache".lightgreen + return {source: source, filename: filename} + else + puts 'Cached archive checksum mismatch. :/ Will download.'.lightred + end + end + end + if @opt_verbose then system('curl', '-v', '--retry', '3', '--progress-bar', '-C', '-', '--insecure', '-L', '-#', url, '-o', filename) else system('curl', '--retry', '3', '--progress-bar', '-C', '-', '--insecure', '-L', '-#', url, '-o', filename) end + abort 'Checksum mismatch. :/ Try again.'.lightred unless Digest::SHA256.hexdigest( File.read("./#{filename}") ) == sha256sum + puts "Archive downloaded".lightgreen + + unless ENV["CREW_CACHE_OPT"].to_s == '' + puts "Caching downloaded archive".lightgreen + FileUtils.cp filename, CREW_CACHE_DIR + end end - puts "Archive downloaded".lightgreen return {source: source, filename: filename} end diff --git a/lib/const.rb b/lib/const.rb index b5fa531d6..84ac5bcc6 100644 --- a/lib/const.rb +++ b/lib/const.rb @@ -1,6 +1,6 @@ # Defines common constants used in different parts of crew -CREW_VERSION = '1.5.1' +CREW_VERSION = '1.5.2' ARCH_ACTUAL = `uname -m`.strip # This helps with virtualized builds on aarch64 machines @@ -16,9 +16,9 @@ else CREW_PREFIX = ENV['CREW_PREFIX'] @pkg.build_from_source = true end + CREW_LIB_PREFIX = CREW_PREFIX + '/' + ARCH_LIB CREW_MAN_PREFIX = CREW_PREFIX + '/share/man' - CREW_LIB_PATH = CREW_PREFIX + '/lib/crew/' CREW_CONFIG_PATH = CREW_PREFIX + '/etc/crew/' CREW_BREW_DIR = CREW_PREFIX + '/tmp/crew/' @@ -33,6 +33,12 @@ else HOME = CREW_PREFIX + ENV['HOME'] end +if ENV['CREW_CACHE_DIR'].to_s == '' + CREW_CACHE_DIR = HOME + '/.cache/crewcache/' +else + CREW_CACHE_DIR = ENV['CREW_CACHE_DIR'] +end + CREW_DEST_HOME = CREW_DEST_DIR + HOME # Set CREW_NPROC from environment variable or `nproc`