class Extracter::Extracter

Constants

ARRAY_REGISTERED_ARCHIVES
#

ARRAY_REGISTERED_ARCHIVES

Archives that can be extracted, have to be registered in this Array.

Sort alphabetically.

The libreoffice format .odt is just like .zip.

#
GEM_UNPACK_COMMAND
#

GEM_UNPACK_COMMAND

The command to use to unpack ruby .gems. We can pass the —target=DIR syntax to extract to a specific location.

#
LAST_UPDATED
#

LAST_UPDATED

When this class was last updated/releasted. This is not a hugely important constat, so do not worry too much if this may be heavily outdated eventually.

#
N
#

N

#
NAMESPACE
#

NAMESPACE

#
SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE
#

SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE

If this constant is set to true then we will only show the shortened name of the archive in question by default.

#
TEMP_DIR
#

If this environment variable is unavailable then use a conservative default value.

#
VERSION
#

VERSION

Which specific version to use for class Extracter.

#

Attributes

run_simulation[RW]
#

Shall we run in simulation mode or not.

#

Public Class Methods

[](i, where_to = Dir.pwd) click to toggle source
#

Extracter::Extracter[]

#
# File lib/extracter/class_methods.rb, line 115
def self.[](i, where_to = Dir.pwd)
  new(i, where_to)
end
extract_what_to( what, to = Dir.pwd, optional_be_silent = false ) { || ... } click to toggle source
#

Extracter::Extracter.extract_what_to

This method provides a convenient API to extract something to a specified directory, as a class method. It defaults to the current working directory, as that is by far the most convenient way to extract a source tarball/archive.

Useage example goes like this:

Extracter.extract_what_to('foo-1.0.tar.xz', '/tmp')
#
# File lib/extracter/class_methods.rb, line 41
def self.extract_what_to(
    what,
    to                 = Dir.pwd, # <- This can also be a Hash.
    optional_be_silent = false
  )
  _ = Extracter.new(what, to, :do_not_run_yet)
  if to.is_a? Hash
    # ===================================================================== #
    # === :prepend_this_namespace
    # ===================================================================== #
    if to.has_key? :prepend_this_namespace
      prefix_with_this = to.delete(:prepend_this_namespace) # Get rid of it from the Hash as well.
      _.prefix_namespace_with(
        prefix_with_this
      )
    end
    # ===================================================================== #
    # === :to
    # ===================================================================== #
    if to.has_key? :to
      to = to.delete(:to)
    end
    # ===================================================================== #
    # === :verbosity
    #
    # Handle how verbose we shall be.
    # ===================================================================== #
    if to.has_key? :verbosity
      _.set_verbosity(
        to.delete(:verbosity)
      )
    end
    # ===================================================================== #
    # === :use_colours
    # ===================================================================== #
    if to.has_key? :use_colours
      _.set_use_colours(to[:use_colours])
    end
    # ===================================================================== #
    # === :pad_opn_with_n_tokens
    # ===================================================================== #
    if to.has_key? :pad_opn_with_n_tokens
      _.set_pad_opn_with_n_tokens(to[:pad_opn_with_n_tokens])
    end
    # ===================================================================== #
    # === :use_opn
    # ===================================================================== #
    if to.has_key? :use_opn
      _.set_use_opn(to[:use_opn])
    end
  end
  if optional_be_silent
    _.be_silent
  else
    _.be_verbose
  end
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    when :show_the_full_name_of_the_archive
      _.do_show_the_full_name_of_the_archive
    end
  end
  _.run
end
is_this_a_valid_archive?( i, array_registered_archives = ARRAY_REGISTERED_ARCHIVES ) click to toggle source
#

Extracter::Extracter.is_this_a_valid_archive?

Query whether the input is a valid archive.

The registered formats are stored in the constant ARRAY_REGISTERED_ARCHIVES.

#
# File lib/extracter/class_methods.rb, line 17
def self.is_this_a_valid_archive?(
    i,
    array_registered_archives = ARRAY_REGISTERED_ARCHIVES
  )
  return_value = false
  array_registered_archives.each {|entry|
    return_value = true if i =~ /#{entry}$/
  }
  return return_value
end
new( what = nil, where_to = nil, run_already = true ) { || ... } click to toggle source
#

initialize

The first argument to this method is the archive that should be extracted. This must be a (locally) existing archive, such as foobar.tar.xz or something similar.

