class Extracter::Extracter

Constants

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.

#

Public Class Methods

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

Extracter::Extracter[]

#
# File lib/extracter/class/extracter.rb, line 1004
def self.[](i = ARGV, extract_where_to = Dir.pwd)
  new(i, extract_where_to)
end
extract_this( i = ARGV, to = {}, &block ) click to toggle source
#

Extracter::Extracter.extract_this

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.

#
# File lib/extracter/class/extracter.rb, line 991
def self.extract_this(
    i  = ARGV,
    to = {}, # This may also be a String rather than a Hash.
    &block
  )
  ::Extracter::Extracter.new(ARGV, to, &block)
end
new( commandline_arguments = ARGV, extract_to = nil, run_already = true ) { || ... } click to toggle source
#

initialize

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

The second argument to this method, called ‘extract_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', '/home/Temp')
#
# File lib/extracter/class/extracter.rb, line 71
def initialize(
    commandline_arguments = ARGV,
    extract_to            = nil,
    run_already           = true,
    &block
  )
  register_sigint
  reset
  @internal_hash[:run_already] = run_already
  set_commandline_arguments(
    commandline_arguments
  )
  if debug? # Some debug-information in this case.
    e "The first argument what is:       `#{commandline_arguments}`"
    e "The second argument where_to is:  `#{extract_to}`"
  end
  case run_already
  # ======================================================================= #
  # === :dont_run_yet
  # ======================================================================= #
  when :dont_run_yet,
       :do_not_run_yet,
       :default
    @internal_hash[:run_already] = false
  end
  set_extract_to(extract_to) if extract_to
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :dont_run_yet
    # ===================================================================== #
    when :dont_run_yet,
         :do_not_run_yet,
         :default
      @internal_hash[:run_already] = false
    # ===================================================================== #
    # === :show_the_full_name_of_the_archive
    # ===================================================================== #
    when :show_the_full_name_of_the_archive
      do_show_the_full_name_of_the_archive
    end
  end
  run if run_already?
end

Public Instance Methods

be_quiet()
Alias for: be_silent
be_silent() click to toggle source
#

be_silent

#
# File lib/extracter/class/extracter.rb, line 407
def be_silent
  set_be_verbose(false)
end
Also aliased as: be_quiet
be_verbose() click to toggle source
#

be_verbose

