extracter 1.1.1

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,90 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'extracter/initialize.rb'
6
+ # =========================================================================== #
7
+ module Extracter
8
+
9
+ class Extracter
10
+
11
+ # ========================================================================= #
12
+ # === initialize
13
+ #
14
+ # The first argument to this method is the archive that should be extracted.
15
+ # This must be a (locally) existing archive, such as foobar.tar.xz or
16
+ # something similar.
17
+ #
18
+ # The second argument to this method, called `to`, specifies the target
19
+ # location, where this class will extract the archive into, if available.
20
+ # Some keywords and shortcuts exist for this option - for instance, TEMP
21
+ # means $MY_TEMP, which can be set by the user.
22
+ #
23
+ # Specific usage example in pure Ruby:
24
+ #
25
+ # x = Extracter.what_to('pry-0.9.9.4.gem', '/Depot/Temp')
26
+ #
27
+ # ========================================================================= #
28
+ def initialize(
29
+ what = nil,
30
+ where_to = nil, # Where to extract into.
31
+ run_already = true
32
+ )
33
+ register_sigint
34
+ reset
35
+ if where_to.is_a? Symbol
36
+ if where_to.to_s.include? 'dont' # This also covers :dont_run_yet
37
+ where_to = nil # This is ok because we expect the user to provide the target location.
38
+ run_already = false
39
+ end
40
+ end
41
+ if debug? # Some debug-information.
42
+ e "The first argument what is: `#{what}`"
43
+ e "The second argument to is: `#{to}`"
44
+ end
45
+ set_source_location(what)
46
+ set_extract_to(where_to)
47
+ # ======================================================================= #
48
+ # === Handle blocks next
49
+ # ======================================================================= #
50
+ if block_given?
51
+ yielded = yield
52
+ # ===================================================================== #
53
+ # === Handle Hash input next:
54
+ # ===================================================================== #
55
+ if yielded.is_a? Hash
56
+ if yielded.has_key? :run_already
57
+ run_already = yielded.delete(:run_already)
58
+ end
59
+ # =================================================================== #
60
+ # === :use_colours
61
+ # =================================================================== #
62
+ if yielded.has_key? :use_colours
63
+ set_use_colours yielded.delete(:use_colours)
64
+ end
65
+ if yielded.has_key? :extract_to
66
+ set_extract_to yielded.delete(:extract_to)
67
+ end
68
+ if yielded.has_key? :run_simulation
69
+ set_run_simulation(yielded.delete(:run_simulation))
70
+ end
71
+ if yielded.has_key? :use_opn
72
+ set_use_opn(yielded.delete(:use_opn))
73
+ end
74
+ else
75
+ case yielded
76
+ when :dont_run_yet,
77
+ :do_not_run_yet
78
+ run_already = false
79
+ end
80
+ end
81
+ end
82
+ case run_already
83
+ when :dont_run_yet,
84
+ :do_not_run_yet
85
+ run_already = false
86
+ end
87
+ run if run_already
88
+ end
89
+
90
+ end; end
@@ -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
+ # === fail_message_not_registered
351
+ #
352
+ # Output a fail message when the archive format is not registered.
353
+ # ========================================================================= #
354
+ def fail_message_not_registered(i)
355
+ copn; e "Can not extract `#{sfancy(i)}` - it is not "\
356
+ "registered."
357
+ end
358
+
359
+ # ========================================================================= #
360
+ # === work_on_the_given_input
361
+ # ========================================================================= #
362
+ def work_on_the_given_input
363
+ if @source_location.empty?
364
+ copn; e 'Can not extract anything as no input has been given.'
365
+ else
366
+ @source_location.each {|entry|
367
+ if Extracter.is_this_a_valid_archive?(entry)
368
+ do_extract_what_to(entry)
369
+ report_to_the_user
370
+ else
371
+ fail_message_not_registered(entry)
372
+ end
373
+ }
374
+ end
375
+ end
376
+
377
+ # ========================================================================= #
378
+ # === create_directory
379
+ #
380
+ # Use this to create directories.
381
+ # ========================================================================= #
382
+ def create_directory(i)
383
+ FileUtils.mkdir_p(i)
384
+ end; alias mkdir create_directory # === mkdir
385
+
386
+ # ========================================================================= #
387
+ # === remove_file_extension
388
+ # ========================================================================= #
389
+ def remove_file_extension(i)
390
+ _ = File.basename(i)
391
+ return RemoveFileSuffix[_]
392
+ end
393
+
394
+ # ========================================================================= #
395
+ # === rds
396
+ # ========================================================================= #
397
+ def rds(i)
398
+ i.squeeze('/')
399
+ end
400
+
401
+ # ========================================================================= #
402
+ # === esystem
403
+ # ========================================================================= #
404
+ def esystem(i)
405
+ e i
406
+ system i
407
+ end
408
+
409
+ # ========================================================================= #
410
+ # === e
411
+ # ========================================================================= #
412
+ def e(i = '')
413
+ puts i
414
+ end
415
+
416
+ # ========================================================================= #
417
+ # === do_show_the_full_name_of_the_archive
418
+ # ========================================================================= #
419
+ def do_show_the_full_name_of_the_archive
420
+ @show_only_the_short_name_of_the_archive = false
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
@@ -0,0 +1,66 @@
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
+ # === reset (reset tag)
11
+ #
12
+ # Reset our main instance variables here.
13
+ # ========================================================================= #
14
+ def reset
15
+ # ======================================================================= #
16
+ # === @namespace
17
+ #
18
+ # Specify the main namespace to be used. This setting can be modified
19
+ # at "runtime".
20
+ # ======================================================================= #
21
+ @namespace = NAMESPACE
22
+ # ======================================================================= #
23
+ # === @debug
24
+ # ======================================================================= #
25
+ @debug = false
26
+ # ======================================================================= #
27
+ # === Extract to this location
28
+ #
29
+ # Next specify where to extract the archive in question, defaulting to
30
+ # the TEMP_DIR constant.
31
+ # ======================================================================= #
32
+ @extract_to_this_location = TEMP_DIR
33
+ # ======================================================================= #
34
+ # === @did_we_extract_already
35
+ #
36
+ # We will keep track of whether we already extracted or not.
37
+ # ======================================================================= #
38
+ @did_we_extract_already = false
39
+ # ======================================================================= #
40
+ # === @do_not_show_name
41
+ # ======================================================================= #
42
+ @do_not_show_name = false
43
+ # ======================================================================= #
44
+ # === @use_opn
45
+ # ======================================================================= #
46
+ @use_opn = true # ← Whether to use Opn by default or not.
47
+ # ======================================================================= #
48
+ # === @show_only_the_short_name_of_the_archive
49
+ # ======================================================================= #
50
+ @show_only_the_short_name_of_the_archive = SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE
51
+ # ======================================================================= #
52
+ # === @skip_extracting
53
+ # ======================================================================= #
54
+ @skip_extracting = false
55
+ # ======================================================================= #
56
+ # === @run_simulation
57
+ # ======================================================================= #
58
+ @run_simulation = false # ← Whether to run in simulation, or for "real".
59
+ set_extract_to_this_location
60
+ do_show_name # We will show the name usually.
61
+ enable_colours
62
+ determine_default_opn_hash
63
+ set_be_verbose(false)
64
+ end
65
+
66
+ end; end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'extracter/version/version.rb'
6
+ # =========================================================================== #
7
+ module Extracter
8
+
9
+ class Extracter
10
+
11
+ # ========================================================================= #
12
+ # === Extracter::VERSION
13
+ #
14
+ # Which specific version to use for class Extracter.
15
+ # ========================================================================= #
16
+ VERSION = '1.1.1'
17
+
18
+ end; end
data/lib/extracter.rb ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'extracter/extracter.rb'
6
+ require 'extracter/extract_it/extract_it.rb'
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ if __FILE__ == $PROGRAM_NAME
6
+ require 'fileutils'
7
+ require 'extracter'
8
+ require 'colours/colours_e_autoinclude.rb'
9
+ extract_to = '/Depot/Temp/'
10
+ e 'Now testing class Extracter - some will fail, do not worry:'
11
+ e
12
+ e Colours.orange('1) Testing Extracter.is_this_a_valid_archive? functionality:')
13
+ e
14
+ e Extracter.is_this_a_valid_archive? 'foo.php'
15
+ # Extracter.new(ARGV)
16
+ FileUtils.cp('/Users/x/SRC/recode/recode-3.7.tar.xz','/Depot/j')
17
+ Extracter.what_to '/Depot/j/recode-3.7.tar.xzg', '/Depot/jj' # This line is deliberately wrong.
18
+ Extracter.what_to '/Depot/j/recode-3.7.tar.xz', '/Depot/jj'
19
+ Extracter.what_to '/Depot/j/recode-3.7.gzip', '/Depot/jj'
20
+ e Colours.orange("2) Next testing "+sfancy("Extracter['/Users/x/SRC/htop/']"))
21
+ Extracter['/Users/x/SRC/htop/']
22
+ e Colours.orange("3) Next testing "+sfancy("Extracter['/Users/x/SRC/libzip/']"))
23
+ Extracter['/Users/x/SRC/libzip/', to: extract_to]
24
+ end # testextracter