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
49 def self.base_path
50   (Pathname(Puppet.settings[:module_working_dir]) + 'cache').cleanpath.tap do |o|
51     o.mkpath unless o.exist?
52   end
53 end
clean() click to toggle source

Clean out all the caches.

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

Instantiate new cache for the repository instance.

   # File lib/puppet/forge/cache.rb
12 def initialize(repository, options = {})
13   @repository = repository
14   @options = options
15 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
44 def path
45   (self.class.base_path + @repository.cache_key).tap{ |o| o.mkpath }
46 end
read_retrieve(uri) click to toggle source

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

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