#
# File lib/extracter/class/extracter.rb, line 400
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/class/extracter.rb, line 426
def be_verbose?
  @internal_hash[: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/class/extracter.rb, line 212
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/class/extracter.rb, line 446
def colour_to_use_for_directories?
  if use_colours?
    return @internal_hash[:colour_to_use_for_directories]
  else
    return ''
  end
end
commandline_arguments?() click to toggle source
#

commandline_arguments?

#
# File lib/extracter/class/extracter.rb, line 275
def commandline_arguments?
  @commandline_arguments
end
consider_verbosity_for(i) click to toggle source
#

consider_verbosity_for

#
# File lib/extracter/class/extract_this_archive.rb, line 16
def consider_verbosity_for(i)
  i = i.dup
  unless be_verbose?
    case i
    # "tar -xvf" must become "tar -xvf" here.
    when COMMAND_TO_EXTRACT_TAR_XZ_FILES
      i.delete!('v')
    end
  end
  return i
end
copn( use_this_hash = use_this_opn_hash? )
Alias for: opnn
create_directory(i) click to toggle source
#

create_directory (mkdir tag)

Use this to create directories.

#
# File lib/extracter/class/extracter.rb, line 842
def create_directory(i)
  FileUtils.mkdir_p(i) unless File.directory?(i)
end
Also aliased as: mkdir
debug?() click to toggle source
#

debug?

#
# File lib/extracter/class/extracter.rb, line 225
def debug?
  @internal_hash[:debug]
end
determine_default_opn_hash( use_this_hash = { namespace: namespace?, use_colours: use_colours? } )
determine_the_default_opn_hash( use_this_hash = { namespace: namespace?, use_colours: use_colours? } )
disable_colours() click to toggle source
#

disable_colours

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

#
# File lib/extracter/class/extracter.rb, line 562
def disable_colours
  set_use_colours(false)
  @internal_hash[:colour_to_use_for_directories] = ''.dup
end
do_extract_to( i, extract_to = extract_to? )
do_extract_what_to( i, extract_to = extract_to? )
do_not_show_name() click to toggle source
#

do_not_show_name

Tells us whether to use opn() or not.

#
# File lib/extracter/class/extracter.rb, line 196
def do_not_show_name
  @internal_hash[:show_the_name] = false
end
do_show_name() click to toggle source
#

do_show_name

If this method is called then the class here will show the name of the file on the commandline, via opn().

#
# File lib/extracter/class/extracter.rb, line 545
def do_show_name
  @internal_hash[:show_the_name] = true
end
do_show_the_full_name_of_the_archive() click to toggle source
#

do_show_the_full_name_of_the_archive

#
# File lib/extracter/class/extracter.rb, line 457
def do_show_the_full_name_of_the_archive
  @internal_hash[:show_the_full_name_of_the_archive] = true
end
enable_colours() click to toggle source
#

enable_colours

#
# File lib/extracter/class/extracter.rb, line 552
def enable_colours
  set_use_colours(true)
  @internal_hash[:colour_to_use_for_directories] = cyan?
end
enable_debug() click to toggle source
#

enable_debug

#
# File lib/extracter/class/extracter.rb, line 239
def enable_debug
  @internal_hash[:debug] = true
end
esystem( i, try_to_use_colours = try_to_use_colours? ) click to toggle source
#

esystem (system tag, esystem tag)

#
# File lib/extracter/class/extracter.rb, line 899
def esystem(
    i, try_to_use_colours = try_to_use_colours?
  )
  i = i.dup if i.frozen?
  # ======================================================================= #
  # Next, consider appending something onto the commandline.
  # ======================================================================= #
  _ = @internal_hash[:append_this_to_the_commandline]
  unless _.empty?
    if i.include? ' '
      splitted = i.split(' ')
      splitted[0] << " #{_}"
      i = splitted.join(' ')
    else
      i << " #{_}"
    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:'
    if be_verbose?
      if try_to_use_colours
        e ::Colours.steelblue(i)
      else
        e i
      end
    end
  else
    if be_verbose?
      if try_to_use_colours
        e ::Colours.steelblue(i)
      else
        e i
      end
    end
    # ===================================================================== #
    # Next, run the sys-command.
    # ===================================================================== #
    begin
      system i
      # =================================================================== #
      # We have to rescue here 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 "  #{i}"
      e
      pp error; e '-----------'; pp error.class; e '-----------'
    end
  end
end
Also aliased as: run_this_system_command
extract_this_archive( i, extract_to = extract_to? ) click to toggle source
#

extract_this_archive (extract tag)

#
# File lib/extracter/class/extract_this_archive.rb, line 31
def extract_this_archive(
    i, extract_to = extract_to?
  )
  i = File.absolute_path(i)
  # ======================================================================= #
  # Next handle the situation when we are on Windows:
  # ======================================================================= #
  if ::Extracter.are_we_on_windows?
    # ===================================================================== #
    # On windows we will overrule this completely, for now.
    # ===================================================================== #
    _ = UNPACK_COMMAND_TO_USE_ON_WINDOWS+' '+
        File.absolute_path(i)+
        ' | '+
        SECOND_UNPACK_COMMAND_TO_USE_ON_WINDOWS
    esystem _
    return
  end
  # ======================================================================= #
  # First determine whether we can extract the archive or whether we can
  # not:
  # ======================================================================= #
  if ::Extracter.is_this_a_valid_archive?(i) or i.end_with?('.pdf')
    if be_verbose?
      if show_only_the_short_name_of_the_archive?
        name_of_the_archive = File.basename(i)
        copn; e "Extracting `#{sfancy(name_of_the_archive)}` "\
                "to `#{sdir(extract_to)}` next."
      else
        copn; e "Extracting `#{sfancy(i)}` to `#{sdir(extract_to)}` "\
                "next."
      end
    end
    unless File.writable? extract_to
      copn; ewarn 'You do not have sufficient permissions to'
      copn; ewarn "modify #{sdir(extract_to)}."
      return
    end
    # ===================================================================== #
    # Next, pad it if it includes a ' ' character or (). This was
    # disabled as of July 2022.
    # ===================================================================== #
    # i = pad(i) if i.include?(' ') or i.include?(')')
    case i # case tag; those listed on top are more important.
    # ===================================================================== #
    # === .rpm
    #
    # This entry point will handle .rpm files.
    # ===================================================================== #
    when /\.rpm$/i
      name_of_the_directory = i.delete_suffix('.rpm')
      mkdir(name_of_the_directory)
      set_extract_to(File.absolute_path(name_of_the_directory))
      cd(name_of_the_directory)
      esystem 'rpm2cpio ../'+File.basename(i)+' | cpio -idmv'
      return # Early return.
    # ===================================================================== #
    # === .tar.xz
    #
    # Note that .txz is just .tar.xz. Support for .txz was added here
    # in January of 2012.
    # ===================================================================== #
    when /\.tar\.xz$/i,
         /\.txz$/i,
         /\.xz$/i
      esystem consider_verbosity_for(COMMAND_TO_EXTRACT_TAR_XZ_FILES)+' '+i+padded_extract_to?
    # ===================================================================== #
    # === .tar
    #
    # This entry point is for simple .tar files.
    # ===================================================================== #
    when /\.tar$/i
      esystem COMMAND_TO_EXTRACT_TAR_FILES+' '+i+padded_extract_to?
    # ===================================================================== #
    # === zip
    # ===================================================================== #
    when /.zip$/i,
         /.xpi$/i,
         /.docx$/i,
         /.odt$/i, # .docx and .odt format types can be unpacked with zip too.
         /.apkg$/
      # =================================================================== #
      # 'ar -jxf' # unzip #{what} <-- this should work as well.
      # =================================================================== #
      i = pad(i) if i.include?(' ') or i.include?(')')
      esystem "unzip #{i}"
    # ===================================================================== #
    # === tgz
    #
    # This entry point will also handle ".tar.Z" files.
    # ===================================================================== #
    when /\.?tgz$/i,
         /\.?tar.Z$/i,
         /\.?taz$/i
      esystem COMMAND_TO_EXTRACT_TGZ_FILES+' '+i+padded_extract_to?
    # ===================================================================== #
    # === 7z
    # ===================================================================== #
    when '.7z' # 7z does not accept the -C commandline.
      # _ << '7za e' # <- Deprecated this variant as of 05.06.2020.
      esystem "7z x #{i}"
    # ===================================================================== #
    # === .tar.bz2
    # ===================================================================== #
    when /\.tar\.bz2$/i,
         /\.tbz$/i
      esystem COMMAND_TO_EXTRACT_TAR_BZ2_FILES+' '+i+padded_extract_to?
    # ===================================================================== #
    # === gz
    # ===================================================================== #
    when /\.?gz$/i,
         /\.?apk$/i
      if i.include? '.tar' # Handle .tar.gz here.
        esystem 'tar -zxvf '+i+padded_extract_to?
      else
        esystem 'gunzip '+i
      end
    # ===================================================================== #
    # === .bz2
    # ===================================================================== #
    when /\.?bz2$/,
         /\.?tbz2$/
      if i.include? '.tar' # Treat it as a .tar file.
        esystem 'tar -vjxf '+i
      else
        esystem 'bunzip2 '+i
      end
    # ===================================================================== #
    # === rar
    # ===================================================================== #
    when '.rar'
      check_whether_rar_is_available
      esystem 'unrar e '+i
    # ===================================================================== #
    # === .zst
    #
    # This entry point is for e. g. "pymol-2.3.0-3-x86_64.pkg.tar.zst".
    # ===================================================================== #
    when '.zst','.tar.zst'
      esystem COMMAND_TO_EXTRACT_ZST_ARCHIVES+' '+i
    # ===================================================================== #
    # === deb
    #
    # We could use dpkg-deb too, such as via "dpkg-deb". But using "ar"
    # seems to be the better choice.
    # ===================================================================== #
    when /\.?deb$/i
      esystem COMMAND_TO_EXTRACT_DEB_FILES+' '+i
    # ===================================================================== #
    # === gem
    #
    # The gem commands needs a specific --target=DIRECTORY option.
    # ===================================================================== #
    when /\.?gem$/i
      esystem GEM_UNPACK_COMMAND+' '+i+" --target=#{extract_to}"
    # ===================================================================== #
    # === lzma
    # ===================================================================== #
    when '.lzma'
      esystem "#{COMMAND_TO_EXTRACT_LZMA_FILES} #{i}"
    # ===================================================================== #
    # === bin
    #
    # This entry point allows the user to handle .bin files. In this
    # context, "handling" means to simply run that file as-is.
    # ===================================================================== #
    when /\.?bin$/i 
      esystem("./#{i}")
    # ===================================================================== #
    # === iso
    # ===================================================================== #
    when /\.?iso$/i
      try_to_extract_this_iso_file(i)
      return
    # ===================================================================== #
    # === img
    #
    # Note that .img in this context refers to squafhs .img files.
    # ===================================================================== #
    when /\.?img$/i,
         /\.?squashfs$/i
      try_to_extract_this_img_file(i)
      return # Must return early in this case.
    # ===================================================================== #
    # === lz
    #
    # This entry point requires lzip to be installed.
    # ===================================================================== #
    when /\.?lz$/i,
         /\.?tar\.?lz$/i
      esystem("#{COMMAND_TO_EXTRACT_LZ_FILES} #{i}#{padded_extract_to?}")
    # ===================================================================== #
    # === mp4
    #
    # If it is a .mp4 file, we delegate to MultimediaParadise.extract_audio.
    #
    # Usage example:
    #
    #   rubyextract foobar.mp4
    #
    # ===================================================================== #
    when /\.?mp4$/i
      begin
        require 'multimedia_paradise/audio/extract_audio/extract_audio.rb'
      rescue LoadError; end
      if Object.const_defined? :MultimediaParadise
        MultimediaParadise.extract_audio(i)
      end
    # ===================================================================== #
    # === ps
    # ===================================================================== #
    when/\.?ps$/i
      esystem 'ps2ascii '+i+padded_extract_to?
    # ===================================================================== #
    # === .jar
    # ===================================================================== #
    when /\.?jar$/i
      esystem COMMAND_TO_EXTRACT_JAR_ARCHIVES+' '+i
    # ===================================================================== #
    # === rpm
    # ===================================================================== #
    when '.rpm'
      esystem "#{COMMAND_TO_EXTRACT_BSDTAR_ARCHIVES} #{i}"
    # ===================================================================== #
    # === sxz
    # ===================================================================== #
    when '.sxz'
      esystem "unsquashfs #{i}"
    # ===================================================================== #
    # === pdf
    #
    # For pdf-files we will tap into the pdf_paradise project, if it
    # is available.
    # ===================================================================== #
    when /\.?pdf$/
      begin
        require 'pdf_paradise/utility_scripts/convert_pdf_to_text.rb'
        _ = PdfParadise::ConvertPdfToText.new(i)
        e _.output_file?
      rescue LoadError => error
        e 'No, not there.'
        pp error # Show the error to the user here.
      end
      return # Must return early in this case.
    else # else tag. We did not find the extension type.
      notify_the_user_that_this_extension_has_not_been_registered_yet(i)
    end
  else
    if File.exist? i
      notify_the_user_that_this_extension_has_not_been_registered_yet(i)
    else
      opnn; e "No file exists at `#{sfile(i)}`."
    end
  end
end
extract_to( i = :temp_dir )
Alias for: set_extract_to
extract_to=( i = :temp_dir )
Alias for: set_extract_to
extract_to?() click to toggle source
#

extract_to?

Note that this method is guaranteed to return a String.

#
# File lib/extracter/class/extracter.rb, line 307
def extract_to?
  @internal_hash[:extract_to].to_s
end
extract_to_this_location?()
Alias for: extract_to?
extracted_path?()
Alias for: extract_to?
extracted_to?()
Alias for: extract_to?
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/class/extracter.rb, line 860
def fail_message_not_registered(i)
  copn; e "Can not extract `#{sfancy(i)}` - it is not registered."
end
first?()
Alias for: first_argument?
first_argument?() click to toggle source
#

first_argument?

#
# File lib/extracter/class/extracter.rb, line 282
def first_argument?
  @commandline_arguments.first
end
Also aliased as: first?
main_hash?()
Alias for: use_this_opn_hash?
menu( i = commandline_arguments? ) click to toggle source
#

menu (menu tag)

#
mkdir(i)
Alias for: create_directory
namespace?() click to toggle source
#

namespace?

#
# File lib/extracter/class/extracter.rb, line 393
def namespace?
  @internal_hash[:namespace]
end
notify_the_user_that_this_extension_has_not_been_registered_yet(i) click to toggle source
#

notify_the_user_that_this_extension_has_not_been_registered_yet

#
# File lib/extracter/class/extracter.rb, line 849
def notify_the_user_that_this_extension_has_not_been_registered_yet(i)
  opnn; e "The archive at `#{i}` is #{tomato('not')}"
  opnn; e "registered as a permissive extension."
  fail_message_not_registered(i)
end
opn( use_this_hash = use_this_opn_hash? )
Alias for: opnn
opnn( use_this_hash = use_this_opn_hash? ) click to toggle source
#

opnn

This variant will also check whether we should show the name or not.

#
# File lib/extracter/class/extracter.rb, line 319
def opnn(
    use_this_hash = use_this_opn_hash?
  )
  if use_opn? and Object.const_defined?(:Opn) and show_the_name?
    Opn.opn(use_this_hash)
  end
end
Also aliased as: opn, copn
pad( i, with_this_character = "'" ) click to toggle source
#

pad (pad tag)

This method must be able to deal with ‘ ’ as well as with ‘()’.

The second character is the character that will be used for the padding.

#
# File lib/extracter/class/extracter.rb, line 376
def pad(
    i, with_this_character = "'"
  )
  if i.include?('(') or i.include?(')')
    i.tr!('(','\(')
    i.tr!(')','\)') if i.include? ')'
    i = pad(i, '"')
  else
    return with_this_character+
           i+
           with_this_character
  end
end
pad_opn_with_n_tokens(n_tokens = nil) click to toggle source
#

pad_opn_with_n_tokens

#
# File lib/extracter/class/extracter.rb, line 472
def pad_opn_with_n_tokens(n_tokens = nil)
  if n_tokens
    determine_default_opn_hash # Update this, just in case.
    main_hash?.update(padding: n_tokens)
  end
end
Also aliased as: set_pad_opn_with_n_tokens
padded_extract_to?() click to toggle source
#

padded_extract_to?

#
# File lib/extracter/class/extracter.rb, line 289
def padded_extract_to?
  " -C #{extract_to?} "
end
prefix_namespace_with(i) click to toggle source
#

prefix_namespace_with

#
# File lib/extracter/class/extracter.rb, line 464
def prefix_namespace_with(i)
  @internal_hash[:namespace] = "#{i}#{@internal_hash[:namespace].dup}"
  update_the_main_hash # Also update the opn-hash here.
end
prepare_the_hash_for_opn( use_this_hash = { namespace: namespace?, use_colours: use_colours? } ) click to toggle source
#

prepare_the_hash_for_opn

#
# File lib/extracter/class/extracter.rb, line 482
def prepare_the_hash_for_opn(
    use_this_hash = {
      namespace:   namespace?,
      use_colours: use_colours?
    }
  )
  # ======================================================================= #
  # === :use_this_opn_hash
  # ======================================================================= #
  @internal_hash[:use_this_opn_hash] = use_this_hash
  return @internal_hash[:use_this_opn_hash]
end
rds(i) click to toggle source
#

rds

#
# File lib/extracter/class/extracter.rb, line 598
def rds(i)
  return i.squeeze('/') if i.respond_to? :squeeze
  return i
end
register_sigint() click to toggle source
#

register_sigint

#
# File lib/extracter/class/extracter.rb, line 433
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_ext(i)
remove_extension(i)
remove_file_extension(i) click to toggle source
#

remove_file_extension

#
# File lib/extracter/class/extracter.rb, line 589
def remove_file_extension(i)
  _ = File.basename(i)
  return ::Extracter.remove_archive_type(_)
end
Also aliased as: remove_extension, remove_ext
report_to_the_user( i, extract_to = extract_to? ) 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/class/extracter.rb, line 960
def report_to_the_user(
    i, extract_to = extract_to?
  )
  if be_verbose?
    opnn; e 'Finished extracting to `'+sdir(
      extract_to+
      remove_file_extension(
        File.basename(i)
      )+ # This is an Array.
      '/'
    )+'`.'
  end
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method Extracter::Base#reset
# File lib/extracter/class/extracter.rb, line 123
def reset
  super()
  # ======================================================================= #
  # === :try_to_use_colours
  # ======================================================================= #
  @internal_hash[:try_to_use_colours] = true
  # ======================================================================= #
  # === :colour_to_use_for_directories
  # ======================================================================= #
  @internal_hash[:colour_to_use_for_directories] = 'cyan'
  # ======================================================================= #
  # === :use_opn
  # ======================================================================= #
  @internal_hash[:use_opn] = true # ← Whether to use make use of Opn by default or not.
  # ======================================================================= #
  # === :show_the_full_name_of_the_archive
  # ======================================================================= #
  @internal_hash[:show_the_full_name_of_the_archive] = false
  # ======================================================================= #
  # === :debug
  # ======================================================================= #
  @internal_hash[:debug] = false
  # ======================================================================= #
  # === :append_this_to_the_commandline
  #
  # This variable can always be used to append onto the commandline.
  # That way we can pass additional options to "tar", for instance.
  # ======================================================================= #
  @internal_hash[:append_this_to_the_commandline] = ''.dup
  # ======================================================================= #
  # === :namespace
  #
  # Specify the main namespace to be used. This setting can be modified
  # at "runtime".
  # ======================================================================= #
  @internal_hash[:namespace] = NAMESPACE
  # ======================================================================= #
  # === :be_verbose
  # ======================================================================= #
  @internal_hash[:be_verbose] = true
  # ======================================================================= #
  # === :show_the_name
  #
  # If this variable is true then the name of the class will be shown
  # on the commandline, via opn().
  # ======================================================================= #
  @internal_hash[:show_the_name] = false
  # ======================================================================= #
  # === :show_only_the_short_name_of_the_archive
  # ======================================================================= #
  @internal_hash[:show_only_the_short_name_of_the_archive] =
    SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE
  # ======================================================================= #
  # === :run_simulation
  # ======================================================================= #
  @internal_hash[:run_simulation] = false # ← Whether to run in simulation, or for "real".
  # ======================================================================= #
  # === :extract_to
  # ======================================================================= #
  @internal_hash[:extract_to] = return_pwd
  do_show_name # We will show the name usually.
  prepare_the_hash_for_opn
  # ======================================================================= #
  # === :use_colours
  # ======================================================================= #
  enable_colours
end
return_pwd() click to toggle source
#

return_pwd

#
# File lib/extracter/class/extracter.rb, line 246
def return_pwd
  "#{Dir.pwd}/".squeeze('/')
end
run() click to toggle source
#

run (run tag, def tag)

#
# File lib/extracter/class/extracter.rb, line 977
def run
  menu
  work_on_the_given_input
end
run_already?() click to toggle source
#

run_already?

#
# File lib/extracter/class/extracter.rb, line 232
def run_already?
  @internal_hash[:run_already]
end
run_simulation=(i) click to toggle source
#

run_simulation=

#
# File lib/extracter/class/extracter.rb, line 515
def run_simulation=(i)
  @internal_hash[:run_simulation] = i
end
run_simulation?() click to toggle source
#

run_simulation?

#
# File lib/extracter/class/extracter.rb, line 522
def run_simulation?
  @internal_hash[:run_simulation]
end
run_this_system_command( i, try_to_use_colours = try_to_use_colours? )
Alias for: esystem
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/class/extracter.rb, line 417
def set_be_verbose(i = false)
  @internal_hash[: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/class/extracter.rb, line 298
def set_colour_for_directories(i)
  @internal_hash[:colour_to_use_for_directories] = ::Colours.beautify(i)
end
set_commandline_arguments(i = '') click to toggle source
#

set_commandline_arguments

#
# File lib/extracter/class/extracter.rb, line 267
def set_commandline_arguments(i = '')
  i = [i].flatten.compact
  @commandline_arguments = i
end
set_extract_to( i = :temp_dir ) click to toggle source
#

set_extract_to

This is the method that should be used to determine into which directory this class will extract archives into.

Note that this target can be modified from the commandline, if the user wants to do so.

#
# File lib/extracter/class/extracter.rb, line 612
def set_extract_to(
    i = :temp_dir
  )
  if i.is_a?(Hash) and i.empty?
    i = :temp_dir
  end
  if i.is_a? Hash
    # ===================================================================== #
    # === :run_already
    # ===================================================================== #
    if i.has_key? :run_already
      @internal_hash[:run_already] = i.delete(:run_already)
    end
    # ===================================================================== #
    # === :prepend_this_namespace
    # ===================================================================== #
    if i.has_key? :prepend_this_namespace
      prefix_with_this = i.delete(:prepend_this_namespace) # Get rid of it from the Hash as well.
      prefix_namespace_with(prefix_with_this)
    end
    # ===================================================================== #
    # === :use_colours
    # ===================================================================== #
    if i.has_key? :use_colours
      set_use_colours(i.delete(:use_colours))
    end
    # ===================================================================== #
    # === :verbosity
    #
    # Handle how verbose the class shall be.
    # ===================================================================== #
    if i.has_key? :verbosity
      set_be_verbose(i.delete(:verbosity))
    # ===================================================================== #
    # === :be_verbose
    # ===================================================================== #
    elsif i.has_key? :be_verbose
      set_be_verbose(i.delete(:be_verbose))
    end
    # ===================================================================== #
    # === :append_this_to_the_commandline
    # ===================================================================== #
    if i.has_key? :append_this_to_the_commandline
      @internal_hash[:append_this_to_the_commandline] = 
        i.delete(:append_this_to_the_commandline)
    end
    # ===================================================================== #
    # === :use_opn
    # ===================================================================== #
    if i.has_key? :use_opn
      set_use_opn(i.delete(:use_opn))
    end
    # ===================================================================== #
    # === :pad_opn_with_n_tokens
    # ===================================================================== #
    if i.has_key? :pad_opn_with_n_tokens
      set_pad_opn_with_n_tokens(i.delete(:pad_opn_with_n_tokens))
    end
    # ===================================================================== #
    # === :run_simulation
    # ===================================================================== #
    if i.has_key? :run_simulation
      set_run_simulation(i.delete(:run_simulation))
    end
    # ===================================================================== #
    # === :use_colours
    # ===================================================================== #
    if i.has_key? :use_colours
      set_use_colours(i.delete(use_colours))
    end
    # ===================================================================== #
    # === :extract_to
    #
    # This entry point allows the user to specify another extract-to
    # directory. Note that :to is treated the same way as :extract_to.
    #
    # This entry point must come last. The idea is that it will then
    # become the new value for i.
    # ===================================================================== #
    if i.has_key? :extract_to
      i = i.delete(:extract_to)
    # ===================================================================== #
    # === :to
    # ===================================================================== #
    elsif i.has_key? :to
      i = i.delete(:to)
    end
  end
  case i # case tag
  # ======================================================================= #
  # === :default
  # ======================================================================= #
  when :default
    i = return_pwd
  # ======================================================================= #
  # === TEMP
  # ======================================================================= #
  when 'TEMP',
       'MY_TEMP',
       'MYTEMP',
       :temp_dir,
       nil
    i = TEMP_DIR
  end
  i = rds(i)
  i.gsub!(/--to=/,'') if i.include? '--to='
  i = i.to_s.dup # We expect a String most definitely.
  @internal_hash[:extract_to] = i
end
set_extract_to_this_location( i = :temp_dir )
Alias for: set_extract_to
set_pad_opn_with_n_tokens(n_tokens = nil)
set_run_simulation(i = false) click to toggle source
#

set_run_simulation

#
# File lib/extracter/class/extracter.rb, line 508
def set_run_simulation(i = false) # false is the default here.
  @internal_hash[:run_simulation] = i
end
set_use_colours(i) click to toggle source
#

set_use_colours

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

set_use_opn

#
# File lib/extracter/class/extracter.rb, line 253
def set_use_opn(i = true)
  @internal_hash[:use_opn] = i
end
set_verbosity(i = false)
Alias for: set_be_verbose
show_help() click to toggle source
#

show_help (help tag)

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

To call this method via the commandline try:

extract --help
#
# File lib/extracter/class/extracter.rb, line 346
def show_help
  e
  opnn; e 'How to extract archives, without helper scripts?'
  e
  e '  tar -zxvf foobar.tar.gz                                        # for .tar.gz'
  e '  tar xvzf foobar.tgz                                            # for .tgz'
  e '  tar xvfJ foobar.tar.xz                                         # for .tar.xz'
  e '  tar jxf foobar.tar.bz2                                         # for .tar.bz2'
  e '  tar -xf foobar.tar.bz2                                         # for .tbz'
  e '  tar --lzip -xvf zutils-1.5.tar.lz                              # for .tar.lz'
  e '  unsquashfs foobar-1.2.3.sxz                                    # for .sxz'
  e '  7z x -so C:\home\x\src\htop\htop-3.0.5.tar.xz | 6z x -si -ttar # on windows'
  e
  opnn; e 'Furthermore, there are some commandline options '\
          'that can'
  opnn; e 'be used for this class (class Extracter).'
  e
  e '  --to=/home/Temp # extract into the '\
    'directory /home/Temp/'
  e
end
show_only_the_short_name_of_the_archive?() click to toggle source
#

show_only_the_short_name_of_the_archive?

#
# File lib/extracter/class/extracter.rb, line 501
def show_only_the_short_name_of_the_archive?
  @internal_hash[:show_only_the_short_name_of_the_archive]
end
show_the_name?() click to toggle source
#

show_the_name?

#
# File lib/extracter/class/extracter.rb, line 203
def show_the_name?
  @internal_hash[:show_the_name]
end
source_package_location()
Alias for: extract_to?
start( i, extract_to = extract_to? )
strip_components( by_n = 1 ) click to toggle source
#

strip_components

The first argument to this method will determine how far “down” tar will strip the components.

#
# File lib/extracter/class/extracter.rb, line 532
def strip_components(
    by_n = 1
  )
  @internal_hash[:append_this_to_the_commandline] <<
    " --strip-components=#{by_n}"
end
try_to_extract_this_img_file(i) click to toggle source
#

try_to_extract_this_img_file

#
# File lib/extracter/class/extracter.rb, line 867
def try_to_extract_this_img_file(i)
  opnn; e 'Handling a squashfs .img file format next:'
  name_without_extension = i.delete_suffix(File.extname(i))
  mkdir(name_without_extension) unless File.directory? name_without_extension
  esystem "mount -o loop -t squashfs #{i} #{name_without_extension}"
  e 'The content of the extracted (or rather, mounted) archive is:'
  pp Dir["#{name_without_extension}*"]
end
try_to_extract_this_iso_file(i) click to toggle source
#

try_to_extract_this_iso_file

#
# File lib/extracter/class/extracter.rb, line 727
def try_to_extract_this_iso_file(i)
  opnn; e 'Extracting an .iso file is a bit more complicated '\
          'than extracting a .tar.gz tarball'
  opnn; e 'archive. This class will first create a helper '\
          'directory; then mount the .iso there,'
  opnn; e 'then copy the content to the main directory.'
  helper_directory = File.dirname(i)+
                     '/READ_ONLY_DIRECTORY_'+
                     File.basename(
                       i.delete_suffix(File.extname(i))
                     )+
                     '/'
  mkdir(helper_directory) unless File.directory? helper_directory
  esystem 'mount -o loop '+i+' '+helper_directory
  e 'The helper directory in use is '\
    '`'+sdir(File.absolute_path(helper_directory))+'`.'
  main_directory = File.dirname(i)+
                   '/'+
                   File.basename(
                     i.delete_suffix(File.extname(i))
                   )+
                   '/'
  e 'Next creating the main directory at `'+sdir(main_directory)+'`.'
  mkdir(main_directory) unless File.directory? main_directory
  e 'Next copying the content of the helper directory recursively '
  e 'from `'+sdir(helper_directory)+'`'
  e 'onto `'+sdir(
    main_directory+File.basename(helper_directory)+'/'
  )+'`.'
  cpr(
    helper_directory,
    main_directory+File.basename(helper_directory)+'/'
  )
  a = main_directory+File.basename(helper_directory)+'/'
  e 'Relocating the files next from:'
  e
  e "  #{sdir(a)}"
  e
  Dir[a+'*'].each {|entry|
    mv(
      entry,
      main_directory
    )
  }
  # ======================================================================= #
  # And remove the directory:
  # ======================================================================= #
  remove_this_directory(a)
  e 'The content of the extracted (or rather, mounted) archive is:'
  e
  pp Dir["#{main_directory}*"]
  e
end
try_to_use_colours?() click to toggle source
#

try_to_use_colours?

#
# File lib/extracter/class/extracter.rb, line 582
def try_to_use_colours?
  @internal_hash[:try_to_use_colours]
end
update_the_main_hash( use_this_hash = { namespace: namespace?, use_colours: use_colours? } )
update_the_opn_hash( use_this_hash = { namespace: namespace?, use_colours: use_colours? } )
use_opn?() click to toggle source
#

use_opn?

#
# File lib/extracter/class/extracter.rb, line 260
def use_opn?
  @internal_hash[:use_opn]
end
use_this_opn_hash?() click to toggle source
#

use_this_opn_hash?

#
# File lib/extracter/class/extracter.rb, line 331
def use_this_opn_hash?
  @internal_hash[:use_this_opn_hash]
end
Also aliased as: main_hash?
work_on_the_given_input( array = commandline_arguments?, extract_to = extract_to? ) click to toggle source
#

work_on_the_given_input

#
# File lib/extracter/class/extracter.rb, line 784
def work_on_the_given_input(
    array      = commandline_arguments?,
    extract_to = extract_to?
  )
  # ======================================================================= #
  # If the user supplied a directory then a random entry will be grabbed
  # from said directory.
  # ======================================================================= #
  if array.is_a?(String) and File.directory?(array)
    array = Dir[rds("#{array}/")+'*'].sample
  end
  case extract_to
  when nil
    extract_to = extract_to?
  end
  if array.empty?
    opnn; e 'No archive (input) was provided. Please provide the file'
    opnn; e 'that is to be extracted.'
  else
    array.each {|this_file|
      # =================================================================== #
      # Create the directory if it does not yet exist.
      # =================================================================== #
      create_directory(extract_to) unless File.directory?(extract_to)
      # =================================================================== #
      # Handle the case when the user did input a number.
      # =================================================================== #
      begin
        if this_file =~ /^\d$/
          this_file = Dir['*'][( this_file.to_i - 1 )] unless File.exist?(this_file)
        end
      rescue ArgumentError => error
        e 'Error for '+sfancy(this_file)+':'
        pp error
      end
      # =================================================================== #
      # If the user supplied a directory then a random entry will be
      # grabbed from said directory.
      #
      # Usage example:
      #
      #  rubyextracter /home/x/src/htop/
      #
      # =================================================================== #
      if File.directory? this_file
        this_file = Dir[rds("#{this_file}/")+'*'].sample
      end
      extract_this_archive(this_file, extract_to)
      report_to_the_user(this_file, extract_to)
    }
  end
end