module Pione::Package::PackageCache

PackageCache is cache mechanism for PIONE package. Cache makes both of PPG package for sharing and directory package for reference.

Public Class Methods

cache(location) click to toggle source

Cache PPG package and directory package on the location. This method returns directory cache location. If you want to PPG archive cache location, use ppg(digest).

# File lib/pione/package/package-cache.rb, line 10
def cache(location)
  if location.directory?
    # if it is directory location, make a PPG archive and expand it
    ppg_location = create_ppg_cache_from_directory(location)
    return create_directory_cache(ppg_location)
  else
    # if it is ppg location, copy a PPG archive and expand it
    if /\.ppg$/i.match(location.basename)
      ppg_location = create_ppg_cache_from_ppg(location)
      return create_directory_cache(ppg_location)
    else
      raise InvalidPackage.new("The location \"%s\" is not PPG archive." % location.address)
    end
  end
end
directory_cache(digest) click to toggle source

Find directory cache location by the digest.

# File lib/pione/package/package-cache.rb, line 46
def directory_cache(digest)
  location = Global.directory_package_cache_directory + digest
  return location.exist? ? location : nil
end
exist?(digest) click to toggle source

Return true cache that has the digest exists

# File lib/pione/package/package-cache.rb, line 27
def exist?(digest)
  ppg_cache(digest) and directory_cache(digest)
end
ppg_cache(digest) click to toggle source

Find PPG cache location by the digest.

# File lib/pione/package/package-cache.rb, line 32
def ppg_cache(digest)
  Global.ppg_cache_directory.entries.each do |entry|
    begin
      if digest == PackageFilename.parse(entry.basename).digest
        return entry
      end
    rescue InvalidPackageFilename
      next
    end
  end
  return nil
end

Private Class Methods

create_directory_cache(ppg_location) click to toggle source

Create directory cache from cached PPG archive.

# File lib/pione/package/package-cache.rb, line 68
def create_directory_cache(ppg_location)
  digest = Util::PackageDigest.generate(ppg_location)
  cache_location = Global.directory_package_cache_directory + digest
  unless cache_location.exist?
    PackageExpander.new(ppg_location).expand(cache_location)
  end
  return digest
end
create_ppg_cache_from_directory(location) click to toggle source

Create PPG archive cache from the location.

# File lib/pione/package/package-cache.rb, line 54
def create_ppg_cache_from_directory(location)
  PackageArchiver.new(location).archive(Global.ppg_package_cache_directory, true)
end
create_ppg_cache_from_ppg(location) click to toggle source

Create PPG archive cache from the location.

# File lib/pione/package/package-cache.rb, line 59
def create_ppg_cache_from_ppg(location)
  filename = PackageFilename.parse(location.basename)
  filename.digest = Util::PackageDigest.generate(location)
  ppg_cache_location = Global.ppg_package_cache_directory + filename.string
  location.copy(ppg_cache_location)
  return ppg_cache_location
end