Sha256: 13f55a53fbd2ce9e823005e7048573248a51b55c480cd26b4311ce908f174182

Contents?: true

Size: 1.38 KB

Versions: 13

Compression:

Stored size: 1.38 KB

Contents

require 'dply/helper'
require 'dply/curl'
require 'uri'
require 'tmpdir'

module Dply
  class Archive

    include Helper

    attr_reader :name, :path, :checksum_path, :uri

    def initialize(url, verify_checksum: true)
      @uri = URI.parse url
      @verify_checksum = verify_checksum
      @name = File.basename(uri.path)
      @path = "tmp/archive/#{name}"
      @checksum_path = "tmp/archive/#{name}.md5"
    end

    def extract(&block)
      download_file
      Dir.mktmpdir "tmp", "./" do |d|
        extracted = "#{d}/extracted"
        FileUtils.mkdir extracted
        cmd "tar xf #{path} -C #{extracted}", display: true
        yield extracted
      end
      cleanup
    end

    private

    def cleanup
      logger.trace "cleaning tmp/archive"
      files = [ path, checksum_path ]
      files.each { |f| FileUtils.rm f if File.exists? f }
    end

    def download_file
      curl.download(uri, path)
      if @verify_checksum
        download_checksum
        error "checksum doesn't match for archive" if not checksum_matches?
      end
    end

    def download_checksum
      curl.download("#{uri}.md5", checksum_path)
    end

    def checksum
      File.read(checksum_path).chomp
    end

    def checksum_matches?
      require 'digest'
      computed_checksum = Digest::MD5.file path
      computed_checksum == checksum
    end

    def curl
      @curl ||= Curl.new
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dply-0.2.17 lib/dply/archive.rb
dply-0.2.16 lib/dply/archive.rb
dply-0.2.15 lib/dply/archive.rb
dply-0.2.14 lib/dply/archive.rb
dply-0.2.13 lib/dply/archive.rb
dply-0.2.11 lib/dply/archive.rb
dply-0.2.10 lib/dply/archive.rb
dply-0.2.9 lib/dply/archive.rb
dply-0.2.8 lib/dply/archive.rb
dply-0.2.7 lib/dply/archive.rb
dply-0.2.6 lib/dply/archive.rb
dply-0.2.5 lib/dply/archive.rb
dply-0.2.4 lib/dply/archive.rb