Class: Siba::Archive::Tar::Archive

Inherits:
Object
  • Object
show all
Includes:
FilePlug, LoggerPlug
Defined in:
lib/siba/plugins/archive/tar/archive.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LoggerPlug

close, create, logger, #logger, opened?

Methods included from FilePlug

#siba_file, siba_file, siba_file=

Constructor Details

#initialize(compression) ⇒ Archive

Returns a new instance of Archive.



10
11
12
13
# File 'lib/siba/plugins/archive/tar/archive.rb', line 10

def initialize(compression)
  @compression = compression
  check_installed
end

Instance Attribute Details

#compressionObject

Returns the value of attribute compression.



8
9
10
# File 'lib/siba/plugins/archive/tar/archive.rb', line 8

def compression
  @compression
end

Class Method Details

.check_compression_type(compression) ⇒ Object

Raises:



85
86
87
# File 'lib/siba/plugins/archive/tar/archive.rb', line 85

def self.check_compression_type(compression)
  raise Siba::CheckError, "'Compression' should be one of the following: #{CompressionTypes.join(', ')}" unless CompressionTypes.include?(compression)
end

Instance Method Details

#archive(sources_dir, dest_dir, dest_file_name) ⇒ Object

Returns the archive name



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/siba/plugins/archive/tar/archive.rb', line 16

def archive(sources_dir, dest_dir, dest_file_name)
  Siba::Archive::Tar::Archive.check_compression_type compression

  options = get_tar_option
  extension = Archive.get_tar_extension compression

  archive_name = "#{dest_file_name}.tar#{extension}"
  archive_path = File.join(dest_dir, archive_name)
  siba_file.run_this do
    raise Siba::Error, "Archive file already exists: #{archive_path}" if siba_file.file_file?(archive_path) || siba_file.file_directory?(archive_path)  

    siba_file.file_utils_cd dest_dir
    command_text = %(tar c#{options}f #{archive_name} -C "#{sources_dir}" .)
    # Using -C 'change directory' option to make it work on Windows
    # because Windows will not understand absolute path to tar: "tar cf c:\dir\file.tar ."
    siba_file.run_shell command_text, "Failed to archive: #{command_text}"
    raise Siba::Error, "Failed to create archive: #{command_text}" unless siba_file.file_file?(archive_path)
  end
  archive_name
end

#check_installedObject

Making sure tar is installed and works: tars and un-tars a test file



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/siba/plugins/archive/tar/archive.rb', line 54

def check_installed
  siba_file.run_this("test installed") do
    siba_file.run_shell("tar --help", "'tar' utility is not found. Please install it.")

    begin
      test_archive_and_extract
    rescue Exception
      logger.error "'tar' utility does not work correctly. Try reinstalling it."
      raise
    end
    logger.debug "TAR archiver is verified"
  end
end

#extract(archive_path, destination_dir) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/siba/plugins/archive/tar/archive.rb', line 37

def extract(archive_path, destination_dir)
  options = get_tar_option
  archive_name = File.basename archive_path
  archive_dir = File.dirname archive_path
  siba_file.file_utils_cd archive_dir 
  command_text = %(tar x#{options}f #{archive_name} -C "#{destination_dir}")
  # Using -C 'change directory' option to make it work on Windows
  # because Windows will not understand absolute path to tar: "tar xf c:\dir\file.tar"

  siba_file.run_this do
    unless siba_file.shell_ok? command_text
      raise Siba::Error, "Failed to extract archive: #{command_text}"
    end
  end
end

#test_archive_and_extractObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/siba/plugins/archive/tar/archive.rb', line 68

def test_archive_and_extract        
  # archive
  src_dir = Siba::TestFiles.prepare_test_dir "tar-archive-src"
  dest_dir = Siba::TestFiles.mkdir_in_tmp_dir "tar-archive-dest"
  file_name = "myname"
  archive src_dir, dest_dir, file_name
  path_to_file = File.join(dest_dir,"#{file_name}.tar#{Archive.get_tar_extension(compression)}")
  raise unless siba_file.file_file? path_to_file

  # extract
  extracted_dir = Siba::TestFiles.mkdir_in_tmp_dir "tar-archive-extracted"
  extract path_to_file, extracted_dir

  # compare
  Siba::FileHelper.dirs_same? src_dir, extracted_dir
end