In Files
Parent
Methods
- ::[]
- ::new
- #absolute?
- #blockdev?
- #byte_size
- #chardev?
- #chmod
- #chmod_r
- #chown
- #chown_r
- #cmp
- #compare_file
- #copy
- #cp
- #cp_r
- #dir?
- #directory!
- #directory?
- #dryrun?
- #each
- #ensure_safe
- #entries
- #executable?
- #executable_real?
- #exist?
- #exists?
- #file!
- #file?
- #file_list
- #filenames
- #fileutils
- #grpowned?
- #identical?
- #install
- #link
- #list
- #ln
- #ln_s
- #ln_sf
- #map_cp
- #map_cp_r
- #map_ln
- #map_ln_s
- #map_ln_sf
- #map_mv
- #map_send
- #mkdir
- #mkdir_p
- #mkpath
- #move
- #mv
- #noop?
- #outofdate?
- #owned?
- #pathnames
- #pipe?
- #pwd
- #readable?
- #readable_real?
- #relative?
- #remove
- #rename
- #rm
- #rm_f
- #rm_r
- #rm_rf
- #rmdir
- #safe?
- #select!
- #setgid?
- #setuid?
- #size
- #size?
- #socket?
- #stage
- #sticky?
- #symlink
- #symlink?
- #to_a
- #touch
- #uptodate?
- #verbose?
- #writable?
- #writable_real?
- #zero?
Included Modules
- Enumerable
Information
Class Index
![show/hide quicksearch [+]](../assets/icon/find.png)
- Ratch
- Ratch::Batch
- Ratch::CLI
- Ratch::ConfigUtils
- Ratch::Console
- Ratch::EmailUtils
- Ratch::Emailer
- Ratch::FTPUtils
- Ratch::FileList
- Ratch::FileNotFound
- Ratch::Help
- Ratch::POMUtils
- Ratch::RDocUtils
- Ratch::Script
- Ratch::Shell
- Ratch::System
- Ratch::TarUtils
- Ratch::XDGUtils
- Ratch::ZlibUtils
- FileTest
- Hash
- NilClass
- Object
- String
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
Public Class Methods
# File lib/ratch/batch.rb, line 15
15: def self.[](*patterns)
16: new('.', *patterns)
17: end
# 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
# File lib/ratch/batch.rb, line 123
123: def absolute? ; all?{ |path| FileTest.absolute?(path) } ; end
# File lib/ratch/batch.rb, line 111
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
94: def byte_size
95: inject(0){ |sum, path| sum + FileTest.size(path) }
96: end
# File lib/ratch/batch.rb, line 104
104: def chardev? ; all?{ |path| FileTest.chardev?(path) } ; end
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
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
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
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
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
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
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
# File lib/ratch/batch.rb, line 101
101: def directory? ; all?{ |path| FileTest.directory?(path) } ; end
# File lib/ratch/batch.rb, line 478
478: def dryrun?
479: noop? && verbose?
480: end
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
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
# File lib/ratch/batch.rb, line 118
118: def executable? ; all?{ |path| FileTest.executable?(path) } ; end
# File lib/ratch/batch.rb, line 126
126: def executable_real? ; all?{ |path| FileTest.executable_real?(path) } ; end
# File lib/ratch/batch.rb, line 105
105: def exist? ; all?{ |path| FileTest.exist?(path) } ; end
# File lib/ratch/batch.rb, line 106
106: def exists? ; all?{ |path| FileTest.exists?(path) } ; end
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 lib/ratch/batch.rb, line 109
109: def file? ; all?{ |path| FileTest.file?(path) } ; end
Returns the the underlying FileList object.
# File lib/ratch/batch.rb, line 39
39: def file_list
40: @file_list
41: end
# File lib/ratch/batch.rb, line 112
112: def grpowned? ; all?{ |path| FileTest.grpowned?(path) } ; end
# File lib/ratch/batch.rb, line 131
131: def identical?(other)
132: all?{ |path| FileTest.identical?(path, other) }
133: 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
266: def install(dir, mode, options={})
267: #src = list.to_a
268: #dest = localize(dest)
269: fileutils.install(list, dir, mode, options)
270: end
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
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
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
# 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
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
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
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
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
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
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
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
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
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
# File lib/ratch/batch.rb, line 468
468: def noop?
469: @options[:noop] or @options[:dryrun]
470: end
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
# File lib/ratch/batch.rb, line 116
116: def owned? ; all?{ |path| FileTest.owned?(path) } ; end
# File lib/ratch/batch.rb, line 108
108: def pipe? ; all?{ |path| FileTest.pipe?(path) } ; end
Present working directory. TODO: Does this make sense?
# File lib/ratch/batch.rb, line 162
162: def pwd
163: @local
164: end
# File lib/ratch/batch.rb, line 103
103: def readable? ; all?{ |path| FileTest.readable?(path) } ; end
# File lib/ratch/batch.rb, line 127
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
122: def relative? ; all?{ |path| FileTest.relative?(path) } ; end
Convenient alias for map_mv.
# File lib/ratch/batch.rb, line 313
313: def rename(options={}, &block)
314: map_mv(options, &block)
315: end
# File lib/ratch/batch.rb, line 238
238: def rm(options={})
239: list = list.to_a
240: fileutils.rm(list, options)
241: end
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
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
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
Remove every directory.
# File lib/ratch/batch.rb, line 188
188: def rmdir(options={})
189: list.map{ |dir| fileutils.rmdir(dir, options) }
190: end
# File lib/ratch/batch.rb, line 119
119: def safe? ; all?{ |path| FileTest.safe?(path) } ; end
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
# File lib/ratch/batch.rb, line 113
113: def setgid? ; all?{ |path| FileTest.setgid?(path) } ; end
# File lib/ratch/batch.rb, line 114
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
49: def size
50: @file_list.size
51: end
# File lib/ratch/batch.rb, line 115
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
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
# File lib/ratch/batch.rb, line 110
110: def sticky? ; all?{ |path| FileTest.sticky?(path) } ; end
# File lib/ratch/batch.rb, line 102
102: def symlink? ; all?{ |path| FileTest.symlink?(path) } ; end
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
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
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
# File lib/ratch/batch.rb, line 473
473: def verbose?
474: @options[:verbose] or @options[:dryrun]
475: end
# File lib/ratch/batch.rb, line 117
117: def writable? ; all?{ |path| FileTest.writable?(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
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
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
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.