Class: Extracter::Extracter

Inherits:
Base
  • Object
show all
Defined in:
lib/extracter/class/extracter.rb

Overview

Extracter::Extracter

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
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.

#
true

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#are_we_on_windows?, #array_archive_types?, #be_verbose, #be_verbose?, #change_directory, #check_whether_the_colours_gem_is_available, #colour_to_use_for_directories?, #commandline_arguments?, #cpr, #create_directory, #create_the_internal_hash, #cyan?, #debug?, #disable_colours, #do_be_quiet, #do_not_make_use_of_colours, #do_try_to_use_colours, #e, #enable_colours, #enable_debug, #ewarn, #first_argument?, #is_archive?, #move, #namespace?, #opne, #opnn, #orev, #rds, #register_sigint, #remove_file_extension, #remove_this_directory, #return_pwd, #rev, #sdir, #set_be_verbose, #set_commandline_arguments, #set_use_colours, #set_use_opn, #sfancy, #sfile, #simp, #steelblue, #tomato, #try_to_require_fileutils, #try_to_use_colours?, #use_opn?, #use_this_opn_hash?

Constructor Details

#initialize(commandline_arguments = ARGV, extract_to = nil, run_already = true, &block) ⇒ Extracter

#

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')
#


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/extracter/class/extracter.rb', line 74

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.
    opne "The first argument what is:       `#{commandline_arguments}`"
    opne "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
    # ===================================================================== #
    # === :be_silent
    # ===================================================================== #
    when :be_silent,
         :be_quiet
      do_be_quiet
    # ===================================================================== #
    # === :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

Class Method Details

.[](i = ARGV, extract_where_to = Extracter.return_pwd) ⇒ Object

#

Extracter::Extracter[]

#


1175
1176
1177
1178
1179
1180
# File 'lib/extracter/class/extracter.rb', line 1175

def self.[](
    i = ARGV,
    extract_where_to = Extracter.return_pwd
  )
  new(i, extract_where_to)
end

.extract_this(i = ARGV, to = {}, &block) ⇒ Object

#

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.

#


1162
1163
1164
1165
1166
1167
1168
# File 'lib/extracter/class/extracter.rb', line 1162

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

Instance Method Details

#check_whether_rar_is_availableObject

#

check_whether_rar_is_available

We try to find out whether unrar is available.

#


801
802
803
804
805
806
807
808
809
# File 'lib/extracter/class/extracter.rb', line 801

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
    opne 'unrar appears to be unavailable. Please install it first.'
  end
end

#consider_verbosity_for(i) ⇒ Object

#

consider_verbosity_for

#


437
438
439
440
441
442
443
444
445
446
447
# File 'lib/extracter/class/extracter.rb', line 437

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

#do_not_show_nameObject

#

do_not_show_name

Tells us whether to use opn() or not.

#


792
793
794
# File 'lib/extracter/class/extracter.rb', line 792

def do_not_show_name
  @internal_hash[:show_the_name] = false
end

#do_show_nameObject

#

do_show_name

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

#


489
490
491
# File 'lib/extracter/class/extracter.rb', line 489

def do_show_name
  @internal_hash[:show_the_name] = true
end

#do_show_the_full_name_of_the_archiveObject

#

do_show_the_full_name_of_the_archive

#


690
691
692
# File 'lib/extracter/class/extracter.rb', line 690

def do_show_the_full_name_of_the_archive
  @internal_hash[:show_the_full_name_of_the_archive] = true
end

#esystem(i, try_to_use_colours = try_to_use_colours? ) ⇒ Object Also known as: run_this_system_command

#

esystem (system tag, esystem tag)

#


540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/extracter/class/extracter.rb', line 540

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?
    opne 'As we are running in simulation mode, the following command '
    opne 'is the one that we would have been used if we were to not run '
    opne 'in simulation mode:'
    if be_verbose?
      if try_to_use_colours
        e steelblue(i)
      else
        e i
      end
    end
  else
    if be_verbose?
      if try_to_use_colours
        e 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

#extract_this_archive(i, extract_to = extract_to? ) ⇒ Object Also known as: do_extract_to, do_extract_what_to, start

#

extract_this_archive (extract tag)

#


883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'lib/extracter/class/extracter.rb', line 883

