class NauktisUtils::Archiver

Wrapper around TAR

Attributes

options[R]

Public Class Methods

new(&block) click to toggle source
# File lib/nauktis_utils/archiver.rb, line 7
def initialize(&block)
        @options = {
                paths: [],
        }
        if block_given?
                instance_eval(&block)
                tar
        end
end

Public Instance Methods

add(filename) click to toggle source
# File lib/nauktis_utils/archiver.rb, line 60
def add(filename)
        @options[:paths] << File.expand_path(filename)
end
bzip2() click to toggle source
# File lib/nauktis_utils/archiver.rb, line 78
def bzip2
        @options[:compression] = :bzip2
end
check_structure() click to toggle source

Checks the tar structure after creating the archive.

# File lib/nauktis_utils/archiver.rb, line 100
def check_structure
     @options[:check_structure] = true
end
clever_exclude() click to toggle source
# File lib/nauktis_utils/archiver.rb, line 86
        def clever_exclude
# Exclude .DS_Store & .dropbox
end
destination(filename) click to toggle source

Sets the destination folder for the archive.

# File lib/nauktis_utils/archiver.rb, line 70
def destination(filename)
        @options[:destination] = filename
end
generate_hash() click to toggle source
# File lib/nauktis_utils/archiver.rb, line 90
def generate_hash
       @options[:generate_hash] = true
  end
gzip() click to toggle source
# File lib/nauktis_utils/archiver.rb, line 74
def gzip
        @options[:compression] = :gzip
end
name(filename) click to toggle source

Sets the name of the archive.

# File lib/nauktis_utils/archiver.rb, line 65
def name(filename)
        @options[:name] = filename
end
paranoid() click to toggle source

Untar the archive after creation to make sure everything is there.

# File lib/nauktis_utils/archiver.rb, line 95
def paranoid
     @options[:paranoid] = true
end
tar() click to toggle source
# File lib/nauktis_utils/archiver.rb, line 17
def tar
        Tracer.info "Creating archive for #{@options[:paths]}"
        raise "TAR is not available" unless command_available?('tar')
        raise "Only one file archiving is supported for now" unless @options[:paths].size == 1
        source_path = File.expand_path(@options[:paths].first)
        raise "#{source_path} doesn't exist" unless File.exist?(source_path)

        destination_path = FileBrowser.ensure_valid_directory(@options[:destination])
        @options[:name] = "#{Time.now.strftime('%Y-%m-%d')}_#{File.basename(source_path)}" if @options[:name].nil?
        @options[:tar_file] = File.join(destination_path, "#{@options[:name]}#{extension}")
        r = nil
        Dir.chdir(File.dirname(source_path)) do
                r = execute_command("tar #{tar_options.join(' ')} -cf \"#{@options[:tar_file]}\" \"#{File.basename(source_path)}\"")
        end
        raise "TAR returned an error" unless r
        raise "TAR was not created" unless File.exist?(@options[:tar_file])
        Tracer.debug "#{@options[:tar_file]} created"

        # Check the tar structure.
        if @options[:check_structure] or @options[:paranoid]
                Tracer.debug "Checking TAR structure"
                r = execute_command("tar #{tar_options.join(' ')} -tf \"#{@options[:tar_file]}\" >/dev/null")
                raise "TAR structure is not correct" unless r
        end

        if @options[:paranoid]
                Tracer.debug "Checking TAR content"
                Dir.mktmpdir do |dir|
                        temp_dir = File.expand_path(dir)
                        Dir.chdir(temp_dir) do
                                r = execute_command("tar #{tar_options.join(' ')} -xf \"#{@options[:tar_file]}\"")
                        end
                        raise "Error while untaring the archive" unless r
                        r = compare(source_path, File.join(temp_dir, File.basename(source_path)))
                        raise "Content doesn't match" unless r
                end
        end

        if @options[:generate_hash]
                Utils::FileDigester.generate_digest_file(@options[:tar_file])
        end
end
verbose() click to toggle source
# File lib/nauktis_utils/archiver.rb, line 82
def verbose
        @options[:verbose] = true
end

Private Instance Methods

command_available?(cmd) click to toggle source
# File lib/nauktis_utils/archiver.rb, line 106
def command_available?(cmd)
     system("which #{cmd} >/dev/null")
end
compare(original, copy) click to toggle source
# File lib/nauktis_utils/archiver.rb, line 130
def compare(original, copy)
     a = File.expand_path(original)
     b = File.expand_path(copy)
     raise "Original file #{original} doesn't exist" unless File.exist?(a)
     if File.directory?(a)
             Dir.chdir(a) do
                     Dir.glob('**/*', File::FNM_DOTMATCH) do |f|
                             if File.exist?(f) and not File.directory?(f)
                                     a_file = File.expand_path(File.join(a, f))
                                     b_file = File.expand_path(File.join(b, f))
                                     logger.debug("Comparing: #{a_file}, #{b_file}")
                                     return false unless File.exist?(b_file) and not File.directory?(b_file)
                                     return false unless Utils::FileDigester.digest(a_file) == Utils::FileDigester.digest(b_file)
                             end
                     end
             end
     else
             return false unless File.exist?(b) and not File.directory?(b)
             return false unless Utils::FileDigester.digest(a) == Utils::FileDigester.digest(b)
     end
     return true
end
execute_command(cmd) click to toggle source
# File lib/nauktis_utils/archiver.rb, line 125
def execute_command(cmd)
     Tracer.debug("Executing: #{cmd}")
     Kernel.system(cmd)
end
extension() click to toggle source
# File lib/nauktis_utils/archiver.rb, line 118
def extension
     s = '.tar'
     s += '.gz' if @options[:compression] == :gzip
     s += '.bz2' if @options[:compression] == :bzip2
     s
end
tar_options() click to toggle source
# File lib/nauktis_utils/archiver.rb, line 110
def tar_options
     s = []
     s << '-v' if @options[:verbose]
     s << '-z' if @options[:compression] == :gzip
     s << '-j' if @options[:compression] == :bzip2
     s
end