Class: Puppet::Forge::Cache

Inherits:
Object show all
Defined in:
lib/puppet/forge/cache.rb

Overview

Cache

Provides methods for reading files from local cache, filesystem or network.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, options = {}) ⇒ Cache

Instantiate new cache for the repository instance.



12
13
14
15
# File 'lib/puppet/forge/cache.rb', line 12

def initialize(repository, options = {})
  @repository = repository
  @options = options
end

Class Method Details

.base_pathObject

Return the base Pathname for all the caches.



49
50
51
52
53
# File 'lib/puppet/forge/cache.rb', line 49

def self.base_path
  (Pathname(Puppet.settings[:module_working_dir]) + 'cache').tap do |o|
    o.mkpath unless o.exist?
  end
end

.cleanObject

Clean out all the caches.



56
57
58
# File 'lib/puppet/forge/cache.rb', line 56

def self.clean
  base_path.rmtree if base_path.exist?
end

Instance Method Details

#pathObject

Return Pathname for repository’s cache directory, create it if needed.



44
45
46
# File 'lib/puppet/forge/cache.rb', line 44

def path
  (self.class.base_path + @repository.cache_key).tap{ |o| o.mkpath }
end

#read_retrieve(uri) ⇒ Object

Return contents of file at the given URI’s uri.



39
40
41
# File 'lib/puppet/forge/cache.rb', line 39

def read_retrieve(uri)
  return uri.read
end

#retrieve(url) ⇒ Object

Return filename retrieved from uri instance. Will download this file and cache it if needed.

TODO: Add checksum support. TODO: Add error checking.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppet/forge/cache.rb', line 22

def retrieve(url)
  (path + File.basename(url.to_s)).tap do |cached_file|
    uri = url.is_a?(::URI) ? url : ::URI.parse(url)
    unless cached_file.file?
      if uri.scheme == 'file'
        # CGI.unescape butchers Uris that are escaped properly
        FileUtils.cp(URI.unescape(uri.path), cached_file)
      else
        # TODO: Handle HTTPS; probably should use repository.contact
        data = read_retrieve(uri)
        cached_file.open('wb') { |f| f.write data }
      end
    end
  end
end