Sha256: 2dfb1b0e70ec69cd744f501f74cf4845465bc2a52a0f860b15a9f52714d7ed36

Contents?: true

Size: 1.5 KB

Versions: 24

Compression:

Stored size: 1.5 KB

Contents

require 'uri'

module 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

24 entries across 24 versions & 3 rubygems

Version Path
puppet-parse-0.1.4 lib/vendor/puppet/forge/cache.rb
puppet-parse-0.1.3 lib/vendor/puppet/forge/cache.rb
puppet-parse-0.1.2 lib/vendor/puppet/forge/cache.rb
puppet-parse-0.1.1 lib/vendor/puppet/forge/cache.rb
puppet-2.7.26 lib/puppet/forge/cache.rb
puppet-2.7.25 lib/puppet/forge/cache.rb
puppet-2.7.24 lib/puppet/forge/cache.rb
puppet-2.7.23 lib/puppet/forge/cache.rb
puppet-2.7.22 lib/puppet/forge/cache.rb
puppet-parse-0.1.0 lib/vendor/puppet/forge/cache.rb
puppet-parse-0.0.6 lib/vendor/puppet/forge/cache.rb
puppet-2.7.21 lib/puppet/forge/cache.rb
puppet-parse-0.0.5 lib/vendor/puppet/forge/cache.rb
puppet-parse-0.0.4 lib/vendor/puppet/forge/cache.rb
puppet-parse-0.0.2 lib/vendor/puppet/forge/cache.rb
puppet-2.7.20 lib/puppet/forge/cache.rb
puppet-2.7.20.rc1 lib/puppet/forge/cache.rb
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/puppet-2.7.18/lib/puppet/forge/cache.rb
puppet-2.7.19 lib/puppet/forge/cache.rb
librarian-puppet-0.9.3 vendor/gems/ruby/1.8/gems/puppet-2.7.18/lib/puppet/forge/cache.rb