Module: IMW::Resources::Archive

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.



20
21
22
# File 'lib/imw/resources/archive.rb', line 20

def archive_settings
  @archive_settings
end

Instance Method Details

#append(*input_paths) ⇒ Object

Append to this archive the given input_paths.

Parameters:



41
42
43
44
45
# File 'lib/imw/resources/archive.rb', line 41

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

#contentsArray

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

Returns:

  • (Array)

    a list of paths in the archive.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/imw/resources/archive.rb', line 58

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) ⇒ Object

Create an archive of the given input_paths.

Parameters:



32
33
34
35
36
# File 'lib/imw/resources/archive.rb', line 32

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

#extractObject

Extract the files from this archive to the current directory.



48
49
50
51
52
53
# File 'lib/imw/resources/archive.rb', line 48

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
end

#is_archive?true, false

Is this file an archive?

Returns:

  • (true, false)


25
26
27
# File 'lib/imw/resources/archive.rb', line 25

def is_archive?
  true
end