class Puppet::Forge::Cache

Cache

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

Public Class Methods

base_path() click to toggle source

Return the base Pathname for all the caches.

   # File lib/puppet/forge/cache.rb
50 def self.base_path
51   (Pathname(Puppet.settings[:module_working_dir]) + 'cache').cleanpath.tap do |o|
52     o.mkpath unless o.exist?
53   end
54 end
clean() click to toggle source

Clean out all the caches.

   # File lib/puppet/forge/cache.rb
57 def self.clean
58   base_path.rmtree if base_path.exist?
59 end
new(repository, options = {}) click to toggle source

Instantiate new cache for the repository instance.

   # File lib/puppet/forge/cache.rb
13 def initialize(repository, options = {})
14   @repository = repository
15   @options = options
16 end

Public Instance Methods

path() click to toggle source

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

   # File lib/puppet/forge/cache.rb
45 def path
46   (self.class.base_path + @repository.cache_key).tap{ |o| o.mkpath }
47 end
read_retrieve(uri) click to toggle source

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

   # File lib/puppet/forge/cache.rb
40 def read_retrieve(uri)
41   return uri.read
42 end
retrieve(url) click to toggle source

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

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

   # File lib/puppet/forge/cache.rb
23 def retrieve(url)
24   (path + File.basename(url.to_s)).tap do |cached_file|
25     uri = url.is_a?(::URI) ? url : ::URI.parse(url)
26     unless cached_file.file?
27       if uri.scheme == 'file'
28         # CGI.unescape butchers Uris that are escaped properly
29         FileUtils.cp(Puppet::Util.uri_unescape(uri.path), cached_file)
30       else
31         # TODO: Handle HTTPS; probably should use repository.contact
32         data = read_retrieve(uri)
33         cached_file.open('wb') { |f| f.write data }
34       end
35     end
36   end
37 end