extracter 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of extracter might be problematic. Click here for more details.

@@ -0,0 +1,305 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'extracter/do_extract_what_to.rb'
6
+ # =========================================================================== #
7
+ module Extracter
8
+
9
+ class Extracter
10
+
11
+ # ========================================================================= #
12
+ # === do_extract_what_to
13
+ #
14
+ # This method should only be called from the method
15
+ # work_on_the_given_input().
16
+ #
17
+ # It will attempt to extract the specified archive.
18
+ # ========================================================================= #
19
+ def do_extract_what_to(
20
+ what, # ← The archive or file that we wish to extract.
21
+ to = extract_to?
22
+ )
23
+ name_of_the_archive = what.dup
24
+ @did_we_extract_already = false
25
+ to = extract_to? if to.nil?
26
+ set_source_location(what)
27
+ set_extract_to_this_location(to) # Keep it in sync.
28
+ # ======================================================================= #
29
+ # Create the directory if it does not yet exist.
30
+ # ======================================================================= #
31
+ create_directory(to) if !File.directory?(to)
32
+ if be_verbose?
33
+ if @show_only_the_short_name_of_the_archive
34
+ name_of_the_archive = File.basename(name_of_the_archive)
35
+ copn; e "Extracting `#{sfancy(name_of_the_archive)}` to `#{sdir(to)}` next."
36
+ else
37
+ copn; e "Extracting `#{sfancy(name_of_the_archive)}` to `#{sdir(to)}` next."
38
+ end
39
+ end
40
+ _ = ''.dup # Default.
41
+ extname = File.extname(what)
42
+ unless ARRAY_REGISTERED_ARCHIVES.include? extname.delete('.')
43
+ opn; e 'We did not register the following extension: '+extname.delete('.')
44
+ fail_message_not_registered(extname)
45
+ @skip_extracting = true
46
+ end
47
+ case extname # Case tag. Those listed on top are more important.
48
+ # ======================================================================= #
49
+ # === .tar
50
+ # ======================================================================= #
51
+ when '.tar',
52
+ '.tar.bz2',
53
+ '.tbz'
54
+ _ << 'tar -xvf'
55
+ # ======================================================================= #
56
+ # === pdf
57
+ #
58
+ # For pdf-files we will tap into the pdf_paradise project.
59
+ # ======================================================================= #
60
+ when /\.?pdf$/
61
+ begin
62
+ require 'pdf_paradise/convert_pdf_to_text.rb'
63
+ _ = PdfParadise::ConvertPdfToText.new(what)
64
+ _.output_file?
65
+ rescue LoadError; end
66
+ return # Must return early in this case.
67
+ # ======================================================================= #
68
+ # === img
69
+ #
70
+ # Note that .img in this context refers to squafhs .img files.
71
+ # ======================================================================= #
72
+ when '.img',
73
+ '.squashfs'
74
+ opnn; e 'Handling a squashfs .img file format next:'
75
+ name_without_extension = what.sub(/#{File.extname(what)}$/,'')
76
+ mkdir(name_without_extension) unless File.directory? name_without_extension
77
+ esystem 'mount -o loop -t squashfs '+what+' '+name_without_extension
78
+ e 'The content of the extracted (or rather, mounted) archive is:'
79
+ pp Dir["#{name_without_extension}*"]
80
+ return # Must return early in this case.
81
+ # ======================================================================= #
82
+ # === iso
83
+ # ======================================================================= #
84
+ when '.iso'
85
+ opnn; e 'Extracting an .iso file is a bit more complicated '\
86
+ 'than a .tar.gz tarball release.'
87
+ opnn; e 'We will first create a directory; and then mount '\
88
+ 'the .iso there.'
89
+ name_without_extension = what.sub(/#{File.extname(what)}$/,'')
90
+ mkdir(name_without_extension) unless File.directory? name_without_extension
91
+ esystem 'mount -o loop '+what+' '+name_without_extension
92
+ e 'The content of the extracted (or rather, mounted) archive is:'
93
+ pp Dir[name_without_extension+'*']
94
+ return
95
+ # ======================================================================= #
96
+ # === sxz
97
+ # ======================================================================= #
98
+ when '.sxz'
99
+ _ = 'unsquashfs '.dup # This requires squashfs with xz-support.
100
+ # ======================================================================= #
101
+ # === lz
102
+ # ======================================================================= #
103
+ when '.lz','.tar.lz'
104
+ _ = 'tar --lzip -xvf '.dup # This requires lzip to be installed.
105
+ # ======================================================================= #
106
+ # === tar.Z
107
+ # ======================================================================= #
108
+ when '.tar.Z','.taz'
109
+ _ << 'tar -xvzf'
110
+ # ======================================================================= #
111
+ # === .jar
112
+ # ======================================================================= #
113
+ when /\.?jar$/i
114
+ _ << 'jar xvf'
115
+ # ======================================================================= #
116
+ # === .zst
117
+ #
118
+ # This entry point is for e. g. "pymol-2.3.0-3-x86_64.pkg.tar.zst".
119
+ # ======================================================================= #
120
+ when '.zst','.tar.zst'
121
+ _ << 'tar -I zstd -xvf '
122
+ # ======================================================================= #
123
+ # === txz
124
+ # ======================================================================= #
125
+ when '.txz'
126
+ _ << 'tar Jxvf' # Since Jan 2012.
127
+ # ======================================================================= #
128
+ # === gz
129
+ # ======================================================================= #
130
+ when '.gz'
131
+ if what.include? '.tar'
132
+ _ << 'tar -zxvf'
133
+ else
134
+ _ << 'gunzip'
135
+ end
136
+ # ======================================================================= #
137
+ # === xz
138
+ # ======================================================================= #
139
+ when '.xz'
140
+ if _.include? '.tar'
141
+ end
142
+ _ << 'tar -xvf' # tar -Jxv #{what} would be an alternative.
143
+ # ======================================================================= #
144
+ # === rpm
145
+ # ======================================================================= #
146
+ when '.rpm'
147
+ _ << 'bsdtar xfv'
148
+ # ======================================================================= #
149
+ # === bin
150
+ # ======================================================================= #
151
+ when '.bin' # handle .bin files here.
152
+ # _ = 'tar -jxf '+package
153
+ _ << "./#{what}"
154
+ # ======================================================================= #
155
+ # === zip
156
+ # ======================================================================= #
157
+ when '.zip','.xpi','.docx','.odt', # .docx and .odt format types can be unpacked with zip too.
158
+ '.apkg'
159
+ # _ << 'ar -jxf' # unzip #{what} <-- this should work as well.
160
+ _ << 'unzip '
161
+ when /\.bz2/,'.tbz2'
162
+ if what.include? '.tar' # Treat it as a .tar file.
163
+ _ << 'tar -vjxf '
164
+ else
165
+ _ << 'bunzip2 '
166
+ end
167
+ # ======================================================================= #
168
+ # === lzma
169
+ # ======================================================================= #
170
+ when '.lzma'
171
+ _ << 'unlzma '
172
+ # ======================================================================= #
173
+ # === 7z
174
+ # ======================================================================= #
175
+ when '.7z' # 7z does not accept the -C commandline.
176
+ # _ << '7za e' # <- Deprecated as of 05.06.2020.
177
+ _ << '7z x'
178
+ # ======================================================================= #
179
+ # === gem
180
+ # ======================================================================= #
181
+ when '.gem'
182
+ _ << GEM_UNPACK_COMMAND
183
+ # ======================================================================= #
184
+ # === rar
185
+ # ======================================================================= #
186
+ when '.rar'
187
+ check_whether_rar_is_available
188
+ _ << 'unrar e'
189
+ # ======================================================================= #
190
+ # === deb
191
+ # ======================================================================= #
192
+ when '.deb'
193
+ #_ = 'dpkg-deb -x' # {to}
194
+ _ << 'ar -x' # ar -x #{what} This could work too.
195
+ # ======================================================================= #
196
+ # === tgz
197
+ # ======================================================================= #
198
+ when '.tgz'
199
+ _ << 'tar -xvzf'
200
+ # ======================================================================= #
201
+ # === ps
202
+ # ======================================================================= #
203
+ when '.ps'
204
+ _ << 'ps2ascii'
205
+ # ======================================================================= #
206
+ # === mp4
207
+ # ======================================================================= #
208
+ when '.mp4' # If it is a .mp4 file, we delegate to ExtractAudio instead.
209
+ if Object.const_defined? :MultimediaParadise
210
+ MultimediaParadise.extract_audio(@source_package_location)
211
+ end
212
+ exit
213
+ else # else tag. We did not find the extension type.
214
+ @skip_extracting = true
215
+ copn; ewarn "We did not find: `#{sfile(what)}`. "
216
+ copn; ewarn 'The file-type (extension) was: `'+simp(extname)+'`.'
217
+ # Try to rescue though.
218
+ result = run_this_system_command("file #{what}")
219
+ copn; e result
220
+ if result.include? 'bzip2 compressed'
221
+ copn; e 'We assume it is a .bz2 file though.'
222
+ _ = 'tar -vjxf '.dup
223
+ @skip_extracting = false
224
+ end
225
+ end unless @skip_extracting
226
+ unless File.exist? what
227
+ @skip_extracting = true
228
+ copn; e "The file `#{sfile(what)}"\
229
+ "` does not exist - can not extract."
230
+ end
231
+ # ======================================================================= #
232
+ # Handle the situation when the given input includes a ')' character.
233
+ # We will pad such an input with '"' characters.
234
+ # ======================================================================= #
235
+ if what.include? ')'
236
+ what = pad(what, '"') #sanitize_input(what)
237
+ end
238
+ # ======================================================================= #
239
+ # Next, pad it if it includes a ' ' character.
240
+ # ======================================================================= #
241
+ what = pad(what) if what.include?(' ')
242
+ _ << " #{what}" unless _.empty?
243
+ if _.include? GEM_UNPACK_COMMAND # Gem command needs a --target=DIR option.
244
+ _ << " --target=#{to}"
245
+ elsif _.include?('ar -x') and ! _.include?('.tar') # Do nothing in this case.
246
+ elsif _.end_with? '.sxz'
247
+ # .sxz does not require the -C option.
248
+ elsif _.end_with? '.zip','.xpi','.7z','.jar','.apkg'
249
+ # Do not use -C option for 7z, as it hates this option.
250
+ # .jar files also do not support the -C option.
251
+ elsif _.include? 'bunzip'
252
+ # Do not use -C option for bunzip, as it hates this option.
253
+ else
254
+ # ===================================================================== #
255
+ # Next, we need to determine the location where we extract our
256
+ # archive to.
257
+ # ===================================================================== #
258
+ _ << ' '
259
+ # ===================================================================== #
260
+ # Add -C option except for .deb packages and gunzip-based archives.
261
+ # ===================================================================== #
262
+ unless _ =~ /deb$/ or _.include?('gunzip')
263
+ _ << '-C '
264
+ _ << to
265
+ end
266
+ end
267
+ if run_simulation?
268
+ copn; e 'As we are running in simulation mode, the following command '
269
+ copn; e 'is the one that we would have been used if we were to not run '
270
+ copn; e 'in simulation mode:'
271
+ e _
272
+ else # Ok, here we are not in a simulation, hence we can run the command.
273
+ unless @skip_extracting
274
+ if File.writable? to
275
+ # ================================================================= #
276
+ # Next, run the sys-command, with some padding.
277
+ # ================================================================= #
278
+ begin
279
+ run_this_system_command(
280
+ " #{_}", :also_show_what_we_will_do
281
+ )
282
+ # ================================================================= #
283
+ # We have to rescue because unrar might not be available and so on.
284
+ # ================================================================= #
285
+ rescue Exception => error
286
+ e 'An error has happened upon attempting to run this system command:'
287
+ e
288
+ e " #{_}"
289
+ e
290
+ pp error
291
+ e '-----------'
292
+ pp error.class
293
+ e '-----------'
294
+ end
295
+ @did_we_extract_already = true
296
+ else
297
+ copn; ewarn 'You do not have sufficient permissions to'
298
+ copn; ewarn "modify #{sdir(to)}."
299
+ end
300
+ end
301
+ end
302
+ end; alias do_extract_to do_extract_what_to # === do_extract_to
303
+ alias start do_extract_what_to # === start
304
+
305
+ end; end
@@ -0,0 +1,237 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Extracter::ExtractIt
6
+ #
7
+ # If no input was provided to this class, but a .zip file exists in
8
+ # the current working directory, then that .zip file will be used.
9
+ #
10
+ # Usage example:
11
+ #
12
+ # Extracter::ExtractIt.new(ARGV)
13
+ #
14
+ # =========================================================================== #
15
+ # require 'extracter/extract_it/extract_it.rb'
16
+ # =========================================================================== #
17
+ module Extracter
18
+
19
+ class ExtractIt # === Extracter::ExtractIt
20
+
21
+ begin
22
+ require 'opn'
23
+ rescue LoadError; end
24
+
25
+ begin
26
+ require 'remove_file_suffix'
27
+ rescue LoadError; end
28
+
29
+ begin
30
+ require 'colours'
31
+ include Colours if Object.const_defined? :Colours
32
+ rescue LoadError; end
33
+
34
+ require 'extracter/extracter.rb'
35
+
36
+ # ========================================================================= #
37
+ # === NAMESPACE
38
+ # ========================================================================= #
39
+ NAMESPACE = inspect
40
+
41
+ # ========================================================================= #
42
+ # === ExtractIt::ARRAY_ARCHIVE_TYPES
43
+ #
44
+ # Register the available (and handled) archive types here.
45
+ # ========================================================================= #
46
+ ARRAY_ARCHIVE_TYPES = %w(
47
+ .apkg
48
+ .xz
49
+ .zip
50
+ .tar
51
+ .bz2
52
+ .7z
53
+ )
54
+
55
+ # ========================================================================= #
56
+ # === initialize
57
+ # ========================================================================= #
58
+ def initialize(
59
+ optional_set_input = nil,
60
+ run_already = true
61
+ )
62
+ reset
63
+ set_input(optional_set_input)
64
+ run if run_already
65
+ end
66
+
67
+ # ========================================================================= #
68
+ # === reset (reset tag)
69
+ # ========================================================================= #
70
+ def reset
71
+ @debug = false
72
+ end
73
+
74
+ # ========================================================================= #
75
+ # === set_input
76
+ #
77
+ # We will work on an Array as value to @input, at the end of this
78
+ # method.
79
+ # ========================================================================= #
80
+ def set_input(i = N)
81
+ case i
82
+ # ======================================================================= #
83
+ # === extract_it --help
84
+ # ======================================================================= #
85
+ when /-?-?help$/i # Show some help stuff here.
86
+ e 'We will show a little bit help, then exit.'
87
+ e
88
+ e 'To extract .tar.xz, do:'
89
+ e
90
+ efancy ' -> tar -xJf *.tar.xz'
91
+ exit
92
+ end
93
+ i = [i] if i.is_a? String # Need an Array.
94
+ if @debug
95
+ opn; e 'The input given to us is: `'+sfile(i)+'`'
96
+ end
97
+ if i.is_a?(Array) and i.empty?
98
+ # ===================================================================== #
99
+ # In this case, try to see if the current directory has a .zip
100
+ # file. We will use this in that case.
101
+ # ===================================================================== #
102
+ is_there_a_zip_file = Dir['*.zip']
103
+ unless is_there_a_zip_file.empty?
104
+ use_this_zip_file = is_there_a_zip_file.first
105
+ notify_the_user_that_no_input_was_given_but_this_file_was_found(use_this_zip_file)
106
+ i << use_this_zip_file
107
+ end
108
+ is_there_at_the_least_one_tar_xz_file = Dir['*.tar.xz']
109
+ unless is_there_at_the_least_one_tar_xz_file.empty?
110
+ i << is_there_at_the_least_one_tar_xz_file.first
111
+ end
112
+ end
113
+ @input = i # Should be an array, always.
114
+ end
115
+
116
+ # ========================================================================= #
117
+ # === be_silent
118
+ # ========================================================================= #
119
+ def be_silent
120
+ @be_silent = true
121
+ end
122
+
123
+ # ========================================================================= #
124
+ # === be_verbose
125
+ # ========================================================================= #
126
+ def be_verbose
127
+ @be_silent = false
128
+ end; alias show_commands_used be_verbose # === show_commands_used
129
+
130
+ # ========================================================================= #
131
+ # === notify_the_user_that_no_input_was_given_but_this_file_was_found
132
+ # ========================================================================= #
133
+ def notify_the_user_that_no_input_was_given_but_this_file_was_found(
134
+ this_file
135
+ )
136
+ opn; e 'No input was given to '+sfancy(NAMESPACE)+' but a '\
137
+ '.zip file was'
138
+ opn; e 'found in this directory ('+sdir(Dir.pwd)+'): '+
139
+ sfancy(this_file)
140
+ opn; e 'We will use this zip file.'
141
+ end
142
+
143
+ # ========================================================================= #
144
+ # === extract_input
145
+ # ========================================================================= #
146
+ def extract_input
147
+ pp @input if @debug
148
+ @input.each {|entry|
149
+ to_this_dir = Dir.pwd
150
+ to_this_dir << '/' unless to_this_dir.end_with? '/'
151
+ unless File.exist? entry
152
+ entry = try_to_glob_on(entry)
153
+ end
154
+ # ===================================================================== #
155
+ # Delegate to class Extracter next.
156
+ # ===================================================================== #
157
+ Extracter.extract_what_to(
158
+ entry,
159
+ to_this_dir,
160
+ @be_silent
161
+ )
162
+ _ = RemoveFileSuffix[entry]
163
+ if File.exist? entry
164
+ # =================================================================== #
165
+ # Must also check whether the extracted directory exists.
166
+ # =================================================================== #
167
+ name_of_the_extracted_archive = to_this_dir+_
168
+ ARRAY_ARCHIVE_TYPES.each {|extension_name|
169
+ if name_of_the_extracted_archive.include? extension_name
170
+ quoted = Regexp.quote(extension_name)
171
+ name_of_the_extracted_archive.sub!(/#{quoted}$/,'')
172
+ end
173
+ }
174
+ if File.exist?(name_of_the_extracted_archive) and
175
+ # ================================================================= #
176
+ # The following check ensures that we really have another name
177
+ # for the extracted directory.
178
+ # ================================================================= #
179
+ !(entry == name_of_the_extracted_archive)
180
+ opn; e "Finished extracting #{sfile(_)} to `#{sdir(to_this_dir)}`."
181
+ else
182
+ opn; e "No file called `#{sfile(name_of_the_extracted_archive)}"\
183
+ "` appears to exist."
184
+ end
185
+ end
186
+ }
187
+ end
188
+
189
+ # ========================================================================= #
190
+ # === is_archive?
191
+ # ========================================================================= #
192
+ def is_archive?(i)
193
+ ARRAY_ARCHIVE_TYPES.include?(
194
+ File.extname(File.basename(i))
195
+ )
196
+ end
197
+
198
+ # ========================================================================= #
199
+ # === try_to_glob_on
200
+ # ========================================================================= #
201
+ def try_to_glob_on(i)
202
+ result = Dir["#{i}*"]
203
+ # ======================================================================= #
204
+ # Next, sort this result to put archives on the beginning of the Array.
205
+ # ======================================================================= #
206
+ result = result.partition {|entry| is_archive?(entry) }
207
+ result.flatten!
208
+ unless result.empty?
209
+ # ===================================================================== #
210
+ # Ok, we grab the first entry next.
211
+ # ===================================================================== #
212
+ i = result.first
213
+ opn; e "No result could be found for the given input, thus "\
214
+ "using #{sfancy(i)} instead."
215
+ end
216
+ i
217
+ end
218
+
219
+ # ========================================================================= #
220
+ # === run (run tag)
221
+ # ========================================================================= #
222
+ def run
223
+ extract_input
224
+ end
225
+
226
+ # ========================================================================= #
227
+ # === Extracter::ExtractIt[]
228
+ # ========================================================================= #
229
+ def self.[](i = '')
230
+ new(i)
231
+ end
232
+
233
+ end; end
234
+
235
+ if __FILE__ == $PROGRAM_NAME
236
+ Extracter::ExtractIt.new(ARGV)
237
+ end # extract_it.rb
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Extracter
6
+ #
7
+ # The purpose of this class is to abstract extracting source files to
8
+ # a target location. You just pass the argument of the file that has
9
+ # to be extracted to this class, and it should handle the rest.
10
+ #
11
+ # We can also "extract" Audio files, by calling the module ExtractAudio.
12
+ # You can try this - just pass a .mp4 file as first argument to
13
+ # this class.
14
+ #
15
+ # Usage examples for class Extracter:
16
+ #
17
+ # Extracter.new('foo.mp3')
18
+ # Extracter.new('xyz-1.0.tar.gz')
19
+ #
20
+ # Usage example:
21
+ #
22
+ # Extracter.new
23
+ #
24
+ # =========================================================================== #
25
+ # require 'extracter/extracter.rb'
26
+ # =========================================================================== #
27
+
28
+ # =========================================================================== #
29
+ # Next, load up .rb files that belong to extracter/.
30
+ # =========================================================================== #
31
+ require 'extracter/version/version.rb'
32
+ require 'extracter/constants/constants.rb'
33
+ require 'extracter/class_methods.rb'
34
+ require 'extracter/colours.rb'
35
+ require 'extracter/do_extract_what_to.rb'
36
+ require 'extracter/help.rb'
37
+ require 'extracter/initialize.rb'
38
+ require 'extracter/misc.rb'
39
+ require 'extracter/reset.rb'
40
+ require 'extracter/opn.rb'
41
+
42
+ module Extracter
43
+
44
+ class Extracter # === Extracter
45
+ end
46
+
47
+ end
48
+
49
+ if __FILE__ == $PROGRAM_NAME
50
+ # The second argument to Extracter.new is where we will
51
+ # extract to.
52
+ if ARGV.size > 2
53
+ _ = Extracter::Extracter.new(ARGV, :default, false)
54
+ else
55
+ _ = Extracter::Extracter.new(ARGV[0], ARGV[1], false)
56
+ end
57
+ _.extract_to Dir.pwd unless ARGV[1]
58
+ # _.enable_debug
59
+ _.be_verbose
60
+ _.run
61
+ end # extracter
62
+ # extracter htop-1.0.2.tar.xz
63
+ # extracter xfig-3.2.5.tar.bz2
64
+ # extract htop* /Depot/
65
+ # extract recode-3.7.tar.xz /Depot/
66
+ # extract qt-4.8.6.tar.xz --to=/home/Temp
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ module Extracter
6
+
7
+ class Extracter
8
+
9
+ # ========================================================================= #
10
+ # === show_help (help tag)
11
+ #
12
+ # This method will show the available - and documented - help options
13
+ # for class Extracter.
14
+ # ========================================================================= #
15
+ def show_help
16
+ e
17
+ opnn; e 'How to extract archives, without helper scripts?'
18
+ e
19
+ opnn; e ' tar -zxvf foobar.tar.gz # for .tar.gz'
20
+ opnn; e ' tar xvzf foobar.tgz # for .tgz'
21
+ opnn; e ' tar xvfJ foobar.tar.xz # for .tar.xz'
22
+ opnn; e ' tar jxf foobar.tar.bz2 # for .tar.bz2'
23
+ opnn; e ' tar -xf foobar.tar.bz2 # for .tbz'
24
+ opnn; e ' tar --lzip -xvf zutils-1.5.tar.lz # for .tar.lz'
25
+ opnn; e ' unsquashfs foobar-1.2.3.sxz # for .sxz'
26
+ e
27
+ opnn; e 'Furthermore, there are some commandline options '\
28
+ 'that can be used for this class (class Extracter).'
29
+ e
30
+ opnn; e ' --to=/home/Temp # extract into the '\
31
+ 'directory /home/Temp/'
32
+ e
33
+ end
34
+
35
+ end; end