Class: Puppet::Forge::Cache Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instantiate new cache for the repository instance.



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

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

Class Method Details

.base_pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the base Pathname for all the caches.



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

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

.cleanObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clean out all the caches.



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

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

Instance Method Details

#pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



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

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

#read_retrieve(uri) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



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

def read_retrieve(uri)
  return uri.read
end

#retrieve(url) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

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



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

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(Puppet::Util.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