module ManifestDL

namespace

namespace

namespace

Constants

DATE
DEFAULT_CACHE
DEFAULT_CONFIG
VERSION

Public Class Methods

_cache!(path, sum, cache_dir) click to toggle source

cache item's sha512sum (based on sha1sum of path)

# File lib/manifest-dl.rb, line 104
def self._cache!(path, sum, cache_dir)
  FileUtils.mkdir_p cache_dir
  File.write _cachefile(cache_dir, path), sum
  nil
end
_cached_sha512sum(path, cache_dir) click to toggle source

get item's cached sha512sum; returns nil if cache file does not exist

# File lib/manifest-dl.rb, line 98
def self._cached_sha512sum(path, cache_dir)
  file = _cachefile cache_dir, path
  File.exist?(file) ? File.read(file) : nil
end
_cachefile(cache_dir, path) click to toggle source

cache file path

# File lib/manifest-dl.rb, line 111
def self._cachefile(cache_dir, path)
  sha1 = Digest::SHA1.hexdigest path
  Pathname.new(cache_dir).join(sha1).to_s
end
_check!(item) click to toggle source

check item

# File lib/manifest-dl.rb, line 44
def self._check!(item)
  raise InvalidItemError,
    "unexpected/missing keys for item #{item.inspect}" \
      unless item.keys.sort == %w{ path sha512sum url }
  raise InvalidItemError,
    "non-string keys for item #{item.inspect}" \
      unless item.keys.all? { |x| String === x }
end
_curl!(url, file, quiet) click to toggle source

download file w/ curl

# File lib/manifest-dl.rb, line 80
def self._curl!(url, file, quiet)
  args = %w{ curl -L } + (quiet ? %w{ -s } : []) + ['-o', file, '--', url]
  # no shell b/c multiple args!
  system(*args) or raise SystemError 'curl returned non-zero'
  nil
end
_dl!(item, quiet, cache_dir) click to toggle source

download, verify, mv file

# File lib/manifest-dl.rb, line 54
def self._dl!(item, quiet, cache_dir)
  unless quiet
    $stderr.puts "==> #{item['path']}"
    $stderr.puts "  ( #{item['url']} )"
  end
  Dir.mktmpdir do |dir|
    tempfile = Pathname.new(dir).join('dl').to_s
    _curl! item['url'], tempfile, quiet
    _verify! tempfile, item['sha512sum']
    FileUtils.mkdir_p File.dirname(item['path'])
    FileUtils.mv tempfile, item['path'], force: true
    _cache! item['path'], item['sha512sum'], cache_dir
  end
  item['path']
end
_dl?(item, cache_dir) click to toggle source

should file be downloaded? (i.e. does not exist or sum has changed)

# File lib/manifest-dl.rb, line 72
def self._dl?(item, cache_dir)
  return true unless File.exist? item['path']
  _cached_sha512sum(item['path'], cache_dir) != item['sha512sum']
end
_verify!(file, sum) click to toggle source

verify file

# File lib/manifest-dl.rb, line 88
def self._verify!(file, sum)
  # no shell b/c multiple args!
  sum2 = IO.popen(%w{ shasum -a 512 } + [file]) { |f| f.gets.split.first }
  raise SystemError 'shasum returned non-zero' unless $?.success?
  raise VerificationError, file unless sum == sum2
  nil
end
run!(opts = {}) click to toggle source

download files in manifest

# File lib/manifest-dl.rb, line 31
def self.run!(opts = {})
  quiet       = opts.fetch(:quiet)        { false           }
  config_file = opts.fetch(:config_file)  { DEFAULT_CONFIG  }
  cache_dir   = opts.fetch(:cache_dir)    { DEFAULT_CACHE   }
  YAML.load(File.read(config_file)).map do |item|
    _check! item
    _dl?(item, cache_dir) ? _dl!(item, quiet, cache_dir) : nil;
  end .compact
end