class Omnibus::Compressor::TGZ

Public Instance Methods

compression_level(val = NULL) click to toggle source

Set or return the level of compression to use when generating the zipped tarball. Default: max compression.

@example

compression_level 9

@param [Fixnum] val

the compression level to use

@return [Fixnum]

# File lib/omnibus/compressors/tgz.rb, line 49
def compression_level(val = NULL)
  if null?(val)
    @compression_level || Zlib::BEST_COMPRESSION
  else
    unless val.is_a?(Integer)
      raise InvalidValue.new(:compression_level, "be an Integer")
    end

    unless val.between?(1, 9)
      raise InvalidValue.new(:compression_level, "be between 1-9")
    end

    @compression_level = val
  end
end
gzipped_tarball() click to toggle source

Create the gzipped tarball. See {#tarball} for how the tarball is constructed. This method uses maximum gzip compression, unless the user specifies a different compression level.

@return [StringIO]

# File lib/omnibus/compressors/tgz.rb, line 130
def gzipped_tarball
  gz = StringIO.new("")
  z = Zlib::GzipWriter.new(gz, compression_level)
  z.write(tarball.string)
  z.close

  # z was closed to write the gzip footer, so
  # now we need a new StringIO
  StringIO.new(gz.string)
end
package_name() click to toggle source

@see Base#package_name

# File lib/omnibus/compressors/tgz.rb, line 73
def package_name
  "#{packager.package_name}.tar.gz"
end
tarball() click to toggle source

Create an in-memory tarball from the given packager.

@return [StringIO]

# File lib/omnibus/compressors/tgz.rb, line 105
def tarball
  tarfile = StringIO.new("")
  Gem::Package::TarWriter.new(tarfile) do |tar|
    path = "#{staging_dir}/#{packager.package_name}"
    name = packager.package_name
    mode = File.stat(path).mode

    tar.add_file(name, mode) do |tf|
      File.open(path, "rb") do |file|
        tf.write(file.read)
      end
    end
  end

  tarfile.rewind
  tarfile
end
write_tgz() click to toggle source

Write the tar.gz to disk, reading in 1024 bytes at a time to reduce memory usage.

@return [void]

# File lib/omnibus/compressors/tgz.rb, line 83
def write_tgz
  # Grab the contents of the gzipped tarball for reading
  contents = gzipped_tarball

  # Write the .tar.gz into the staging directory
  File.open("#{staging_dir}/#{package_name}", "wb") do |tgz|
    while chunk = contents.read(1024)
      tgz.write(chunk)
    end
  end

  # Copy the .tar.gz into the package directory
  FileSyncer.glob("#{staging_dir}/*.tar.gz").each do |tgz|
    copy_file(tgz, Config.package_dir)
  end
end