def extract_this_archive(
    i,
    extract_to = extract_to?
  )
  i = File.absolute_path(i)
  # ======================================================================= #
  # First determine whether we can extract the archive or whether we can
  # not:
  # ======================================================================= #
  unless ::Extracter.is_this_a_valid_archive?(i) or i.end_with?('.pdf')
    opne 'The given input, '+i+', is not a valid archive format.'
    return
  end
  # ======================================================================= #
  # Next handle the situation when we are on Windows:
  # ======================================================================= #
  if ::Extracter.are_we_on_windows?
    # ===================================================================== #
    # On windows we will overrule this completely, for now.
    # ===================================================================== #
    Extract.this_on_windows(i)
    return
  end
  if be_verbose?
    if show_only_the_short_name_of_the_archive?
      name_of_the_archive = File.basename(i)
      opne "#{rev}Extracting `#{sfancy(name_of_the_archive)}#{rev}` "\
           "to `#{sdir(extract_to)}#{rev}`."
    else
      opne "#{rev}Extracting `#{sfancy(i)}` to "\
           "`#{sdir(extract_to)}#{rev}`."               
    end
  end
  unless File.writable? extract_to
    copn; ewarn 'You do not have sufficient permissions to'
    copn; ewarn "modify #{sdir(extract_to)}#{swarn('.')}"
    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.
  # ===================================================================== #
  # === 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.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?
  # ===================================================================== #
  # === .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
  #
  # 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?
  # ===================================================================== #
  # === .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.
    if File.exist? i
      notify_the_user_that_this_extension_has_not_been_registered_yet(i)
    else
      opne "#{rev}No file exists at `#{sfile(i)}#{rev}`."
    end
  end
end

#extract_to?Boolean Also known as: extract_to_this_location?, extracted_to?, extracted_path?, source_package_location

#

extract_to?

Note that this method is guaranteed to return a String.

#

Returns:

  • (Boolean)


830
831
832
# File 'lib/extracter/class/extracter.rb', line 830

def extract_to?
  @internal_hash[:extract_to].to_s
end

#fail_message_not_registered(i) ⇒ Object

#

fail_message_not_registered

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

#


507
508
509
510
# File 'lib/extracter/class/extracter.rb', line 507

def fail_message_not_registered(i)
  opne "#{rev}Can not extract `#{sfancy(i)}#{rev}` - it is "\
       "not registered."
end
#

menu (menu tag)

#


515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/extracter/class/extracter.rb', line 515

def menu(
    i = commandline_arguments?
  )
  if debug?
    opne 'The input given to this method was: `'+sfile(i)+'`'
  end
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i
    # ===================================================================== #
    # === extracter --help
    #
    # Entry point for showing help.
    # ===================================================================== #
    when /^-?-?help$/i
      show_help
      exit
    end
  end
end

#notify_the_user_that_no_input_was_given_but_this_file_was_found(this_file) ⇒ Object

#

notify_the_user_that_no_input_was_given_but_this_file_was_found

#


191
192
193
194
195
196
197
198
199
# File 'lib/extracter/class/extracter.rb', line 191

def notify_the_user_that_no_input_was_given_but_this_file_was_found(
    this_file
  )
  opne "No input was given to #{sfancy(NAMESPACE)} but a "\
       ".zip file was"
  opne 'found in this directory ('+sdir(return_pwd)+rev+
       '): '+sfancy(this_file)
  opne 'We will use this zip file.'
end

#notify_the_user_that_this_extension_has_not_been_registered_yet(i) ⇒ Object

#

notify_the_user_that_this_extension_has_not_been_registered_yet

#


496
497
498
499
500
# File 'lib/extracter/class/extracter.rb', line 496

def notify_the_user_that_this_extension_has_not_been_registered_yet(i)
  opne "The archive at `#{i}` is #{tomato('not')}"
  opne "registered as a permissive extension."
  fail_message_not_registered(i)
end

#pad(i, with_this_character = "'") ⇒ Object

#

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.

#


285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/extracter/class/extracter.rb', line 285

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) ⇒ Object Also known as: set_pad_opn_with_n_tokens

#

pad_opn_with_n_tokens

#


840
841
842
843
844
845
# File 'lib/extracter/class/extracter.rb', line 840

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

#padded_extract_to?Boolean

#

padded_extract_to?

#

Returns:

  • (Boolean)


774
775
776
# File 'lib/extracter/class/extracter.rb', line 774

def padded_extract_to?
  " -C #{extract_to?} "
end

#prefix_namespace_with(i) ⇒ Object

#

prefix_namespace_with

#


729
730
731
732
# File 'lib/extracter/class/extracter.rb', line 729

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?, show_the_name: show_the_name? }) ⇒ Object Also known as: update_the_opn_hash, determine_default_opn_hash, determine_the_default_opn_hash, update_the_main_hash

#

prepare_the_hash_for_opn

#


