class Ratch::Batch
Proccess a list of files in batch.
The Batch
interface mimics the Shell
class in most respects.
Constants
- SAFE_METHODS
# Force copy the list of files recursively in batch, using a block to # determine the new file names. If the block returns nil, the file will not # be copied. # # This is similar to cp_rf, but allows for detailed control over the new # file names. def map_cp_rf(options={}, &block)
map_send(:cp_rf, options={}, &block)
end
Attributes
Public Class Methods
# File lib/ratch/batch.rb, line 15 def self.[](*patterns) new('.', *patterns) end
# File lib/ratch/batch.rb, line 20 def initialize(local, *patterns) @local = Pathname.new(local) @options = (Hash === patterns.last ? patterns.pop : {}).rekey(&:to_sym) @file_list = FileList.all patterns.each do |pattern| if @local == Pathname.new('.') @file_list.add(pattern) else @file_list.add(File.join(local,pattern)) end end end
Public Instance Methods
# File lib/ratch/batch.rb, line 123 def absolute? ; all?{ |path| FileTest.absolute?(path) } ; end
# File lib/ratch/batch.rb, line 111 def blockdev? ; all?{ |path| FileTest.blockdev?(path) } ; end
This is called size
in FileTest
but must be renamed to avoid the clash with the Enumerable mixin.
# File lib/ratch/batch.rb, line 94 def byte_size inject(0){ |sum, path| sum + FileTest.size(path) } end
# File lib/ratch/batch.rb, line 104 def chardev? ; all?{ |path| FileTest.chardev?(path) } ; end
Change mode of files.
# File lib/ratch/batch.rb, line 273 def chmod(mode, options={}) #list = list.to_a fileutils.chmod(mode, list, options) end
Change mode of files, following directories recursively.
# File lib/ratch/batch.rb, line 279 def chmod_r(mode, options={}) #list = list.to_a fileutils.chmod_r(mode, list, options) end
Change owner of files.
# File lib/ratch/batch.rb, line 286 def chown(user, group, options={}) #list = list.to_a fileutils.chown(user, group, list, options) end
Change owner of files, following directories recursively.
# File lib/ratch/batch.rb, line 292 def chown_r(user, group, options={}) #list = list.to_a fileutils.chown_r(user, group, list, options) end
cp(list, dir, options={})
# File lib/ratch/batch.rb, line 215 def cp(dir, options={}) #src = list.to_a #dest = localize(dest) fileutils.cp(list, dir, options) end
cp_r
(list, dir, options={})
# File lib/ratch/batch.rb, line 223 def cp_r(dir, options={}) #src = list.to_a #dest = localize(dest) fileutils.cp_r(list, dir, options) end
Limit list to directories.
# File lib/ratch/batch.rb, line 78 def directory! @file_list = @file_list.select{ |f| File.directory?(f) } end
# File lib/ratch/batch.rb, line 101 def directory? ; all?{ |path| FileTest.directory?(path) } ; end
# File lib/ratch/batch.rb, line 478 def dryrun? noop? && verbose? end
Iterate over pathnames.
# File lib/ratch/batch.rb, line 44 def each(&block) @file_list.each{ |file| block.call(Pathname.new(file)) } end
Returns an Array
of file paths relative to local
.
# File lib/ratch/batch.rb, line 68 def entries map{ |entry| entry.sub(local.to_s+'/','') } end
# File lib/ratch/batch.rb, line 118 def executable? ; all?{ |path| FileTest.executable?(path) } ; end
# File lib/ratch/batch.rb, line 126 def executable_real? ; all?{ |path| FileTest.executable_real?(path) } ; end
# File lib/ratch/batch.rb, line 105 def exist? ; all?{ |path| FileTest.exist?(path) } ; end
# File lib/ratch/batch.rb, line 106 def exists? ; all?{ |path| FileTest.exists?(path) } ; end
Limit list to files.
# File lib/ratch/batch.rb, line 73 def file! @file_list = @file_list.select{ |f| File.file?(f) } end
# File lib/ratch/batch.rb, line 109 def file? ; all?{ |path| FileTest.file?(path) } ; end
Returns the the underlying FileList
object.
# File lib/ratch/batch.rb, line 39 def file_list @file_list end
# File lib/ratch/batch.rb, line 112 def grpowned? ; all?{ |path| FileTest.grpowned?(path) } ; end
# File lib/ratch/batch.rb, line 131 def identical?(other) all?{ |path| FileTest.identical?(path, other) } end
Install files to a directory with given mode. Unlike cp
, this will not copy the file if an up-to-date copy already exists.
# File lib/ratch/batch.rb, line 266 def install(dir, mode, options={}) #src = list.to_a #dest = localize(dest) fileutils.install(list, dir, mode, options) end
Returns the list of files as Strings, rather than Pathname objects.
# File lib/ratch/batch.rb, line 62 def list @file_list.to_a end
ln(list, destdir, options={})
# File lib/ratch/batch.rb, line 193 def ln(dir, options={}) #src = list.to_a #new = localize(new) fileutils.ln(list, dir, options) end
ln_s
(list, destdir, options={})
# File lib/ratch/batch.rb, line 201 def ln_s(dir, options={}) #src = list.to_a #new = localize(new) fileutils.ln_s(list, dir, options) end
# File lib/ratch/batch.rb, line 208 def ln_sf(dir, options={}) #src = list.to_a #new = localize(new) fileutils.ln_sf(list, dir, options) end
Copy the list of files in batch, using a block to determine the new file names. If the block returns nil, the file will not be copied.
This is similar to cp
, but allows for detailed control over the new file names.
# File lib/ratch/batch.rb, line 361 def map_cp(options={}, &block) map_send(:cp, options={}, &block) end
Copy the list of files recursively in batch, using a block to determine the new file names. If the block returns nil, the file will not be copied.
This is similar to cp_r
, but allows for detailed control over the new file names.
# File lib/ratch/batch.rb, line 370 def map_cp_r(options={}, &block) map_send(:cp_r, options={}, &block) end
Hard link the list of files in batch, using a block to determine the new names. If the block returns nil, the file will not be linked.
This is similar to ln
, but allows for detailed control over the link names.
# File lib/ratch/batch.rb, line 334 def map_ln(options={}, &block) map_send(:ln, options={}, &block) end
Soft link the list of files in batch, using a block to determine the new names. If the block returns nil, the file will not be linked.
This is similar to ln_s
, but allows for detailed control over the link names.
# File lib/ratch/batch.rb, line 342 def map_ln_s(options={}, &block) map_send(:ln_s, options={}, &block) end
Force soft linking of the list of files in batch, using a block to determine the new names. If the block returns nil, the file will not be linked.
This is similar to ln_sf
, but allows for detailed control over the link names.
# File lib/ratch/batch.rb, line 352 def map_ln_sf(options={}, &block) map_send(:ln_sf, options={}, &block) end
Rename the list of files in batch, using a block to determine the new names. If the block returns nil, the file will not be renamed.
This is similar to mv
, but allows for detailed control over the renaming.
Unlike the other ‘map_*` methods, this changes the Batch
list in-place, since the files renamed no loner exist.
Returns the changed FileList
instance.
# File lib/ratch/batch.rb, line 326 def map_mv(options={}, &block) @file_list = map_send(:mv, options={}, &block) end
Make a directory for every entry.
# File lib/ratch/batch.rb, line 167 def mkdir(options={}) list.each do |dir| if File.exist?(dir) raise Errno::EEXIST, "File exists - #{dir}" end end list.map{ |dir| fileutils.mkdir(dir, options) } end
Make a directory for every entry.
# File lib/ratch/batch.rb, line 177 def mkdir_p(options={}) list.each do |dir| if File.exist?(dir) && !File.directory?(dir) raise Errno::EEXIST, "File exists - #{dir}" end end list.map{ |dir| fileutils.mkdir_p(dir, options) } end
mv(list, dir, options={})
# File lib/ratch/batch.rb, line 230 def mv(dir, options={}) #src = list.to_a #dest = localize(dest) fileutils.mv(list, dir, options) end
# File lib/ratch/batch.rb, line 468 def noop? @options[:noop] or @options[:dryrun] end
Is path
out-of-date in comparsion to all files in batch.
# File lib/ratch/batch.rb, line 458 def outofdate?(path) fileutils.outofdate?(path, to_a) end
# File lib/ratch/batch.rb, line 116 def owned? ; all?{ |path| FileTest.owned?(path) } ; end
# File lib/ratch/batch.rb, line 108 def pipe? ; all?{ |path| FileTest.pipe?(path) } ; end
Present working directory. TODO: Does this make sense?
# File lib/ratch/batch.rb, line 162 def pwd @local end
# File lib/ratch/batch.rb, line 103 def readable? ; all?{ |path| FileTest.readable?(path) } ; end
# File lib/ratch/batch.rb, line 127 def readable_real? ; all?{ |path| FileTest.readable_real?(path) } ; end
TODO: Will this work since all paths are localized?
# File lib/ratch/batch.rb, line 122 def relative? ; all?{ |path| FileTest.relative?(path) } ; end
Convenient alias for map_mv
.
# File lib/ratch/batch.rb, line 313 def rename(options={}, &block) map_mv(options, &block) end
# File lib/ratch/batch.rb, line 238 def rm(options={}) list = list.to_a fileutils.rm(list, options) end
Remove, with force option.
# File lib/ratch/batch.rb, line 253 def rm_f(options={}) #list = list.to_a fileutils.rm_f(list, options) end
Remove, recursively removing the contents of directories.
# File lib/ratch/batch.rb, line 247 def rm_r(options={}) #list = list.to_a fileutils.rm_r(list, options) end
Remove with force option, recursively removing the contents of directories.
# File lib/ratch/batch.rb, line 259 def rm_rf(options={}) #list = list.to_a fileutils.rm_rf(list, options) end
Remove every directory.
# File lib/ratch/batch.rb, line 188 def rmdir(options={}) list.map{ |dir| fileutils.rmdir(dir, options) } end
# File lib/ratch/batch.rb, line 119 def safe? ; all?{ |path| FileTest.safe?(path) } ; end
Limit list to selection block.
# File lib/ratch/batch.rb, line 83 def select!(&block) #@file_list = FileList.all(*to_a.select{ |f| block.call(f) }) @file_list = @file_list.select{ |f| block.call(f) } end
# File lib/ratch/batch.rb, line 113 def setgid? ; all?{ |path| FileTest.setgid?(path) } ; end
# File lib/ratch/batch.rb, line 114 def setuid? ; all?{ |path| FileTest.setuid?(path) } ; end
Returns the Integer size of the list of files.
# File lib/ratch/batch.rb, line 49 def size @file_list.size end
# File lib/ratch/batch.rb, line 115 def socket? ; all?{ |path| FileTest.socket?(path) } ; end
Stage files. This is like install
but uses hardlinks.
# File lib/ratch/batch.rb, line 305 def stage(dir) #dir = localize(directory) #files = localize(files) #list = list.to_a fileutils.stage(dir, local, list) end
# File lib/ratch/batch.rb, line 110 def sticky? ; all?{ |path| FileTest.sticky?(path) } ; end
# File lib/ratch/batch.rb, line 102 def symlink? ; all?{ |path| FileTest.symlink?(path) } ; end
Return the list of files as Pathname objects.
# File lib/ratch/batch.rb, line 54 def to_a @file_list.map{ |file| Pathname.new(file) } end
Touch each file.
# File lib/ratch/batch.rb, line 299 def touch(options={}) #list = list.to_a fileutils.touch(list, options) end
Is path
up-to-date in comparsion to all files in batch.
# File lib/ratch/batch.rb, line 463 def uptodate?(path) fileutils.uptodate?(path, to_a) end
# File lib/ratch/batch.rb, line 473 def verbose? @options[:verbose] or @options[:dryrun] end
# File lib/ratch/batch.rb, line 117 def writable? ; all?{ |path| FileTest.writable?(path) } ; end
# File lib/ratch/batch.rb, line 125 def writable_real? ; all?{ |path| FileTest.writable_real?(path) } ; end
# File lib/ratch/batch.rb, line 107 def zero? ; all?{ |path| FileTest.zero?(path) } ; end
Private Instance Methods
Given a map of source to destination, this method ensures a FileUtils operation can is “safe” –that the destination does not yet and exist and/or the source and destination are compatible. If nota file rrror is raised.
# File lib/ratch/batch.rb, line 416 def ensure_safe(map) map.each do |src, dest| raise unless File.exist?(src) if File.directory?(src) if File.directory?(dest) new_map = {} Dir.entries(src).each do |e| next if e == '.' or e == '..' new_map[File.join(src, e)] = File.join(dest, e) end ensure_safe_destination(new_map) else raise Errno::EISDIR, "Is a directory - #{src}" end else if File.directory?(dest) check = File.join(dest, src) if File.exist?(check) raise Errno::EEXIST, "File exists - #{check}" end else raise Errno::EEXIST, "File exists - #{dest}" if File.exist?(dest) end end end end
Returns FileUtils module based on mode.
# File lib/ratch/batch.rb, line 485 def fileutils if dryrun? ::FileUtils::DryRun elsif noop? ::FileUtils::Noop elsif verbose? ::FileUtils::Verbose else ::FileUtils end end
Generic name mapping procedure which can be used for any FileUtils method that has a ‘src, dest` interface.
# File lib/ratch/batch.rb, line 393 def map_send(method, options={}, &block) map = {} list.each do |file| if dest = block.call(file) map[file] = dest end end if options[:safe] or !options[:force] or SAFE_METHODS.include?(method) ensure_safe(map) end map.each do |src, dest| fileutils.__send__(method, src, dest, options) end FileList.all(*map.values) end