Sha256: 5f697127327b41a1fc6c8eeca55fc692c8181a88511f33522bb9f7a55f24d794

Contents?: true

Size: 1.49 KB

Versions: 25

Compression:

Stored size: 1.49 KB

Contents

require 'uri'

class Puppet::Forge
  # = Cache
  #
  # Provides methods for reading files from local cache, filesystem or network.
  class Cache

    # Instantiate new cahe for the +repositry+ instance.
    def initialize(repository, options = {})
      @repository = repository
      @options = options
    end

    # Return filename retrieved from +uri+ instance. Will download this file and
    # cache it if needed.
    #
    # TODO: Add checksum support.
    # TODO: Add error checking.
    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'
            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

    # Return contents of file at the given URI's +uri+.
    def read_retrieve(uri)
      return uri.read
    end

    # Return Pathname for repository's cache directory, create it if needed.
    def path
      (self.class.base_path + @repository.cache_key).tap{ |o| o.mkpath }
    end

    # Return the base Pathname for all the caches.
    def self.base_path
      Pathname(Puppet.settings[:module_working_dir]) + 'cache'
    end

    # Clean out all the caches.
    def self.clean
      base_path.rmtree if base_path.exist?
    end
  end
end

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
puppet-3.2.4 lib/puppet/forge/cache.rb
puppet-3.2.3 lib/puppet/forge/cache.rb
puppet-3.2.3.rc1 lib/puppet/forge/cache.rb
puppet-3.2.2 lib/puppet/forge/cache.rb
puppet-3.2.1 lib/puppet/forge/cache.rb
puppet-3.2.1.rc1 lib/puppet/forge/cache.rb
puppet-3.2.0.rc2 lib/puppet/forge/cache.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/puppet-3.1.0/lib/puppet/forge/cache.rb
puppet-3.2.0.rc1 lib/puppet/forge/cache.rb
puppet-3.1.1 lib/puppet/forge/cache.rb
librarian-puppet-0.9.8 vendor/gems/ruby/1.9.1/gems/puppet-3.1.0/lib/puppet/forge/cache.rb
puppet-3.1.0 lib/puppet/forge/cache.rb
puppet-3.1.0.rc2 lib/puppet/forge/cache.rb
puppet-3.1.0.rc1 lib/puppet/forge/cache.rb
puppet-3.0.2 lib/puppet/forge/cache.rb
puppet-3.0.2.rc3 lib/puppet/forge/cache.rb
puppet-3.0.2.rc2 lib/puppet/forge/cache.rb
puppet-3.0.2.rc1 lib/puppet/forge/cache.rb
puppet-3.0.1 lib/puppet/forge/cache.rb
puppet-3.0.1.rc1 lib/puppet/forge/cache.rb