class Revamp::Parser::PuppetTarball

This class is a parser for Puppet's tarballs format

Public Class Methods

new(tarball_file) click to toggle source
# File lib/revamp/parser/puppet-tarball.rb, line 9
def initialize(tarball_file)
  @tarball = tarball_file
end

Public Instance Methods

parse() click to toggle source
# File lib/revamp/parser/puppet-tarball.rb, line 13
def parse
  model = nil
  File.open(@tarball, 'rb') do |file|
    Zlib::GzipReader.wrap(file) do |gz|
      Gem::Package::TarReader.new(gz) do |tar|
        model = Revamp::Model::PuppetModule.new
        tar.each do |tarfile|
          entry = Entry.new(tarfile)
          parse_metadata(model, entry) if entry.metadata?
          model.add_file(entry.name, entry.content) if entry.file?
        end
      end
    end
  end
  normalize(model)
end

Private Instance Methods

normalize(model) click to toggle source
# File lib/revamp/parser/puppet-tarball.rb, line 32
def normalize(model)
  strip = "#{model.slugname}-#{model.version}/"
  model.files = Hash[model.files.map { |file, content| [file.gsub(strip, ''), content] }]
  model
end
parse_dependencies(attr, value) click to toggle source
# File lib/revamp/parser/puppet-tarball.rb, line 50
def parse_dependencies(attr, value)
  return unless attr == :dependencies
  value.tap do |dependencies|
    dependencies.each do |dep|
      dep['version_requirement'] ||= dep['versionRequirement'] || '>= 0.0.0'
    end
  end
end
parse_metadata(model, entry) click to toggle source
# File lib/revamp/parser/puppet-tarball.rb, line 38
def parse_metadata(model, entry)
  data = JSON.parse(entry.content)
  model.metadata = data
  model.name = data['name'].tr('-', '/') if data['name']
  model.attributes.each do |attr|
    value = data[attr.to_s]
    fail ArgumentError, "No #{attr} module metadata provided for #{name}" unless value
    parse_dependencies(attr, value)
    model.send(attr.to_s + '=', value)
  end
end