Module: IMW::Archives::Base

Included in:
Rar, Tar, Tarbz2, Targz, Zip
Defined in:
lib/imw/archives.rb

Overview

Defines methods for creating, appending to, extracting, and listing an archive file. This module isn’t used to directly extend an IMW::Resource – instead, format specifc modules (e.g. - IMW::Resources::Archives::Tarbz2) include this module and define the specific settings (command-line flags, &c.) required to make things work.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#archive_settingsObject

Returns the value of attribute archive_settings.



31
32
33
# File 'lib/imw/archives.rb', line 31

def archive_settings
  @archive_settings
end

Instance Method Details

#append(*input_paths) ⇒ IMW::Resource

Append to this archive the given input_paths.

The input paths must be strings and will be shell-escaped before further processing. This means you cannot use a shell glob!

Parameters:

  • input_paths (String)

    the paths to add to this archive

Returns:



62
63
64
65
66
# File 'lib/imw/archives.rb', line 62

def append *input_paths
  should_have_archive_setting!("Cannot append to archive #{path}", :program, :append)
  IMW.system archive_settings[:program], archive_settings[:append], path, *input_paths.flatten
  self
end

#contentsArray<String>

Return a (sorted) list of contents in this archive.

Returns:



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/imw/archives.rb', line 82

def contents
  should_exist!("Cannot list archive contents.")
  should_have_archive_setting!("Cannot list archive #{path}", :list, [:unarchiving_program, :program])
  program = archive_settings[:unarchiving_program] || archive_settings[:program]
  # FIXME this needs to be more robust
  flags = archive_settings[:list]
  flags = flags.join(' ') if flags.is_a?(Array)
  command = [program, flags, path.gsub(' ', '\ ')].join(' ')
  output  = `#{command}`
  archive_contents_string_to_array(output)
end

#create(*input_paths) ⇒ IMW::Resource

Create an archive of the given input_paths.

The input paths must be strings and will be shell-escaped before further processing. This means you cannot use a shell glob!

Parameters:

  • input_paths (String)

    the paths to add to this archive

Returns:



48
49
50
51
52
# File 'lib/imw/archives.rb', line 48

def create *input_paths
  should_have_archive_setting!("Cannot create archive #{path}", :program, :create)
  IMW.system archive_settings[:program], archive_settings[:create], path, *input_paths.flatten
  self
end

#extractIMW::Resource

Extract the files from this archive to the current directory.

Returns:



71
72
73
74
75
76
77
# File 'lib/imw/archives.rb', line 71

def extract
  should_exist!("Cannot extract archive.")        
  should_have_archive_setting!("Cannot extract archive #{path}", :extract, [:unarchving_program, :program])
  program = archive_settings[:unarchiving_program] || archive_settings[:program]
  IMW.system program, archive_settings[:extract], path
  self
end

#is_archive?true, false

Is this file an archive?

Returns:

  • (true, false)


36
37
38
# File 'lib/imw/archives.rb', line 36

def is_archive?
  true
end