Ratch  Ratch::Batch

[Validate]
Generated with Newfish 0.1.0

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
15:     def self.[](*patterns)
16:       new('.', *patterns)
17:     end
new(local, *patterns) click to toggle source
    # File lib/ratch/batch.rb, line 20
20:     def initialize(local, *patterns)
21:       @local   = Pathname.new(local)
22:       @options = (Hash === patterns.last ? patterns.pop : {}).rekey(&:to_sym)
23: 
24:       @file_list = FileList.all
25: 
26:       patterns.each do |pattern|
27:         if @local == Pathname.new('.')
28:           @file_list.add(pattern)
29:         else
30:           @file_list.add(File.join(local,pattern))
31:         end
32:       end
33:     end

Public Instance Methods

absolute?() click to toggle source
     # File lib/ratch/batch.rb, line 123
123:     def absolute?   ; all?{ |path| FileTest.absolute?(path)   } ; end
blockdev?() click to toggle source
     # File lib/ratch/batch.rb, line 111
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
94:     def byte_size
95:       inject(0){ |sum, path| sum + FileTest.size(path) }
96:     end
Also aliased as: size?
chardev?() click to toggle source
     # File lib/ratch/batch.rb, line 104
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
273:     def chmod(mode, options={})
274:       #list = list.to_a
275:       fileutils.chmod(mode, list, options)
276:     end
chmod_r(mode, options={}) click to toggle source

Change mode of files, following directories recursively.

     # File lib/ratch/batch.rb, line 279
279:     def chmod_r(mode, options={})
280:       #list = list.to_a
281:       fileutils.chmod_r(mode, list, options)
282:     end
chown(user, group, options={}) click to toggle source

Change owner of files.

     # File lib/ratch/batch.rb, line 286
286:     def chown(user, group, options={})
287:       #list = list.to_a
288:       fileutils.chown(user, group, list, options)
289:     end
chown_r(user, group, options={}) click to toggle source

Change owner of files, following directories recursively.

     # File lib/ratch/batch.rb, line 292
292:     def chown_r(user, group, options={})
293:       #list = list.to_a
294:       fileutils.chown_r(user, group, list, options)
295:     end
cmp(other) click to toggle source
compare_file(other) click to toggle source

TODO: Really?

copy(dir, options={}) click to toggle source
cp(dir, options={}) click to toggle source

cp(list, dir, options={})

     # File lib/ratch/batch.rb, line 215
215:     def cp(dir, options={})
216:       #src  = list.to_a
217:       #dest = localize(dest)
218:       fileutils.cp(list, dir, options)
219:     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
223:     def cp_r(dir, options={})
224:       #src  = list.to_a
225:       #dest = localize(dest)
226:       fileutils.cp_r(list, dir, options)
227:     end
dir?() click to toggle source
directory!() click to toggle source

Limit list to directories.

    # File lib/ratch/batch.rb, line 78
78:     def directory!
79:       @file_list = @file_list.select{ |f| File.directory?(f) }
80:     end
directory?() click to toggle source
     # File lib/ratch/batch.rb, line 101
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
478:     def dryrun?
479:       noop? && verbose?
480:     end
each(&block) click to toggle source

Iterate over pathnames.

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

Returns an Array of file paths relative to local.

    # File lib/ratch/batch.rb, line 68
68:     def entries
69:       map{ |entry| entry.sub(local.to_s+'/','') }
70:     end
executable?() click to toggle source
     # File lib/ratch/batch.rb, line 118
118:     def executable? ; all?{ |path| FileTest.executable?(path) } ; end
executable_real?() click to toggle source
     # File lib/ratch/batch.rb, line 126
126:     def executable_real? ; all?{ |path| FileTest.executable_real?(path) } ; end
exist?() click to toggle source
     # File lib/ratch/batch.rb, line 105
105:     def exist?      ; all?{ |path| FileTest.exist?(path)      } ; end
exists?() click to toggle source
     # File lib/ratch/batch.rb, line 106
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
73:     def file!
74:       @file_list = @file_list.select{ |f| File.file?(f) }
75:     end
file?() click to toggle source
     # File lib/ratch/batch.rb, line 109
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
39:     def file_list
40:       @file_list
41:     end
filenames() click to toggle source
grpowned?() click to toggle source
     # File lib/ratch/batch.rb, line 112
112:     def grpowned?   ; all?{ |path| FileTest.grpowned?(path)   } ; end
identical?(other) click to toggle source
     # File lib/ratch/batch.rb, line 131
