extracter 1.1.8

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

@@ -0,0 +1,126 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ module Extracter
6
+
7
+ class Extracter
8
+
9
+ begin
10
+ require 'colours'
11
+ rescue LoadError; end
12
+
13
+ # ========================================================================= #
14
+ # === use_colours?
15
+ #
16
+ # Determine whether we will use colours in class Extracter.
17
+ # ========================================================================= #
18
+ def use_colours?
19
+ @use_colours
20
+ end
21
+
22
+ # ========================================================================= #
23
+ # === set_use_colours
24
+ # ========================================================================= #
25
+ def set_use_colours(i)
26
+ # ======================================================================= #
27
+ # We must also sync this towards our main Hash, for opn(). The next
28
+ # line of code achieves precisely that.
29
+ # ======================================================================= #
30
+ @use_this_opn_hash.update(use_colours: i)
31
+ @use_colours = i
32
+ end
33
+
34
+ # ========================================================================= #
35
+ # === disable_colours
36
+ #
37
+ # Use this method if you want to disable colour-support of this class.
38
+ # ========================================================================= #
39
+ def disable_colours
40
+ @use_colours = false
41
+ @colour_to_use_for_directories = ''.dup
42
+ end
43
+
44
+ # ========================================================================= #
45
+ # === enable_colours
46
+ # ========================================================================= #
47
+ def enable_colours
48
+ @use_colours = true
49
+ @colour_to_use_for_directories = cyan?
50
+ end
51
+
52
+ # ========================================================================= #
53
+ # === set_colour_for_directories
54
+ #
55
+ # Set the colour for directories to use.
56
+ # ========================================================================= #
57
+ def set_colour_for_directories(i)
58
+ @colour_to_use_for_directories = Colours.beautify(i)
59
+ end
60
+
61
+ # ========================================================================= #
62
+ # === colour_to_use_for_directories?
63
+ # ========================================================================= #
64
+ def colour_to_use_for_directories?
65
+ if @use_colours
66
+ @colour_to_use_for_directories
67
+ else
68
+ ''
69
+ end
70
+ end
71
+
72
+ # ========================================================================= #
73
+ # === cyan
74
+ # ========================================================================= #
75
+ def cyan?
76
+ if @use_colours
77
+ Colours::CYAN
78
+ else
79
+ ''
80
+ end
81
+ end
82
+
83
+ # ========================================================================= #
84
+ # === ewarn
85
+ # ========================================================================= #
86
+ def ewarn(i = '')
87
+ if use_colours?
88
+ e Colours.swarn(i)
89
+ else
90
+ e i
91
+ end
92
+ end
93
+
94
+ # ========================================================================= #
95
+ # === simp
96
+ # ========================================================================= #
97
+ def simp(i = '')
98
+ return Colours.simp(i) if @use_colours
99
+ return i
100
+ end
101
+
102
+ # ========================================================================= #
103
+ # === sfile
104
+ # ========================================================================= #
105
+ def sfile(i = '')
106
+ return Colours.sfancy(i) if @use_colours
107
+ return i
108
+ end
109
+
110
+ # ========================================================================= #
111
+ # === sfancy
112
+ # ========================================================================= #
113
+ def sfancy(i = '')
114
+ return Colours.sfancy(i) if @use_colours
115
+ return i
116
+ end
117
+
118
+ # ========================================================================= #
119
+ # === sdir
120
+ # ========================================================================= #
121
+ def sdir(i = '')
122
+ return Colours.sdir(i) if @use_colours
123
+ return i
124
+ end
125
+
126
+ end; end
@@ -0,0 +1,100 @@
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
+ # === NAMESPACE
11
+ # ========================================================================= #
12
+ NAMESPACE = inspect
13
+
14
+ # ========================================================================= #
15
+ # === N
16
+ # ========================================================================= #
17
+ N = "\n"
18
+
19
+ # ========================================================================= #
20
+ # === LAST_UPDATED
21
+ #
22
+ # When this class was last updated/releasted. This is not a hugely
23
+ # important constat, so do not worry too much if this may be heavily
24
+ # outdated eventually.
25
+ # ========================================================================= #
26
+ LAST_UPDATED = '14 Apr 2021'
27
+
28
+ # ========================================================================= #
29
+ # === TEMP_DIR
30
+ #
31
+ # Set the temp dir here.
32
+ # ========================================================================= #
33
+ if ENV['MY_TEMP']
34
+ TEMP_DIR = ENV['MY_TEMP'].to_s+'/'
35
+ else
36
+ # ======================================================================= #
37
+ # If this environment variable is unavailable then use a conservative
38
+ # default value.
39
+ # ======================================================================= #
40
+ TEMP_DIR = '/tmp/'
41
+ end
42
+
43
+ # ========================================================================= #
44
+ # === SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE
45
+ #
46
+ # If this constant is set to true then we will only show the shortened
47
+ # name of the archive in question by default.
48
+ # ========================================================================= #
49
+ SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE = true
50
+
51
+ # ========================================================================= #
52
+ # === GEM_UNPACK_COMMAND
53
+ #
54
+ # The command to use to unpack ruby .gems.
55
+ # We can pass the ---target=DIR syntax to extract to a specific location.
56
+ # ========================================================================= #
57
+ GEM_UNPACK_COMMAND = 'gem unpack'
58
+
59
+ # ========================================================================= #
60
+ # === ARRAY_REGISTERED_ARCHIVES
61
+ #
62
+ # Archives that can be extracted, have to be registered in this Array.
63
+ #
64
+ # Sort alphabetically.
65
+ #
66
+ # The libreoffice format .odt is just like .zip.
67
+ # ========================================================================= #
68
+ ARRAY_REGISTERED_ARCHIVES = %w(
69
+ 7z
70
+ apkg
71
+ bin
72
+ bz2
73
+ deb
74
+ gem
75
+ gz
76
+ img
77
+ iso
78
+ jar
79
+ lz
80
+ lzma
81
+ odt
82
+ mp4
83
+ pdf
84
+ rar
85
+ rpm
86
+ squashfs
87
+ sxz
88
+ tar
89
+ taz
90
+ tbz
91
+ tgz
92
+ txz
93
+ xpi
94
+ xz
95
+ zip
96
+ Z
97
+ zst
98
+ )
99
+
100
+ end; end
@@ -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