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,430 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'extracter/misc.rb'
6
+ # =========================================================================== #
7
+ module Extracter
8
+
9
+ class Extracter # === Extracter
10
+
11
+ require 'fileutils'
12
+
13
+ begin
14
+ require 'multimedia_paradise/audio/extract_audio/extract_audio.rb'
15
+ rescue LoadError; end
16
+
17
+ begin
18
+ require 'remove_file_suffix'
19
+ rescue LoadError; end
20
+
21
+ # ========================================================================= #
22
+ # Shall we run in simulation mode or not.
23
+ # ========================================================================= #
24
+ attr_accessor :run_simulation
25
+
26
+ # ========================================================================= #
27
+ # === debug?
28
+ # ========================================================================= #
29
+ def debug?
30
+ @debug
31
+ end
32
+
33
+ # ========================================================================= #
34
+ # === enable_debug
35
+ # ========================================================================= #
36
+ def enable_debug
37
+ @debug = true
38
+ end
39
+
40
+ # ========================================================================= #
41
+ # === do_not_show_name
42
+ #
43
+ # Tells us whether to use opn() or not.
44
+ # ========================================================================= #
45
+ def do_not_show_name
46
+ @do_not_show_name = true
47
+ end
48
+
49
+ # ========================================================================= #
50
+ # === do_show_name
51
+ #
52
+ # If this is enabled, we will show the name of the file when we invoke
53
+ # copn().
54
+ # ========================================================================= #
55
+ def do_show_name
56
+ @do_not_show_name = false
57
+ end
58
+
59
+ # ========================================================================= #
60
+ # === sanitize_input
61
+ # ========================================================================= #
62
+ def sanitize_input(i)
63
+ if i.include? '('
64
+ i.gsub!(/\(/,'\(')
65
+ i.gsub!(/\)/,'\)') if i.include? ')'
66
+ i = pad(i, '"')
67
+ end
68
+ return i
69
+ end
70
+
71
+ # ========================================================================= #
72
+ # === check_whether_rar_is_available
73
+ #
74
+ # We try to find out whether unrar is available.
75
+ # ========================================================================= #
76
+ def check_whether_rar_is_available
77
+ is_available = false
78
+ ENV['PATH'].split(':').each {|entry|
79
+ is_available = true if File.exist? entry+'/unrar'
80
+ }
81
+ unless is_available
82
+ copn; e 'Sorry, unrar is not available. Please install it first.'
83
+ end
84
+ end
85
+
86
+ # ========================================================================= #
87
+ # === run_simulation?
88
+ # ========================================================================= #
89
+ def run_simulation?
90
+ @run_simulation
91
+ end
92
+
93
+ # ========================================================================= #
94
+ # === set_run_simulation
95
+ # ========================================================================= #
96
+ def set_run_simulation(i)
97
+ @run_simulation = i
98
+ end
99
+
100
+ # ========================================================================= #
101
+ # === pad
102
+ # ========================================================================= #
103
+ def pad(i, with_this_character = "'")
104
+ return with_this_character+i+with_this_character
105
+ end
106
+
107
+ # ========================================================================= #
108
+ # === extract_to?
109
+ #
110
+ # Simply output the result of @extract_to_this_location variable.
111
+ # ========================================================================= #
112
+ def extract_to?
113
+ @extract_to_this_location.to_s
114
+ end; alias source_package_location extract_to? # === source_package
115
+
116
+ # ========================================================================= #
117
+ # === set_source_location
118
+ #
119
+ # Use this method to designate the source location of a given (input)
120
+ # program. In other words - the tarball or archive that must be
121
+ # extracted. It will however be stored as Array.
122
+ #
123
+ # If we pass a hash to this method, we assume that the user wants
124
+ # to also populate some other values.
125
+ # ========================================================================= #
126
+ def set_source_location(i = nil)
127
+ if i.nil?
128
+ copn; e 'You should set a file.'
129
+ end
130
+ if i.is_a? Array
131
+ i = i.flatten
132
+ else
133
+ i = [i]
134
+ end # After this point, we will have an Array.
135
+ case i.first # case tag
136
+ when /^-?-?help$/i
137
+ show_help
138
+ exit
139
+ end
140
+ i.map! {|entry| # Iterate over our Array next.
141
+ # ======================================================================= #
142
+ # Handle the case when the user did input a number.
143
+ # ======================================================================= #
144
+ begin
145
+ if entry =~ /^\d$/
146
+ entry = Dir['*'][( entry.to_i - 1 )] unless File.exist?(entry)
147
+ end
148
+ rescue ArgumentError => error
149
+ e 'Error for '+sfancy(entry)+':'
150
+ pp error
151
+ end
152
+ entry = rds(entry.to_s)
153
+ # ======================================================================= #
154
+ # Next, find the proper working directory.
155
+ # ======================================================================= #
156
+ unless entry.include? '/'
157
+ entry = rds( (Dir.pwd+'/'+entry) )
158
+ end
159
+ # ======================================================================= #
160
+ # If the user supplied a directory instead, we will randomly grab an
161
+ # entry from said directory.
162
+ # ======================================================================= #
163
+ if File.directory? entry
164
+ entry = Dir[rds(entry+'/')+'*'].sample
165
+ end
166
+ entry
167
+ } # Sanitize the result, just in case.
168
+ i = [i] unless i.is_a? Array # Much more convenient to work with an array.
169
+ @source_location = i
170
+ end; alias set_what set_source_location # === set_what
171
+
172
+ # ========================================================================= #
173
+ # === source_location?
174
+ # ========================================================================= #
175
+ def source_location?
176
+ @source_location.first
177
+ end; alias input? source_location? # === input?
178
+
179
+ # ========================================================================= #
180
+ # === did_we_extract_already?
181
+ #
182
+ # Whether we already did extract or whether we did not.
183
+ # ========================================================================= #
184
+ def did_we_extract_already?
185
+ @did_we_extract_already
186
+ end; alias did_we_extract_already did_we_extract_already? # === did_we_extract_already
187
+
188
+ # ========================================================================= #
189
+ # === extract_to_this_location?
190
+ #
191
+ # Extract to this location.
192
+ # ========================================================================= #
193
+ def extract_to_this_location?
194
+ @extract_to_this_location
195
+ end; alias extract_to_this_location extract_to_this_location? # === extract_to_this_location
196
+
197
+ # ========================================================================= #
198
+ # === extracted_to?
199
+ #
200
+ # This method is different from extract_to?.
201
+ #
202
+ # It will keep track of the directory to where we extracted to
203
+ # exactly.
204
+ # ========================================================================= #
205
+ def extracted_to?
206
+ rds(
207
+ extract_to?+
208
+ File.basename(input?).sub(/\.xz$/,'').sub(/\.gz$/,'').
209
+ sub(/\.tar$/,'')+'/'
210
+ )
211
+ end; alias extracted_path? extracted_to? # === extracted_path?
212
+
213
+ # ========================================================================= #
214
+ # === namespace?
215
+ # ========================================================================= #
216
+ def namespace?
217
+ @namespace
218
+ end
219
+
220
+ # ========================================================================= #
221
+ # === set_extract_to_this_location
222
+ #
223
+ # Use this when setting the variable @extract_to_this_location.
224
+ #
225
+ # This can be modified from the commandline such as by doing this:
226
+ # ========================================================================= #
227
+ def set_extract_to_this_location(
228
+ i = TEMP_DIR
229
+ )
230
+ if i.is_a? Hash
231
+ if i.has_key? :to
232
+ i = i.delete :to
233
+ elsif i.has_key? :extract_to
234
+ i = i.delete :extract_to
235
+ end
236
+ end
237
+ case i # case tag
238
+ when :default
239
+ i = Dir.pwd
240
+ when 'TEMP',
241
+ 'MY_TEMP',
242
+ 'MYTEMP'
243
+ i = TEMP_DIR
244
+ end
245
+ i = TEMP_DIR if i.nil?
246
+ i = i.to_s.dup
247
+ i << '/' unless i.end_with? '/'
248
+ i = rds(i)
249
+ i.gsub!(/--to=/,'') if i.include? '--to='
250
+ @extract_to_this_location = i
251
+ end; alias set_extract_to set_extract_to_this_location # === set_extract_to
252
+ alias extract_to= set_extract_to_this_location # === extract_to=
253
+ alias extract_to set_extract_to_this_location # === extract_to
254
+
255
+ # ========================================================================= #
256
+ # === run_this_system_command
257
+ # ========================================================================= #
258
+ def run_this_system_command(
259
+ i, instruction = :do_nothing_special
260
+ )
261
+ if instruction == :also_show_what_we_will_do
262
+ copn; e i
263
+ end
264
+ return `#{i}` # system tag
265
+ end
266
+
267
+ # ========================================================================= #
268
+ # === prefix_namespace_with
269
+ # ========================================================================= #
270
+ def prefix_namespace_with(i)
271
+ @namespace = "#{i}#{@namespace.dup}"
272
+ update_the_opn_hash # Also update the opn-hash here.
273
+ end
274
+
275
+ # ========================================================================= #
276
+ # === report_to_the_user
277
+ #
278
+ # This method reports to the user. Usually this is done only via this
279
+ # file here though.
280
+ # ========================================================================= #
281
+ def report_to_the_user
282
+ if @be_verbose
283
+ unless @skip_extracting
284
+ copn; e 'Finished extracting to `'+sdir(
285
+ extract_to?+remove_file_extension(
286
+ @source_location.first # This is an Array.
287
+ )+'/'
288
+ )+'`.'
289
+ end
290
+ end
291
+ end
292
+
293
+ # ========================================================================= #
294
+ # === be_verbose?
295
+ #
296
+ # Getter method for whether we will be verbose or not.
297
+ # ========================================================================= #
298
+ def be_verbose?
299
+ @be_verbose
300
+ end
301
+
302
+ # ========================================================================= #
303
+ # === be_verbose
304
+ # ========================================================================= #
305
+ def be_verbose
306
+ set_be_verbose(true)
307
+ end
308
+
309
+ # ========================================================================= #
310
+ # === be_silent
311
+ # ========================================================================= #
312
+ def be_silent
313
+ set_be_verbose(false)
314
+ end
315
+
316
+ # ========================================================================= #
317
+ # === set_be_verbose
318
+ #
319
+ # This sets the verbosity level of the class. Use only this method
320
+ # when you wish to modify the @be_verbose instance variable.
321
+ # ========================================================================= #
322
+ def set_be_verbose(i = false)
323
+ @be_verbose = i
324
+ end; alias set_verbosity set_be_verbose # === set_verbosity
325
+
326
+ # ========================================================================= #
327
+ # === determine_default_opn_hash
328
+ # ========================================================================= #
329
+ def determine_default_opn_hash
330
+ @use_this_opn_hash = {
331
+ namespace: namespace?,
332
+ use_colours: use_colours?
333
+ }
334
+ end; alias update_the_opn_hash determine_default_opn_hash # === update_the_opn_hash
335
+
336
+ # ========================================================================= #
337
+ # === register_sigint
338
+ # ========================================================================= #
339
+ def register_sigint
340
+ Signal.trap('SIGINT') {
341
+ e sfancy('Requesting a graceful exit from ')+
342
+ colour_to_use_for_directories?+
343
+ 'class Extracter'+
344
+ sfancy('. Exiting now.')
345
+ exit
346
+ }
347
+ end
348
+
349
+ # ========================================================================= #
350
+ # === create_directory
351
+ #
352
+ # Use this to create directories.
353
+ # ========================================================================= #
354
+ def create_directory(i)
355
+ FileUtils.mkdir_p(i)
356
+ end; alias mkdir create_directory # === mkdir
357
+
358
+ # ========================================================================= #
359
+ # === remove_file_extension
360
+ # ========================================================================= #
361
+ def remove_file_extension(i)
362
+ _ = File.basename(i)
363
+ return RemoveFileSuffix[_]
364
+ end
365
+
366
+ # ========================================================================= #
367
+ # === rds
368
+ # ========================================================================= #
369
+ def rds(i)
370
+ i.squeeze('/')
371
+ end
372
+
373
+ # ========================================================================= #
374
+ # === esystem
375
+ # ========================================================================= #
376
+ def esystem(i)
377
+ e i
378
+ system i
379
+ end
380
+
381
+ # ========================================================================= #
382
+ # === e
383
+ # ========================================================================= #
384
+ def e(i = '')
385
+ puts i
386
+ end
387
+
388
+ # ========================================================================= #
389
+ # === do_show_the_full_name_of_the_archive
390
+ # ========================================================================= #
391
+ def do_show_the_full_name_of_the_archive
392
+ @show_only_the_short_name_of_the_archive = false
393
+ end
394
+
395
+ # ========================================================================= #
396
+ # === fail_message_not_registered
397
+ #
398
+ # Output a fail message when the archive format is not registered.
399
+ # ========================================================================= #
400
+ def fail_message_not_registered(i)
401
+ copn; e "Can not extract `#{sfancy(i)}` - it is not "\
402
+ "registered."
403
+ end
404
+
405
+ # ========================================================================= #
406
+ # === work_on_the_given_input
407
+ # ========================================================================= #
408
+ def work_on_the_given_input
409
+ if @source_location.empty?
410
+ copn; e 'Can not extract anything as no input has been given.'
411
+ else
412
+ @source_location.each {|entry|
413
+ if Extracter.is_this_a_valid_archive?(entry)
414
+ do_extract_what_to(entry)
415
+ report_to_the_user
416
+ else
417
+ fail_message_not_registered(entry)
418
+ end
419
+ }
420
+ end
421
+ end
422
+
423
+ # ========================================================================= #
424
+ # === run (run tag, def tag)
425
+ # ========================================================================= #
426
+ def run
427
+ work_on_the_given_input
428
+ end
429
+
430
+ end; end
@@ -0,0 +1,47 @@
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 'opn'
11
+ rescue LoadError; end
12
+
13
+ # ========================================================================= #
14
+ # === custom_opn
15
+ #
16
+ # This is like opn(), except that we also check whether
17
+ # we should show the name or not.
18
+ # ========================================================================= #
19
+ def custom_opn
20
+ opnn unless @do_not_show_name
21
+ end; alias copn custom_opn # === copn
22
+
23
+ # ========================================================================= #
24
+ # === opnn
25
+ # ========================================================================= #
26
+ def opnn
27
+ if @use_opn and Object.const_defined?(:Opn)
28
+ Opn.opn(@use_this_opn_hash)
29
+ end
30
+ end
31
+
32
+ # ========================================================================= #
33
+ # === set_use_opn
34
+ # ========================================================================= #
35
+ def set_use_opn(i = true)
36
+ @use_opn = i
37
+ end
38
+
39
+ # ========================================================================= #
40
+ # === pad_opn_with_n_tokens
41
+ # ========================================================================= #
42
+ def pad_opn_with_n_tokens(n_tokens)
43
+ determine_default_opn_hash # Update this, just in case.
44
+ @use_this_opn_hash.update(padding: n_tokens)
45
+ end; alias set_pad_opn_with_n_tokens pad_opn_with_n_tokens # === set_pad_opn_with_n_tokens
46
+
47
+ end; end