buildr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From boisv...@apache.org
Subject svn commit: r917064 - in /buildr/trunk: CHANGELOG lib/buildr/packaging/tar.rb lib/buildr/packaging/ziptask.rb spec/packaging/archive_spec.rb
Date Sat, 27 Feb 2010 22:35:48 GMT
Author: boisvert
Date: Sat Feb 27 22:35:47 2010
New Revision: 917064

URL: http://svn.apache.org/viewvc?rev=917064&view=rev
Log:
BUILDR-326 Support unzipping tar.gz files (Antoine Toulme)

Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/lib/buildr/packaging/tar.rb
    buildr/trunk/lib/buildr/packaging/ziptask.rb
    buildr/trunk/spec/packaging/archive_spec.rb

Modified: buildr/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=917064&r1=917063&r2=917064&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Sat Feb 27 22:35:47 2010
@@ -17,6 +17,7 @@
 * Added:  "buildr test=only" will only run tests explicitly specified on the
           command line (and ignore transitive test dependencies)
 * Added:  ArtifactNamespace.{keys,clear} methods
+* Added:  BUILDR-326 Support unzipping tar.gz files (Antoine Toulme)
 * Added:  BUILDR-375 Buildr now recognizes buildfile.rb and Buildfile.rb
           (Kerry Wilson)
 * Change: Updated to JRuby 1.4.0

Modified: buildr/trunk/lib/buildr/packaging/tar.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/packaging/tar.rb?rev=917064&r1=917063&r2=917064&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/packaging/tar.rb (original)
+++ buildr/trunk/lib/buildr/packaging/tar.rb Sat Feb 27 22:35:47 2010
@@ -41,7 +41,7 @@
 
     def initialize(*args, &block) #:nodoc:
       super
-      self.gzip = name =~ /\.[t?]gz$/
+      self.gzip = name =~ /\.t?gz$/
       self.mode = '0755'
     end
 

Modified: buildr/trunk/lib/buildr/packaging/ziptask.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/packaging/ziptask.rb?rev=917064&r1=917063&r2=917064&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/packaging/ziptask.rb (original)
+++ buildr/trunk/lib/buildr/packaging/ziptask.rb Sat Feb 27 22:35:47 2010
@@ -106,7 +106,7 @@
   end
 
 
-  # An object for unzipping a file into a target directory. You can tell it to include
+  # An object for unzipping/untarring a file into a target directory. You can tell it to
include
   # or exclude only specific files and directories, and also to map files from particular
   # paths inside the zip file into the target directory. Once ready, call #extract.
   #
@@ -131,7 +131,7 @@
     # :call-seq:
     #   extract
     #
-    # Extract the zip file into the target directory.
+    # Extract the zip/tgz file into the target directory.
     #
     # You can call this method directly. However, if you are using the #unzip method,
     # it creates a file task for the target directory: use that task instead as a
@@ -148,16 +148,36 @@
 
       # Otherwise, empty unzip creates target as a file when touching.
       mkpath target.to_s
-      Zip::ZipFile.open(zip_file.to_s) do |zip|
-        entries = zip.collect
-        @paths.each do |path, patterns|
-          patterns.map(entries).each do |dest, entry|
-            next if entry.directory?
-            dest = File.expand_path(dest, target.to_s)
-            trace "Extracting #{dest}"
-            mkpath File.dirname(dest) rescue nil
-            entry.restore_permissions = true
-            entry.extract(dest) { true }
+      if zip_file.to_s.match /\.t?gz$/
+        #un-tar.gz
+        Zlib::GzipReader.open(zip_file.to_s) { |tar|
+          Archive::Tar::Minitar::Input.open(tar) do |inp|
+            inp.each do |tar_entry|
+              @paths.each do |path, patterns|
+                patterns.map([tar_entry]).each do |dest, entry|
+                  next if entry.directory?
+                  dest = File.expand_path(dest, target.to_s)
+                  trace "Extracting #{dest}"
+                  mkpath File.dirname(dest) rescue nil
+                  #entry.restore_permissions = true
+                  File.open(dest, 'w') {|f| f.write entry.read}
+                end
+              end
+            end
+          end
+        }
+      else
+        Zip::ZipFile.open(zip_file.to_s) do |zip|
+          entries = zip.collect
+          @paths.each do |path, patterns|
+            patterns.map(entries).each do |dest, entry|
+              next if entry.directory?
+              dest = File.expand_path(dest, target.to_s)
+              trace "Extracting #{dest}"
+              mkpath File.dirname(dest) rescue nil
+              entry.restore_permissions = true
+              entry.extract(dest) { true }
+            end
           end
         end
       end
