extracter 1.1.3

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,98 @@
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
24
+ # heavily outdated eventually.
25
+ # ========================================================================= #
26
+ LAST_UPDATED = '26 Oct 2020'
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
+ rar
84
+ rpm
85
+ squashfs
86
+ sxz
87
+ tar
88
+ taz
89
+ tgz
90
+ txz
91
+ xpi
92
+ xz
93
+ zip
94
+ Z
95
+ zst
96
+ )
97
+
98
+ end; end
@@ -0,0 +1,291 @@
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
+ # === img
50
+ #
51
+ # Note that .img in this context refers to squafhs .img files.
52
+ # ======================================================================= #
53
+ when '.img',
54
+ '.squashfs'
55
+ opnn; e 'Handling a squashfs .img file format next:'
56
+ name_without_extension = what.sub(/#{File.extname(what)}$/,'')
57
+ mkdir(name_without_extension) unless File.directory? name_without_extension
58
+ esystem 'mount -o loop -t squashfs '+what+' '+name_without_extension
59
+ e 'The content of the extracted (or rather, mounted) archive is:'
60
+ pp Dir["#{name_without_extension}*"]
61
+ return
62
+ # ======================================================================= #
63
+ # === iso
64
+ # ======================================================================= #
65
+ when '.iso'
66
+ opnn; e 'Extracting an .iso file is a bit more complicated '\
67
+ 'than a .tar.gz tarball release.'
68
+ opnn; e 'We will first create a directory; and then mount '\
69
+ 'the .iso there.'
70
+ name_without_extension = what.sub(/#{File.extname(what)}$/,'')
71
+ mkdir(name_without_extension) unless File.directory? name_without_extension
72
+ esystem 'mount -o loop '+what+' '+name_without_extension
73
+ e 'The content of the extracted (or rather, mounted) archive is:'
74
+ pp Dir[name_without_extension+'*']
75
+ return
76
+ # ======================================================================= #
77
+ # === sxz
78
+ # ======================================================================= #
79
+ when '.sxz'
80
+ _ = 'unsquashfs '.dup # This requires squashfs with xz-support.
81
+ # ======================================================================= #
82
+ # === lz
83
+ # ======================================================================= #
84
+ when '.lz','.tar.lz'
85
+ _ = 'tar --lzip -xvf '.dup # This requires lzip to be installed.
86
+ # ======================================================================= #
87
+ # === tar.Z
88
+ # ======================================================================= #
89
+ when '.tar.Z','.taz'
90
+ _ << 'tar -xvzf'
91
+ # ======================================================================= #
92
+ # === .jar
93
+ # ======================================================================= #
94
+ when /\.?jar$/i
95
+ _ << 'jar xvf'
96
+ # ======================================================================= #
97
+ # === .tar
98
+ # ======================================================================= #
99
+ when '.tar','.tar.bz2'
100
+ _ << 'tar -xvf'
101
+ # ======================================================================= #
102
+ # === .zst
103
+ #
104
+ # This entry point is for e. g. "pymol-2.3.0-3-x86_64.pkg.tar.zst".
105
+ # ======================================================================= #
106
+ when '.zst','.tar.zst'
107
+ _ << 'tar -I zstd -xvf '
108
+ # ======================================================================= #
109
+ # === txz
110
+ # ======================================================================= #
111
+ when '.txz'
112
+ _ << 'tar Jxvf' # Since Jan 2012.
113
+ # ======================================================================= #
114
+ # === gz
115
+ # ======================================================================= #
116
+ when '.gz'
117
+ if what.include? '.tar'
118
+ _ << 'tar -zxvf'
119
+ else
120
+ _ << 'gunzip'
121
+ end
122
+ # ======================================================================= #
123
+ # === xz
124
+ # ======================================================================= #
125
+ when '.xz'
126
+ if _.include? '.tar'
127
+ end
128
+ _ << 'tar -xvf' # tar -Jxv #{what} would be an alternative.
129
+ # ======================================================================= #
130
+ # === rpm
131
+ # ======================================================================= #
132
+ when '.rpm'
133
+ _ << 'bsdtar xfv'
134
+ # ======================================================================= #
135
+ # === bin
136
+ # ======================================================================= #
137
+ when '.bin' # handle .bin files here.
138
+ # _ = 'tar -jxf '+package
139
+ _ << "./#{what}"
140
+ # ======================================================================= #
141
+ # === zip
142
+ # ======================================================================= #
143
+ when '.zip','.xpi','.docx','.odt', # .docx and .odt format types can be unpacked with zip too.
144
+ '.apkg'
145
+ # _ << 'ar -jxf' # unzip #{what} <-- this should work as well.
146
+ _ << 'unzip '
147
+ when /\.bz2/,'.tbz2'
148
+ if what.include? '.tar' # Treat it as a .tar file.
149
+ _ << 'tar -vjxf '
150
+ else
151
+ _ << 'bunzip2 '
152
+ end
153
+ # ======================================================================= #
154
+ # === lzma
155
+ # ======================================================================= #
156
+ when '.lzma'
157
+ _ << 'unlzma '
158
+ # ======================================================================= #
159
+ # === 7z
160
+ # ======================================================================= #
161
+ when '.7z' # 7z does not accept the -C commandline.
162
+ # _ << '7za e' # <- Deprecated as of 05.06.2020.
163
+ _ << '7z x'
164
+ # ======================================================================= #
165
+ # === gem
166
+ # ======================================================================= #
167
+ when '.gem'
168
+ _ << GEM_UNPACK_COMMAND
169
+ # ======================================================================= #
170
+ # === rar
171
+ # ======================================================================= #
172
+ when '.rar'
173
+ check_whether_rar_is_available
174
+ _ << 'unrar e'
175
+ # ======================================================================= #
176
+ # === deb
177
+ # ======================================================================= #
178
+ when '.deb'
179
+ #_ = 'dpkg-deb -x' # {to}
180
+ _ << 'ar -x' # ar -x #{what} This could work too.
181
+ # ======================================================================= #
182
+ # === tgz
183
+ # ======================================================================= #
184
+ when '.tgz'
185
+ _ << 'tar -xvzf'
186
+ # ======================================================================= #
187
+ # === ps
188
+ # ======================================================================= #
189
+ when '.ps'
190
+ _ << 'ps2ascii'
191
+ # ======================================================================= #
192
+ # === mp4
193
+ # ======================================================================= #
194
+ when '.mp4' # If it is a .mp4 file, we delegate to ExtractAudio instead.
195
+ if Object.const_defined? :MultimediaParadise
196
+ MultimediaParadise.extract_audio(@source_package_location)
197
+ end
198
+ exit
199
+ else # else tag. We did not find the extension type.
200
+ @skip_extracting = true
201
+ copn; ewarn "We did not find: `#{sfile(what)}`. "
202
+ copn; ewarn 'The file-type (extension) was: `'+simp(extname)+'`.'
203
+ # Try to rescue though.
204
+ result = run_this_system_command("file #{what}")
205
+ copn; e result
206
+ if result.include? 'bzip2 compressed'
207
+ copn; e 'We assume it is a .bz2 file though.'
208
+ _ = 'tar -vjxf '.dup
209
+ @skip_extracting = false
210
+ end
211
+ end unless @skip_extracting
212
+ unless File.exist? what
213
+ @skip_extracting = true
214
+ copn; e "The file `#{sfile(what)}"\
215
+ "` does not exist - can not extract."
216
+ end
217
+ # ======================================================================= #
218
+ # Handle the situation when the given input includes a ')' character.
219
+ # We will pad such an input with '"' characters.
220
+ # ======================================================================= #
221
+ if what.include? ')'
222
+ what = pad(what, '"') #sanitize_input(what)
223
+ end
224
+ # ======================================================================= #
225
+ # Next, pad it if it includes a ' ' character.
226
+ # ======================================================================= #
227
+ what = pad(what) if what.include?(' ')
228
+ _ << " #{what}" unless _.empty?
229
+ if _.include? GEM_UNPACK_COMMAND # Gem command needs a --target=DIR option.
230
+ _ << " --target=#{to}"
231
+ elsif _.include?('ar -x') and ! _.include?('.tar') # Do nothing in this case.
232
+ elsif _.end_with? '.sxz'
233
+ # .sxz does not require the -C option.
234
+ elsif _.end_with? '.zip','.xpi','.7z','.jar','.apkg'
235
+ # Do not use -C option for 7z, as it hates this option.
236
+ # .jar files also do not support the -C option.
237
+ elsif _.include? 'bunzip'
238
+ # Do not use -C option for bunzip, as it hates this option.
239
+ else
240
+ # ===================================================================== #
241
+ # Next, we need to determine the location where we extract our
242
+ # archive to.
243
+ # ===================================================================== #
244
+ _ << ' '
245
+ # ===================================================================== #
246
+ # Add -C option except for .deb packages and gunzip-based archives.
247
+ # ===================================================================== #
248
+ unless _ =~ /deb$/ or _.include?('gunzip')
249
+ _ << '-C '
250
+ _ << to
251
+ end
252
+ end
253
+ if run_simulation?
254
+ copn; e 'As we are running in simulation mode, the following command '
255
+ copn; e 'is the one that we would have been used if we were to not run '
256
+ copn; e 'in simulation mode:'
257
+ e _
258
+ else # Ok, here we are not in a simulation, hence we can run the command.
259
+ unless @skip_extracting
260
+ if File.writable? to
261
+ # ================================================================= #
262
+ # Next, run the sys-command, with some padding.
263
+ # ================================================================= #
264
+ begin
265
+ run_this_system_command(
266
+ " #{_}", :also_show_what_we_will_do
267
+ )
268
+ # ================================================================= #
269
+ # We have to rescue because unrar might not be available and so on.
270
+ # ================================================================= #
271
+ rescue Exception => error
272
+ e 'An error has happened upon attempting to run this system command:'
273
+ e
274
+ e " #{_}"
275
+ e
276
+ pp error
277
+ e '-----------'
278
+ pp error.class
279
+ e '-----------'
280
+ end
281
+ @did_we_extract_already = true
282
+ else
283
+ copn; ewarn 'You do not have sufficient permissions to'
284
+ copn; ewarn "modify #{sdir(to)}."
285
+ end
286
+ end
287
+ end
288
+ end; alias do_extract_to do_extract_what_to # === do_extract_to
289
+ alias start do_extract_what_to # === start
290
+
291
+ end; end