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,237 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Extracter::ExtractIt
6
+ #
7
+ # If no input was provided to this class, but a .zip file exists in
8
+ # the current working directory, then that .zip file will be used.
9
+ #
10
+ # Usage example:
11
+ #
12
+ # Extracter::ExtractIt.new(ARGV)
13
+ #
14
+ # =========================================================================== #
15
+ # require 'extracter/extract_it/extract_it.rb'
16
+ # =========================================================================== #
17
+ module Extracter
18
+
19
+ class ExtractIt # === Extracter::ExtractIt
20
+
21
+ begin
22
+ require 'opn'
23
+ rescue LoadError; end
24
+
25
+ begin
26
+ require 'remove_file_suffix'
27
+ rescue LoadError; end
28
+
29
+ begin
30
+ require 'colours'
31
+ include Colours if Object.const_defined? :Colours
32
+ rescue LoadError; end
33
+
34
+ require 'extracter/extracter.rb'
35
+
36
+ # ========================================================================= #
37
+ # === NAMESPACE
38
+ # ========================================================================= #
39
+ NAMESPACE = inspect
40
+
41
+ # ========================================================================= #
42
+ # === ExtractIt::ARRAY_ARCHIVE_TYPES
43
+ #
44
+ # Register the available (and handled) archive types here.
45
+ # ========================================================================= #
46
+ ARRAY_ARCHIVE_TYPES = %w(
47
+ .apkg
48
+ .xz
49
+ .zip
50
+ .tar
51
+ .bz2
52
+ .7z
53
+ )
54
+
55
+ # ========================================================================= #
56
+ # === initialize
57
+ # ========================================================================= #
58
+ def initialize(
59
+ optional_set_input = nil,
60
+ run_already = true
61
+ )
62
+ reset
63
+ set_input(optional_set_input)
64
+ run if run_already
65
+ end
66
+
67
+ # ========================================================================= #
68
+ # === reset (reset tag)
69
+ # ========================================================================= #
70
+ def reset
71
+ @debug = false
72
+ end
73
+
74
+ # ========================================================================= #
75
+ # === set_input
76
+ #
77
+ # We will work on an Array as value to @input, at the end of this
78
+ # method.
79
+ # ========================================================================= #
80
+ def set_input(i = N)
81
+ case i
82
+ # ======================================================================= #
83
+ # === extract_it --help
84
+ # ======================================================================= #
85
+ when /-?-?help$/i # Show some help stuff here.
86
+ e 'We will show a little bit help, then exit.'
87
+ e
88
+ e 'To extract .tar.xz, do:'
89
+ e
90
+ efancy ' -> tar -xJf *.tar.xz'
91
+ exit
92
+ end
93
+ i = [i] if i.is_a? String # Need an Array.
94
+ if @debug
95
+ opn; e 'The input given to us is: `'+sfile(i)+'`'
96
+ end
97
+ if i.is_a?(Array) and i.empty?
98
+ # ===================================================================== #
99
+ # In this case, try to see if the current directory has a .zip
100
+ # file. We will use this in that case.
101
+ # ===================================================================== #
102
+ is_there_a_zip_file = Dir['*.zip']
103
+ unless is_there_a_zip_file.empty?
104
+ use_this_zip_file = is_there_a_zip_file.first
105
+ notify_the_user_that_no_input_was_given_but_this_file_was_found(use_this_zip_file)
106
+ i << use_this_zip_file
107
+ end
108
+ is_there_at_the_least_one_tar_xz_file = Dir['*.tar.xz']
109
+ unless is_there_at_the_least_one_tar_xz_file.empty?
110
+ i << is_there_at_the_least_one_tar_xz_file.first
111
+ end
112
+ end
113
+ @input = i # Should be an array, always.
114
+ end
115
+
116
+ # ========================================================================= #
117
+ # === be_silent
118
+ # ========================================================================= #
119
+ def be_silent
120
+ @be_silent = true
121
+ end
122
+
123
+ # ========================================================================= #
124
+ # === be_verbose
125
+ # ========================================================================= #
126
+ def be_verbose
127
+ @be_silent = false
128
+ end; alias show_commands_used be_verbose # === show_commands_used
129
+
130
+ # ========================================================================= #
131
+ # === notify_the_user_that_no_input_was_given_but_this_file_was_found
132
+ # ========================================================================= #
133
+ def notify_the_user_that_no_input_was_given_but_this_file_was_found(
134
+ this_file
135
+ )
136
+ opn; e 'No input was given to '+sfancy(NAMESPACE)+' but a '\
137
+ '.zip file was'
138
+ opn; e 'found in this directory ('+sdir(Dir.pwd)+'): '+
139
+ sfancy(this_file)
140
+ opn; e 'We will use this zip file.'
141
+ end
142
+
143
+ # ========================================================================= #
144
+ # === extract_input
145
+ # ========================================================================= #
146
+ def extract_input
147
+ pp @input if @debug
148
+ @input.each {|entry|
149
+ to_this_dir = Dir.pwd
150
+ to_this_dir << '/' unless to_this_dir.end_with? '/'
151
+ unless File.exist? entry
152
+ entry = try_to_glob_on(entry)
153
+ end
154
+ # ===================================================================== #
155
+ # Delegate to class Extracter next.
156
+ # ===================================================================== #
157
+ Extracter.extract_what_to(
158
+ entry,
159
+ to_this_dir,
160
+ @be_silent
161
+ )
162
+ _ = RemoveFileSuffix[entry]
163
+ if File.exist? entry
164
+ # =================================================================== #
165
+ # Must also check whether the extracted directory exists.
166
+ # =================================================================== #
167
+ name_of_the_extracted_archive = to_this_dir+_
168
+ ARRAY_ARCHIVE_TYPES.each {|extension_name|
169
+ if name_of_the_extracted_archive.include? extension_name
170
+ quoted = Regexp.quote(extension_name)
171
+ name_of_the_extracted_archive.sub!(/#{quoted}$/,'')
172
+ end
173
+ }
174
+ if File.exist?(name_of_the_extracted_archive) and
175
+ # ================================================================= #
176
+ # The following check ensures that we really have another name
177
+ # for the extracted directory.
178
+ # ================================================================= #
179
+ !(entry == name_of_the_extracted_archive)
180
+ opn; e "Finished extracting #{sfile(_)} to `#{sdir(to_this_dir)}`."
181
+ else
182
+ opn; e "No file called `#{sfile(name_of_the_extracted_archive)}"\
183
+ "` appears to exist."
184
+ end
185
+ end
186
+ }
187
+ end
188
+
189
+ # ========================================================================= #
190
+ # === is_archive?
191
+ # ========================================================================= #
192
+ def is_archive?(i)
193
+ ARRAY_ARCHIVE_TYPES.include?(
194
+ File.extname(File.basename(i))
195
+ )
196
+ end
197
+
198
+ # ========================================================================= #
199
+ # === try_to_glob_on
200
+ # ========================================================================= #
201
+ def try_to_glob_on(i)
202
+ result = Dir["#{i}*"]
203
+ # ======================================================================= #
204
+ # Next, sort this result to put archives on the beginning of the Array.
205
+ # ======================================================================= #
206
+ result = result.partition {|entry| is_archive?(entry) }
207
+ result.flatten!
208
+ unless result.empty?
209
+ # ===================================================================== #
210
+ # Ok, we grab the first entry next.
211
+ # ===================================================================== #
212
+ i = result.first
213
+ opn; e "No result could be found for the given input, thus "\
214
+ "using #{sfancy(i)} instead."
215
+ end
216
+ i
217
+ end
218
+
219
+ # ========================================================================= #
220
+ # === run (run tag)
221
+ # ========================================================================= #
222
+ def run
223
+ extract_input
224
+ end
225
+
226
+ # ========================================================================= #
227
+ # === Extracter::ExtractIt[]
228
+ # ========================================================================= #
229
+ def self.[](i = '')
230
+ new(i)
231
+ end
232
+
233
+ end; end
234
+
235
+ if __FILE__ == $PROGRAM_NAME
236
+ Extracter::ExtractIt.new(ARGV)
237
+ end # extract_it.rb
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Extracter
6
+ #
7
+ # The purpose of this class is to abstract extracting source files to
8
+ # a target location. You just pass the argument of the file that has
9
+ # to be extracted to this class, and it should handle the rest.
10
+ #
11
+ # We can also "extract" Audio files, by calling the module ExtractAudio.
12
+ # You can try this - just pass a .mp4 file as first argument to
13
+ # this class.
14
+ #
15
+ # Usage examples for class Extracter:
16
+ #
17
+ # Extracter.new('foo.mp3')
18
+ # Extracter.new('xyz-1.0.tar.gz')
19
+ #
20
+ # Usage example:
21
+ #
22
+ # Extracter.new
23
+ #
24
+ # =========================================================================== #
25
+ # require 'extracter/extracter.rb'
26
+ # =========================================================================== #
27
+
28
+ # =========================================================================== #
29
+ # Next, load up .rb files that belong to extracter/.
30
+ # =========================================================================== #
31
+ require 'extracter/version/version.rb'
32
+ require 'extracter/constants/constants.rb'
33
+ require 'extracter/class_methods.rb'
34
+ require 'extracter/colours.rb'
35
+ require 'extracter/do_extract_what_to.rb'
36
+ require 'extracter/help.rb'
37
+ require 'extracter/initialize.rb'
38
+ require 'extracter/misc.rb'
39
+ require 'extracter/reset.rb'
40
+ require 'extracter/opn.rb'
41
+
42
+ module Extracter
43
+
44
+ class Extracter # === Extracter
45
+ end
46
+
47
+ end
48
+
49
+ if __FILE__ == $PROGRAM_NAME
50
+ # The second argument to Extracter.new is where we will
51
+ # extract to.
52
+ if ARGV.size > 2
53
+ _ = Extracter::Extracter.new(ARGV, :default, false)
54
+ else
55
+ _ = Extracter::Extracter.new(ARGV[0], ARGV[1], false)
56
+ end
57
+ _.extract_to Dir.pwd unless ARGV[1]
58
+ # _.enable_debug
59
+ _.be_verbose
60
+ _.run
61
+ end # extracter
62
+ # extracter htop-1.0.2.tar.xz
63
+ # extracter xfig-3.2.5.tar.bz2
64
+ # extract htop* /Depot/
65
+ # extract recode-3.7.tar.xz /Depot/
66
+ # extract qt-4.8.6.tar.xz --to=/home/Temp
@@ -0,0 +1,35 @@
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
+ # === show_help (help tag)
11
+ #
12
+ # This method will show the available - and documented - help options
13
+ # for class Extracter.
14
+ # ========================================================================= #
15
+ def show_help
16
+ e
17
+ opnn; e 'How to extract archives, without helper scripts?'
18
+ e
19
+ opnn; e ' tar -zxvf foobar.tar.gz # for .tar.gz'
20
+ opnn; e ' tar xvzf foobar.tgz # for .tgz'
21
+ opnn; e ' tar xvfJ foobar.tar.xz # for .tar.xz'
22
+ opnn; e ' tar jxf foobar.tar.bz2 # for .tar.bz2'
23
+ opnn; e ' tar -xf foobar.tar.bz2 # for .tbz'
24
+ opnn; e ' tar --lzip -xvf zutils-1.5.tar.lz # for .tar.lz'
25
+ opnn; e ' unsquashfs foobar-1.2.3.sxz # for .sxz'
26
+ e
27
+ opnn; e 'Furthermore, there are some commandline options '\
28
+ 'that can be used for this class (class Extracter).'
29
+ e
30
+ opnn; e ' --to=/home/Temp # extract into the '\
31
+ 'directory /home/Temp/'
32
+ e
33
+ end
34
+
35
+ end; end
@@ -0,0 +1,114 @@
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
+ &block
33
+ )
34
+ register_sigint
35
+ reset
36
+ if where_to.is_a? Symbol
37
+ if where_to.to_s.include? 'dont' # This also covers :dont_run_yet
38
+ where_to = nil # This is ok because we expect the user to provide the target location.
39
+ run_already = false
40
+ end
41
+ end
42
+ if debug? # Some debug-information.
43
+ e "The first argument what is: `#{what}`"
44
+ e "The second argument to is: `#{to}`"
45
+ end
46
+ set_source_location(what)
47
+ set_extract_to(where_to)
48
+ # ======================================================================= #
49
+ # === Handle blocks next
50
+ # ======================================================================= #
51
+ if block_given?
52
+ yielded = yield
53
+ # ===================================================================== #
54
+ # === Handle Hash input next:
55
+ # ===================================================================== #
56
+ if yielded.is_a? Hash
57
+ if yielded.has_key? :run_already
58
+ run_already = yielded.delete(:run_already)
59
+ end
60
+ # =================================================================== #
61
+ # === :use_colours
62
+ # =================================================================== #
63
+ if yielded.has_key? :use_colours
64
+ set_use_colours yielded.delete(:use_colours)
65
+ end
66
+ # =================================================================== #
67
+ # === :extract_to
68
+ # =================================================================== #
69
+ if yielded.has_key? :extract_to
70
+ set_extract_to yielded.delete(:extract_to)
71
+ end
72
+ # =================================================================== #
73
+ # === :run_simulation
74
+ # =================================================================== #
75
+ if yielded.has_key? :run_simulation
76
+ set_run_simulation(yielded.delete(:run_simulation))
77
+ end
78
+ # =================================================================== #
79
+ # === :use_opn
80
+ # =================================================================== #
81
+ if yielded.has_key? :use_opn
82
+ set_use_opn(yielded.delete(:use_opn))
83
+ end
84
+ else
85
+ case yielded
86
+ when :dont_run_yet,
87
+ :do_not_run_yet
88
+ run_already = false
89
+ end
90
+ end
91
+ end
92
+ case run_already
93
+ when :dont_run_yet,
94
+ :do_not_run_yet
95
+ run_already = false
96
+ end
97
+ run if run_already
98
+ end
99
+
100
+ end
101
+
102
+ # =========================================================================== #
103
+ # === Extracter.new
104
+ # =========================================================================== #
105
+ def self.new(
106
+ what = nil,
107
+ where_to = nil, # Where to extract into.
108
+ run_already = true,
109
+ &block
110
+ )
111
+ ::Extracter::Extracter.new(what, where_to, run_already, &block)
112
+ end
113
+
114
+ end