archive-tar-external 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,10 @@
1
+ == 1.2.3 - 25-Sep-2009
2
+ * Fixed a packaging bug.
3
+ * Added cygwin and mingw to Windows checks.
4
+ * Now requires open3 properly for Ruby 1.9 on Windows.
5
+ * Minor modifications to the gemspec.
6
+ * Added the 'gem' rake task.
7
+
1
8
  == 1.2.2 - 28-Jul-2009
2
9
  * Compatibility fixes for Ruby 1.9.x and JRuby.
3
10
  * Singleton methods that were previously synonyms are now true aliases.
data/MANIFEST CHANGED
@@ -5,7 +5,4 @@
5
5
  * archive-tar-external.gemspec
6
6
  * doc/tar_external.txt
7
7
  * lib/archive/tar_external.rb
8
- * test/tc_archive.rb
9
- * test/temp1.txt
10
- * test/temp2.txt
11
- * test/temp3.txt
8
+ * test/test_archive_tar_external.rb
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ desc "Install the archive-tar-external library (non-gem)"
5
+ task :install do
6
+ dest = File.join(Config::CONFIG['sitelibdir'], 'archive')
7
+ Dir.mkdir(dest) unless File.exists? dest
8
+ cp 'lib/archive/tar_external.rb', dest, :verbose => true
9
+ end
10
+
11
+ desc 'Build the archive-tar-external gem'
12
+ task :gem do
13
+ spec = eval(IO.read('archive-tar-external.gemspec'))
14
+ Gem::Builder.new(spec).build
15
+ end
16
+
17
+ desc 'Install the archive-tar-external library as a gem'
18
+ task :install_gem => [:gem] do
19
+ file = Dir["*.gem"].first
20
+ sh "gem install #{file}"
21
+ end
22
+
23
+ Rake::TestTask.new do |t|
24
+ t.warning = true
25
+ t.verbose = true
26
+ end
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'rbconfig'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'archive-tar-external'
6
+ s.version = '1.2.3'
7
+ s.summary = 'A simple way to create tar archives using external calls'
8
+ s.license = 'Artistic 2.0'
9
+ s.author = 'Daniel Berger'
10
+ s.email = 'djberg96@gmail.com'
11
+ s.homepage = 'http://www.rubyforge.org/shards'
12
+ s.test_file = 'test/test_archive_tar_external.rb'
13
+ s.has_rdoc = true
14
+ s.files = Dir['**/*'].reject{ |f| f.include?('CVS') }
15
+
16
+ s.rubyforge_project = 'shards'
17
+
18
+ s.extra_rdoc_files = [
19
+ 'README',
20
+ 'CHANGES',
21
+ 'MANIFEST',
22
+ 'doc/tar_external.txt'
23
+ ]
24
+
25
+ s.description = <<-EOF
26
+ The archive-tar-external is a simple wrapper interface for creating
27
+ tar files using your system's tar command. You can also easily compress
28
+ your tar files with your system's compression programs such as zip, gzip,
29
+ or bzip2.
30
+ EOF
31
+
32
+ s.add_development_dependency('test-unit', '>= 2.0.3')
33
+ s.add_development_dependency('ptools', '>= 1.1.7')
34
+
35
+ if Config::CONFIG['host_os'] =~ /mswin|dos|win32|cygwin|mingw|win32/i
36
+ if RUBY_VERSION.to_f < 1.9 && RUBY_PLATFORM !~ /java/i
37
+ s.add_dependency('win32-open3', '>= 0.3.1')
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,303 @@
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
@@ -38,7 +38,7 @@ class TC_ArchiveTarExternal < Test::Unit::TestCase
38
38
  end
39
39
 
40
40
  def test_version
41
- assert_equal('1.2.2', Tar::External::VERSION)
41
+ assert_equal('1.2.3', Tar::External::VERSION)
42
42
  end
43
43
 
44
44
  def test_constructor
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.2.2
4
+ version: 1.2.3
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: 2009-07-28 00:00:00 -06:00
12
+ date: 2009-09-25 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -44,11 +44,14 @@ extra_rdoc_files:
44
44
  - MANIFEST
45
45
  - doc/tar_external.txt
46
46
  files:
47
- - doc/tar_external.txt
48
- - test/test_archive_tar_external.rb
49
- - README
47
+ - archive-tar-external.gemspec
50
48
  - CHANGES
49
+ - doc/tar_external.txt
50
+ - lib/archive/tar_external.rb
51
51
  - MANIFEST
52
+ - Rakefile
53
+ - README
54
+ - test/test_archive_tar_external.rb
52
55
  has_rdoc: true
53
56
  homepage: http://www.rubyforge.org/shards
54
57
  licenses: