Class: Puppet::Module::Tool::Cache

Inherits:
Object
  • Object
show all
Includes:
Utils::URI
Defined in:
lib/puppet/module/tool/cache.rb

Overview

Cache

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::URI

#normalize

Constructor Details

#initialize(repository) ⇒ Cache

Instantiate new cahe for the repositry instance.



10
11
12
# File 'lib/puppet/module/tool/cache.rb', line 10

def initialize(repository)
  @repository = repository
end

Class Method Details

.base_pathObject

Return the base Pathname for all the caches.



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

def self.base_path
  return(Puppet::Module::Tool.working_dir + 'cache')
end

.cleanObject

Clean out all the caches.



50
51
52
# File 'lib/puppet/module/tool/cache.rb', line 50

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.



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

def path
  return @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.



35
36
37
# File 'lib/puppet/module/tool/cache.rb', line 35

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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppet/module/tool/cache.rb', line 19

def retrieve(url)
  returning(path + File.basename(url.to_s)) do |cached_file|
    uri = normalize(url)
    unless cached_file.file?
      if uri.scheme == 'file'
        FileUtils.cp(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