Class: NauktisUtils::Archiver

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/nauktis_utils/archiver.rb

Overview

Wrapper around TAR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=

Constructor Details

#initialize(&block) ⇒ Archiver

Returns a new instance of Archiver.



7
8
9
10
11
12
13
14
15
# File 'lib/nauktis_utils/archiver.rb', line 7

def initialize(&block)
	@options = {
		paths: [],
	}
	if block_given?
		instance_eval(&block)
		tar
	end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/nauktis_utils/archiver.rb', line 5

def options
  @options
end

Instance Method Details

#add(filename) ⇒ Object



60
61
62
# File 'lib/nauktis_utils/archiver.rb', line 60

def add(filename)
	@options[:paths] << File.expand_path(filename)
end

#bzip2Object



78
79
80
# File 'lib/nauktis_utils/archiver.rb', line 78

def bzip2
	@options[:compression] = :bzip2
end

#check_structureObject

Checks the tar structure after creating the archive.



100
101
102
# File 'lib/nauktis_utils/archiver.rb', line 100

def check_structure
	@options[:check_structure] = true
end

#clever_excludeObject



86
87
88
# File 'lib/nauktis_utils/archiver.rb', line 86

def clever_exclude
 	# Exclude .DS_Store & .dropbox
end

#destination(filename) ⇒ Object

Sets the destination folder for the archive.



70
71
72
# File 'lib/nauktis_utils/archiver.rb', line 70

def destination(filename)
	@options[:destination] = filename
end

#generate_hashObject



90
91
92
# File 'lib/nauktis_utils/archiver.rb', line 90

def generate_hash
	@options[:generate_hash] = true
end

#gzipObject



74
75
76
# File 'lib/nauktis_utils/archiver.rb', line 74

def gzip
	@options[:compression] = :gzip
end

#name(filename) ⇒ Object

Sets the name of the archive.



65
66
67
# File 'lib/nauktis_utils/archiver.rb', line 65

def name(filename)
	@options[:name] = filename
end

#paranoidObject

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



95
96
97
# File 'lib/nauktis_utils/archiver.rb', line 95

def paranoid
	@options[:paranoid] = true
end

#tarObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

#verboseObject



82
83
84
# File 'lib/nauktis_utils/archiver.rb', line 82

def verbose
	@options[:verbose] = true
end