131:     def identical?(other)
132:       all?{ |path| FileTest.identical?(path, other) }
133:     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
266:     def install(dir, mode, options={})
267:       #src = list.to_a
268:       #dest = localize(dest)
269:       fileutils.install(list, dir, mode, options)
270:     end
list() click to toggle source

Returns the list of files as Strings, rather than Pathname objects.

    # File lib/ratch/batch.rb, line 62
62:     def list
63:       @file_list.to_a
64:     end
Also aliased as: filenames
ln(dir, options={}) click to toggle source

ln(list, destdir, options={})

     # File lib/ratch/batch.rb, line 193
193:     def ln(dir, options={})
194:       #src = list.to_a
195:       #new = localize(new)
196:       fileutils.ln(list, dir, options)
197:     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
201:     def ln_s(dir, options={})
202:       #src = list.to_a
203:       #new = localize(new)
204:       fileutils.ln_s(list, dir, options)
205:     end
Also aliased as: symlink
ln_sf(dir, options={}) click to toggle source
     # File lib/ratch/batch.rb, line 208
208:     def ln_sf(dir, options={})
209:       #src = list.to_a
210:       #new = localize(new)
211:       fileutils.ln_sf(list, dir, options)
212:     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
361:     def map_cp(options={}, &block)
362:       map_send(:cp, options={}, &block)
363:     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
370:     def map_cp_r(options={}, &block)
371:       map_send(:cp_r, options={}, &block)
372:     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
334:     def map_ln(options={}, &block)
335:       map_send(:ln, options={}, &block)
336:     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
342:     def map_ln_s(options={}, &block)
343:       map_send(:ln_s, options={}, &block)
344:     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
352:     def map_ln_sf(options={}, &block)
353:       map_send(:ln_sf, options={}, &block)
354:     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
326:     def map_mv(options={}, &block)
327:       @file_list = map_send(:mv, options={}, &block)
328:     end
mkdir(options={}) click to toggle source

Make a directory for every entry.

     # File lib/ratch/batch.rb, line 167
167:     def mkdir(options={})
168:       list.each do |dir|
169:         if File.exist?(dir)
170:           raise Errno::EEXIST, "File exists - #{dir}"
171:         end
172:       end
173:       list.map{ |dir| fileutils.mkdir(dir, options) }
174:     end
mkdir_p(options={}) click to toggle source

Make a directory for every entry.

     # File lib/ratch/batch.rb, line 177
177:     def mkdir_p(options={})
178:       list.each do |dir|
179:         if File.exist?(dir) && !File.directory?(dir)
180:           raise Errno::EEXIST, "File exists - #{dir}"
181:         end
182:       end
183:       list.map{ |dir| fileutils.mkdir_p(dir, options) }
184:     end
Also aliased as: mkpath
mkpath(options={}) click to toggle source
move(dir, options={}) click to toggle source
mv(dir, options={}) click to toggle source

mv(list, dir, options={})

     # File lib/ratch/batch.rb, line 230
230:     def mv(dir, options={})
231:       #src  = list.to_a
232:       #dest = localize(dest)
233:       fileutils.mv(list, dir, options)
234:     end
Also aliased as: move
noop?() click to toggle source
     # File lib/ratch/batch.rb, line 468
468:     def noop?
469:       @options[:noop] or @options[:dryrun]
470:     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
458:     def outofdate?(path)
459:       fileutils.outofdate?(path, to_a)
460:     end
owned?() click to toggle source
     # File lib/ratch/batch.rb, line 116
116:     def owned?      ; all?{ |path| FileTest.owned?(path)      } ; end
pathnames() click to toggle source

Return the list of files as Pathname objects.

pipe?() click to toggle source
     # File lib/ratch/batch.rb, line 108
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
162:     def pwd
163:       @local
164:     end
readable?() click to toggle source
     # File lib/ratch/batch.rb, line 103
103:     def readable?   ; all?{ |path| FileTest.readable?(path)   } ; end
readable_real?() click to toggle source
     # File lib/ratch/batch.rb, line 127
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
122:     def relative?   ; all?{ |path| FileTest.relative?(path)   } ; end
remove(options={}) click to toggle source

Alias for rm.

rename(options={}, &block) click to toggle source

Convenient alias for map_mv.

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

Remove, with force option.

     # File lib/ratch/batch.rb, line 253
253:     def rm_f(options={})
254:       #list = list.to_a
255:       fileutils.rm_f(list, options)
256:     end
rm_r(options={}) click to toggle source