@@ -165,6 +185,24 @@
       touch target.to_s
     end
 
+    #reads the includes/excludes and apply them to the entry_name
+    def included?(entry_name)
+      @paths.each do |path, patterns|
+        return true if path.nil?
+        if entry_name =~ /^#{path}/
+          short = entry_name.sub(path, '')
+          if patterns.include.any? { |pattern| File.fnmatch(pattern, entry_name) } &&
+            !patterns.exclude.any? { |pattern| File.fnmatch(pattern, entry_name) }
+            # trace "tar_entry.full_name " + entry_name + " is included"
+            return true
+          end
+        end
+      end
+      # trace "tar_entry.full_name " + entry_name + " is excluded"
+      return false
+    end
+
+
     # :call-seq:
     #   include(*files) => self
     #   include(*files, :path=>name) => self
@@ -303,7 +341,7 @@
   #
   # For example:
   #   unzip('all'=>'test.zip')
-  #   unzip('src'=>'test.zip').include('README', 'LICENSE') 
+  #   unzip('src'=>'test.zip').include('README', 'LICENSE')
   #   unzip('libs'=>'test.zip').from_path('libs')
   def unzip(args)
     target, arg_names, zip_file = Buildr.application.resolve_args([args])

Modified: buildr/trunk/spec/packaging/archive_spec.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/packaging/archive_spec.rb?rev=917064&r1=917063&r2=917064&view=diff
==============================================================================
--- buildr/trunk/spec/packaging/archive_spec.rb (original)
+++ buildr/trunk/spec/packaging/archive_spec.rb Sat Feb 27 22:35:47 2010
@@ -410,6 +410,8 @@
     @files = %w{Test1.txt Text2.html}.map { |file| File.join(@dir, file) }.
       each { |file| write file, content_for(file) }
     @target = File.expand_path('target')
+    @targz = File.expand_path('test.tar.gz')
+    @targz2 = File.expand_path('test.tgz')
   end
 
   # Not too smart, we just create some content based on file name to
@@ -418,6 +420,16 @@
     "Content for #{File.basename(file)}"
   end
 
+  def with_tar(*args)
+    tar(@targz).include(*args.empty? ? @files : args).invoke
+    yield
+  end
+  
+  def with_tar_too(*args)
+    tar(@targz2).include(*args.empty? ? @files : args).invoke
+    yield
+  end
+  
   def with_zip(*args)
     zip(@zip).include(*args.empty? ? @files : args).invoke
     yield
@@ -438,6 +450,20 @@
       @files.each { |f| File.read(File.join(@target, File.basename(f))).should eql(content_for(f))
}
     end
   end
+  
+  it 'should expand files from a tar.gz file' do
+    with_tar do
+      unzip(@target=>@targz).target.invoke
+      @files.each { |f| File.read(File.join(@target, File.basename(f))).should eql(content_for(f))
}
+    end
+  end
+  
+  it 'should expand files from a .tgz file' do
+    with_tar_too do
+      unzip(@target=>@targz2).target.invoke
+      @files.each { |f| File.read(File.join(@target, File.basename(f))).should eql(content_for(f))
}
+    end
+  end
 
   it 'should expand all files' do
     with_zip do
@@ -445,6 +471,13 @@
       FileList[File.join(@target, '*')].size.should be(@files.size)
     end
   end
+  
+  it 'should expand all files from a .tar.gz file' do
+    with_tar do
+      unzip(@target=>@targz).target.invoke
+      FileList[File.join(@target, '*')].size.should be(@files.size)
+    end
+  end
 
   it 'should expand only included files' do
     with_zip do
@@ -454,6 +487,15 @@
       FileList[File.join(@target, '*')].size.should be(1)
     end
   end
+  
+  it 'should expand only included files from a .tar.gz file' do
+    with_tar do
+      only = File.basename(@files.first)
+      unzip(@target=>@targz).include(only).target.invoke
+      FileList[File.join(@target, '*')].should include(File.expand_path(only, @target))
+      FileList[File.join(@target, '*')].size.should be(1)
+    end
+  end
 
   it 'should expand all but excluded files' do
     with_zip do
