Sha256: d70dbd559282a37863fcd4693b281268f1fb2cf613ecd302c72d9f41c0d9555c

Contents?: true

Size: 1.57 KB

Versions: 186

Compression:

Stored size: 1.57 KB

Contents

require 'uri'

require 'puppet/forge'

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

    # Instantiate new cache for the +repository+ 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').tap do |o|
        o.mkpath unless o.exist?
      end
    end

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

Version data entries

186 entries across 186 versions & 2 rubygems

Version Path
puppet-retrospec-1.3.1 vendor/gems/puppet-4.5.2/lib/puppet/forge/cache.rb
puppet-4.9.3 lib/puppet/forge/cache.rb
puppet-4.9.3-x86-mingw32 lib/puppet/forge/cache.rb
puppet-4.9.3-x64-mingw32 lib/puppet/forge/cache.rb
puppet-4.9.3-universal-darwin lib/puppet/forge/cache.rb
puppet-4.9.2 lib/puppet/forge/cache.rb
puppet-4.9.2-x86-mingw32 lib/puppet/forge/cache.rb
puppet-4.9.2-x64-mingw32 lib/puppet/forge/cache.rb
puppet-4.9.2-universal-darwin lib/puppet/forge/cache.rb
puppet-4.9.1 lib/puppet/forge/cache.rb
puppet-4.9.1-x86-mingw32 lib/puppet/forge/cache.rb
puppet-4.9.1-x64-mingw32 lib/puppet/forge/cache.rb
puppet-4.9.1-universal-darwin lib/puppet/forge/cache.rb
puppet-retrospec-1.3.0 vendor/gems/puppet-4.5.2/lib/puppet/forge/cache.rb
puppet-4.9.0 lib/puppet/forge/cache.rb
puppet-4.9.0-x86-mingw32 lib/puppet/forge/cache.rb
puppet-4.9.0-x64-mingw32 lib/puppet/forge/cache.rb
puppet-4.9.0-universal-darwin lib/puppet/forge/cache.rb
puppet-4.8.2 lib/puppet/forge/cache.rb
puppet-4.8.2-x86-mingw32 lib/puppet/forge/cache.rb