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,95 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ class Extracter
6
+
7
+ # ========================================================================= #
8
+ # === NAMESPACE
9
+ # ========================================================================= #
10
+ NAMESPACE = inspect
11
+
12
+ # ========================================================================= #
13
+ # === N
14
+ # ========================================================================= #
15
+ N = "\n"
16
+
17
+ # ========================================================================= #
18
+ # === LAST_UPDATED
19
+ #
20
+ # When this class was last updated/releasted. This is not a hugely
21
+ # important constat, so do not worry too much if this may be
22
+ # heavily outdated eventually.
23
+ # ========================================================================= #
24
+ LAST_UPDATED = '14 Jun 2020'
25
+
26
+ # ========================================================================= #
27
+ # === TEMP_DIR
28
+ #
29
+ # Set the temp dir here.
30
+ # ========================================================================= #
31
+ if ENV['MY_TEMP']
32
+ TEMP_DIR = ENV['MY_TEMP'].to_s+'/'
33
+ else
34
+ # ======================================================================= #
35
+ # If this environment variable is unavailable then use a conservative
36
+ # default value.
37
+ # ======================================================================= #
38
+ TEMP_DIR = '/tmp/'
39
+ end
40
+
41
+ # ========================================================================= #
42
+ # === SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE
43
+ #
44
+ # If this constant is set to true then we will only show the shortened
45
+ # name of the archive in question by default.
46
+ # ========================================================================= #
47
+ SHOW_ONLY_THE_SHORT_NAME_OF_THE_ARCHIVE = true
48
+
49
+ # ========================================================================= #
50
+ # === GEM_UNPACK_COMMAND
51
+ #
52
+ # The command to use to unpack ruby .gems.
53
+ # We can pass the ---target=DIR syntax to extract to a specific location.
54
+ # ========================================================================= #
55
+ GEM_UNPACK_COMMAND = 'gem unpack'
56
+
57
+ # ========================================================================= #
58
+ # === ARRAY_REGISTERED_ARCHIVES
59
+ #
60
+ # Archives that can be extracted, have to be registered in this Array.
61
+ #
62
+ # Sort alphabetically.
63
+ #
64
+ # The libreoffice format .odt is just like .zip.
65
+ # ========================================================================= #
66
+ ARRAY_REGISTERED_ARCHIVES = %w(
67
+ 7z
68
+ bin
69
+ bz2
70
+ deb
71
+ gem
72
+ gz
73
+ img
74
+ iso
75
+ jar
76
+ lz
77
+ lzma
78
+ odt
79
+ mp4
80
+ rar
81
+ rpm
82
+ squashfs
83
+ sxz
84
+ tar
85
+ taz
86
+ tgz
87
+ txz
88
+ xpi
89
+ xz
90
+ zip
91
+ Z
92
+ zst
93
+ )
94
+
95
+ end
@@ -0,0 +1,288 @@
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
+ class Extracter
8
+
9
+ # ========================================================================= #
10
+ # === do_extract_what_to
11
+ #
12
+ # This method should only be called from the method
13
+ # work_on_the_given_input().
14
+ #
15
+ # It will attempt to extract the specified archive.
16
+ # ========================================================================= #
17
+ def do_extract_what_to(
18
+ what, # <- The archive or file that we wish to extract.
19
+ to = extract_to?
20
+ )
21
+ name_of_the_archive = what.dup
22
+ @did_we_extract_already = false
23
+ to = extract_to? if to.nil?
24
+ set_source_location(what)
25
+ set_extract_to_this_location(to) # Keep it in sync.
26
+ # ======================================================================= #
27
+ # Create the directory if it does not yet exist.
28
+ # ======================================================================= #
29
+ create_directory(to) if ! File.directory?(to)
30
+ if be_verbose?
31
+ if @show_only_the_short_name_of_the_archive
32
+ name_of_the_archive = File.basename(name_of_the_archive)
33
+ copn; e "Extracting `#{sfancy(name_of_the_archive)}` to `#{sdir(to)}` next."
34
+ else
35
+ copn; e "Extracting `#{sfancy(name_of_the_archive)}` to `#{sdir(to)}` next."
36
+ end
37
+ end
38
+ _ = ''.dup # Default.
39
+ extname = File.extname(what)
40
+ unless ARRAY_REGISTERED_ARCHIVES.include? extname.delete('.')
41
+ opn; e 'We did not register the following extension: '+extname.delete('.')
42
+ fail_message_not_registered(extname)
43
+ @skip_extracting = true
44
+ end
45
+ case extname # Case tag. Those listed on top are more important.
46
+ # ======================================================================= #
47
+ # === img
48
+ #
49
+ # Note that .img in this context refers to squafhs .img files.
50
+ # ======================================================================= #
51
+ when '.img',
52
+ '.squashfs'
53
+ opnn; e 'Handling a squashfs .img file format next:'
54
+ name_without_extension = what.sub(/#{File.extname(what)}$/,'')
55
+ mkdir(name_without_extension) unless File.directory? name_without_extension
56
+ esystem 'mount -o loop -t squashfs '+what+' '+name_without_extension
57
+ e 'The content of the extracted (or rather, mounted) archive is:'
58
+ pp Dir["#{name_without_extension}*"]
59
+ return
60
+ # ======================================================================= #
61
+ # === iso
62
+ # ======================================================================= #
63
+ when '.iso'
64
+ opnn; e 'Extracting an .iso file is a bit more complicated '\
65
+ 'than a .tar.gz tarball release.'
66
+ opnn; e 'We will first create a directory; and then mount '\
67
+ 'the .iso there.'
68
+ name_without_extension = what.sub(/#{File.extname(what)}$/,'')
69
+ mkdir(name_without_extension) unless File.directory? name_without_extension
70
+ esystem 'mount -o loop '+what+' '+name_without_extension
71
+ e 'The content of the extracted (or rather, mounted) archive is:'
72
+ pp Dir[name_without_extension+'*']
73
+ return
74
+ # ======================================================================= #
75
+ # === sxz
76
+ # ======================================================================= #
77
+ when '.sxz'
78
+ _ = 'unsquashfs '.dup # This requires squashfs with xz-support.
79
+ # ======================================================================= #
80
+ # === lz
81
+ # ======================================================================= #
82
+ when '.lz','.tar.lz'
83
+ _ = 'tar --lzip -xvf '.dup # This requires lzip to be installed.
84
+ # ======================================================================= #
85
+ # === tar.Z
86
+ # ======================================================================= #
87
+ when '.tar.Z','.taz'
88
+ _ << 'tar -xvzf'
89
+ # ======================================================================= #
90
+ # === .jar
91
+ # ======================================================================= #
92
+ when /\.?jar$/i
93
+ _ << 'jar xvf'
94
+ # ======================================================================= #
95
+ # === .tar
96
+ # ======================================================================= #
97
+ when '.tar','.tar.bz2'
98
+ _ << 'tar -xvf'
99
+ # ======================================================================= #
100
+ # === .zst
101
+ #
102
+ # This entry point is for e. g. "pymol-2.3.0-3-x86_64.pkg.tar.zst".
103
+ # ======================================================================= #
104
+ when '.zst','.tar.zst'
105
+ _ << 'tar -I zstd -xvf '
106
+ # ======================================================================= #
107
+ # === txz
108
+ # ======================================================================= #
109
+ when '.txz'
110
+ _ << 'tar Jxvf' # Since Jan 2012.
111
+ # ======================================================================= #
112
+ # === gz
113
+ # ======================================================================= #
114
+ when '.gz'
115
+ if what.include? '.tar'
116
+ _ << 'tar -zxvf'
117
+ else
118
+ _ << 'gunzip'
119
+ end
120
+ # ======================================================================= #
121
+ # === xz
122
+ # ======================================================================= #
123
+ when '.xz'
124
+ if _.include? '.tar'
125
+ end
126
+ _ << 'tar -xvf' # tar -Jxv #{what} would be an alternative.
127
+ # ======================================================================= #
128
+ # === rpm
129
+ # ======================================================================= #
130
+ when '.rpm'
131
+ _ << 'bsdtar xfv'
132
+ # ======================================================================= #
133
+ # === bin
134
+ # ======================================================================= #
135
+ when '.bin' # handle .bin files here.
136
+ # _ = 'tar -jxf '+package
137
+ _ << "./#{what}"
138
+ # ======================================================================= #
139
+ # === zip
140
+ # ======================================================================= #
141
+ when '.zip','.xpi','.docx','.odt' # .docx and .odt format types can be unpacked with zip too.
142
+ # _ << 'ar -jxf' # unzip #{what} <-- this should work as well.
143
+ _ << 'unzip '
144
+ when /\.bz2/,'.tbz2'
145
+ if what.include? '.tar' # Treat it as a .tar file.
146
+ _ << 'tar -vjxf '
147
+ else
148
+ _ << 'bunzip2 '
149
+ end
150
+ # ======================================================================= #
151
+ # === lzma
152
+ # ======================================================================= #
153
+ when '.lzma'
154
+ _ << 'unlzma '
155
+ # ======================================================================= #
156
+ # === 7z
157
+ # ======================================================================= #
158
+ when '.7z' # 7z does not accept the -C commandline.
159
+ # _ << '7za e' # <- Deprecated as of 05.06.2020.
160
+ _ << '7z x'
161
+ # ======================================================================= #
162
+ # === gem
163
+ # ======================================================================= #
164
+ when '.gem'
165
+ _ << GEM_UNPACK_COMMAND
166
+ # ======================================================================= #
167
+ # === rar
168
+ # ======================================================================= #
169
+ when '.rar'
170
+ check_whether_rar_is_available
171
+ _ << 'unrar e'
172
+ # ======================================================================= #
173
+ # === deb
174
+ # ======================================================================= #
175
+ when '.deb'
176
+ #_ = 'dpkg-deb -x' # {to}
177
+ _ << 'ar -x' # ar -x #{what} This could work too.
178
+ # ======================================================================= #
179
+ # === tgz
180
+ # ======================================================================= #
181
+ when '.tgz'
182
+ _ << 'tar -xvzf'
183
+ # ======================================================================= #
184
+ # === ps
185
+ # ======================================================================= #
186
+ when '.ps'
187
+ _ << 'ps2ascii'
188
+ # ======================================================================= #
189
+ # === mp4
190
+ # ======================================================================= #
191
+ when '.mp4' # If it is a .mp4 file, we delegate to ExtractAudio instead.
192
+ if Object.const_defined? :MultimediaParadise
193
+ MultimediaParadise.extract_audio(@source_package_location)
194
+ end
195
+ exit
196
+ else # else tag. We did not find the extension type.
197
+ @skip_extracting = true
198
+ copn; ewarn "We did not find: `#{sfile(what)}`. "
199
+ copn; ewarn 'The file-type (extension) was: `'+simp(extname)+'`.'
200
+ # Try to rescue though.
201
+ result = run_this_system_command("file #{what}")
202
+ copn; e result
203
+ if result.include? 'bzip2 compressed'
204
+ copn; e 'We assume it is a .bz2 file though.'
205
+ _ = 'tar -vjxf '.dup
206
+ @skip_extracting = false
207
+ end
208
+ end unless @skip_extracting
209
+ unless File.exist? what
210
+ @skip_extracting = true
211
+ copn; e "The file `#{sfile(what)}"\
212
+ "` does not exist - can not extract."
213
+ end
214
+ # ======================================================================= #
215
+ # Handle the situation when the given input includes a ')' character.
216
+ # We will pad such an input with '"' characters.
217
+ # ======================================================================= #
218
+ if what.include? ')'
219
+ what = pad(what, '"') #sanitize_input(what)
220
+ end
221
+ # ======================================================================= #
222
+ # Next, pad it if it includes a ' ' character.
223
+ # ======================================================================= #
224
+ what = pad(what) if what.include?(' ')
225
+ _ << " #{what}" unless _.empty?
226
+ if _.include? GEM_UNPACK_COMMAND # Gem command needs a --target=DIR option.
227
+ _ << " --target=#{to}"
228
+ elsif _.include?('ar -x') and ! _.include?('.tar') # Do nothing in this case.
229
+ elsif _.end_with? '.sxz'
230
+ # .sxz does not require the -C option.
231
+ elsif _.end_with? '.zip','.xpi','.7z','.jar'
232
+ # Do not use -C option for 7z, as it hates this option.
233
+ # .jar files also do not support the -C option.
234
+ elsif _.include? 'bunzip'
235
+ # Do not use -C option for bunzip, as it hates this option.
236
+ else
237
+ # ===================================================================== #
238
+ # Next, we need to determine the location where we extract our
239
+ # archive to.
240
+ # ===================================================================== #
241
+ _ << ' '
242
+ # ===================================================================== #
243
+ # Add -C option except for .deb packages and gunzip-based archives.
244
+ # ===================================================================== #
245
+ unless _ =~ /deb$/ or _.include?('gunzip')
246
+ _ << '-C '
247
+ _ << to
248
+ end
249
+ end
250
+ if run_simulation?
251
+ copn; e 'As we are running in simulation mode, the following command '
252
+ copn; e 'is the one that we would have been used if we were to not run '
253
+ copn; e 'in simulation mode:'
254
+ e _
255
+ else # Ok, here we are not in a simulation, hence we can run the command.
256
+ unless @skip_extracting
257
+ if File.writable? to
258
+ # ================================================================= #
259
+ # Next, run the sys-command, with some padding.
260
+ # ================================================================= #
261
+ begin
262
+ run_this_system_command(
263
+ " #{_}", :also_show_what_we_will_do
264
+ )
265
+ # ================================================================= #
266
+ # We have to rescue because unrar might not be available and so on.
267
+ # ================================================================= #
268
+ rescue Exception => error
269
+ e 'An error has happened upon attempting to run this system command:'
270
+ e
271
+ e " #{_}"
272
+ e
273
+ pp error
274
+ e '-----------'
275
+ pp error.class
276
+ e '-----------'
277
+ end
278
+ @did_we_extract_already = true
279
+ else
280
+ copn; ewarn 'You do not have sufficient permissions to'
281
+ copn; ewarn "modify #{sdir(to)}."
282
+ end
283
+ end
284
+ end
285
+ end; alias do_extract_to do_extract_what_to # === do_extract_to
286
+ alias start do_extract_what_to # === start
287
+
288
+ end
@@ -0,0 +1,62 @@
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'
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
+ class Extracter # === Extracter
43
+ end
44
+
45
+ if __FILE__ == $PROGRAM_NAME
46
+ # The second argument to Extracter.new is where we will
47
+ # extract to.
48
+ if ARGV.size > 2
49
+ _ = Extracter.new(ARGV, :default, false)
50
+ else
51
+ _ = Extracter.new(ARGV[0], ARGV[1], false)
52
+ end
53
+ _.extract_to Dir.pwd unless ARGV[1]
54
+ # _.enable_debug
55
+ _.be_verbose
56
+ _.run
57
+ end # extracter
58
+ # extracter htop-1.0.2.tar.xz
59
+ # extracter xfig-3.2.5.tar.bz2
60
+ # extract htop* /Depot/
61
+ # extract recode-3.7.tar.xz /Depot/
62
+ # extract qt-4.8.6.tar.xz --to=/home/Temp
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ class Extracter
6
+
7
+ # ========================================================================= #
8
+ # === show_help (help tag)
9
+ #
10
+ # This method will show the available - and documented - help options
11
+ # for class Extracter.
12
+ # ========================================================================= #
13
+ def show_help
14
+ e
15
+ opnn; e 'How to extract archives, without helper scripts?'
16
+ e
17
+ opnn; e ' tar -zxvf foobar.tar.gz # for .tar.gz'
18
+ opnn; e ' tar xvzf foobar.tgz # for .tgz'
19
+ opnn; e ' tar xvfJ foobar.tar.xz # for .tar.xz'
20
+ opnn; e ' tar jxf foobar.tar.bz2 # for .tar.bz2'
21
+ opnn; e ' tar --lzip -xvf zutils-1.5.tar.lz # for .tar.lz'
22
+ opnn; e ' unsquashfs foobar-1.2.3.sxz # for .sxz'
23
+ e
24
+ opnn; e 'Furthermore, there are some commandline options '\
25
+ 'that can be used for this class (class Extracter).'
26
+ e
27
+ opnn; e ' --to=/home/Temp # extract into the '\
28
+ 'directory /home/Temp/'
29
+ e
30
+ end
31
+
32
+ end