class Debeasy::Package

Attributes

control_file_contents[R]
filelist[R]
package_file[R]
path[R]
postinst_contents[R]
postrm_contents[R]
preinst_contents[R]
prerm_contents[R]

Public Class Methods

new(path) click to toggle source

Create a new Debeasy::Package object.

Arguments:

path: (String)
# File lib/debeasy/package.rb, line 21
def initialize(path)
  @path = path
  raise NotAPackageError, "#{path} is not a Debian package" unless is_package_file?
  @package_file = Archive.read_open_filename(path)
  @fields = {}
  @filelist = []
  extract_files
  parse_control_file
  generate_checksums
  get_size
  get_filename
end

Public Instance Methods

[](field) click to toggle source

Utility method to get the field in a hash-like way.

# File lib/debeasy/package.rb, line 52
def [](field)
  @fields.has_key?(field.to_s) ? @fields[field.to_s] : nil
end
fields() click to toggle source

Lists all the available fields on the package.

# File lib/debeasy/package.rb, line 36
def fields
  @fields.keys
end
method_missing(m, *args, &block) click to toggle source
# File lib/debeasy/package.rb, line 46
def method_missing(m, *args, &block)
  @fields.has_key?(m.to_s) ? @fields[m.to_s] : nil
end
to_hash() click to toggle source

Get package metadata as a hash.

# File lib/debeasy/package.rb, line 42
def to_hash
  @fields
end

Private Instance Methods

extract_files() click to toggle source

Poke inside the package to find the control file, the pre/post install scripts, and a list of all the files it will deploy.

# File lib/debeasy/package.rb, line 85
def extract_files
  while file = @package_file.next_header
    if file.pathname == "control.tar.gz"
      control_tar_gz = Archive.read_open_memory(@package_file.read_data)
      while control_entry = control_tar_gz.next_header
        case control_entry.pathname
        when "./control"
          @control_file_contents = control_tar_gz.read_data
        when "./preinst"
          @preinst_contents = control_tar_gz.read_data
        when "./prerm"
          @prerm_contents = control_tar_gz.read_data
        when "./postinst"
          @postinst_contents = control_tar_gz.read_data
        when "./postrm"
          @postrm_contents = control_tar_gz.read_data
        end
      end
    end
    if file.pathname == "data.tar.gz"
      data_tar_gz = Archive.read_open_memory(@package_file.read_data)
      while data_entry = data_tar_gz.next_header
        # Skip dirs; they're listed with a / as the last character
        @filelist << data_entry.pathname.sub(/^\./, "") unless data_entry.pathname =~ /\/$/
      end
    end
  end
end
generate_checksums() click to toggle source
# File lib/debeasy/package.rb, line 75
def generate_checksums
  @fields["MD5sum"] = Digest::MD5.hexdigest(File.read @path)
  @fields["SHA1"] = Digest::SHA1.hexdigest(File.read @path)
  @fields["SHA256"] = Digest::SHA256.hexdigest(File.read @path)
end
get_filename() click to toggle source
# File lib/debeasy/package.rb, line 71
def get_filename
  @fields["filename"] = File.basename @path
end
get_size() click to toggle source
# File lib/debeasy/package.rb, line 67
def get_size
  @fields["size"] = File.size(@path)
end
is_package_file?() click to toggle source
# File lib/debeasy/package.rb, line 58
def is_package_file?
  fm = FileMagic.new
  if fm.file(@path) =~ /Debian binary package/
    true
  else
    false
  end
end
parse_control_file() click to toggle source

Parse the available fields out of the Debian control file.

# File lib/debeasy/package.rb, line 116
def parse_control_file
  @control_file_contents.scan(/^([\w-]+?): (.*?)\n(?! )/m).each do |entry|
    field, value = entry
    @fields[field.gsub("-", "_").downcase] = value
  end
  @fields["installed_size"] = @fields["installed_size"].to_i * 1024 unless @fields["installed_size"].nil?
end