Class: Omnibus::Compressor::TGZ

Inherits:
Base show all
Defined in:
lib/omnibus/compressors/tgz.rb

Constant Summary

Constants included from Util

Util::SHELLOUT_OPTIONS

Constants included from NullArgumentable

NullArgumentable::NULL

Instance Attribute Summary

Attributes inherited from Base

#packager, #project

Attributes inherited from Packager::Base

#project

DSL methods collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods inherited from Packager::Base

build, #exclusions, id, #id, #initialize, #install_dir, #package_path, #resource_path, #resources_path, #run!, setup, #staging_dir

Methods included from Util

#copy_file, #create_directory, #create_file, #create_link, included, #remove_directory, #remove_file, #shellout, #shellout!, #windows_safe_path

Methods included from Templating

included, #render_template

Methods included from NullArgumentable

included, #null?

Methods included from Logging

included

Methods included from Digestable

#digest, #digest_directory

Constructor Details

This class inherits a constructor from Omnibus::Compressor::Base

Instance Method Details

#compression_level(val = NULL) ⇒ Fixnum

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

Examples:

compression_level 9

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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_tarballStringIO

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.


130
131
132
133
134
135
136
137
138
139
# 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_nameObject


73
74
75
# File 'lib/omnibus/compressors/tgz.rb', line 73

def package_name
  "#{packager.package_name}.tar.gz"
end

#tarballStringIO

Create an in-memory tarball from the given packager.


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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_tgzvoid

This method returns an undefined value.

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


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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