class Ratch::Batch

Proccess a list of files in batch.

The Batch interface mimics the Shell class in most respects.

TODO: Should FileList use Pathname, or only in Batch?

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

local[R]

Public Class Methods

[](*patterns) click to toggle source
# File lib/ratch/batch.rb, line 15
def self.[](*patterns)
  new('.', *patterns)
end
new(local, *patterns) click to toggle source
# 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

absolute?() click to toggle source
# File lib/ratch/batch.rb, line 123
def absolute?   ; all?{ |path| FileTest.absolute?(path)   } ; end
blockdev?() click to toggle source
# File lib/ratch/batch.rb, line 111
def blockdev?   ; all?{ |path| FileTest.blockdev?(path)   } ; end
byte_size() click to toggle source

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
Also aliased as: size?
chardev?() click to toggle source
# File lib/ratch/batch.rb, line 104
def chardev?    ; all?{ |path| FileTest.chardev?(path)    } ; end
chmod(mode, options={}) click to toggle source

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
chmod_r(mode, options={}) click to toggle source

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
chown(user, group, options={}) click to toggle source

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
chown_r(user, group, options={}) click to toggle source

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
cmp(other)
Alias for: identical?
compare_file(other)

TODO: Really?

Alias for: identical?
copy(dir, options={})
Alias for: cp
cp(dir, options={}) click to toggle source

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
Also aliased as: copy
cp_r(dir, options={}) click to toggle source

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
dir?()
Alias for: directory?
directory!() click to toggle source

Limit list to directories.

# File lib/ratch/batch.rb, line 78
def directory!
  @file_list = @file_list.select{ |f| File.directory?(f) }
end
directory?() click to toggle source
# File lib/ratch/batch.rb, line 101
def directory?  ; all?{ |path| FileTest.directory?(path)  } ; end
Also aliased as: dir?
dryrun?() click to toggle source
# File lib/ratch/batch.rb, line 478
def dryrun?
  noop? && verbose?
end
each(&block) click to toggle source

Iterate over pathnames.

# File lib/ratch/batch.rb, line 44
def each(&block)
  @file_list.each{ |file| block.call(Pathname.new(file)) }
end
entries() click to toggle source

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
executable?() click to toggle source
# File lib/ratch/batch.rb, line 118
def executable? ; all?{ |path| FileTest.executable?(path) } ; end
executable_real?() click to toggle source
# File lib/ratch/batch.rb, line 126
def executable_real? ; all?{ |path| FileTest.executable_real?(path) } ; end
exist?() click to toggle source
# File lib/ratch/batch.rb, line 105
def exist?      ; all?{ |path| FileTest.exist?(path)      } ; end
exists?() click to toggle source
# File lib/ratch/batch.rb, line 106
def exists?     ; all?{ |path| FileTest.exists?(path)     } ; end
file!() click to toggle source

Limit list to files.

# File lib/ratch/batch.rb, line 73
def file!
  @file_list = @file_list.select{ |f| File.file?(f) }
end
file?() click to toggle source
# File lib/ratch/batch.rb, line 109
def file?       ; all?{ |path| FileTest.file?(path)       } ; end
file_list() click to toggle source

Returns the the underlying FileList object.

# File lib/ratch/batch.rb, line 39
def file_list
  @file_list
end
filenames()
Alias for: list
grpowned?() click to toggle source
# File lib/ratch/batch.rb, line 112
def grpowned?   ; all?{ |path| FileTest.grpowned?(path)   } ; end
identical?(other) click to toggle source
# File lib/ratch/batch.rb, line 131
def identical?(other)
  all?{ |path| FileTest.identical?(path, other) }
end
Also aliased as: compare_file, cmp
install(dir, mode, options={}) click to toggle source

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
list() click to toggle source

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
Also aliased as: filenames
ln(dir, options={}) click to toggle source

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
Also aliased as: link
ln_s(dir, options={}) click to toggle source

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
Also aliased as: symlink
ln_sf(dir, options={}) click to toggle source
# 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
map_cp(options={}, &block) click to toggle source

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
map_cp_r(options={}, &block) click to toggle source

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
map_ln(options={}, &block) click to toggle source

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
map_ln_s(options={}, &block) click to toggle source

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
map_ln_sf(options={}, &block) click to toggle source

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
map_mv(options={}, &block) click to toggle source

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
mkdir(options={}) click to toggle source

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
mkdir_p(options={}) click to toggle source

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
Also aliased as: mkpath
mkpath(options={})
Alias for: mkdir_p
move(dir, options={})
Alias for: mv
mv(dir, options={}) click to toggle source

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
Also aliased as: move
noop?() click to toggle source
# File lib/ratch/batch.rb, line 468
def noop?
  @options[:noop] or @options[:dryrun]
