archive-tar-external 1.2.3 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +94 -87
- data/MANIFEST +7 -7
- data/README +47 -29
- data/Rakefile +26 -26
- data/archive-tar-external.gemspec +40 -40
- data/doc/tar_external.txt +109 -136
- data/lib/archive/tar/external.rb +281 -0
- data/test/test_archive_tar_external.rb +203 -201
- metadata +14 -4
- data/lib/archive/tar_external.rb +0 -303
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: archive-tar-external
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Berger
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-01-16 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -32,7 +32,17 @@ dependencies:
|
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 1.1.7
|
34
34
|
version:
|
35
|
-
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: win32-open3
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.3.1
|
44
|
+
version:
|
45
|
+
description: " The archive-tar-external is a simple wrapper interface for creating\n tar files using your system's tar command. You can also easily compress\n your tar files with your system's compression programs such as zip, gzip,\n or bzip2.\n"
|
36
46
|
email: djberg96@gmail.com
|
37
47
|
executables: []
|
38
48
|
|
@@ -47,7 +57,7 @@ files:
|
|
47
57
|
- archive-tar-external.gemspec
|
48
58
|
- CHANGES
|
49
59
|
- doc/tar_external.txt
|
50
|
-
- lib/archive/
|
60
|
+
- lib/archive/tar/external.rb
|
51
61
|
- MANIFEST
|
52
62
|
- Rakefile
|
53
63
|
- README
|
data/lib/archive/tar_external.rb
DELETED
@@ -1,303 +0,0 @@
|
|
1
|
-
require 'rbconfig'
|
2
|
-
|
3
|
-
if Config::CONFIG['host_os'] =~ /mswin|dos|win32|cygwin|mingw/i
|
4
|
-
if RUBY_VERSION.to_f < 1.9 && RUBY_PLATFORM !~ /java/i
|
5
|
-
require 'win32/open3'
|
6
|
-
else
|
7
|
-
require 'open3'
|
8
|
-
end
|
9
|
-
else
|
10
|
-
require 'open3'
|
11
|
-
end
|
12
|
-
|
13
|
-
# The Archive module serves as a namespace only.
|
14
|
-
module Archive
|
15
|
-
|
16
|
-
# The Tar class serves as a toplevel class namespace only.
|
17
|
-
class Tar
|
18
|
-
|
19
|
-
# Raised if something goes wrong during the execution of any methods
|
20
|
-
# which use the tar command internally.
|
21
|
-
class Error < StandardError; end
|
22
|
-
|
23
|
-
# Raised if something goes wrong during the Tar#compress_archive or
|
24
|
-
# Tar#uncompress_archive methods.
|
25
|
-
class CompressError < StandardError; end
|
26
|
-
|
27
|
-
# This class encapsulates tar & zip operations.
|
28
|
-
class Tar::External
|
29
|
-
# The version of the archive-tar-external library.
|
30
|
-
VERSION = '1.2.3'
|
31
|
-
|
32
|
-
# The name of the archive file to be used, e.g. "test.tar"
|
33
|
-
attr_accessor :archive_name
|
34
|
-
|
35
|
-
# The name of the tar program you wish to use. The default is "tar".
|
36
|
-
attr_accessor :tar_program
|
37
|
-
|
38
|
-
# The name of the archive file after compression, e.g. "test.tar.gz"
|
39
|
-
attr_reader :compressed_archive_name
|
40
|
-
|
41
|
-
# Returns an Archive::Tar::External object. The +archive_name+ is the
|
42
|
-
# name of the tarball. While a .tar extension is recommended based on
|
43
|
-
# years of convention, it is not enforced.
|
44
|
-
#
|
45
|
-
# Note that this does not actually create the archive unless you
|
46
|
-
# pass a value to +file_pattern+. This then becomes a shortcut for
|
47
|
-
# Archive::Tar::External.new + Archive::Tar::External#create_archive.
|
48
|
-
#
|
49
|
-
# If +program+ is provided, then it compresses the archive as well by
|
50
|
-
# calling Archive::Tar::External#compress_archive internally.
|
51
|
-
#
|
52
|
-
def initialize(archive_name, file_pattern=nil, program=nil)
|
53
|
-
@archive_name = archive_name.to_s
|
54
|
-
@compressed_archive_name = nil
|
55
|
-
@tar_program = 'tar'
|
56
|
-
|
57
|
-
if file_pattern
|
58
|
-
create_archive(file_pattern)
|
59
|
-
end
|
60
|
-
|
61
|
-
if program
|
62
|
-
compress_archive(program)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
# Assign a compressed archive name. This autogenerates the archive_name
|
67
|
-
# based on the extension of the name provided, unless you provide the
|
68
|
-
# extension yourself. If the extension is '.tgz', then the base of the
|
69
|
-
# name + '.tar' will be the new archive name.
|
70
|
-
#
|
71
|
-
# This should only be used if you have a pre-existing, compressed archive
|
72
|
-
# that you want to uncompress, and want to have a Tar::External object
|
73
|
-
# around. Otherwise, use the class method Tar::External.uncompress.
|
74
|
-
#
|
75
|
-
def compressed_archive_name=(name, ext=File.extname(name))
|
76
|
-
if ext.downcase == '.tgz'
|
77
|
-
@archive_name = File.basename(name, ext.downcase) + '.tar'
|
78
|
-
else
|
79
|
-
@archive_name = File.basename(name, ext)
|
80
|
-
end
|
81
|
-
@compressed_archive_name = name
|
82
|
-
end
|
83
|
-
|
84
|
-
# Creates the archive using +file_pattern+. Any errors that occur
|
85
|
-
# here will raise a Error.
|
86
|
-
#
|
87
|
-
def create_archive(file_pattern)
|
88
|
-
cmd = "#{@tar_program} cf #{@archive_name} #{file_pattern}"
|
89
|
-
|
90
|
-
Open3.popen3(cmd){ |tar_in, tar_out, tar_err|
|
91
|
-
err = tar_err.gets
|
92
|
-
if err
|
93
|
-
raise Error, err.chomp
|
94
|
-
end
|
95
|
-
}
|
96
|
-
|
97
|
-
self
|
98
|
-
end
|
99
|
-
|
100
|
-
alias :create :create_archive
|
101
|
-
|
102
|
-
# Compresses the archive with +program+, or gzip if no program is
|
103
|
-
# provided. If you want to pass arguments to +program+, merely include
|
104
|
-
# them as part of the program name, e.g. "gzip -f".
|
105
|
-
#
|
106
|
-
# Any errors that occur here will raise a Tar::CompressError.
|
107
|
-
#
|
108
|
-
def compress_archive(program='gzip')
|
109
|
-
cmd = "#{program} #{@archive_name}"
|
110
|
-
|
111
|
-
Open3.popen3(cmd){ |prog_in, prog_out, prog_err|
|
112
|
-
err = prog_err.gets
|
113
|
-
if err
|
114
|
-
raise CompressError, err.chomp
|
115
|
-
end
|
116
|
-
|
117
|
-
# Find the new file name with the extension. There's probably a more
|
118
|
-
# reliable way to do this, but this should work 99% of the time.
|
119
|
-
name = Dir["#{@archive_name}.{gz,bz2,cpio,zip}"].first
|
120
|
-
@compressed_archive_name = name
|
121
|
-
}
|
122
|
-
|
123
|
-
self
|
124
|
-
end
|
125
|
-
|
126
|
-
alias :compress :compress_archive
|
127
|
-
|
128
|
-
# Uncompresses the tarball using the program you pass to this method. The
|
129
|
-
# default is "gunzip". Just as for +compress_archive+, you can pass
|
130
|
-
# arguments along as part of the argument.
|
131
|
-
#
|
132
|
-
# Note that this is only for use with archives that have been zipped up
|
133
|
-
# with gunzip, or whatever. If you want to *extract* the files from the
|
134
|
-
# tarball, use Tar::External#extract instead.
|
135
|
-
#
|
136
|
-
# Any errors that occur here will raise a Tar::CompressError.
|
137
|
-
#
|
138
|
-
def uncompress_archive(program="gunzip")
|
139
|
-
unless @compressed_archive_name
|
140
|
-
raise CompressError, "no compressed file found"
|
141
|
-
end
|
142
|
-
|
143
|
-
cmd = "#{program} #{@compressed_archive_name}"
|
144
|
-
|
145
|
-
Open3.popen3(cmd){ |prog_in, prog_out, prog_err|
|
146
|
-
err = prog_err.gets
|
147
|
-
if err
|
148
|
-
raise CompressError, err.chomp
|
149
|
-
end
|
150
|
-
@compressed_archive_name = nil
|
151
|
-
}
|
152
|
-
self
|
153
|
-
end
|
154
|
-
|
155
|
-
alias :uncompress :uncompress_archive
|
156
|
-
|
157
|
-
# Uncompress an existing archive, using +program+ to uncompress it.
|
158
|
-
# The default decompression program is gunzip.
|
159
|
-
#
|
160
|
-
def self.uncompress_archive(archive, program='gunzip')
|
161
|
-
cmd = "#{program} #{archive}"
|
162
|
-
|
163
|
-
Open3.popen3(cmd){ |prog_in, prog_out, prog_err|
|
164
|
-
err = prog_err.gets
|
165
|
-
if err
|
166
|
-
raise CompressError, err.chomp
|
167
|
-
end
|
168
|
-
}
|
169
|
-
end
|
170
|
-
|
171
|
-
class << self
|
172
|
-
alias uncompress uncompress_archive
|
173
|
-
end
|
174
|
-
|
175
|
-
# An alias for Tar::External.uncompress_archive.
|
176
|
-
#
|
177
|
-
#def self.uncompress(file, program='gunzip')
|
178
|
-
# self.uncompress_archive(file, program)
|
179
|
-
#end
|
180
|
-
|
181
|
-
# Returns an array of file names that are included within the tarball.
|
182
|
-
# This method does not extract the archive.
|
183
|
-
#
|
184
|
-
def archive_info
|
185
|
-
result = []
|
186
|
-
cmd = "#{@tar_program} tf #{@archive_name}"
|
187
|
-
Open3.popen3(cmd){ |ain, aout, aerr|
|
188
|
-
err = aerr.gets
|
189
|
-
if err
|
190
|
-
raise Error, err.chomp
|
191
|
-
end
|
192
|
-
|
193
|
-
while output = aout.gets
|
194
|
-
result << output.chomp
|
195
|
-
end
|
196
|
-
}
|
197
|
-
result
|
198
|
-
end
|
199
|
-
|
200
|
-
alias :info :archive_info
|
201
|
-
|
202
|
-
# Adds +files+ to an already existing archive.
|
203
|
-
#
|
204
|
-
def add_to_archive(*files)
|
205
|
-
if files.empty?
|
206
|
-
raise Error, "there must be at least one file specified"
|
207
|
-
end
|
208
|
-
|
209
|
-
cmd = "#{@tar_program} rf #{@archive_name} #{files.join(" ")}"
|
210
|
-
Open3.popen3(cmd){ |ain, aout, aerr|
|
211
|
-
err = aerr.gets
|
212
|
-
|
213
|
-
if err
|
214
|
-
raise Error, err.chomp
|
215
|
-
end
|
216
|
-
}
|
217
|
-
self
|
218
|
-
end
|
219
|
-
|
220
|
-
alias :add :add_to_archive
|
221
|
-
|
222
|
-
# Updates the given +files+ in the archive, i.e. they are added if they
|
223
|
-
# are not already in the archive or have been modified.
|
224
|
-
#
|
225
|
-
def update_archive(*files)
|
226
|
-
if files.empty?
|
227
|
-
raise Error, "there must be at least one file specified"
|
228
|
-
end
|
229
|
-
|
230
|
-
cmd = "#{@tar_program} uf #{@archive_name} #{files.join(" ")}"
|
231
|
-
|
232
|
-
Open3.popen3(cmd){ |ain, aout, aerr|
|
233
|
-
err = aerr.gets
|
234
|
-
if err
|
235
|
-
raise Error, err.chomp
|
236
|
-
end
|
237
|
-
}
|
238
|
-
|
239
|
-
self
|
240
|
-
end
|
241
|
-
|
242
|
-
alias :update :update_archive
|
243
|
-
|
244
|
-
# Expands the contents of the tarball. It does NOT delete the tarball.
|
245
|
-
# If +files+ are provided, then only those files are extracted.
|
246
|
-
# Otherwise, all files are extracted.
|
247
|
-
#
|
248
|
-
# Note that some tar programs, notably the tar program shipped by Sun,
|
249
|
-
# does not issue any sort of warning or error if you try to extract a
|
250
|
-
# file that does not exist in the archive.
|
251
|
-
#
|
252
|
-
def extract_archive(*files)
|
253
|
-
cmd = "#{@tar_program} xf #{@archive_name}"
|
254
|
-
|
255
|
-
unless files.empty?
|
256
|
-
cmd << " " << files.join(" ")
|
257
|
-
end
|
258
|
-
|
259
|
-
Open3.popen3(cmd){ |ain, aout, aerr|
|
260
|
-
err = aerr.gets
|
261
|
-
|
262
|
-
if err
|
263
|
-
raise Error, err.chomp
|
264
|
-
end
|
265
|
-
}
|
266
|
-
|
267
|
-
self
|
268
|
-
end
|
269
|
-
|
270
|
-
alias :expand_archive :extract_archive
|
271
|
-
alias :extract :extract_archive
|
272
|
-
alias :expand :extract_archive
|
273
|
-
|
274
|
-
# A class method that behaves identically to the equivalent instance
|
275
|
-
# method, except that you must specifiy that tarball as the first
|
276
|
-
# argument. Also, the tar program is hard coded to 'tar xf'.
|
277
|
-
#
|
278
|
-
def self.extract_archive(archive, *files)
|
279
|
-
cmd = "tar xf #{archive}"
|
280
|
-
|
281
|
-
unless files.empty?
|
282
|
-
cmd << " " << files.join(" ")
|
283
|
-
end
|
284
|
-
|
285
|
-
Open3.popen3(cmd){ |ain, aout, aerr|
|
286
|
-
err = aerr.gets
|
287
|
-
|
288
|
-
if err
|
289
|
-
raise Error, err.chomp
|
290
|
-
end
|
291
|
-
}
|
292
|
-
|
293
|
-
self
|
294
|
-
end
|
295
|
-
|
296
|
-
class << self
|
297
|
-
alias expand_archive extract_archive
|
298
|
-
alias extract extract_archive
|
299
|
-
alias expand extract_archive
|
300
|
-
end
|
301
|
-
end
|
302
|
-
end
|
303
|
-
end
|