@@ -463,6 +505,15 @@
       FileList[File.join(@target, '*')].size.should be(@files.size - 1)
     end
   end
+  
+  it 'should expand all but excluded files with a .tar.gz file' do
+    with_tar do
+      except = File.basename(@files.first)
+      unzip(@target=>@targz).exclude(except).target.invoke
+      FileList[File.join(@target, '*')].should_not include(File.expand_path(except, @target))
+      FileList[File.join(@target, '*')].size.should be(@files.size - 1)
+    end
+  end
 
   it 'should include with nested path patterns' do
     with_zip @files, :path=>'test/path' do
@@ -479,6 +530,22 @@
       FileList[File.join(@target, 'test/path/*')].size.should be(2)
     end
   end
+  
+  it 'should include with nested path patterns with a .tar.gz file' do
+    with_tar @files, :path=>'test/path' do
+      only = File.basename(@files.first)
+      unzip(@target=>@targz).include(only).target.invoke
+      FileList[File.join(@target, '*')].should be_empty
+
+      Rake::Task.clear ; rm_rf @target
+      unzip(@target=>@targz).include('test/path/' + only).target.invoke
+      FileList[File.join(@target, 'test/path/*')].size.should be(1)
+
+      Rake::Task.clear ; rm_rf @target
+      unzip(@target=>@targz).include('test/**/*').target.invoke
+      FileList[File.join(@target, 'test/path/*')].size.should be(2)
+    end
+  end
 
   it 'should include with relative path' do
     with_zip @files, :path=>'test/path' do
@@ -500,6 +567,26 @@
     end
   end
   
+  it 'should include with relative path with a .tar.gz file' do
+    with_tar @files, :path=>'test/path' do
+      only = File.basename(@files.first)
+      unzip(@target=>@targz).tap { |unzip| unzip.from_path('test').include(only) }.target.invoke
+      FileList[File.join(@target, '*')].should be_empty
+
+      Rake::Task.clear ; rm_rf @target
+      unzip(@target=>@targz).tap { |unzip| unzip.from_path('test').include('test/*') }.target.invoke
+      FileList[File.join(@target, 'path/*')].should be_empty
+
+      Rake::Task.clear ; rm_rf @target
+      unzip(@target=>@targz).tap { |unzip| unzip.from_path('test').include('path/*' +
only) }.target.invoke
+      FileList[File.join(@target, 'path/*')].size.should be(1)
+
+      Rake::Task.clear ; rm_rf @target
+      unzip(@target=>@targz).tap { |unzip| unzip.from_path('test').include('path/*') }.target.invoke
+      FileList[File.join(@target, 'path/*')].size.should be(2)
+    end
+  end
+  
   it 'should exclude with relative path' do
     with_zip @files, :path=>'test' do
       except = File.basename(@files.first)
@@ -508,6 +595,15 @@
       FileList[File.join(@target, '*')].size.should be(@files.size - 1)
     end
   end
+  
+  it 'should exclude with relative path on a tar.gz file' do
+    with_tar @files, :path=>'test' do
+      except = File.basename(@files.first)
+      unzip(@target=>@targz).tap { |unzip| unzip.from_path('test').exclude(except) }.target.invoke
+      FileList[File.join(@target, '*')].should include(File.join(@target, File.basename(@files[1])))
+      FileList[File.join(@target, '*')].size.should be(@files.size - 1)
+    end
+  end
 
   it "should handle relative paths without any includes or excludes" do
     lib_files = %w{Test3.so Test4.rb}.
@@ -518,6 +614,16 @@
     unzip(@target=>@zip).tap { |unzip| unzip.from_path('lib') }.target.invoke
     FileList[File.join(@target, '**/*')].should have(2).files
   end
+  
+  it "should handle relative paths without any includes or excludes with a tar.gz file" do
+    lib_files = %w{Test3.so Test4.rb}.
+      map { |file| File.join(@dir, file) }.
+      each { |file| write file, content_for(file) }
+    tar(@targz).include(@files, :path => 'src').include(lib_files, :path => 'lib').invoke
+
+    unzip(@target=>@targz).tap { |unzip| unzip.from_path('lib') }.target.invoke
+    FileList[File.join(@target, '**/*')].should have(2).files
+  end
 
   it 'should return itself from root method' do
     task = unzip(@target=>@zip)



Mime
View raw message