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:
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rook/helper/fileutils.rb', line 111 def _store(cp_cmd, src, dest, ={}) # :nodoc: (, :preserve, :verbose, :noop) 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:
317 318 319 320 321 322 323 324 |
# File 'lib/rook/helper/fileutils.rb', line 317 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:
328 329 330 331 332 333 334 335 336 |
# File 'lib/rook/helper/fileutils.rb', line 328 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 |
# File 'lib/rook/helper/fileutils.rb', line 61 def cp_a(src, dest, ={}) (, :preserve, :verbose, :noop) 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')
87 88 89 |
# File 'lib/rook/helper/fileutils.rb', line 87 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
105 106 107 |
# File 'lib/rook/helper/fileutils.rb', line 105 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
96 97 98 |
# File 'lib/rook/helper/fileutils.rb', line 96 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')
229 230 231 |
# File 'lib/rook/helper/fileutils.rb', line 229 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')
291 292 293 294 295 |
# File 'lib/rook/helper/fileutils.rb', line 291 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')
259 260 261 262 |
# File 'lib/rook/helper/fileutils.rb', line 259 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']
245 246 247 |
# File 'lib/rook/helper/fileutils.rb', line 245 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']
309 310 311 312 313 |
# File 'lib/rook/helper/fileutils.rb', line 309 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']
276 277 278 279 |
# File 'lib/rook/helper/fileutils.rb', line 276 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']
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/rook/helper/fileutils.rb', line 195 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'
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rook/helper/fileutils.rb', line 149 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.
178 179 180 181 |
# File 'lib/rook/helper/fileutils.rb', line 178 def zip_r(zipfilename, filenames, ={}) ( ||= {})[:recursive] = true zip(zipfilename, filenames, ) end |