Remove, recursively removing the contents of directories.

     # File lib/ratch/batch.rb, line 247
247:     def rm_r(options={})
248:       #list = list.to_a
249:       fileutils.rm_r(list, options)
250:     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
259:     def rm_rf(options={})
260:       #list = list.to_a
261:       fileutils.rm_rf(list, options)
262:     end
rmdir(options={}) click to toggle source

Remove every directory.

     # File lib/ratch/batch.rb, line 188
188:     def rmdir(options={})
189:       list.map{ |dir| fileutils.rmdir(dir, options) }
190:     end
safe?() click to toggle source
     # File lib/ratch/batch.rb, line 119
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
83:     def select!(&block)
84:       #@file_list = FileList.all(*to_a.select{ |f| block.call(f) })
85:       @file_list = @file_list.select{ |f| block.call(f) }
86:     end
setgid?() click to toggle source
     # File lib/ratch/batch.rb, line 113
113:     def setgid?     ; all?{ |path| FileTest.setgid?(path)     } ; end
setuid?() click to toggle source
     # File lib/ratch/batch.rb, line 114
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
49:     def size
50:       @file_list.size
51:     end
size?() click to toggle source

An alias for byte_size.

socket?() click to toggle source
     # File lib/ratch/batch.rb, line 115
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
305:     def stage(dir)
306:       #dir   = localize(directory)
307:       #files = localize(files)
308:       #list = list.to_a
309:       fileutils.stage(dir, local, list)
310:     end
sticky?() click to toggle source
     # File lib/ratch/batch.rb, line 110
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
54:     def to_a
55:       @file_list.map{ |file| Pathname.new(file) }
56:     end
Also aliased as: pathnames
touch(options={}) click to toggle source

Touch each file.

     # File lib/ratch/batch.rb, line 299
299:     def touch(options={})
300:       #list = list.to_a
301:       fileutils.touch(list, options)
302:     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
463:     def uptodate?(path)
464:       fileutils.uptodate?(path, to_a)
465:     end
verbose?() click to toggle source
     # File lib/ratch/batch.rb, line 473
473:     def verbose?
474:       @options[:verbose] or @options[:dryrun]
475:     end
writable?() click to toggle source
     # File lib/ratch/batch.rb, line 117
117:     def writable?   ; all?{ |path| FileTest.writable?(path)   } ; end
writable_real?() click to toggle source
     # File lib/ratch/batch.rb, line 125
125:     def writable_real?   ; all?{ |path| FileTest.writable_real?(path)   } ; end
zero?() click to toggle source
     # File lib/ratch/batch.rb, line 107
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
416:     def ensure_safe(map)
417:       map.each do |src, dest|
418:         raise unless File.exist?(src)
419:         if File.directory?(src)
420:           if File.directory?(dest)
421:             new_map = {}
422:             Dir.entries(src).each do |e|
423:               next if e == '.' or e == '..'
424:               new_map[File.join(src, e)] = File.join(dest, e)
425:             end
426:             ensure_safe_destination(new_map)
427:           else
428:             raise Errno::EISDIR, "Is a directory - #{src}"
429:           end
430:         else
431:           if File.directory?(dest)
432:             check = File.join(dest, src)
433:             if File.exist?(check)
434:               raise Errno::EEXIST, "File exists - #{check}"
435:             end
436:           else
437:             raise Errno::EEXIST, "File exists - #{dest}" if File.exist?(dest)
438:           end
439:         end
440:       end
441:     end
fileutils() click to toggle source

Returns FileUtils module based on mode.

     # File lib/ratch/batch.rb, line 485
485:     def fileutils
486:       if dryrun?
487:         ::FileUtils::DryRun
488:       elsif noop?
489:         ::FileUtils::Noop
490:       elsif verbose?
491:         ::FileUtils::Verbose
492:       else
493:         ::FileUtils
494:       end
495:     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
393:     def map_send(method, options={}, &block)
394: 
395:       map = {}
396:       list.each do |file|
397:         if dest = block.call(file)
398:           map[file] = dest
399:         end
400:       end
401: 
402:       if options[:safe] or !options[:force] or SAFE_METHODS.include?(method) 
403:         ensure_safe(map)
404:       end
405: 
406:       map.each do |src, dest|
407:         fileutils.__send__(method, src, dest, options)
408:       end
409:       FileList.all(*map.values)
410:     end

Disabled; run with --debug to generate this.