Module: FileUtils

Defined in:
lib/rook/helper/fileutils.rb

Overview

extends FileUtils

requires ‘rubyzip’, ‘bz2’, and ‘minitar’ library.

Class Method Summary collapse

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, options={})  # :nodoc:
  begin
    fu_check_options(options, :preserve, :verbose, :noop)
  rescue ArgumentError
    fu_check_options(options, [:preserve, :verbose, :noop])
  end
  if options[:verbose]
    opt_str = options[:preserve] ? ' -p' : ''
    arg_str = [src, dest].flatten.join(' ')
    fu_output_message("store#{opt_str} #{arg_str}")
  end
  return if options[:noop]
  options.delete(:verbose)
  options.delete(:noop)
  mkdir_options = options.dup
  cp_options    = options.dup
  (src.is_a?(Array) ? src : [ src ]).each do |filename|
    if test(?d, filename)
      path = "#{dest}/#{filename}"
      mkdir_p(path, mkdir_options) unless test(?d, path)
    else
      dir  = File.dirname(filename)
      base = File.basename(filename)
      path = "#{dest}/#{dir}"
      mkdir_p(path, mkdir_options) unless test(?d, path)
      __send__(cp_cmd, filename, path, cp_options)
    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, options, taropt, writer_class)  # :nodoc:
  require 'archive/tar/minitar'
  puts "tar #{taropt} #{tarfilename} #{filenames.to_a.join(' ')}" if options[:verbose]
  return if options[: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, options, taropt, reader_class)  #:nodoc:
  require 'archive/tar/minitar'
  puts "tar #{taropt} #{tarfilename} #{filenames.to_a.join(' ')}" if options[:verbose]
  return if options[:noop]
  basedir = options[: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, options={})
  begin
    fu_check_options(options, :preserve, :verbose, :noop)
  rescue ArgumentError
    fu_check_options(options, [:preserve, :verbose, :noop])
  end
  if options[:verbose]
    opt_str = options[:preserve] ? ' -ap' : ' -p'
    arg_str = [src, dest].flatten.join(' ')
    fu_output_message("cp#{opt_str} #{arg_str}")
  end
  return if options[: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, options[: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, options={})
  (options ||= {})[:preserve] = true
  cp(src, dest, options)
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, options={})
  (options ||= {})[:preserve] = true
  cp_r(src, dest, options)
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, options={})
  _store(:cp, src, dest, options)
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, options={})
  _store(:cp_a, src, dest, options)
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, options={})
  _store(:cp_p, src, dest, options)
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, options={})
  _tar_cf(tarfilename, filenames, options, '-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, options={})
  require 'bz2'
  require 'rook/helper/bz2'
  _tar_cf(tarfilename, filenames, options, '-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, options={})
  require 'zlib'
  _tar_cf(tarfilename, filenames, options, '-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=[], options={})
  _tar_xf(tarfilename, filenames, options, '-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=[], options={})
  require 'bz2'
  require 'rook/bz2-helper'
  _tar_xf(tarfilename, filenames, options, "-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=[], options={})
  require 'zlib'
  _tar_xf(tarfilename, filenames, options, "-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, options={})
  require 'zip/zip'
  basedir = options[:basedir]
  filenames = [ filenames ] if filenames && !filenames.is_a?(Array)
  if options[:verbose]
    opt_str = ''
    opt_str << " -r #{basedir}" if options[:basedir]
    fu_output_message("unzip#{opt_str} #{zipfilename} #{filenames ? filenames.join(' ') : ''}")
  end
  return if options[: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, options={})
  require 'zip/zip'
  basedir = options[:basedir]
  filenames = [ filenames ] unless filenames.is_a?(Array)
  if options[:verbose]
    opt_str = ''
    opt_str << 'r' if options[:recursive]
    opt_str = ' -' + opt_str unless opt_str.empty?
    fu_output_message("zip#{opt_str} #{zipfilename} #{filenames.join(' ')}")
  end
  return if options[:noop]
  ::Zip::ZipFile.open(zipfilename, true) do |zipfile|
    filenames.each do |fname|
      list = test(?d, fname) && options[: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, options={})
  (options ||= {})[:recursive] = true
  zip(zipfilename, filenames, options)
end