C0 code coverage information
Generated on Wed Oct 07 08:34:05 -0700 2009 with rcov 0.8.2.1
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with this
3 # work for additional information regarding copyright ownership. The ASF
4 # licenses this file to you under the Apache License, Version 2.0 (the
5 # "License"); you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations under
14 # the License.
15
16
17 require 'buildr/packaging/archive'
18 gem 'archive-tar-minitar' ; autoload :Archive, 'archive/tar/minitar'
19
20
21 module Buildr
22
23 # The TarTask creates a new Tar file. You can include any number of files and and directories,
24 # use exclusion patterns, and include files into specific directories.
25 #
26 # To create a GZipped Tar, either set the gzip option to true, or use the .tgz or .gz suffix.
27 #
28 # For example:
29 # tar("test.tgz").tap do |task|
30 # task.include "srcs"
31 # task.include "README", "LICENSE"
32 # end
33 #
34 # See Buildr#tar and ArchiveTask.
35 class TarTask < ArchiveTask
36
37 # To create a GZipped Tar, either set this option to true, or use the .tgz/.gz suffix.
38 attr_accessor :gzip
39 # Permission mode for files contained in the Tar. Defaults to 0755.
40 attr_accessor :mode
41
42 def initialize(*args, &block) #:nodoc:
43 super
44 self.gzip = name =~ /\.[t?]gz$/
45 self.mode = '0755'
46 end
47
48 # :call-seq:
49 # entry(name) => Entry
50 #
51 # Returns a Tar file entry. You can use this to check if the entry exists and its contents,
52 # for example:
53 # package(:tar).entry("src/LICENSE").should contain(/Apache Software License/)
54 def entry(entry_name)
55 Buildr::TarEntry.new(self, entry_name)
56 end
57
58 def entries() #:nodoc:
59 tar_entries = nil
60 with_uncompressed_tar { |tar| tar_entries = tar.entries }
61 tar_entries
62 end
63
64 # :call-seq:
65 # with_uncompressed_tar { |tar_entries| ... }
66 #
67 # Yields an Archive::Tar::Minitar::Input object to the provided block.
68 # Opening, closing and Gzip-decompressing is automatically taken care of.
69 def with_uncompressed_tar &block
70 if gzip
71 Zlib::GzipReader.open(name) { |tar| Archive::Tar::Minitar.open(tar, &block) }
72 else
73 Archive::Tar::Minitar.open(name, &block)
74 end
75 end
76
77 private
78
79 def create_from(file_map)
80 if gzip
81 StringIO.new.tap do |io|
82 create_tar io, file_map
83 io.seek 0
84 Zlib::GzipWriter.open(name) { |gzip| gzip.write io.read }
85 end
86 else
87 File.open(name, 'wb') { |file| create_tar file, file_map }
88 end
89 end
90
91 def create_tar(out, file_map)
92 Archive::Tar::Minitar::Writer.open(out) do |tar|
93 options = { :mode=>mode || '0755', :mtime=>Time.now }
94
95 file_map.each do |path, content|
96 if content.respond_to?(:call)
97 tar.add_file(path, options) { |os, opts| content.call os }
98 elsif content.nil? || File.directory?(content.to_s)
99 else
100 File.open content.to_s, 'rb' do |is|
101 tar.add_file path, options.merge(:mode=>is.stat.mode, :mtime=>is.stat.mtime, :uid=>is.stat.uid, :gid=>is.stat.gid) do |os, opts|
102 while data = is.read(4096)
103 os.write(data)
104 end
105 end
106 end
107 end
108 end
109 end
110 end
111
112 end
113
114
115 class TarEntry #:nodoc:
116
117 def initialize(tar_task, entry_name)
118 @tar_task = tar_task
119 @entry_name = entry_name
120 end
121
122 # :call-seq:
123 # contain?(*patterns) => boolean
124 #
125 # Returns true if this Tar file entry matches against all the arguments. An argument may be
126 # a string or regular expression.
127 def contain?(*patterns)
128 content = read_content_from_tar
129 patterns.map { |pattern| Regexp === pattern ? pattern : Regexp.new(Regexp.escape(pattern.to_s)) }.
130 all? { |pattern| content =~ pattern }
131 end
132
133 # :call-seq:
134 # empty?() => boolean
135 #
136 # Returns true if this entry is empty.
137 def empty?()
138 read_content_from_tar.nil?
139 end
140
141 # :call-seq:
142 # exist() => boolean
143 #
144 # Returns true if this entry exists.
145 def exist?()
146 exist = false
147 @tar_task.with_uncompressed_tar { |tar| exist = tar.any? { |entry| entry.name == @entry_name } }
148 exist
149 end
150
151 def to_s #:nodoc:
152 @entry_name
153 end
154
155 private
156
157 def read_content_from_tar
158 content = Errno::ENOENT.new("No such file or directory - #{@entry_name}")
159 @tar_task.with_uncompressed_tar do |tar|
160 content = tar.inject(content) { |content, entry| entry.name == @entry_name ? entry.read : content }
161 end
162 raise content if Exception === content
163 content
164 end
165 end
166
167 end
168
169
170 # :call-seq:
171 # tar(file) => TarTask
172 #
173 # The TarTask creates a new Tar file. You can include any number of files and
174 # and directories, use exclusion patterns, and include files into specific
175 # directories.
176 #
177 # To create a GZipped Tar, either set the gzip option to true, or use the .tgz or .gz suffix.
178 #
179 # For example:
180 # tar("test.tgz").tap do |tgz|
181 # tgz.include "srcs"
182 # tgz.include "README", "LICENSE"
183 # end
184 def tar(file)
185 TarTask.define_task(file)
186 end
Generated using the rcov code coverage analysis tool for Ruby
version 0.8.2.1.