extracter 1.1.2

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,97 @@
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 = '14 Jun 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
+ bin
71
+ bz2
72
+ deb
73
+ gem
74
+ gz
75
+ img
76
+ iso
77
+ jar
78
+ lz
79
+ lzma
80
+ odt
81
+ mp4
82
+ rar
83
+ rpm
84
+ squashfs
85
+ sxz
86
+ tar
87
+ taz
88
+ tgz
89
+ txz
90
+ xpi
91
+ xz
92
+ zip
93
+ Z
94
+ zst
95
+ )
96
+
97
+ end; end
@@ -0,0 +1,290 @@
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
+ # _ << 'ar -jxf' # unzip #{what} <-- this should work as well.
145
+ _ << 'unzip '
146
+ when /\.bz2/,'.tbz2'
147
+ if what.include? '.tar' # Treat it as a .tar file.
148
+ _ << 'tar -vjxf '
149
+ else
150
+ _ << 'bunzip2 '
151
+ end
152
+ # ======================================================================= #
153
+ # === lzma
154
+ # ======================================================================= #
155
+ when '.lzma'
156
+ _ << 'unlzma '
157
+ # ======================================================================= #
158
+ # === 7z
159
+ # ======================================================================= #
160
+ when '.7z' # 7z does not accept the -C commandline.
161
+ # _ << '7za e' # <- Deprecated as of 05.06.2020.
162
+ _ << '7z x'
163
+ # ======================================================================= #
164
+ # === gem
165
+ # ======================================================================= #
166
+ when '.gem'
167
+ _ << GEM_UNPACK_COMMAND
168
+ # ======================================================================= #
169
+ # === rar
170
+ # ======================================================================= #
171
+ when '.rar'
172
+ check_whether_rar_is_available
173
+ _ << 'unrar e'
174
+ # ======================================================================= #
175
+ # === deb
176
+ # ======================================================================= #
177
+ when '.deb'
178
+ #_ = 'dpkg-deb -x' # {to}
179
+ _ << 'ar -x' # ar -x #{what} This could work too.
180
+ # ======================================================================= #
181
+ # === tgz
182
+ # ======================================================================= #
183
+ when '.tgz'
184
+ _ << 'tar -xvzf'
185
+ # ======================================================================= #
186
+ # === ps
187
+ # ======================================================================= #
188
+ when '.ps'
189
+ _ << 'ps2ascii'
190
+ # ======================================================================= #
191
+ # === mp4
192
+ # ======================================================================= #
193
+ when '.mp4' # If it is a .mp4 file, we delegate to ExtractAudio instead.
194
+ if Object.const_defined? :MultimediaParadise
195
+ MultimediaParadise.extract_audio(@source_package_location)
196
+ end
197
+ exit
198
+ else # else tag. We did not find the extension type.
199
+ @skip_extracting = true
200
+ copn; ewarn "We did not find: `#{sfile(what)}`. "
201
+ copn; ewarn 'The file-type (extension) was: `'+simp(extname)+'`.'
202
+ # Try to rescue though.
203
+ result = run_this_system_command("file #{what}")
204
+ copn; e result
205
+ if result.include? 'bzip2 compressed'
206
+ copn; e 'We assume it is a .bz2 file though.'
207
+ _ = 'tar -vjxf '.dup
208
+ @skip_extracting = false
209
+ end
210
+ end unless @skip_extracting
211
+ unless File.exist? what
212
+ @skip_extracting = true
213
+ copn; e "The file `#{sfile(what)}"\
214
+ "` does not exist - can not extract."
215
+ end
216
+ # ======================================================================= #
217
+ # Handle the situation when the given input includes a ')' character.
218
+ # We will pad such an input with '"' characters.
219
+ # ======================================================================= #
220
+ if what.include? ')'
221
+ what = pad(what, '"') #sanitize_input(what)
222
+ end
223
+ # ======================================================================= #
224
+ # Next, pad it if it includes a ' ' character.
225
+ # ======================================================================= #
226
+ what = pad(what) if what.include?(' ')
227
+ _ << " #{what}" unless _.empty?
228
+ if _.include? GEM_UNPACK_COMMAND # Gem command needs a --target=DIR option.
229
+ _ << " --target=#{to}"
230
+ elsif _.include?('ar -x') and ! _.include?('.tar') # Do nothing in this case.
231
+ elsif _.end_with? '.sxz'
232
+ # .sxz does not require the -C option.
233
+ elsif _.end_with? '.zip','.xpi','.7z','.jar'
234
+ # Do not use -C option for 7z, as it hates this option.
235
+ # .jar files also do not support the -C option.
236
+ elsif _.include? 'bunzip'
237
+ # Do not use -C option for bunzip, as it hates this option.
238
+ else
239
+ # ===================================================================== #
240
+ # Next, we need to determine the location where we extract our
241
+ # archive to.
242
+ # ===================================================================== #
243
+ _ << ' '
244
+ # ===================================================================== #
245
+ # Add -C option except for .deb packages and gunzip-based archives.
246
+ # ===================================================================== #
247
+ unless _ =~ /deb$/ or _.include?('gunzip')
248
+ _ << '-C '
249
+ _ << to
250
+ end
251
+ end
252
+ if run_simulation?
253
+ copn; e 'As we are running in simulation mode, the following command '
254
+ copn; e 'is the one that we would have been used if we were to not run '
255
+ copn; e 'in simulation mode:'
256
+ e _
257
+ else # Ok, here we are not in a simulation, hence we can run the command.
258
+ unless @skip_extracting
259
+ if File.writable? to
260
+ # ================================================================= #
261
+ # Next, run the sys-command, with some padding.
262
+ # ================================================================= #
263
+ begin
264
+ run_this_system_command(
265
+ " #{_}", :also_show_what_we_will_do
266
+ )
267
+ # ================================================================= #
268
+ # We have to rescue because unrar might not be available and so on.
269
+ # ================================================================= #
270
+ rescue Exception => error
271
+ e 'An error has happened upon attempting to run this system command:'
272
+ e
273
+ e " #{_}"
274
+ e
275
+ pp error
276
+ e '-----------'
277
+ pp error.class
278
+ e '-----------'
279
+ end
280
+ @did_we_extract_already = true
281
+ else
282
+ copn; ewarn 'You do not have sufficient permissions to'
283
+ copn; ewarn "modify #{sdir(to)}."
284
+ end
285
+ end
286
+ end
287
+ end; alias do_extract_to do_extract_what_to # === do_extract_to
288
+ alias start do_extract_what_to # === start
289
+
290
+ end; end