Sha256: 669c9e9ffd117afea49cb3db557d58eafadfbaf163a080f31de2471564cde5c0

Contents?: true

Size: 1.85 KB

Versions: 8

Compression:

Stored size: 1.85 KB

Contents

require 'dply/helper'
require 'uri'
module Dply
  class Archive

    include Helper

    def initialize(url, verify_checksum: true)
      @url = url
      @verify_checksum = verify_checksum
    end

    def extract_to(extraction_path)
      download_file if not @downloaded
      FileUtils.rm_rf extraction_path if File.exists? extraction_path
      FileUtils.mkdir_p extraction_path
      cmd "tar xf #{path} -C #{extraction_path}", display: true
    end

    def clean
      logger.trace "cleaning cache"
      files = [ "tmp/cache/#{name}", "tmp/cache/#{name}.md5" ]
      files.each { |f| FileUtils.rm f if File.exists? f }
    end
    private

    def download_file
      if File.exists? path
        download if not verify_checksum
      else
        download(uri, path)
      end
      raise if not verify_checksum
      @downloaded = true
    end
    
    def uri
      @uri ||= URI.parse(@url)
    end

    def name
      @name ||= File.basename(uri.path)
    end

    def path
      @path = "tmp/cache/#{name}"
    end

    def checksum
      @checksum ||= load_checksum
    end

    def load_checksum
      file = "tmp/cache/#{name}.md5"
      if File.exists? file
        checksum = File.read(file).chomp
        return checksum if checksum.size == 32
      end
      download("#{uri}.md5", file)
      checksum = File.read(file).chomp
      raise if checksum.size != 32
      return checksum
    end

    def verify_checksum
      return true if not @verify_checksum
      require 'digest'
      computed_checksum = Digest::MD5.file path
      computed_checksum == checksum
    end

    def download(url, outfile)
      logger.bullet "downloading #{url} to #{outfile}"
      http_status = `curl -w "%{http_code}" -f -s -o '#{outfile}' '#{url}' `
      if http_status != "200"
        error "failed to download #{outfile}, http status #{http_status}"
      end
    end
  
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
dply-0.1.6 lib/dply/archive.rb
dply-0.1.5 lib/dply/archive.rb
dply-0.1.4 lib/dply/archive.rb
dply-0.1.2 lib/dply/archive.rb
dply-0.1.1 lib/dply/archive.rb
dply-0.1.0 lib/dply/archive.rb
dply-0.0.8 lib/dply/archive.rb
dply-0.0.7 lib/dply/archive.rb