archive-tar-external 1.3.0 → 1.3.1

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