The second argument to this method, called `to`, specifies the target location, where this class will extract the archive into, if available. Some keywords and shortcuts exist for this option - for instance, TEMP means $MY_TEMP, which can be set by the user.

Specific usage example in pure Ruby:

x = Extracter.what_to('pry-0.9.9.4.gem', '/Depot/Temp')
#
# File lib/extracter/initialize.rb, line 28
def initialize(
    what        = nil,
    where_to    = nil, # Where to extract into.
    run_already = true,
    &block
  )
  register_sigint
  reset
  if where_to.is_a? Symbol
    if where_to.to_s.include? 'dont' # This also covers :dont_run_yet
      where_to    = nil # This is ok because we expect the user to provide the target location.
      run_already = false
    end
  end
  if debug? # Some debug-information.
    e "The first argument what is: `#{what}`"
    e "The second argument to is:  `#{to}`"
  end
  set_source_location(what)
  set_extract_to(where_to)
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    # ===================================================================== #
    # === Handle Hash input next:
    # ===================================================================== #
    if yielded.is_a? Hash
      if yielded.has_key? :run_already
        run_already = yielded.delete(:run_already)
      end
      # =================================================================== #
      # === :use_colours
      # =================================================================== #
      if yielded.has_key? :use_colours
        set_use_colours yielded.delete(:use_colours)
      end
      # =================================================================== #
      # === :extract_to
      # =================================================================== #
      if yielded.has_key? :extract_to
        set_extract_to yielded.delete(:extract_to)
      end
      # =================================================================== #
      # === :run_simulation
      # =================================================================== #
      if yielded.has_key? :run_simulation
        set_run_simulation(yielded.delete(:run_simulation))
      end
      # =================================================================== #
      # === :use_opn
      # =================================================================== #
      if yielded.has_key? :use_opn
        set_use_opn(yielded.delete(:use_opn))
      end
    else
      case yielded
      when :dont_run_yet,
           :do_not_run_yet
        run_already = false
      end
    end
  end
  case run_already
  when :dont_run_yet,
       :do_not_run_yet
    run_already = false
  end
  run if run_already
end

Public Instance Methods

be_silent() click to toggle source
#

be_silent

#
# File lib/extracter/misc.rb, line 312
def be_silent
  set_be_verbose(false)
end
be_verbose() click to toggle source
#

be_verbose

#
# File lib/extracter/misc.rb, line 305
def be_verbose
  set_be_verbose(true)
end
be_verbose?() click to toggle source
#

be_verbose?

Getter method for whether we will be verbose or not.

#
# File lib/extracter/misc.rb, line 298
def be_verbose?
  @be_verbose
end
check_whether_rar_is_available() click to toggle source
#

check_whether_rar_is_available

We try to find out whether unrar is available.

#
# File lib/extracter/misc.rb, line 76
def check_whether_rar_is_available
  is_available = false
  ENV['PATH'].split(':').each {|entry|
    is_available = true if File.exist? entry+'/unrar'
  }
  unless is_available
    copn; e 'Sorry, unrar is not available. Please install it first.'
  end
end
colour_to_use_for_directories?() click to toggle source
#

colour_to_use_for_directories?

#
# File lib/extracter/colours.rb, line 64
def colour_to_use_for_directories?
  if @use_colours
    @colour_to_use_for_directories
  else
    ''
  end
end
copn()
Alias for: custom_opn
create_directory(i) click to toggle source
#

create_directory

Use this to create directories.

#
# File lib/extracter/misc.rb, line 354
def create_directory(i)
  FileUtils.mkdir_p(i)
end
Also aliased as: mkdir
custom_opn() click to toggle source
#

custom_opn

This is like opn(), except that we also check whether we should show the name or not.

#
# File lib/extracter/opn.rb, line 19
def custom_opn
  opnn unless @do_not_show_name
end
Also aliased as: copn
cyan?() click to toggle source
#

cyan

#
# File lib/extracter/colours.rb, line 75
def cyan?
  if @use_colours
    Colours::CYAN
  else
    ''
  end
end
debug?() click to toggle source
#

debug?

#
# File lib/extracter/misc.rb, line 29
def debug?
  @debug
end
determine_default_opn_hash() click to toggle source
#

determine_default_opn_hash

