Module: FileUtils
- Defined in:
- lib/rook/helper/fileutils.rb
Overview
extends FileUtils
requires ‘rubyzip’, ‘bz2’, and ‘minitar’ library.
-
rubyzip - raa.ruby-lang.org/project/rubyzip/
-
minitar - raa.ruby-lang.org/project/minitar/
Class Method Summary collapse
-
._store(cp_cmd, src, dest, options = {}) ⇒ Object
:nodoc:.
-
._tar_cf(tarfilename, filenames, options, taropt, writer_class) ⇒ Object
:nodoc:.
-
._tar_xf(tarfilename, filenames, options, taropt, reader_class) ⇒ Object
:nodoc:.
-
.cp_a(src, dest, options = {}) ⇒ Object
copy files into dest keeping file type (symbolic link, device file, etc).
-
.cp_p(src, dest, options = {}) ⇒ Object
copy files into dest with timestamp preserved.
-
.cp_pr(src, dest, options = {}) ⇒ Object
copy files into dest recursively with timestamp preserved.
-
.store(src, dest, options = {}) ⇒ Object
copy files to dest with keeping it’s filepath.
-
.store_a(src, dest, options = {}) ⇒ Object
copy files to dest with keeping it’s filepath and file type.
-
.store_p(src, dest, options = {}) ⇒ Object
copy files to dest with keeping it’s filepath and timestamp.
-
.tar_cf(tarfilename, filenames, options = {}) ⇒ Object
create *.tar file.
-
.tar_cjf(tarfilename, filenames, options = {}) ⇒ Object
create *.tar.bz2 file.
-
.tar_czf(tarfilename, filenames, options = {}) ⇒ Object
create *.tar.gz file.
-
.tar_xf(tarfilename, filenames = [], options = {}) ⇒ Object
extract *.tar file.
-
.tar_xjf(tarfilename, filenames = [], options = {}) ⇒ Object
extract *.tar.bz2 file.
-
.tar_xzf(tarfilename, filenames = [], options = {}) ⇒ Object
extract *.tar.gz file.
-
.unzip(zipfilename, filenames = nil, options = {}) ⇒ Object
unzip *.zip file.
-
.zip(zipfilename, filenames, options = {}) ⇒ Object
create *.zip file.
-
.zip_r(zipfilename, filenames, options = {}) ⇒ Object
create *.zip file with keeping filepath.
Class Method Details
._store(cp_cmd, src, dest, options = {}) ⇒ Object
:nodoc:
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rook/helper/fileutils.rb', line 115 def _store(cp_cmd, src, dest, ={}) # :nodoc: begin (, :preserve, :verbose, :noop) rescue ArgumentError (, [:preserve, :verbose, :noop]) end if [:verbose] opt_str = [:preserve] ? ' -p' : '' arg_str = [src, dest].flatten.join(' ') ("store#{opt_str} #{arg_str}") end return if [:noop] .delete(:verbose) .delete(:noop) = .dup = .dup (src.is_a?(Array) ? src : [ src ]).each do |filename| if test(?d, filename) path = "#{dest}/#{filename}" mkdir_p(path, ) unless test(?d, path) else dir = File.dirname(filename) base = File.basename(filename) path = "#{dest}/#{dir}" mkdir_p(path, ) unless test(?d, path) __send__(cp_cmd, filename, path, ) end end end |
._tar_cf(tarfilename, filenames, options, taropt, writer_class) ⇒ Object
:nodoc:
325 326 327 328 329 330 331 332 |
# File 'lib/rook/helper/fileutils.rb', line 325 def _tar_cf(tarfilename, filenames, , taropt, writer_class) # :nodoc: require 'archive/tar/minitar' puts "tar #{taropt} #{tarfilename} #{filenames.to_a.join(' ')}" if [:verbose] return if [:noop] f = File.open(tarfilename, 'wb') writer = writer_class ? writer_class.new(f) : f ::Archive::Tar::Minitar.pack(filenames, writer) # Warning: writer will be closed! end |
._tar_xf(tarfilename, filenames, options, taropt, reader_class) ⇒ Object
:nodoc:
336 337 338 339 340 341 342 343 344 |
# File 'lib/rook/helper/fileutils.rb', line 336 def _tar_xf(tarfilename, filenames, , taropt, reader_class) #:nodoc: require 'archive/tar/minitar' puts "tar #{taropt} #{tarfilename} #{filenames.to_a.join(' ')}" if [:verbose] return if [:noop] basedir = [:basedir] || '.' f = File.open(tarfilename, 'rb') reader = reader_class ? reader_class.new(f) : f ::Archive::Tar::Minitar.unpack(reader, basedir) # Warning: reader will be closed! end |
.cp_a(src, dest, options = {}) ⇒ Object
copy files into dest keeping file type (symbolic link, device file, etc)
ex1.
cp_a('file1', 'file2')
ex2.
cp_a(['file1', 'file2', 'file3'], 'dir')
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rook/helper/fileutils.rb', line 61 def cp_a(src, dest, ={}) begin (, :preserve, :verbose, :noop) rescue ArgumentError (, [:preserve, :verbose, :noop]) end if [:verbose] opt_str = [:preserve] ? ' -ap' : ' -p' arg_str = [src, dest].flatten.join(' ') ("cp#{opt_str} #{arg_str}") end return if [:noop] dereference_root = false #options[:dereference_root] (src.is_a?(Array) ? src : [src]).each do |item| Dir.glob(item).each do |filename| copy_entry(item, dest, [:preserve], dereference_root) end end end |
.cp_p(src, dest, options = {}) ⇒ Object
copy files into dest with timestamp preserved
ex1.
cp_p('file1', 'file2')
ex2.
cp_p(['file1', 'file2', 'file3'], 'dir')
31 32 33 34 |
# File 'lib/rook/helper/fileutils.rb', line 31 def cp_p(src, dest, ={}) ( ||= {})[:preserve] = true cp(src, dest, ) end |
.cp_pr(src, dest, options = {}) ⇒ Object
copy files into dest recursively with timestamp preserved
ex1.
cp_pr('file1', 'file2')
ex2.
cp_pr(['file1', 'file2', 'file3'], 'dir')
46 47 48 49 |
# File 'lib/rook/helper/fileutils.rb', line 46 def cp_pr(src, dest, ={}) ( ||= {})[:preserve] = true cp_r(src, dest, ) end |
.store(src, dest, options = {}) ⇒ Object
copy files to dest with keeping it’s filepath
ex1. copy ‘lib/rook/rook.rb’ into ‘tmp/lib/rook/rook.rb’
store('lib/rook/rook.rb', 'tmp')
ex2. copy all ‘lib/*/.rb’ into ‘tmp/lib/*/.rb’
cp_a(Dir.glob('lib/**/*.rb'), 'tmp')
91 92 93 |
# File 'lib/rook/helper/fileutils.rb', line 91 def store(src, dest, ={}) _store(:cp, src, dest, ) end |
.store_a(src, dest, options = {}) ⇒ Object
copy files to dest with keeping it’s filepath and file type
109 110 111 |
# File 'lib/rook/helper/fileutils.rb', line 109 def store_a(src, dest, ={}) _store(:cp_a, src, dest, ) end |
.store_p(src, dest, options = {}) ⇒ Object
copy files to dest with keeping it’s filepath and timestamp
100 101 102 |
# File 'lib/rook/helper/fileutils.rb', line 100 def store_p(src, dest, ={}) _store(:cp_p, src, dest, ) end |
.tar_cf(tarfilename, filenames, options = {}) ⇒ Object
create *.tar file
requires ‘minitar’ library.
ex1.
tar_cf 'foo.tar', Dir.glob('*.txt')
237 238 239 |
# File 'lib/rook/helper/fileutils.rb', line 237 def tar_cf(tarfilename, filenames, ={}) _tar_cf(tarfilename, filenames, , '-cf', null) end |
.tar_cjf(tarfilename, filenames, options = {}) ⇒ Object
create *.tar.bz2 file
requires ‘bz2’ and ‘minitar’ library.
ex1.
tar_cjf 'foo.tar.bz2', Dir.glob('*.txt')
299 300 301 302 303 |
# File 'lib/rook/helper/fileutils.rb', line 299 def tar_cjf(tarfilename, filenames, ={}) require 'bz2' require 'rook/helper/bz2' _tar_cf(tarfilename, filenames, , '-cjf', ::BZ2::Writer) end |
.tar_czf(tarfilename, filenames, options = {}) ⇒ Object
create *.tar.gz file
requires ‘minitar’ library.
ex1.
tar_czf 'foo.tar.gz', Dir.glob('*.txt')
267 268 269 270 |
# File 'lib/rook/helper/fileutils.rb', line 267 def tar_czf(tarfilename, filenames, ={}) require 'zlib' _tar_cf(tarfilename, filenames, , '-czf', ::Zlib::GzipWriter) end |
.tar_xf(tarfilename, filenames = [], options = {}) ⇒ Object
extract *.tar file
requires ‘minitar’ library.
ex1.
tar_xf 'foo.tar'
ex2.
tar_xf 'foo.tar', ['file1.txt', 'file2.txt']
253 254 255 |
# File 'lib/rook/helper/fileutils.rb', line 253 def tar_xf(tarfilename, filenames=[], ={}) _tar_xf(tarfilename, filenames, , '-xf', null) end |
.tar_xjf(tarfilename, filenames = [], options = {}) ⇒ Object
extract *.tar.bz2 file
requires ‘bz2’ and ‘minitar’ library.
ex1.
tar_xjf 'foo.tar.bz2'
ex2.
tar_xjf 'foo.tar.bz2', ['file1.txt', 'file2.txt']
317 318 319 320 321 |
# File 'lib/rook/helper/fileutils.rb', line 317 def tar_xjf(tarfilename, filenames=[], ={}) require 'bz2' require 'rook/bz2-helper' _tar_xf(tarfilename, filenames, , "-xjf", ::BZ2::Writer) end |
.tar_xzf(tarfilename, filenames = [], options = {}) ⇒ Object
extract *.tar.gz file
requires ‘minitar’ library.
ex1.
tar_xzf 'foo.tar.gz'
ex2.
tar_xzf 'foo.tar', ['file1.txt', 'file2.txt']
284 285 286 287 |
# File 'lib/rook/helper/fileutils.rb', line 284 def tar_xzf(tarfilename, filenames=[], ={}) require 'zlib' _tar_xf(tarfilename, filenames, , "-xzf", ::Zlib::GzipReader) end |
.unzip(zipfilename, filenames = nil, options = {}) ⇒ Object
unzip *.zip file
requires ‘rubyzip’ library.
ex1.
unzip 'foo-1.1.zip', :basedir=>'foo'
ex2.
unzip 'foo-1.1.zip', ['foo1.txt', 'foo2.txt']
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/rook/helper/fileutils.rb', line 203 def unzip(zipfilename, filenames=nil, ={}) require 'zip/zip' basedir = [:basedir] filenames = [ filenames ] if filenames && !filenames.is_a?(Array) if [:verbose] opt_str = '' opt_str << " -r #{basedir}" if [:basedir] ("unzip#{opt_str} #{zipfilename} #{filenames ? filenames.join(' ') : ''}") end return if [:noop] ::Zip::ZipFile.open(zipfilename) do |zipfile| zipfile.each do |entry| # entry.class == Zip::ZipEntry next unless filenames.nil? || filenames.include?(entry.name) filename = basedir ? "#{basedir}/#{entry}" : "#{entry}" if test(?e, filename) FileUtils.rm_rf(filename) # if entry.file? || !test(?d, filename) else FileUtils.mkdir_p(File.dirname(filename)) end zipfile.extract(entry, filename) end end end |
.zip(zipfilename, filenames, options = {}) ⇒ Object
create *.zip file
requires ‘rubyzip’ library.
ex1.
zip 'file.zip', Dir.glob('*.txt')
ex2.
zip 'file.zip', Dir.glob('*.txt'), :basedir=>'path'
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rook/helper/fileutils.rb', line 157 def zip(zipfilename, filenames, ={}) require 'zip/zip' basedir = [:basedir] filenames = [ filenames ] unless filenames.is_a?(Array) if [:verbose] opt_str = '' opt_str << 'r' if [:recursive] opt_str = ' -' + opt_str unless opt_str.empty? ("zip#{opt_str} #{zipfilename} #{filenames.join(' ')}") end return if [:noop] ::Zip::ZipFile.open(zipfilename, true) do |zipfile| filenames.each do |fname| list = test(?d, fname) && [:recursive] ? Dir.glob("#{fname}/**/*") : [fname] list.each do |filename| entry = basedir ? "#{basedir}/#{fname}" : filename zipfile.add(entry, filename) end end end end |
.zip_r(zipfilename, filenames, options = {}) ⇒ Object
create *.zip file with keeping filepath
requires ‘rubyzip’ library.
186 187 188 189 |
# File 'lib/rook/helper/fileutils.rb', line 186 def zip_r(zipfilename, filenames, ={}) ( ||= {})[:recursive] = true zip(zipfilename, filenames, ) end |