Sha256: 7c74a84c01ebc121019630e5b3df695c2f532faa57d5ea6383ce14a91c4d9a3e

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 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(uri, path) 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}' `
      exit_status = $?.existstatus
      if (http_status != "200" || exit_status != 0)
        error "failed to download #{outfile}, http status #{http_status}, exit_status #{exit_status}"
      end
    end
  
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dply-0.1.9 lib/dply/archive.rb