#
# File lib/extracter/misc.rb, line 329
def determine_default_opn_hash
  @use_this_opn_hash = {
    namespace:   namespace?,
    use_colours: use_colours?
  }
end
Also aliased as: update_the_opn_hash
did_we_extract_already()
did_we_extract_already?() click to toggle source
#

did_we_extract_already?

Whether we already did extract or whether we did not.

#
# File lib/extracter/misc.rb, line 184
def did_we_extract_already?
  @did_we_extract_already
end
Also aliased as: did_we_extract_already
disable_colours() click to toggle source
#

disable_colours

Use this method if you want to disable colour-support of this class.

#
# File lib/extracter/colours.rb, line 39
def disable_colours
  @use_colours = false
  @colour_to_use_for_directories = ''.dup
end
do_extract_to( what, to = extract_to? )
Alias for: do_extract_what_to
do_extract_what_to( what, to = extract_to? ) click to toggle source
#

do_extract_what_to

This method should only be called from the method work_on_the_given_input().

It will attempt to extract the specified archive.

#
# File lib/extracter/do_extract_what_to.rb, line 19
def do_extract_what_to(
    what, # ← The archive or file that we wish to extract.
    to = extract_to?
  )
  name_of_the_archive = what.dup
  @did_we_extract_already = false
  to = extract_to? if to.nil?
  set_source_location(what)
  set_extract_to_this_location(to) # Keep it in sync.
  # ======================================================================= #
  # Create the directory if it does not yet exist.
  # ======================================================================= #
  create_directory(to) if !File.directory?(to)
  if be_verbose?
    if @show_only_the_short_name_of_the_archive
      name_of_the_archive = File.basename(name_of_the_archive)
      copn; e "Extracting `#{sfancy(name_of_the_archive)}` to `#{sdir(to)}` next."
    else
      copn; e "Extracting `#{sfancy(name_of_the_archive)}` to `#{sdir(to)}` next."
    end
  end
  _ = ''.dup # Default.
  extname = File.extname(what)
  unless ARRAY_REGISTERED_ARCHIVES.include? extname.delete('.')
    opn; e 'We did not register the following extension: '+extname.delete('.')
    fail_message_not_registered(extname)
    @skip_extracting = true
  end
  case extname # Case tag. Those listed on top are more important.
  # ======================================================================= #
  # === .tar
  # ======================================================================= #
  when '.tar',
       '.tar.bz2',
       '.tbz'
    _ << 'tar -xvf'
  # ======================================================================= #
  # === pdf
  #
  # For pdf-files we will tap into the pdf_paradise project.
  # ======================================================================= #
  when /\.?pdf$/
    begin
      require 'pdf_paradise/convert_pdf_to_text.rb'
      _ = PdfParadise::ConvertPdfToText.new(what)
      _.output_file?
    rescue LoadError; end
    return # Must return early in this case.
  # ======================================================================= #
  # === img
  #
  # Note that .img in this context refers to squafhs .img files.
  # ======================================================================= #
  when '.img',
       '.squashfs'
    opnn; e 'Handling a squashfs .img file format next:'
    name_without_extension = what.sub(/#{File.extname(what)}$/,'')
    mkdir(name_without_extension) unless File.directory? name_without_extension
    esystem 'mount -o loop -t squashfs '+what+' '+name_without_extension
    e 'The content of the extracted (or rather, mounted) archive is:'
    pp Dir["#{name_without_extension}*"]
    return # Must return early in this case.
  # ======================================================================= #
  # === iso
  # ======================================================================= #
  when '.iso'
    opnn; e 'Extracting an .iso file is a bit more complicated '\
            'than a .tar.gz tarball release.'
    opnn; e 'We will first create a directory; and then mount '\
            'the .iso there.'
    name_without_extension = what.sub(/#{File.extname(what)}$/,'')
    mkdir(name_without_extension) unless File.directory? name_without_extension
    esystem 'mount -o loop '+what+' '+name_without_extension
    e 'The content of the extracted (or rather, mounted) archive is:'
    pp Dir[name_without_extension+'*']
    return
  # ======================================================================= #
  # === sxz
  # ======================================================================= #
  when '.sxz'
    _ = 'unsquashfs '.dup # This requires squashfs with xz-support.
  # ======================================================================= #
  # === lz
  # ======================================================================= #
  when '.lz','.tar.lz'
    _ = 'tar --lzip -xvf '.dup # This requires lzip to be installed.
  # ======================================================================= #
  # === tar.Z
  # ======================================================================= #
  when '.tar.Z','.taz'
    _ << 'tar -xvzf'
  # ======================================================================= #
  # === .jar
  # ======================================================================= #
  when /\.?jar$/i
    _ << 'jar xvf'
  # ======================================================================= #
  # === .zst
  #
  # This entry point is for e. g. "pymol-2.3.0-3-x86_64.pkg.tar.zst".
  # ======================================================================= #
  when '.zst','.tar.zst'
    _ << 'tar -I zstd -xvf '
  # ======================================================================= #
  # === txz
  # ======================================================================= #
  when '.txz'
    _ << 'tar Jxvf' # Since Jan 2012.
  # ======================================================================= #
  # === gz
  # ======================================================================= #
  when '.gz'
    if what.include? '.tar'
      _ << 'tar -zxvf'
    else
      _ << 'gunzip'
    end
  # ======================================================================= #
  # === xz
  # ======================================================================= #
  when '.xz'
    if _.include? '.tar'
    end
    _ << 'tar -xvf' # tar -Jxv #{what} would be an alternative.
  # ======================================================================= #
  # === rpm
  # ======================================================================= #
  when '.rpm'
    _ << 'bsdtar xfv'
  # ======================================================================= #
  # === bin
  # ======================================================================= #
  when '.bin' # handle .bin files here.
    # _ = 'tar -jxf '+package
    _ << "./#{what}"
  # ======================================================================= #
  # === zip
  # ======================================================================= #
  when '.zip','.xpi','.docx','.odt', # .docx and .odt format types can be unpacked with zip too.
       '.apkg'
    # _ << 'ar -jxf' # unzip #{what} <-- this should work as well.
    _ << 'unzip '
  when /\.bz2/,'.tbz2'
    if what.include? '.tar' # Treat it as a .tar file.
      _ << 'tar -vjxf '
    else
      _ << 'bunzip2 '
    end
  # ======================================================================= #
  # === lzma
  # ======================================================================= #
  when '.lzma'
    _ << 'unlzma '
  # ======================================================================= #
  # === 7z
  # ======================================================================= #
  when '.7z' # 7z does not accept the -C commandline.
    # _ << '7za e' # <- Deprecated as of 05.06.2020.
    _ << '7z x'
  # ======================================================================= #
  # === gem
  # ======================================================================= #
  when '.gem'
    _ << GEM_UNPACK_COMMAND
  # ======================================================================= #
  # === rar
  # ======================================================================= #
  when '.rar'
    check_whether_rar_is_available
    _ << 'unrar e'
  # ======================================================================= #
  # === deb
  # ======================================================================= #
  when '.deb'
    #_ = 'dpkg-deb -x' # {to}
    _ << 'ar -x' # ar -x #{what} This could work too.
  # ======================================================================= #
  # === tgz
  # ======================================================================= #
  when '.tgz'
    _ << 'tar -xvzf'
  # ======================================================================= #
  # === ps
  # ======================================================================= #
  when '.ps'
    _ << 'ps2ascii'
  # ======================================================================= #
  # === mp4
  # ======================================================================= #
  when '.mp4' # If it is a .mp4 file, we delegate to ExtractAudio instead.
    if Object.const_defined? :MultimediaParadise
      MultimediaParadise.extract_audio(@source_package_location)
    end
    exit
  else # else tag. We did not find the extension type.
    @skip_extracting = true
    copn; ewarn "We did not find: `#{sfile(what)}`. "
    copn; ewarn 'The file-type (extension) was: `'+simp(extname)+'`.'
    # Try to rescue though.
    result = run_this_system_command("file #{what}")
    copn; e result
    if result.include? 'bzip2 compressed'
      copn; e 'We assume it is a .bz2 file though.'
      _ = 'tar -vjxf '.dup
      @skip_extracting = false
    end
  end unless @skip_extracting
  unless File.exist? what
    @skip_extracting = true
    copn; e "The file `#{sfile(what)}"\
            "` does not exist - can not extract."
  end
  # ======================================================================= #
  # Handle the situation when the given input includes a ')' character.
  # We will pad such an input with '"' characters.
  # ======================================================================= #
  if what.include? ')'
    what = pad(what, '"') #sanitize_input(what)
  end
  # ======================================================================= #
  # Next, pad it if it includes a ' ' character.
  # ======================================================================= #
  what = pad(what) if what.include?(' ')
  _ << " #{what}" unless _.empty?
  if _.include? GEM_UNPACK_COMMAND # Gem command needs a --target=DIR option.
    _ << " --target=#{to}"
  elsif _.include?('ar -x') and ! _.include?('.tar') # Do nothing in this case.
  elsif _.end_with? '.sxz'
    # .sxz does not require the -C option.
  elsif _.end_with? '.zip','.xpi','.7z','.jar','.apkg'
    # Do not use -C option for 7z, as it hates this option.
    # .jar files also do not support the -C option.
  elsif _.include? 'bunzip'
    # Do not use -C option for bunzip, as it hates this option.
  else
    # ===================================================================== #
    # Next, we need to determine the location where we extract our
    # archive to.
    # ===================================================================== #
    _ << ' '
    # ===================================================================== #
    # Add -C option except for .deb packages and gunzip-based archives.
    # ===================================================================== #
    unless _ =~ /deb$/ or _.include?('gunzip')
      _ << '-C '
      _ << to
    end
  end
  if run_simulation?
    copn; e 'As we are running in simulation mode, the following command '
    copn; e 'is the one that we would have been used if we were to not run '
    copn; e 'in simulation mode:'
    e _
  else # Ok, here we are not in a simulation, hence we can run the command.
    unless @skip_extracting
      if File.writable? to
        # ================================================================= #
        # Next, run the sys-command, with some padding.
        # ================================================================= #
        begin
          run_this_system_command(
            "  #{_}", :also_show_what_we_will_do
          )
        # ================================================================= #
        # We have to rescue because unrar might not be available and so on.
        # ================================================================= #
        rescue Exception => error
          e 'An error has happened upon attempting to run this system command:'
          e
          e "  #{_}"
          e
          pp error
          e '-----------'
          pp error.class
          e '-----------'
        end
        @did_we_extract_already = true
      else
        copn; ewarn 'You do not have sufficient permissions to'
        copn; ewarn "modify #{sdir(to)}."
      end
    end
  end
end
Also aliased as: do_extract_to, start
do_not_show_name() click to toggle source
#

do_not_show_name

Tells us whether to use opn() or not.

#
# File lib/extracter/misc.rb, line 45
def do_not_show_name
  @do_not_show_name = true
end
do_show_name() click to toggle source
#

do_show_name

If this is enabled, we will show the name of the file when we invoke copn().

#
# File lib/extracter/misc.rb, line 55
def do_show_name
  @do_not_show_name = false
end
do_show_the_full_name_of_the_archive() click to toggle source
#

do_show_the_full_name_of_the_archive

#
# File lib/extracter/misc.rb, line 391
def do_show_the_full_name_of_the_archive
  @show_only_the_short_name_of_the_archive = false
end
e(i = '') click to toggle source
#

e

#
# File lib/extracter/misc.rb, line 384
def e(i = '')
  puts i
end
enable_colours() click to toggle source
#

enable_colours

#
# File lib/extracter/colours.rb, line 47
def enable_colours
  @use_colours = true
  @colour_to_use_for_directories = cyan?
end
enable_debug() click to toggle source
#

enable_debug

#
# File lib/extracter/misc.rb, line 36
def enable_debug
  @debug = true
end
esystem(i) click to toggle source
#

esystem

#
# File lib/extracter/misc.rb, line 376
def esystem(i)
  e i
  system i
end
ewarn(i = '') click to toggle source
#

ewarn

#
# File lib/extracter/colours.rb, line 86
def ewarn(i = '')
  if use_colours?
    e Colours.swarn(i)
  else
    e i
  end
end
extract_to( i = TEMP_DIR )
extract_to=( i = TEMP_DIR )
extract_to?() click to toggle source
#

extract_to?

Simply output the result of @extract_to_this_location variable.

#
# File lib/extracter/misc.rb, line 112
def extract_to?
  @extract_to_this_location.to_s
end
Also aliased as: source_package_location
extract_to_this_location()
extract_to_this_location?() click to toggle source
#

extract_to_this_location?

Extract to this location.

#
# File lib/extracter/misc.rb, line 193
def extract_to_this_location?
  @extract_to_this_location
end
Also aliased as: extract_to_this_location
extracted_path?()
Alias for: extracted_to?
extracted_to?() click to toggle source
#

extracted_to?

This method is different from extract_to?.

It will keep track of the directory to where we extracted to exactly.

#
# File lib/extracter/misc.rb, line 205
def extracted_to?
  rds(
    extract_to?+
    File.basename(input?).sub(/\.xz$/,'').sub(/\.gz$/,'').
                          sub(/\.tar$/,'')+'/'
  )
end
Also aliased as: extracted_path?
fail_message_not_registered(i) click to toggle source
#

fail_message_not_registered

Output a fail message when the archive format is not registered.

#
# File lib/extracter/misc.rb, line 400
def fail_message_not_registered(i)
  copn; e "Can not extract `#{sfancy(i)}` - it is not "\
          "registered."
end
input?()
Alias for: source_location?
mkdir(i)
Alias for: create_directory
namespace?() click to toggle source
#

namespace?

#
# File lib/extracter/misc.rb, line 216
def namespace?
  @namespace
end
opnn() click to toggle source
#

opnn

#
# File lib/extracter/opn.rb, line 26
def opnn
  if @use_opn and Object.const_defined?(:Opn)
    Opn.opn(@use_this_opn_hash)
  end
end
pad(i, with_this_character = "'") click to toggle source
#

pad

#
# File lib/extracter/misc.rb, line 103
def pad(i, with_this_character = "'")
  return with_this_character+i+with_this_character
end
pad_opn_with_n_tokens(n_tokens) click to toggle source
#

pad_opn_with_n_tokens

#
# File lib/extracter/opn.rb, line 42
def pad_opn_with_n_tokens(n_tokens)
  determine_default_opn_hash # Update this, just in case.
  @use_this_opn_hash.update(padding: n_tokens)
end
Also aliased as: set_pad_opn_with_n_tokens
prefix_namespace_with(i) click to toggle source
#

prefix_namespace_with

#
# File lib/extracter/misc.rb, line 270
def prefix_namespace_with(i)
  @namespace = "#{i}#{@namespace.dup}"
  update_the_opn_hash # Also update the opn-hash here.
end
rds(i) click to toggle source
#

rds

#
# File lib/extracter/misc.rb, line 369
def rds(i)
  i.squeeze('/')
end
register_sigint() click to toggle source
#

register_sigint

#
# File lib/extracter/misc.rb, line 339
def register_sigint
  Signal.trap('SIGINT') {
    e sfancy('Requesting a graceful exit from ')+
      colour_to_use_for_directories?+
     'class Extracter'+
     sfancy('. Exiting now.')
    exit
  }
end
remove_file_extension(i) click to toggle source
#

remove_file_extension

#
# File lib/extracter/misc.rb, line 361
def remove_file_extension(i)
  _ = File.basename(i)
  return RemoveFileSuffix[_]
end
report_to_the_user() click to toggle source
#

report_to_the_user

This method reports to the user. Usually this is done only via this file here though.

#
# File lib/extracter/misc.rb, line 281
def report_to_the_user
  if @be_verbose
    unless @skip_extracting
      copn; e 'Finished extracting to `'+sdir(
        extract_to?+remove_file_extension(
          @source_location.first # This is an Array.
        )+'/'
      )+'`.'
    end
  end
end
reset() click to toggle source
#

reset (reset tag)

Reset our main instance variables here.

#
# File lib/extracter/reset.rb, line 14
def reset
  # ======================================================================= #
  # === @namespace
  #
  # Specify the main namespace to be used. This setting can be modified
  # at "runtime".
  # ======================================================================= #
  @namespace = NAMESPACE
  # ======================================================================= #
  # === @debug
  # ======================================================================= #
  @debug = false
  # ======================================================================= #
  # === Extract to this location
  #
  # Next specify where to extract the archive in question, defaulting to
  # the TEMP_DIR constant.
  # ======================================================================= #
  @extract_to_this_location = TEMP_DIR
  # ======================================================================= #
  # === @did_we_extract_already
  #
  # We will keep track of whether we already extracted or not.
  # ======================================================================= #
  @did_we_extract_already = false
  # ======================================================================= #
  # === @do_not_show_name
  # ======================================================================= #
  @do_not_show_name = false
  # ======================================================================= #
  # === @use_opn
  # ======================================================================= #
  @use_opn = true # ← Whether to use Opn by default or not.
  # ======================================================================= #
  # === @show_only_the_short_name_of_the_archive
  # ======================================================================= #
  @show_only_the_short_name_of_the_archive = SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE
  # ======================================================================= #
  # === @skip_extracting
  # ======================================================================= #
  @skip_extracting = false
  # ======================================================================= #
  # === @run_simulation
  # ======================================================================= #
  @run_simulation = false # ← Whether to run in simulation, or for "real".
  set_extract_to_this_location
  do_show_name # We will show the name usually.
  enable_colours
  determine_default_opn_hash
  set_be_verbose(false)
end
run() click to toggle source
#

run (run tag, def tag)

#
# File lib/extracter/misc.rb, line 426
def run
  work_on_the_given_input
end
run_simulation?() click to toggle source
#

run_simulation?

#
# File lib/extracter/misc.rb, line 89
def run_simulation?
  @run_simulation
end
run_this_system_command( i, instruction = :do_nothing_special ) click to toggle source
#

run_this_system_command

#
# File lib/extracter/misc.rb, line 258
def run_this_system_command(
    i, instruction = :do_nothing_special
  )
  if instruction == :also_show_what_we_will_do
    copn; e i
  end
  return `#{i}` # system tag
end
sanitize_input(i) click to toggle source
#

sanitize_input

#
# File lib/extracter/misc.rb, line 62
def sanitize_input(i)
  if i.include? '('
    i.gsub!(/\(/,'\(')
    i.gsub!(/\)/,'\)') if i.include? ')'
    i = pad(i, '"')
  end
  return i
end
sdir(i = '') click to toggle source
#

sdir

#
# File lib/extracter/colours.rb, line 121
def sdir(i = '')
  return Colours.sdir(i) if @use_colours
  return i
end
set_be_verbose(i = false) click to toggle source
#

set_be_verbose

This sets the verbosity level of the class. Use only this method when you wish to modify the @be_verbose instance variable.

#
# File lib/extracter/misc.rb, line 322
def set_be_verbose(i = false)
  @be_verbose = i
end
Also aliased as: set_verbosity
set_colour_for_directories(i) click to toggle source
#

set_colour_for_directories

Set the colour for directories to use.

#
# File lib/extracter/colours.rb, line 57
def set_colour_for_directories(i)
  @colour_to_use_for_directories = Colours.beautify(i)
end
set_extract_to( i = TEMP_DIR )
set_extract_to_this_location( i = TEMP_DIR ) click to toggle source
#

set_extract_to_this_location

Use this when setting the variable @extract_to_this_location.

This can be modified from the commandline such as by doing this:

#
# File lib/extracter/misc.rb, line 227
def set_extract_to_this_location(
    i = TEMP_DIR
  )
  if i.is_a? Hash
    if i.has_key? :to
      i = i.delete :to
    elsif i.has_key? :extract_to
      i = i.delete :extract_to
    end
  end
  case i # case tag
  when :default
    i = Dir.pwd
  when 'TEMP',
       'MY_TEMP',
       'MYTEMP'
    i = TEMP_DIR
  end
  i = TEMP_DIR if i.nil?
  i = i.to_s.dup
  i << '/' unless i.end_with? '/'
  i = rds(i)
  i.gsub!(/--to=/,'') if i.include? '--to='
  @extract_to_this_location = i
end
set_pad_opn_with_n_tokens(n_tokens)
set_run_simulation(i) click to toggle source
#

set_run_simulation

#
# File lib/extracter/misc.rb, line 96
def set_run_simulation(i)
  @run_simulation = i
end
set_source_location(i = nil) click to toggle source
#

set_source_location

Use this method to designate the source location of a given (input) program. In other words - the tarball or archive that must be extracted. It will however be stored as Array.

If we pass a hash to this method, we assume that the user wants to also populate some other values.

#
# File lib/extracter/misc.rb, line 126
def set_source_location(i = nil)
  if i.nil?
    copn; e 'You should set a file.'
  end
  if i.is_a? Array
    i = i.flatten
  else
    i = [i]
  end # After this point, we will have an Array.
  case i.first # case tag
  when /^-?-?help$/i
    show_help
    exit
  end
  i.map! {|entry| # Iterate over our Array next.
    # ======================================================================= #
    # Handle the case when the user did input a number.
    # ======================================================================= #
    begin
      if entry =~ /^\d$/
        entry = Dir['*'][( entry.to_i - 1 )] unless File.exist?(entry)
      end
    rescue ArgumentError => error
      e 'Error for '+sfancy(entry)+':'
      pp error
    end
    entry = rds(entry.to_s)
    # ======================================================================= #
    # Next, find the proper working directory.
    # ======================================================================= #
    unless entry.include? '/'
      entry = rds( (Dir.pwd+'/'+entry) )
    end
    # ======================================================================= #
    # If the user supplied a directory instead, we will randomly grab an
    # entry from said directory.
    # ======================================================================= #
    if File.directory? entry
      entry = Dir[rds(entry+'/')+'*'].sample
    end
    entry
  } # Sanitize the result, just in case.
  i = [i] unless i.is_a? Array # Much more convenient to work with an array.
  @source_location = i
end
Also aliased as: set_what
set_use_colours(i) click to toggle source
#

set_use_colours

#
# File lib/extracter/colours.rb, line 25
def set_use_colours(i)
  # ======================================================================= #
  # We must also sync this towards our main Hash, for opn(). The next
  # line of code achieves precisely that.
  # ======================================================================= #
  @use_this_opn_hash.update(use_colours: i)
  @use_colours = i
end
set_use_opn(i = true) click to toggle source
#

set_use_opn

#
# File lib/extracter/opn.rb, line 35
def set_use_opn(i = true)
  @use_opn = i
end
set_verbosity(i = false)
Alias for: set_be_verbose
set_what(i = nil)
Alias for: set_source_location
sfancy(i = '') click to toggle source
#

sfancy

#
# File lib/extracter/colours.rb, line 113
def sfancy(i = '')
  return Colours.sfancy(i) if @use_colours
  return i
end
sfile(i = '') click to toggle source
#

sfile

#
# File lib/extracter/colours.rb, line 105
def sfile(i = '')
  return Colours.sfancy(i) if @use_colours
  return i
end
show_help() click to toggle source
#

show_help (help tag)

This method will show the available - and documented - help options for class Extracter.

#
# File lib/extracter/help.rb, line 15
def show_help
  e
  opnn; e 'How to extract archives, without helper scripts?'
  e
  opnn; e '  tar -zxvf foobar.tar.gz           # for .tar.gz'
  opnn; e '  tar xvzf foobar.tgz               # for .tgz'
  opnn; e '  tar xvfJ foobar.tar.xz            # for .tar.xz'
  opnn; e '  tar jxf foobar.tar.bz2            # for .tar.bz2'
  opnn; e '  tar -xf foobar.tar.bz2            # for .tbz'
  opnn; e '  tar --lzip -xvf zutils-1.5.tar.lz # for .tar.lz'
  opnn; e '  unsquashfs foobar-1.2.3.sxz       # for .sxz'
  e
  opnn; e 'Furthermore, there are some commandline options '\
          'that can be used for this class (class Extracter).'
  e
  opnn; e '  --to=/home/Temp # extract into the '\
          'directory /home/Temp/'
  e
end
simp(i = '') click to toggle source
#

simp

#
# File lib/extracter/colours.rb, line 97
def simp(i = '')
  return Colours.simp(i) if @use_colours
  return i
end
source_location?() click to toggle source
#

source_location?

#
# File lib/extracter/misc.rb, line 175
def source_location?
  @source_location.first
end
Also aliased as: input?
source_package_location()
Alias for: extract_to?
start( what, to = extract_to? )
Alias for: do_extract_what_to
update_the_opn_hash()
use_colours?() click to toggle source
#

use_colours?

Determine whether we will use colours in class Extracter.

#
# File lib/extracter/colours.rb, line 18
def use_colours?
  @use_colours
end
work_on_the_given_input() click to toggle source
#

work_on_the_given_input

#
# File lib/extracter/misc.rb, line 408
def work_on_the_given_input
  if @source_location.empty?
    copn; e 'Can not extract anything as no input has been given.'
  else
    @source_location.each {|entry|
      if Extracter.is_this_a_valid_archive?(entry)
        do_extract_what_to(entry)
        report_to_the_user
      else
        fail_message_not_registered(entry)
      end
    }
  end
end