extracter 1.1.0

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