737
738
739
740
741
742
743
744
745
746
747
748
749
# File 'lib/extracter/class/extracter.rb', line 737

def prepare_the_hash_for_opn(
    use_this_hash = {
      namespace:     namespace?,
      use_colours:   use_colours?,
      show_the_name: show_the_name?
    }
  )
  # ======================================================================= #
  # === :use_this_opn_hash
  # ======================================================================= #
  @internal_hash[:use_this_opn_hash] = use_this_hash
  return @internal_hash[:use_this_opn_hash]
end

#report_to_the_user(i, extract_to = extract_to? ) ⇒ Object

#

report_to_the_user

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

#


605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/extracter/class/extracter.rb', line 605

def report_to_the_user(
    i,
    extract_to = extract_to?
  )
  if be_verbose?
    target = extract_to+
             remove_file_extension(File.basename(i))+
             '/'
     opne "#{rev}Finished extracting to `"\
          "#{sdir(target)}#{rev}`." # This is an Array.
  end
end

#resetObject

#

reset (reset tag)

#


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/extracter/class/extracter.rb', line 132

def reset
  super()
  # ======================================================================= #
  # === :namespace
  #
  # Specify the main namespace to be used. This setting can be modified
  # at "runtime".
  # ======================================================================= #
  @internal_hash[:namespace] = NAMESPACE
  # ======================================================================= #
  # === :array_work_on_these_files
  # ======================================================================= #
  @internal_hash[:array_work_on_these_files] = []
  # ======================================================================= #
  # === :try_to_use_colours
  # ======================================================================= #
  @internal_hash[:try_to_use_colours] = true
  # ======================================================================= #
  # === :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
  # ======================================================================= #
  # === :show_the_full_name_of_the_archive
  # ======================================================================= #
  @internal_hash[:show_the_full_name_of_the_archive] = false
  # ======================================================================= #
  # === :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

#runObject

#

run (run tag, def tag)

#


1143
1144
1145
1146
1147
1148
1149
1150
1151
# File 'lib/extracter/class/extracter.rb', line 1143

def run
  menu
  _ = commandline_arguments?
  _.flatten.select {|entry|
    File.file?(entry) or File.directory?(entry)
  }
  set_work_on_these_files(_)
  work_on_the_given_input
end

#run_already?Boolean

#

run_already?

#

Returns:

  • (Boolean)


821
822
823
# File 'lib/extracter/class/extracter.rb', line 821

def run_already?
  @internal_hash[:run_already]
end

#run_simulation=(i) ⇒ Object

#

run_simulation=

#


424
425
426
# File 'lib/extracter/class/extracter.rb', line 424

def run_simulation=(i)
  @internal_hash[:run_simulation] = i
end

#run_simulation?Boolean

#

run_simulation?

#

Returns:

  • (Boolean)


431
432
433
# File 'lib/extracter/class/extracter.rb', line 431

def run_simulation?
  @internal_hash[:run_simulation]
end

#set_colour_for_directories(i) ⇒ Object

#

set_colour_for_directories

Set the colour for directories to use.

#


783
784
785
# File 'lib/extracter/class/extracter.rb', line 783

def set_colour_for_directories(i)
  @internal_hash[:colour_to_use_for_directories] = ::Colours.beautify(i)
end

#set_extract_to(i = :temp_dir) ⇒ Object Also known as: set_extract_to_this_location, extract_to=, extract_to

#

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.

#


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/extracter/class/extracter.rb', line 309

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_run_simulation(i = false) ⇒ Object

#

set_run_simulation

This should always default to false, if no other argument has been supplied.

#


767
768
769
# File 'lib/extracter/class/extracter.rb', line 767

def set_run_simulation(i = false)
  @internal_hash[:run_simulation] = i
end

#set_work_on_these_files(i = []) ⇒ Object Also known as: set_input

#

set_work_on_these_files

#


850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
# File 'lib/extracter/class/extracter.rb', line 850

def set_work_on_these_files(
    i = []
  )
  if i.is_a?(Array) and i.empty?
    # ===================================================================== #
    # === Check for .zip files
    #
    # In this case, try to see if the current directory has a .zip
    # file. We will use this in that case.
    # ===================================================================== #
    is_there_a_zip_file = Dir['*.zip']
    unless is_there_a_zip_file.empty?
      use_this_zip_file = is_there_a_zip_file.first
      notify_the_user_that_no_input_was_given_but_this_file_was_found(use_this_zip_file)
      i << use_this_zip_file
    end
    is_there_at_the_least_one_tar_xz_file = Dir['*.tar.xz']
    unless is_there_at_the_least_one_tar_xz_file.empty?
      i << is_there_at_the_least_one_tar_xz_file.first
    end
  end
  i = [i].compact.flatten.map {|entry|
    unless File.exist? entry
      entry = try_to_glob_on(entry)
    end
    entry
  }
  @internal_hash[:array_work_on_these_files] = i
