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,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
@@ -0,0 +1,236 @@
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 # === Roebe::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
+ .xz
48
+ .zip
49
+ .tar
50
+ .bz2
51
+ .7z
52
+ )
53
+
54
+ # ========================================================================= #
55
+ # === initialize
56
+ # ========================================================================= #
57
+ def initialize(
58
+ optional_set_input = nil,
59
+ run_already = true
60
+ )
61
+ reset
62
+ set_input(optional_set_input)
63
+ run if run_already
64
+ end
65
+
66
+ # ========================================================================= #
67
+ # === reset (reset tag)
68
+ # ========================================================================= #
69
+ def reset
70
+ @debug = false
71
+ end
72
+
73
+ # ========================================================================= #
74
+ # === set_input
75
+ #
76
+ # We will work on an Array as value to @input, at the end of this
77
+ # method.
78
+ # ========================================================================= #
79
+ def set_input(i = N)
80
+ case i
81
+ # ======================================================================= #
82
+ # === extract_it --help
83
+ # ======================================================================= #
84
+ when /-?-?help$/i # Show some help stuff here.
85
+ e 'We will show a little bit help, then exit.'
86
+ e
87
+ e 'To extract .tar.xz, do:'
88
+ e
89
+ efancy ' -> tar -xJf *.tar.xz'
90
+ exit
91
+ end
92
+ i = [i] if i.is_a? String # Need an Array.
93
+ if @debug
94
+ opn; e 'The input given to us is: `'+sfile(i)+'`'
95
+ end
96
+ if i.is_a?(Array) and i.empty?
97
+ # ===================================================================== #
98
+ # In this case, try to see if the current directory has a .zip
99
+ # file. We will use this in that case.
100
+ # ===================================================================== #
101
+ is_there_a_zip_file = Dir['*.zip']
102
+ unless is_there_a_zip_file.empty?
103
+ use_this_zip_file = is_there_a_zip_file.first
104
+ notify_the_user_that_no_input_was_given_but_this_file_was_found(use_this_zip_file)
105
+ i << use_this_zip_file
106
+ end
107
+ is_there_at_the_least_one_tar_xz_file = Dir['*.tar.xz']
108
+ unless is_there_at_the_least_one_tar_xz_file.empty?
109
+ i << is_there_at_the_least_one_tar_xz_file.first
110
+ end
111
+ end
112
+ @input = i # Should be an array, always.
113
+ end
114
+
115
+ # ========================================================================= #
116
+ # === be_silent
117
+ # ========================================================================= #
118
+ def be_silent
119
+ @be_silent = true
120
+ end
121
+
122
+ # ========================================================================= #
123
+ # === be_verbose
124
+ # ========================================================================= #
125
+ def be_verbose
126
+ @be_silent = false
127
+ end; alias show_commands_used be_verbose # === show_commands_used
128
+
129
+ # ========================================================================= #
130
+ # === notify_the_user_that_no_input_was_given_but_this_file_was_found
131
+ # ========================================================================= #
132
+ def notify_the_user_that_no_input_was_given_but_this_file_was_found(
133
+ this_file
134
+ )
135
+ opn; e 'No input was given to '+sfancy(NAMESPACE)+' but a .zip '\
136
+ 'file was'
137
+ opn; e 'found in this directory ('+sdir(Dir.pwd)+'): '+
138
+ sfancy(this_file)
139
+ opn; e 'We will use this zip file.'
140
+ end
141
+
142
+ # ========================================================================= #
143
+ # === extract_input
144
+ # ========================================================================= #
145
+ def extract_input
146
+ pp @input if @debug
147
+ @input.each {|entry|
148
+ to_this_dir = Dir.pwd
149
+ to_this_dir << '/' unless to_this_dir.end_with? '/'
150
+ unless File.exist? entry
151
+ entry = try_to_glob_on(entry)
152
+ end
153
+ # ===================================================================== #
154
+ # Delegate to class Extracter next.
155
+ # ===================================================================== #
156
+ Extracter.extract_what_to(
157
+ entry,
158
+ to_this_dir,
159
+ @be_silent
160
+ )
161
+ _ = RemoveFileSuffix[entry]
162
+ if File.exist? entry
163
+ # =================================================================== #
164
+ # Must also check whether the extracted directory exists.
165
+ # =================================================================== #
166
+ name_of_the_extracted_archive = to_this_dir+_
167
+ ARRAY_ARCHIVE_TYPES.each {|extension_name|
168
+ if name_of_the_extracted_archive.include? extension_name
169
+ quoted = Regexp.quote(extension_name)
170
+ name_of_the_extracted_archive.sub!(/#{quoted}$/,'')
171
+ end
172
+ }
173
+ if File.exist?(name_of_the_extracted_archive) and
174
+ # ================================================================= #
175
+ # The following check ensures that we really have another name
176
+ # for the extracted directory.
177
+ # ================================================================= #
178
+ !(entry == name_of_the_extracted_archive)
179
+ opn; e "Finished extracting #{sfile(_)} to `#{sdir(to_this_dir)}`."
180
+ else
181
+ opn; e "No file called `#{sfile(name_of_the_extracted_archive)}"\
182
+ "` appears to exist."
183
+ end
184
+ end
185
+ }
186
+ end
187
+
188
+ # ========================================================================= #
189
+ # === is_archive?
190
+ # ========================================================================= #
191
+ def is_archive?(i)
192
+ ARRAY_ARCHIVE_TYPES.include?(
193
+ File.extname(File.basename(i))
194
+ )
195
+ end
196
+
197
+ # ========================================================================= #
198
+ # === try_to_glob_on
199
+ # ========================================================================= #
200
+ def try_to_glob_on(i)
201
+ result = Dir["#{i}*"]
202
+ # ======================================================================= #
203
+ # Next, sort this result to put archives on the beginning of the Array.
204
+ # ======================================================================= #
205
+ result = result.partition {|entry| is_archive?(entry) }
206
+ result.flatten!
207
+ unless result.empty?
208
+ # ===================================================================== #
209
+ # Ok, we grab the first entry next.
210
+ # ===================================================================== #
211
+ i = result.first
212
+ opn; e "No result could be found for the given input, thus "\
213
+ "using #{sfancy(i)} instead."
214
+ end
215
+ i
216
+ end
217
+
218
+ # ========================================================================= #
219
+ # === run (run tag)
220
+ # ========================================================================= #
221
+ def run
222
+ extract_input
223
+ end
224
+
225
+ # ========================================================================= #
226
+ # === Extracter::ExtractIt[]
227
+ # ========================================================================= #
228
+ def self.[](i = '')
229
+ new(i)
230
+ end
231
+
232
+ end; end
233
+
234
+ if __FILE__ == $PROGRAM_NAME
235
+ Extracter::ExtractIt.new(ARGV)
236
+ 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,34 @@
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 --lzip -xvf zutils-1.5.tar.lz # for .tar.lz'
24
+ opnn; e ' unsquashfs foobar-1.2.3.sxz # for .sxz'
25
+ e
26
+ opnn; e 'Furthermore, there are some commandline options '\
27
+ 'that can be used for this class (class Extracter).'
28
+ e
29
+ opnn; e ' --to=/home/Temp # extract into the '\
30
+ 'directory /home/Temp/'
31
+ e
32
+ end
33
+
34
+ end; end