end
outofdate?(path) click to toggle source

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
owned?() click to toggle source
# File lib/ratch/batch.rb, line 116
def owned?      ; all?{ |path| FileTest.owned?(path)      } ; end
pathnames()

Return the list of files as Pathname objects.

Alias for: to_a
pipe?() click to toggle source
# File lib/ratch/batch.rb, line 108
def pipe?       ; all?{ |path| FileTest.pipe?(path)       } ; end
pwd() click to toggle source

Present working directory. TODO: Does this make sense?

# File lib/ratch/batch.rb, line 162
def pwd
  @local
end
readable?() click to toggle source
# File lib/ratch/batch.rb, line 103
def readable?   ; all?{ |path| FileTest.readable?(path)   } ; end
readable_real?() click to toggle source
# File lib/ratch/batch.rb, line 127
def readable_real?   ; all?{ |path| FileTest.readable_real?(path)   } ; end
relative?() click to toggle source

TODO: Will this work since all paths are localized?

# File lib/ratch/batch.rb, line 122
def relative?   ; all?{ |path| FileTest.relative?(path)   } ; end
remove(options={})

Alias for rm.

Alias for: rm
rename(options={}, &block) click to toggle source

Convenient alias for map_mv.

# File lib/ratch/batch.rb, line 313
def rename(options={}, &block)
  map_mv(options, &block)
end
rm(options={}) click to toggle source
# File lib/ratch/batch.rb, line 238
def rm(options={})
  list = list.to_a
  fileutils.rm(list, options)
end
Also aliased as: remove
rm_f(options={}) click to toggle source

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
rm_r(options={}) click to toggle source

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
rm_rf(options={}) click to toggle source

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
rmdir(options={}) click to toggle source

Remove every directory.

# File lib/ratch/batch.rb, line 188
def rmdir(options={})
  list.map{ |dir| fileutils.rmdir(dir, options) }
end
safe?() click to toggle source
# File lib/ratch/batch.rb, line 119
def safe?       ; all?{ |path| FileTest.safe?(path)       } ; end
select!(&block) click to toggle source

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
setgid?() click to toggle source
# File lib/ratch/batch.rb, line 113
def setgid?     ; all?{ |path| FileTest.setgid?(path)     } ; end
setuid?() click to toggle source
# File lib/ratch/batch.rb, line 114
def setuid?     ; all?{ |path| FileTest.setuid?(path)     } ; end
size() click to toggle source

Returns the Integer size of the list of files.

# File lib/ratch/batch.rb, line 49
def size
  @file_list.size
end
size?()

An alias for byte_size.

Alias for: byte_size
socket?() click to toggle source
# File lib/ratch/batch.rb, line 115
def socket?     ; all?{ |path| FileTest.socket?(path)     } ; end
stage(dir) click to toggle source

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
sticky?() click to toggle source
# File lib/ratch/batch.rb, line 110
def sticky?     ; all?{ |path| FileTest.sticky?(path)     } ; end
to_a() click to toggle source

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
Also aliased as: pathnames
touch(options={}) click to toggle source

Touch each file.

# File lib/ratch/batch.rb, line 299
def touch(options={})
  #list = list.to_a
  fileutils.touch(list, options)
end
uptodate?(path) click to toggle source

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
verbose?() click to toggle source
# File lib/ratch/batch.rb, line 473
def verbose?
  @options[:verbose] or @options[:dryrun]
end
writable?() click to toggle source
# File lib/ratch/batch.rb, line 117
def writable?   ; all?{ |path| FileTest.writable?(path)   } ; end
writable_real?() click to toggle source
# File lib/ratch/batch.rb, line 125
def writable_real?   ; all?{ |path| FileTest.writable_real?(path)   } ; end
zero?() click to toggle source
# File lib/ratch/batch.rb, line 107
def zero?       ; all?{ |path| FileTest.zero?(path)       } ; end

Private Instance Methods

ensure_safe(map) click to toggle source

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
fileutils() click to toggle source

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
map_send(method, options={}, &block) click to toggle source

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