end

#show_helpObject

#

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
#


705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/extracter/class/extracter.rb', line 705

def show_help
  e
  opne '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 | 7z x -si -ttar  # → on windows'
  e
  opne 'Furthermore, there are some commandline options that can'
  opne '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?Boolean

#

show_only_the_short_name_of_the_archive?

#

Returns:

  • (Boolean)


757
758
759
# File 'lib/extracter/class/extracter.rb', line 757

def show_only_the_short_name_of_the_archive?
  @internal_hash[:show_only_the_short_name_of_the_archive]
end

#show_the_name?Boolean

#

show_the_name?

#

Returns:

  • (Boolean)


814
815
816
# File 'lib/extracter/class/extracter.rb', line 814

def show_the_name?
  @internal_hash[:show_the_name]
end

#strip_components(by_n = 1) ⇒ Object

#

strip_components

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

#


476
477
478
479
480
481
# File 'lib/extracter/class/extracter.rb', line 476

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

#

try_to_extract_this_img_file

#


678
679
680
681
682
683
684
685
# File 'lib/extracter/class/extracter.rb', line 678

def try_to_extract_this_img_file(i)
  opne '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}"
  opne 'The content of the extracted (or rather, mounted) archive is:'
  pp Dir["#{name_without_extension}*"]
end

#try_to_extract_this_iso_file(i) ⇒ Object

#

try_to_extract_this_iso_file

#


621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/extracter/class/extracter.rb', line 621

def try_to_extract_this_iso_file(i)
  opne 'Extracting an .iso file is a bit more complicated '\
       'than extracting a .tar.gz tarball'
  opne 'archive. This class will first create a helper '\
       'directory; then mount the .iso there,'
  opne '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
  opne '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))
                   )+
                   '/'
  opne 'Next creating the main directory at `'+sdir(main_directory)+'`.'
  mkdir(main_directory) unless File.directory? main_directory
  opne 'Next copying the content of the helper directory recursively '
  opne 'from `'+sdir(helper_directory)+'`'
  opne 'onto `'+sdir(
    main_directory+File.basename(helper_directory)+'/'
  )+'`.'
  cpr(
    helper_directory,
    main_directory+File.basename(helper_directory)+'/'
  )
  a = main_directory+File.basename(helper_directory)+'/'
  opne '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)
  opne 'The content of the extracted (or rather, mounted) archive is:'
  e
  pp Dir["#{main_directory}*"]
  e
end

#try_to_glob_on(i) ⇒ Object

#

try_to_glob_on

#


452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/extracter/class/extracter.rb', line 452

def try_to_glob_on(i)
  result = Dir["#{i}*"]
  # ======================================================================= #
  # Next, sort this result to put archives on the beginning of the Array.
  # ======================================================================= #
  result = result.partition {|entry| is_archive?(entry) }
  result.flatten!
  unless result.empty?
    # ===================================================================== #
    # Ok, we grab the first entry next.
    # ===================================================================== #
    i = result.first
    opne "#{rev}No result could be found for the given input, "\
         "thus using #{sfancy(i)} #{rev}instead."
  end
  return i
end

#work_on_the_given_input(array = , extract_to = extract_to? ) ⇒ Object Also known as: extract_input

#

work_on_the_given_input

#


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/extracter/class/extracter.rb', line 204

def work_on_the_given_input(
    array      = @internal_hash[:array_work_on_these_files],
    extract_to = extract_to?
  )
  pp array if debug?
  case extract_to
  # ======================================================================= #
  # === :default
  # ======================================================================= #
  when :default,
       nil
    extract_to = extract_to?
  end
  # ======================================================================= #
  # 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
  if array.empty?
    opne 'No archive (input) was provided. Please provide the file'
    opne 'that is to be extracted.'
  else
    array.each {|this_file|
      # =================================================================== #
      # The next if-clause is for my home setup, for pseudo Symbols:
      # =================================================================== #
      if this_file.is_a?(String) and this_file.start_with?(':')
        _ = '/home/x/src/'
        if File.directory?(_+this_file.delete(':'))
          target = _+this_file.delete(':')+'/*'
          possible_files = Dir[target].select {|entry|
            File.file?(entry)
          }
          unless possible_files.empty?
            this_file = possible_files.first
          end
        end
      end
      # =================================================================== #
      # 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
        opne 'An error occurred 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