Class: Smithy::DownloadCache

Inherits:
Object
  • Object
show all
Defined in:
lib/smithy/download_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p, fname = nil) ⇒ DownloadCache

Returns a new instance of DownloadCache.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/smithy/download_cache.rb', line 5

def initialize(p, fname = nil)
  @url = ''
  @checksums = {}
  if p.is_a? Formula
    @url = p.try(:url)
    @checksums.merge!(:md5 => p.md5) if p.try(:md5)
    @checksums.merge!(:sha1 => p.sha1) if p.try(:sha1)
    @checksums.merge!(:sha256 => p.sha256) if p.try(:sha256)
    @name = p.try(:package).try(:name)
    @version = p.try(:package).try(:version)
  elsif p.is_a? Package
    @name = p.name
    @version = p.version
  end

  @name = fname unless fname.blank?
end

Instance Attribute Details

#checksumsObject

Returns the value of attribute checksums.



3
4
5
# File 'lib/smithy/download_cache.rb', line 3

def checksums
  @checksums
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/smithy/download_cache.rb', line 3

def name
  @name
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/smithy/download_cache.rb', line 3

def url
  @url
end

#versionObject

Returns the value of attribute version.



3
4
5
# File 'lib/smithy/download_cache.rb', line 3

def version
  @version
end

Instance Method Details

#checksum_downloadObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/smithy/download_cache.rb', line 45

def checksum_download
  return true if checksums.empty?
  return false unless downloaded?

  checksums.keys.each do |type|
    checksum = checksums[type]
    digest = ''
    case type
    when :md5
      require 'digest/md5'
      digest = Digest::MD5.hexdigest(File.read(downloaded_file_path))
    when :sha1
      require 'digest/sha1'
      digest = Digest::SHA1.hexdigest(File.read(downloaded_file_path))
    when :sha256
      require 'digest/sha2'
      digest = Digest::SHA256.hexdigest(File.read(downloaded_file_path))
    end

    if checksum != digest
      raise <<-EOF.strip_heredoc
        file does not match expected #{type.to_s.upcase} checksum
          expected: #{checksum}
          got:      #{digest}
      EOF
    else
      return true
    end
  end
end

#downloadObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/smithy/download_cache.rb', line 76

def download
  curl = '/usr/bin/curl'
  curl = `which curl` unless File.exist? curl
  raise "curl cannot be located, without it files cannot be downloaded" if curl.blank?

  if downloaded?
    puts "downloaded ".rjust(12).color(:green) + downloaded_file_path
    return true
  else
    puts "download ".rjust(12).color(:green) + url
  end

  args = ['-qf#L']
  args << "--silent" unless $stdout.tty?
  args << '-o'
  args << downloaded_file_path
  args << url

  FileUtils.mkdir_p downloaded_file_dir
  [downloaded_file_dir, File.join(downloaded_file_dir, '..')].each do |dir|
    FileOperations.set_group(dir, Smithy::Config.global[:"file-group-name"])
    FileOperations.make_group_writable(dir) unless Smithy::Config.global[:"disable-group-writable"]
  end

  if system(curl, *args)
    FileOperations.set_group(downloaded_file_path, Smithy::Config.global[:"file-group-name"])
    FileOperations.make_group_writable(downloaded_file_path) unless Smithy::Config.global[:"disable-group-writable"]
    return true
  else
    return false
  end
end

#download_cache_dirObject



23
24
25
26
27
# File 'lib/smithy/download_cache.rb', line 23

def download_cache_dir
  dir = Smithy::Config.global[:"download-cache"]
  dir = File.join(Smithy::Config.homedir, '.smithy/cache') if dir.blank?
  dir
end

#downloaded?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/smithy/download_cache.rb', line 41

def downloaded?
  File.exists?(downloaded_file_path)
end

#downloaded_file_dirObject



33
34
35
# File 'lib/smithy/download_cache.rb', line 33

def downloaded_file_dir
  File.join(download_cache_dir, name, version)
end

#downloaded_file_nameObject



29
30
31
# File 'lib/smithy/download_cache.rb', line 29

def downloaded_file_name
  url_filename(url)
end

#downloaded_file_pathObject



37
38
39
# File 'lib/smithy/download_cache.rb', line 37

def downloaded_file_path
  File.join(downloaded_file_dir, downloaded_file_name)
end

#get(passed_url = nil) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/smithy/download_cache.rb', line 109

def get(passed_url= nil)
  @url = passed_url if passed_url
  if download && checksum_download
    return downloaded_file_path
  else
    return false
  end
end