repackage 1.0.30 → 1.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +118 -0
- data/bin/{repackager → repackage} +0 -0
- data/doc/README.gen +75 -0
- data/lib/repackage/class/constants.rb +93 -0
- data/lib/repackage/class/misc.rb +934 -0
- data/lib/repackage/class/repackage.rb +44 -723
- data/lib/repackage/colours/colours.rb +102 -0
- data/lib/repackage/requires/require_the_sinatra_interface.rb +1 -1
- data/lib/repackage/version/version.rb +3 -3
- data/lib/repackage/www/embeddable_interface.rb +5 -4
- data/lib/repackage.rb +1 -1
- data/repackage.gemspec +15 -24
- metadata +19 -53
@@ -2,759 +2,80 @@
|
|
2
2
|
# Encoding: UTF-8
|
3
3
|
# frozen_string_literal: true
|
4
4
|
# =========================================================================== #
|
5
|
-
# === Repackage
|
5
|
+
# === Repackage::Repackage
|
6
6
|
#
|
7
|
-
# The purpose of this class is to repackage a .tar.gz
|
8
|
-
# a .tar.bz2 file or into another format.
|
9
|
-
# are files such as "foobar-1.2.3.tar.gz" or something similar
|
10
|
-
# to this.
|
7
|
+
# The primary purpose of this class is to repackage a .tar.gz archive into
|
8
|
+
# a .tar.bz2 file or into another format.
|
11
9
|
#
|
12
|
-
#
|
13
|
-
# to
|
10
|
+
# Thus, the ideal input to this class are files such as "foobar-1.2.3.tar.gz"
|
11
|
+
# or something similar to this. These will be stored as an Array internally,
|
12
|
+
# to allow batch-processing from the get go.
|
13
|
+
#
|
14
|
+
# The default target format is to .tar.xz or more accurately, to whatever
|
15
|
+
# the value is that has been specified via the constant
|
14
16
|
# DEFAULT_TARGET_FORMAT_TYPE.
|
15
17
|
#
|
16
18
|
# Historically this class was first created at the end of August 2005,
|
17
|
-
# so it used to be a fairly "old" class.
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
19
|
+
# so it used to be a fairly "old" class. Obviously its use case was
|
20
|
+
# necessary even a long time ago as-is.
|
21
|
+
#
|
22
|
+
# Over the years the class has been extended to allow for more
|
23
|
+
# flexibility. In June 2021 it was re-written from scratch, primarily
|
24
|
+
# to make use of @internal_hash rather than a multitude of different
|
25
|
+
# instance variables; this coincided with making this functionality
|
26
|
+
# available via sinatra as well, thus allowing web-related use.
|
27
|
+
#
|
28
|
+
# In May 2022 the main class has again been rewritten. It was made
|
29
|
+
# more logical, made most of its external gems optional (including
|
30
|
+
# colour support) and more extensively documented - at the least
|
31
|
+
# compared to the pre-2022 state.
|
22
32
|
#
|
23
33
|
# Usage example:
|
24
34
|
#
|
25
|
-
# Repackage.new(ARGV)
|
35
|
+
# Repackage::Repackage.new(ARGV)
|
36
|
+
# Repackage.new(ARGV) # or this shorter variant.
|
26
37
|
#
|
27
38
|
# =========================================================================== #
|
28
39
|
# require 'repackage/class/repackage.rb'
|
29
40
|
# =========================================================================== #
|
30
|
-
|
31
|
-
|
32
|
-
require 'fileutils'
|
33
|
-
require 'repackage/version/version.rb'
|
41
|
+
module Repackage
|
34
42
|
|
35
|
-
|
43
|
+
class Repackage # === Repackage::Repackage
|
36
44
|
|
37
|
-
|
38
|
-
require 'totarxz'
|
45
|
+
alias e puts
|
39
46
|
|
40
|
-
begin
|
41
|
-
|
42
|
-
|
43
|
-
|
47
|
+
require 'extracter' # This external gem is mandatory - no need to begin/rescue it.
|
48
|
+
require 'fileutils'
|
49
|
+
require 'repackage/colours/colours.rb'
|
50
|
+
require 'repackage/class/constants.rb'
|
51
|
+
require 'repackage/class/misc.rb'
|
52
|
+
require 'repackage/version/version.rb'
|
44
53
|
|
45
54
|
begin
|
46
55
|
require 'opn'
|
47
56
|
rescue LoadError; end
|
48
57
|
|
49
|
-
begin
|
50
|
-
require 'extracter'
|
51
|
-
rescue LoadError; end
|
52
|
-
|
53
|
-
# ========================================================================= #
|
54
|
-
# === NAMESPACE
|
55
|
-
# ========================================================================= #
|
56
|
-
NAMESPACE = inspect
|
57
|
-
|
58
|
-
# ========================================================================= #
|
59
|
-
# === LAST_DOWNLOADED_FILE
|
60
|
-
# ========================================================================= #
|
61
|
-
begin
|
62
|
-
require 'wget'
|
63
|
-
LAST_DOWNLOADED_FILE = Wget.download_to?
|
64
|
-
rescue LoadError
|
65
|
-
LAST_DOWNLOADED_FILE = ENV['HOME'].to_s+'/LAST_DOWNLOADED_FILE.md'
|
66
|
-
end
|
67
|
-
|
68
|
-
# ========================================================================= #
|
69
|
-
# === DEFAULT_TARGET_FORMAT_TYPE
|
70
|
-
#
|
71
|
-
# The target format comes here.
|
72
|
-
# ========================================================================= #
|
73
|
-
DEFAULT_TARGET_FORMAT_TYPE = '.tar.xz' # '.tar.bz2'
|
74
|
-
|
75
|
-
# ========================================================================= #
|
76
|
-
# === MY_TEMP
|
77
|
-
# ========================================================================= #
|
78
|
-
if ENV['MY_TEMP']
|
79
|
-
EXTRACT_TO = ENV['MY_TEMP'].to_s+'/' # Denote where to extract the stuff to.
|
80
|
-
else
|
81
|
-
EXTRACT_TO = '/tmp/'
|
82
|
-
end
|
83
|
-
|
84
|
-
# ========================================================================= #
|
85
|
-
# === CREATE_TAR_XZ
|
86
|
-
#
|
87
|
-
# This is the command that is used when a .tar.xz file is to be created.
|
88
|
-
# ========================================================================= #
|
89
|
-
CREATE_TAR_XZ = 'tar -vcJf'
|
90
|
-
|
91
|
-
# ========================================================================= #
|
92
|
-
# === CREATE_TAR_GZ
|
93
|
-
#
|
94
|
-
# Command to create a .tar.gz archive.
|
95
|
-
# ========================================================================= #
|
96
|
-
CREATE_TAR_GZ = 'tar cfvz'
|
97
|
-
|
98
|
-
# ========================================================================= #
|
99
|
-
# === CREATE_TAR_BZ2
|
100
|
-
#
|
101
|
-
# Command to create a .tar.bz2 archive.
|
102
|
-
# ========================================================================= #
|
103
|
-
CREATE_TAR_BZ2 = 'tar cfvj'
|
104
|
-
|
105
|
-
# ========================================================================= #
|
106
|
-
# === CREATE_ZIP
|
107
|
-
#
|
108
|
-
# Command to create a .zip archive.
|
109
|
-
# ========================================================================= #
|
110
|
-
CREATE_ZIP = 'zip -r '
|
111
|
-
|
112
|
-
# ========================================================================= #
|
113
|
-
# === SHALL_WE_DELETE_THE_OLD_ARCHIVE
|
114
|
-
#
|
115
|
-
# Remove the old source if this constant is true. Since as of June 2021
|
116
|
-
# this defaults to false, except in certain conditions met on my home
|
117
|
-
# setup.
|
118
|
-
# ========================================================================= #
|
119
|
-
SHALL_WE_DELETE_THE_OLD_ARCHIVE = false
|
120
|
-
|
121
|
-
# ========================================================================= #
|
122
|
-
# === initialize
|
123
|
-
#
|
124
|
-
# The first argument given to this class must be the name of (or path
|
125
|
-
# to) a locally existing file, such as "foobar-1.0.tar.gz".
|
126
|
-
# ========================================================================= #
|
127
|
-
def initialize(
|
128
|
-
commandline_arguments = nil,
|
129
|
-
run_already = true
|
130
|
-
)
|
131
|
-
register_sigint
|
132
|
-
reset
|
133
|
-
set_commandline_arguments(
|
134
|
-
commandline_arguments
|
135
|
-
)
|
136
|
-
# ========================================================================= #
|
137
|
-
# === Handle given blocks next
|
138
|
-
# ========================================================================= #
|
139
|
-
if block_given?
|
140
|
-
yielded = yield
|
141
|
-
case yielded
|
142
|
-
# ===================================================================== #
|
143
|
-
# === :do_not_delete_the_old_source
|
144
|
-
# ===================================================================== #
|
145
|
-
when :do_not_delete_the_old_source
|
146
|
-
set_shall_we_delete_old_source(false)
|
147
|
-
# ===================================================================== #
|
148
|
-
# === :run_already
|
149
|
-
# ===================================================================== #
|
150
|
-
when :run_already
|
151
|
-
run_already = true
|
152
|
-
end
|
153
|
-
end
|
154
|
-
run if run_already
|
155
|
-
end
|
156
|
-
|
157
|
-
# ========================================================================= #
|
158
|
-
# === reset (reset tag)
|
159
|
-
# ========================================================================= #
|
160
|
-
def reset
|
161
|
-
# ========================================================================= #
|
162
|
-
# === @internal_hash
|
163
|
-
# ========================================================================= #
|
164
|
-
@internal_hash = {}
|
165
|
-
set_start_dir
|
166
|
-
# ========================================================================= #
|
167
|
-
# === :repackage_to_this_format
|
168
|
-
# ========================================================================= #
|
169
|
-
set_repackage_to_this_format(DEFAULT_TARGET_FORMAT_TYPE)
|
170
|
-
# ========================================================================= #
|
171
|
-
# === :the_new_file_is_at
|
172
|
-
#
|
173
|
-
# This entry will keep track at which new location the file can be
|
174
|
-
# found.
|
175
|
-
# ========================================================================= #
|
176
|
-
@internal_hash[:the_new_file_is_at] = nil
|
177
|
-
# ========================================================================= #
|
178
|
-
# === :file_size
|
179
|
-
# ========================================================================= #
|
180
|
-
@internal_hash[:file_size] = 0
|
181
|
-
set_shall_we_delete_the_old_archive(:default)
|
182
|
-
set_extract_to(:default)
|
183
|
-
end
|
184
|
-
|
185
|
-
# ========================================================================= #
|
186
|
-
# === determine_file_size
|
187
|
-
# ========================================================================= #
|
188
|
-
def determine_file_size(
|
189
|
-
i = filename?
|
190
|
-
)
|
191
|
-
if i and File.exist?(i)
|
192
|
-
@internal_hash[:file_size] = File.stat(i).size? # Obtain some information.
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
# ========================================================================= #
|
197
|
-
# === shall_we_delete_the_old_archive?
|
198
|
-
# ========================================================================= #
|
199
|
-
def shall_we_delete_the_old_archive?
|
200
|
-
@internal_hash[:shall_we_delete_the_old_archive]
|
201
|
-
end
|
202
|
-
|
203
|
-
# ========================================================================= #
|
204
|
-
# === set_repackage_to_this_format
|
205
|
-
#
|
206
|
-
# We will repackage to this format here.
|
207
|
-
# ========================================================================= #
|
208
|
-
def set_repackage_to_this_format(
|
209
|
-
i = DEFAULT_TARGET_FORMAT_TYPE
|
210
|
-
)
|
211
|
-
i = i.downcase if i # Only want it downcased.
|
212
|
-
case i # case tag
|
213
|
-
# ========================================================================= #
|
214
|
-
# === .tar.xz
|
215
|
-
# ========================================================================= #
|
216
|
-
when 'xz',
|
217
|
-
'tar.xz'
|
218
|
-
i = '.tar.xz'
|
219
|
-
# ========================================================================= #
|
220
|
-
# === .tar.bz2
|
221
|
-
# ========================================================================= #
|
222
|
-
when 'bz2',
|
223
|
-
'.tar.bz2'
|
224
|
-
i = '.tar.bz2'
|
225
|
-
# ========================================================================= #
|
226
|
-
# === .tar.gz
|
227
|
-
# ========================================================================= #
|
228
|
-
when 'targz',
|
229
|
-
'gz',
|
230
|
-
'tar.gz'
|
231
|
-
i = '.tar.gz'
|
232
|
-
# ========================================================================= #
|
233
|
-
# === nil
|
234
|
-
# ========================================================================= #
|
235
|
-
when nil # Assume a default here.
|
236
|
-
i = DEFAULT_TARGET_FORMAT_TYPE
|
237
|
-
else # else tag
|
238
|
-
# warn 'Did not find registered format type.'
|
239
|
-
end
|
240
|
-
@internal_hash[:repackage_to_this_format] = i
|
241
|
-
end; alias set_target_format_type set_repackage_to_this_format # === set_target_format_type
|
242
|
-
alias format= set_repackage_to_this_format # === format=
|
243
|
-
|
244
|
-
# ========================================================================= #
|
245
|
-
# === repackage_to_which_format?
|
246
|
-
# ========================================================================= #
|
247
|
-
def repackage_to_which_format?
|
248
|
-
@internal_hash[:repackage_to_this_format]
|
249
|
-
end; alias format? repackage_to_which_format? # === format?
|
250
|
-
alias target_format? repackage_to_which_format? # === target_format?
|
251
|
-
alias target_format_type? repackage_to_which_format? # === target_format_type?
|
252
|
-
|
253
|
-
# ========================================================================= #
|
254
|
-
# === return_pwd
|
255
|
-
# ========================================================================= #
|
256
|
-
def return_pwd
|
257
|
-
"#{Dir.pwd}/".squeeze('/')
|
258
|
-
end
|
259
|
-
|
260
|
-
# ========================================================================= #
|
261
|
-
# === register_sigint
|
262
|
-
# ========================================================================= #
|
263
|
-
def register_sigint
|
264
|
-
Signal.trap('SIGINT') { exit }
|
265
|
-
end
|
266
|
-
|
267
|
-
# ========================================================================= #
|
268
|
-
# === set_commandline_arguments
|
269
|
-
# ========================================================================= #
|
270
|
-
def set_commandline_arguments(i = '')
|
271
|
-
i = [i].flatten.compact
|
272
|
-
@commandline_arguments = i
|
273
|
-
end
|
274
|
-
|
275
|
-
# ========================================================================= #
|
276
|
-
# === commandline_arguments?
|
277
|
-
# ========================================================================= #
|
278
|
-
def commandline_arguments?
|
279
|
-
@commandline_arguments
|
280
|
-
end
|
281
|
-
|
282
|
-
# ========================================================================= #
|
283
|
-
# === first_argument?
|
284
|
-
# ========================================================================= #
|
285
|
-
def first_argument?
|
286
|
-
@commandline_arguments.first
|
287
|
-
end; alias first? first_argument? # === first?
|
288
|
-
|
289
|
-
# ========================================================================= #
|
290
|
-
# === determine_relevant_entries
|
291
|
-
# ========================================================================= #
|
292
|
-
def determine_relevant_entries
|
293
|
-
set_repackage_this_file(first?)
|
294
|
-
second_argument = @commandline_arguments[1]
|
295
|
-
if second_argument
|
296
|
-
# ===================================================================== #
|
297
|
-
# Let the user determine the format type:
|
298
|
-
# ===================================================================== #
|
299
|
-
set_target_format_type(
|
300
|
-
second_argument
|
301
|
-
)
|
302
|
-
end
|
303
|
-
end
|
304
|
-
|
305
|
-
# ========================================================================= #
|
306
|
-
# === real_file_name
|
307
|
-
#
|
308
|
-
# This method will return the filename of this class.
|
309
|
-
# ========================================================================= #
|
310
|
-
def real_file_name
|
311
|
-
return File.basename(__FILE__)+': '
|
312
|
-
end
|
313
|
-
|
314
|
-
# ========================================================================= #
|
315
|
-
# === remove
|
316
|
-
#
|
317
|
-
# Remove a file or a directory with this method.
|
318
|
-
# ========================================================================= #
|
319
|
-
def remove(i)
|
320
|
-
if File.directory?(i)
|
321
|
-
FileUtils.rm_rf(i) unless i.strip == '/'
|
322
|
-
elsif File.file?(i)
|
323
|
-
File.delete(i)
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
# ========================================================================= #
|
328
|
-
# === cliner
|
329
|
-
# ========================================================================= #
|
330
|
-
def cliner
|
331
|
-
e ('=' * 80)
|
332
|
-
end
|
333
|
-
|
334
|
-
# ========================================================================= #
|
335
|
-
# === set_extract_to
|
336
|
-
#
|
337
|
-
# Set the @extract_to variable here.
|
338
|
-
# ========================================================================= #
|
339
|
-
def set_extract_to(i = EXTRACT_TO)
|
340
|
-
case i
|
341
|
-
when nil, :default
|
342
|
-
i = EXTRACT_TO
|
343
|
-
end
|
344
|
-
i = i.to_s.dup
|
345
|
-
i = '/tmp/' if i.empty? # Hardcoded in this case.
|
346
|
-
i << '/' unless i.end_with? '/' # A directory has a trailing /.
|
347
|
-
@internal_hash[:extract_to] = i
|
348
|
-
end
|
349
|
-
|
350
|
-
# ========================================================================= #
|
351
|
-
# === extract_to?
|
352
|
-
#
|
353
|
-
# Defaults to /home/Temp/ on my home system.
|
354
|
-
# ========================================================================= #
|
355
|
-
def extract_to?
|
356
|
-
@internal_hash[:extract_to]
|
357
|
-
end; alias extract_to extract_to? # === extract_to
|
358
|
-
|
359
|
-
# ========================================================================= #
|
360
|
-
# === set_repackage_this_file
|
361
|
-
# ========================================================================= #
|
362
|
-
def set_repackage_this_file(i = first?)
|
363
|
-
if i.is_a? Array
|
364
|
-
i = i.first
|
365
|
-
end
|
366
|
-
case i
|
367
|
-
when nil
|
368
|
-
# ===================================================================== #
|
369
|
-
# Since as of May 2013 we try to fetch a random file from a list.
|
370
|
-
# ===================================================================== #
|
371
|
-
_ = Dir['*'].reject {|entry| File.directory? entry}
|
372
|
-
i = _.first if _.size == 1 # if we only have one entry, continue here.
|
373
|
-
# ========================================================================= #
|
374
|
-
# === --last
|
375
|
-
# ========================================================================= #
|
376
|
-
when 'LAST','LAST_DOWNLOADED',
|
377
|
-
/^-?-?last/i,
|
378
|
-
'-l'
|
379
|
-
i = File.readlines(LAST_DOWNLOADED_FILE).first
|
380
|
-
# ===================================================================== #
|
381
|
-
# The format of the file has changed a bit. We have to check
|
382
|
-
# whether it includes a '#' character. If so then we discard
|
383
|
-
# all that comes after said '#' token.
|
384
|
-
# ===================================================================== #
|
385
|
-
if i.include? '#'
|
386
|
-
i = i[0..(i.index('#')-1)].strip
|
387
|
-
end
|
388
|
-
end
|
389
|
-
i = i.to_s.dup
|
390
|
-
if File.directory?(i) and !i.end_with?('/')
|
391
|
-
i << '/'
|
392
|
-
end
|
393
|
-
if i.nil? or i.empty?
|
394
|
-
raise 'Please provide a valid archive to repackage.'
|
395
|
-
end
|
396
|
-
determine_file_size if File.exist? i
|
397
|
-
@internal_hash[:repackage_this_file] = i
|
398
|
-
# ========================================================================= #
|
399
|
-
# === :original_directory_where_the_archive_was_kept
|
400
|
-
#
|
401
|
-
# This variant will always refer to the directory where the archive
|
402
|
-
# is situated in.
|
403
|
-
# ========================================================================= #
|
404
|
-
@internal_hash[:original_directory_where_the_archive_was_kept] = rds(
|
405
|
-
File.absolute_path(
|
406
|
-
File.dirname(i)
|
407
|
-
)+'/'
|
408
|
-
)
|
409
|
-
end; alias set_package set_repackage_this_file # === set_package
|
410
|
-
|
411
|
-
# ========================================================================= #
|
412
|
-
# === rds
|
413
|
-
# ========================================================================= #
|
414
|
-
def rds(i)
|
415
|
-
i.squeeze '/'
|
416
|
-
end
|
417
|
-
|
418
|
-
# ========================================================================= #
|
419
|
-
# === change_dir_to (cd tag)
|
420
|
-
# ========================================================================= #
|
421
|
-
def change_dir_to(
|
422
|
-
where_to = extract_to?
|
423
|
-
)
|
424
|
-
Dir.chdir(where_to)
|
425
|
-
end; alias cd change_dir_to # === cd
|
426
|
-
|
427
|
-
# ========================================================================= #
|
428
|
-
# === is_on_roebe?
|
429
58
|
# ========================================================================= #
|
430
|
-
|
431
|
-
ENV['IS_ROEBE'].to_s == '1'
|
432
|
-
end
|
433
|
-
|
59
|
+
# === Repackage::Repackage[]
|
434
60
|
# ========================================================================= #
|
435
|
-
|
436
|
-
# ========================================================================= #
|
437
|
-
def set_start_dir(
|
438
|
-
i = return_pwd
|
439
|
-
)
|
440
|
-
@internal_hash[:start_dir] = i
|
441
|
-
end
|
442
|
-
|
443
|
-
# ========================================================================= #
|
444
|
-
# === remove_archive_from
|
445
|
-
# ========================================================================= #
|
446
|
-
def remove_archive_from(i)
|
447
|
-
i.delete_suffix('.xz').
|
448
|
-
delete_suffix('.gz').
|
449
|
-
delete_suffix('.bz2').
|
450
|
-
delete_suffix('.zip').
|
451
|
-
delete_suffix('.tar')
|
452
|
-
end; alias remove_extension remove_archive_from # === remove_extensions
|
453
|
-
|
454
|
-
# ========================================================================= #
|
455
|
-
# === set_the_new_file_is_at
|
456
|
-
# ========================================================================= #
|
457
|
-
def set_the_new_file_is_at(i)
|
458
|
-
@internal_hash[:the_new_file_is_at] = i
|
459
|
-
end; alias set_final_location set_the_new_file_is_at # === set_final_location
|
460
|
-
|
461
|
-
# ========================================================================= #
|
462
|
-
# === the_final_location_is_at?
|
463
|
-
# ========================================================================= #
|
464
|
-
def the_final_location_is_at?
|
465
|
-
@internal_hash[:the_new_file_is_at]
|
466
|
-
end; alias the_new_file_is_at? the_final_location_is_at? # === the_new_file_is_at?
|
467
|
-
alias the_file_is_where? the_final_location_is_at? # === the_file_is_where?
|
468
|
-
|
469
|
-
# ========================================================================= #
|
470
|
-
# === remove_extracted_data
|
471
|
-
# ========================================================================= #
|
472
|
-
def remove_extracted_data # remove the extracted archive again.
|
473
|
-
_ = "#{extract_to?}/#{remove_extension(filename?)}"
|
474
|
-
remove(_) if File.exist? _ # We remove a possibly-existing, extracted directory first.
|
475
|
-
end
|
476
|
-
|
477
|
-
# ========================================================================= #
|
478
|
-
# === internal_hash?
|
479
|
-
# ========================================================================= #
|
480
|
-
def internal_hash?
|
481
|
-
@internal_hash
|
482
|
-
end; alias hash? internal_hash? # === hash?
|
483
|
-
|
484
|
-
# ========================================================================= #
|
485
|
-
# === create_archive_from_this_directory (create tag)
|
486
|
-
# ========================================================================= #
|
487
|
-
def create_archive_from_this_directory(
|
488
|
-
this_directory,
|
489
|
-
repackage_to_this_format = repackage_to_which_format?
|
490
|
-
)
|
491
|
-
esystem "#{CREATE_TAR_XZ} #{this_directory}#{repackage_to_this_format} #{File.basename(this_directory)}"
|
492
|
-
end; alias package_this_directory create_archive_from_this_directory # === package_this_directory
|
493
|
-
|
494
|
-
# ========================================================================= #
|
495
|
-
# === Repackage.repackage
|
496
|
-
# ========================================================================= #
|
497
|
-
def self.repackage(this)
|
61
|
+
def self.[](this = ARGV)
|
498
62
|
new(this)
|
499
|
-
end; self.instance_eval { alias []
|
63
|
+
end; self.instance_eval { alias repackage [] } # === Repackage::Repackage[]
|
500
64
|
|
501
|
-
|
502
|
-
# === package_full_name?
|
503
|
-
# ========================================================================= #
|
504
|
-
def package_full_name?(
|
505
|
-
i = repackage_which_file?
|
506
|
-
)
|
507
|
-
File.absolute_path(i)
|
508
|
-
end
|
509
|
-
|
510
|
-
# ========================================================================= #
|
511
|
-
# === repackage_which_file?
|
512
|
-
# ========================================================================= #
|
513
|
-
def repackage_which_file?
|
514
|
-
@internal_hash[:repackage_this_file]
|
515
|
-
end; alias filename? repackage_which_file? # === filename?
|
516
|
-
|
517
|
-
# ========================================================================= #
|
518
|
-
# === move_this_package_to (move tag)
|
519
|
-
#
|
520
|
-
# Use this method to move a package to a new location.
|
521
|
-
# ========================================================================= #
|
522
|
-
def move_this_package_to(
|
523
|
-
new_location = the_file_is_where?,
|
524
|
-
where_to = start_dir?
|
525
|
-
)
|
526
|
-
package_full_name = File.absolute_path(new_location)
|
527
|
-
if File.exist? package_full_name
|
528
|
-
opn; e 'Moving the package at '+sfile(package_full_name)+
|
529
|
-
' to '+sfile(where_to)+' next.'
|
530
|
-
FileUtils.mv(package_full_name, where_to)
|
531
|
-
set_final_location(where_to)
|
532
|
-
else
|
533
|
-
e 'No file at '+sfile(package_full_name)+' exists, thus '\
|
534
|
-
'we can not move anything.'
|
535
|
-
end
|
536
|
-
end
|
537
|
-
|
538
|
-
# ========================================================================= #
|
539
|
-
# === start_dir?
|
540
|
-
# ========================================================================= #
|
541
|
-
def start_dir?
|
542
|
-
@internal_hash[:start_dir]
|
543
|
-
end
|
544
|
-
|
545
|
-
# ========================================================================= #
|
546
|
-
# === cd_to_the_extract_to_directory_then_set_it_as_the_start_dir
|
547
|
-
# ========================================================================= #
|
548
|
-
def cd_to_the_extract_to_directory_then_set_it_as_the_start_dir
|
549
|
-
cd EXTRACT_TO
|
550
|
-
set_start_dir EXTRACT_TO
|
551
|
-
end
|
552
|
-
|
553
|
-
# ========================================================================= #
|
554
|
-
# === consider_refusing_the_repackaging_action
|
555
|
-
#
|
556
|
-
# This method acts as a tiny "safeguard", so we don't repackage when
|
557
|
-
# the target format is the same as the original archive format.
|
558
|
-
# ========================================================================= #
|
559
|
-
def consider_refusing_the_repackaging_action(
|
560
|
-
i = repackage_which_file?
|
561
|
-
)
|
562
|
-
target_format_type = target_format_type?
|
563
|
-
if i.include? target_format_type
|
564
|
-
opn; e "We can not repackage #{simp(i)} into the"
|
565
|
-
opn; e "same target archive format (#{sfancy(target_format_type)})."
|
566
|
-
exit
|
567
|
-
end
|
568
|
-
end
|
569
|
-
|
570
|
-
# ========================================================================= #
|
571
|
-
# === try_glob
|
572
|
-
#
|
573
|
-
# Try a glob with this method.
|
574
|
-
# ========================================================================= #
|
575
|
-
def try_glob(i)
|
576
|
-
return Dir[i+'*']
|
577
|
-
end
|
578
|
-
|
579
|
-
# ========================================================================= #
|
580
|
-
# === dir_where_the_archive_resides?
|
581
|
-
# ========================================================================= #
|
582
|
-
def dir_where_the_archive_resides?
|
583
|
-
@internal_hash[:original_directory_where_the_archive_was_kept]
|
584
|
-
end
|
585
|
-
|
586
|
-
# ========================================================================= #
|
587
|
-
# === extract (extract tag)
|
588
|
-
#
|
589
|
-
# Extract it here. Before we do so, though, we must check if the target
|
590
|
-
# does not exist yet.
|
591
|
-
# ========================================================================= #
|
592
|
-
def extract(
|
593
|
-
what = filename?
|
594
|
-
)
|
595
|
-
if what.frozen?
|
596
|
-
what = what.dup
|
597
|
-
end
|
598
|
-
unless what.include? '/'
|
599
|
-
what = dir_where_the_archive_resides?+File.basename(what)
|
600
|
-
end
|
601
|
-
remove_extracted_data
|
602
|
-
Extracter.extract_what_to(what, extract_to?)
|
603
|
-
end
|
604
|
-
|
605
|
-
# ========================================================================= #
|
606
|
-
# === warn_about_missing_file_then_exit
|
607
|
-
# ========================================================================= #
|
608
|
-
def warn_about_missing_file_then_exit(
|
609
|
-
i = target_file?
|
610
|
-
)
|
611
|
-
opn;e swarn('The file `')+
|
612
|
-
sfile(i)+
|
613
|
-
swarn('` does not exist.')
|
614
|
-
raise "The argument must be an existing (local) file. As "\
|
615
|
-
"the file does not exist, this class can not continue."
|
616
|
-
end
|
617
|
-
|
618
|
-
# ========================================================================= #
|
619
|
-
# === check_whether_the_archive_exists
|
620
|
-
# ========================================================================= #
|
621
|
-
def check_whether_the_archive_exists(
|
622
|
-
_ = dir_where_the_archive_resides?+repackage_which_file?
|
623
|
-
)
|
624
|
-
cliner
|
625
|
-
if File.file? _
|
626
|
-
opn; e "Good, the file `#{sfile(_)}` exists."
|
627
|
-
opn; e 'We will try to repackage it into `'+
|
628
|
-
simportant(target_format_type?)+'` format type.'
|
629
|
-
opn; e 'We will extract into the directory '+sdir(extract_to?)+'.'
|
630
|
-
else
|
631
|
-
# ===================================================================== #
|
632
|
-
# Ok - in this case the file does not exist. We will first try
|
633
|
-
# to check if we have a number as input; if so we then we may
|
634
|
-
# try a glob-action. And if not, we will raise an error.
|
635
|
-
# ===================================================================== #
|
636
|
-
if first? and (first? =~ /^\d$/) # If input is a (positional) number like "3"
|
637
|
-
_ = Dir['*'].sort[first?.to_i - 1]
|
638
|
-
set_repackage_this_file(_)
|
639
|
-
else
|
640
|
-
# =================================================================== #
|
641
|
-
# Try a glob first before giving up.
|
642
|
-
# =================================================================== #
|
643
|
-
_ = try_glob(first?) if first?
|
644
|
-
if _.empty?
|
645
|
-
warn_about_missing_file_then_exit
|
646
|
-
else
|
647
|
-
check_if_this_target_exists_or_not(_.first)
|
648
|
-
end
|
649
|
-
end
|
650
|
-
end
|
651
|
-
cliner
|
652
|
-
end; alias check_if_this_target_exists_or_not check_whether_the_archive_exists # === check_if_this_target_exists_or_not
|
653
|
-
alias check_if_target_exists_or_not check_whether_the_archive_exists # === check_if_target_exists_or_not
|
654
|
-
|
655
|
-
# ========================================================================= #
|
656
|
-
# === do_repackage_everything
|
657
|
-
#
|
658
|
-
# This is the main workforce of this class, the "powerhorse method".
|
659
|
-
#
|
660
|
-
# Call it when you are ready to go and wish to re-package a given
|
661
|
-
# archive.
|
662
|
-
# ========================================================================= #
|
663
|
-
def do_repackage_everything
|
664
|
-
check_whether_the_archive_exists
|
665
|
-
consider_refusing_the_repackaging_action
|
666
|
-
target_file = repackage_which_file?
|
667
|
-
# ======================================================================= #
|
668
|
-
# Next extract this file:
|
669
|
-
# ======================================================================= #
|
670
|
-
extract(target_file)
|
671
|
-
# ======================================================================= #
|
672
|
-
# Now we have to repackage the extracted directory:
|
673
|
-
# ======================================================================= #
|
674
|
-
repackage_to_this_format = repackage_to_which_format?
|
675
|
-
_ = extract_to?+remove_archive_from(File.basename(target_file))
|
676
|
-
cd extract_to? # Ideally, we should not change directory ever.
|
677
|
-
case repackage_to_this_format # case tag
|
678
|
-
when '.tar.xz',
|
679
|
-
'xz',
|
680
|
-
'.tar.bz2',
|
681
|
-
'tar.bz2',
|
682
|
-
'tarbz2',
|
683
|
-
'totarbz2',
|
684
|
-
'tbz2',
|
685
|
-
'zip'
|
686
|
-
if File.directory? _
|
687
|
-
create_archive_from_this_directory(_, repackage_to_this_format)
|
688
|
-
end
|
689
|
-
new_location = _+repackage_to_this_format
|
690
|
-
if File.exist? new_location
|
691
|
-
set_the_new_file_is_at(new_location)
|
692
|
-
cd @internal_hash[:original_directory_where_the_archive_was_kept]
|
693
|
-
move_this_package_to(
|
694
|
-
new_location,
|
695
|
-
@internal_hash[:original_directory_where_the_archive_was_kept]
|
696
|
-
)
|
697
|
-
cd start_dir?
|
698
|
-
if shall_we_delete_the_old_archive?
|
699
|
-
remove_the_old_archive
|
700
|
-
remove_extracted_data
|
701
|
-
end
|
702
|
-
end
|
703
|
-
else
|
704
|
-
e tomato('Unhandled format type: ')+
|
705
|
-
steelblue(repackage_to_this_format)
|
706
|
-
end
|
707
|
-
end; alias repackage_data do_repackage_everything # === repackage_data
|
708
|
-
|
709
|
-
# ========================================================================= #
|
710
|
-
# === set_shall_we_delete_the_old_archive
|
711
|
-
# ========================================================================= #
|
712
|
-
def set_shall_we_delete_the_old_archive(
|
713
|
-
i = :default
|
714
|
-
)
|
715
|
-
case i
|
716
|
-
# ======================================================================= #
|
717
|
-
# === :default
|
718
|
-
# ======================================================================= #
|
719
|
-
when :default,
|
720
|
-
nil
|
721
|
-
i = SHALL_WE_DELETE_THE_OLD_ARCHIVE
|
722
|
-
if is_on_roebe?
|
723
|
-
i = true
|
724
|
-
end
|
725
|
-
end
|
726
|
-
@internal_hash[:shall_we_delete_the_old_archive] = i
|
727
|
-
end; alias set_shall_we_delete_old_source set_shall_we_delete_the_old_archive # === set_shall_we_delete_old_source
|
728
|
-
|
729
|
-
# ========================================================================= #
|
730
|
-
# === remove_the_old_archive
|
731
|
-
#
|
732
|
-
# Only call this when we are sure to remove the old source. I recommend
|
733
|
-
# to delete the old source. Of course we must make sure to delete the
|
734
|
-
# right package.
|
735
|
-
# ========================================================================= #
|
736
|
-
def remove_the_old_archive(
|
737
|
-
i = dir_where_the_archive_resides?+filename?
|
738
|
-
)
|
739
|
-
opn; e "Removing the old archive at #{sfile(i)} next, as requested."
|
740
|
-
remove(i)
|
741
|
-
end
|
65
|
+
end
|
742
66
|
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
opn; e 'Working from the directory '+sdir(start_dir?)+'.'
|
750
|
-
do_repackage_everything
|
751
|
-
end
|
67
|
+
# =========================================================================== #
|
68
|
+
# === Repackage.new
|
69
|
+
# =========================================================================== #
|
70
|
+
def self.new(i = ARGV)
|
71
|
+
::Repackage::Repackage.new(i)
|
72
|
+
end; self.instance_eval { alias repackage new } # === Repackage.repackage
|
752
73
|
|
753
74
|
end
|
754
75
|
|
755
76
|
if __FILE__ == $PROGRAM_NAME
|
756
77
|
Repackage.new(ARGV)
|
757
|
-
end
|
78
|
+
end # repackage_class
|
758
79
|
# =========================================================================== #
|
759
80
|
# Usage examples with full syntax:
|
760
81
|
#
|