Class: Ratch::Batch
Overview
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?
Instance Attribute Summary collapse
-
#local ⇒ Object
readonly
Returns the value of attribute local.
Class Method Summary collapse
Instance Method Summary collapse
- #absolute? ⇒ Boolean
- #blockdev? ⇒ Boolean
-
#byte_size ⇒ Object
(also: #size?)
This is called #size in FileTest but must be renamed to avoid the clash with the Enumerable mixin.
- #chardev? ⇒ Boolean
-
#chmod(mode, options = {}) ⇒ Object
Change mode of files.
-
#chmod_r(mode, options = {}) ⇒ Object
Change mode of files, following directories recursively.
-
#chown(user, group, options = {}) ⇒ Object
Change owner of files.
-
#chown_r(user, group, options = {}) ⇒ Object
Change owner of files, following directories recursively.
-
#cp(dir, options = {}) ⇒ Object
(also: #copy)
cp(list, dir, options={}).
-
#cp_r(dir, options = {}) ⇒ Object
cp_r(list, dir, options={}).
-
#directory! ⇒ Object
Limit list to directories.
- #directory? ⇒ Boolean (also: #dir?)
- #dryrun? ⇒ Boolean
-
#each(&block) ⇒ Object
Iterate over pathnames.
-
#entries ⇒ Object
Returns an Array of file paths relative to
local. - #executable? ⇒ Boolean
- #executable_real? ⇒ Boolean
- #exist? ⇒ Boolean
- #exists? ⇒ Boolean
-
#file! ⇒ Object
Limit list to files.
- #file? ⇒ Boolean
-
#file_list ⇒ Object
Returns the the underlying FileList object.
- #grpowned? ⇒ Boolean
- #identical?(other) ⇒ Boolean (also: #compare_file, #cmp)
-
#initialize(local, *patterns) ⇒ Batch
constructor
A new instance of Batch.
-
#install(dir, mode, options = {}) ⇒ Object
Install files to a directory with given mode.
-
#list ⇒ Object
(also: #filenames)
Returns the list of files as Strings, rather than Pathname objects.
-
#ln(dir, options = {}) ⇒ Object
(also: #link)
ln(list, destdir, options={}).
-
#ln_s(dir, options = {}) ⇒ Object
(also: #symlink)
ln_s(list, destdir, options={}).
- #ln_sf(dir, options = {}) ⇒ Object
-
#map_cp(options = {}, &block) ⇒ Object
Copy the list of files in batch, using a block to determine the new file names.
-
#map_cp_r(options = {}, &block) ⇒ Object
Copy the list of files recursively in batch, using a block to determine the new file names.
-
#map_ln(options = {}, &block) ⇒ Object
Hard link the list of files in batch, using a block to determine the new names.
-
#map_ln_s(options = {}, &block) ⇒ Object
Soft link the list of files in batch, using a block to determine the new names.
-
#map_ln_sf(options = {}, &block) ⇒ Object
Force soft linking of the list of files in batch, using a block to determine the new names.
-
#map_mv(options = {}, &block) ⇒ Object
Rename the list of files in batch, using a block to determine the new names.
-
#mkdir(options = {}) ⇒ Object
Make a directory for every entry.
-
#mkdir_p(options = {}) ⇒ Object
(also: #mkpath)
Make a directory for every entry.
-
#mv(dir, options = {}) ⇒ Object
(also: #move)
mv(list, dir, options={}).
- #noop? ⇒ Boolean
-
#outofdate?(path) ⇒ Boolean
Is
pathout-of-date in comparsion to all files in batch. - #owned? ⇒ Boolean
- #pipe? ⇒ Boolean
-
#pwd ⇒ Object
Present working directory.
- #readable? ⇒ Boolean
- #readable_real? ⇒ Boolean
-
#relative? ⇒ Boolean
TODO: Will this work since all paths are localized?.
-
#rename(options = {}, &block) ⇒ Object
Convenient alias for #map_mv.
- #rm(options = {}) ⇒ Object (also: #remove)
-
#rm_f(options = {}) ⇒ Object
Remove, with force option.
-
#rm_r(options = {}) ⇒ Object
Remove, recursively removing the contents of directories.
-
#rm_rf(options = {}) ⇒ Object
Remove with force option, recursively removing the contents of directories.
-
#rmdir(options = {}) ⇒ Object
Remove every directory.
- #safe? ⇒ Boolean
-
#select!(&block) ⇒ Object
Limit list to selection block.
- #setgid? ⇒ Boolean
- #setuid? ⇒ Boolean
-
#size ⇒ Object
Returns the Integer size of the list of files.
- #socket? ⇒ Boolean
-
#stage(dir) ⇒ Object
Stage files.
- #sticky? ⇒ Boolean
- #symlink? ⇒ Boolean
-
#to_a ⇒ Object
(also: #pathnames)
Return the list of files as Pathname objects.
-
#touch(options = {}) ⇒ Object
Touch each file.
-
#uptodate?(path) ⇒ Boolean
Is
pathup-to-date in comparsion to all files in batch. - #verbose? ⇒ Boolean
- #writable? ⇒ Boolean
- #writable_real? ⇒ Boolean
- #zero? ⇒ Boolean
Constructor Details
#initialize(local, *patterns) ⇒ Batch
Returns a new instance of Batch.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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 |
Instance Attribute Details
#local ⇒ Object (readonly)
Returns the value of attribute local.
36 37 38 |
# File 'lib/ratch/batch.rb', line 36 def local @local end |
Class Method Details
.[](*patterns) ⇒ Object
15 16 17 |
# File 'lib/ratch/batch.rb', line 15 def self.[](*patterns) new('.', *patterns) end |
Instance Method Details
#absolute? ⇒ Boolean
123 |
# File 'lib/ratch/batch.rb', line 123 def absolute? ; all?{ |path| FileTest.absolute?(path) } ; end |
#blockdev? ⇒ Boolean
111 |
# File 'lib/ratch/batch.rb', line 111 def blockdev? ; all?{ |path| FileTest.blockdev?(path) } ; end |
#byte_size ⇒ Object Also known as: size?
This is called #size in FileTest but must be renamed to avoid the clash with the Enumerable mixin.
94 95 96 |
# File 'lib/ratch/batch.rb', line 94 def byte_size inject(0){ |sum, path| sum + FileTest.size(path) } end |
#chardev? ⇒ Boolean
104 |
# File 'lib/ratch/batch.rb', line 104 def chardev? ; all?{ |path| FileTest.chardev?(path) } ; end |
#chmod(mode, options = {}) ⇒ Object
Change mode of files.
273 274 275 276 |
# File 'lib/ratch/batch.rb', line 273 def chmod(mode, ={}) #list = list.to_a fileutils.chmod(mode, list, ) end |
#chmod_r(mode, options = {}) ⇒ Object
Change mode of files, following directories recursively.
279 280 281 282 |
# File 'lib/ratch/batch.rb', line 279 def chmod_r(mode, ={}) #list = list.to_a fileutils.chmod_r(mode, list, ) end |
#chown(user, group, options = {}) ⇒ Object
Change owner of files.
286 287 288 289 |
# File 'lib/ratch/batch.rb', line 286 def chown(user, group, ={}) #list = list.to_a fileutils.chown(user, group, list, ) end |
#chown_r(user, group, options = {}) ⇒ Object
Change owner of files, following directories recursively.
292 293 294 295 |
# File 'lib/ratch/batch.rb', line 292 def chown_r(user, group, ={}) #list = list.to_a fileutils.chown_r(user, group, list, ) end |
#cp(dir, options = {}) ⇒ Object Also known as: copy
cp(list, dir, options={})
215 216 217 218 219 |
# File 'lib/ratch/batch.rb', line 215 def cp(dir, ={}) #src = list.to_a #dest = localize(dest) fileutils.cp(list, dir, ) end |
#cp_r(dir, options = {}) ⇒ Object
cp_r(list, dir, options={})
223 224 225 226 227 |
# File 'lib/ratch/batch.rb', line 223 def cp_r(dir, ={}) #src = list.to_a #dest = localize(dest) fileutils.cp_r(list, dir, ) end |
#directory! ⇒ Object
Limit list to directories.
78 79 80 |
# File 'lib/ratch/batch.rb', line 78 def directory! @file_list = @file_list.select{ |f| File.directory?(f) } end |
#directory? ⇒ Boolean Also known as: dir?
101 |
# File 'lib/ratch/batch.rb', line 101 def directory? ; all?{ |path| FileTest.directory?(path) } ; end |
#dryrun? ⇒ Boolean
478 479 480 |
# File 'lib/ratch/batch.rb', line 478 def dryrun? noop? && verbose? end |
#each(&block) ⇒ Object
Iterate over pathnames.
44 45 46 |
# File 'lib/ratch/batch.rb', line 44 def each(&block) @file_list.each{ |file| block.call(Pathname.new(file)) } end |
#entries ⇒ Object
Returns an Array of file paths relative to local.
68 69 70 |
# File 'lib/ratch/batch.rb', line 68 def entries map{ |entry| entry.sub(local.to_s+'/','') } end |
#executable? ⇒ Boolean
118 |
# File 'lib/ratch/batch.rb', line 118 def executable? ; all?{ |path| FileTest.executable?(path) } ; end |
#executable_real? ⇒ Boolean
126 |
# File 'lib/ratch/batch.rb', line 126 def executable_real? ; all?{ |path| FileTest.executable_real?(path) } ; end |
#exist? ⇒ Boolean
105 |
# File 'lib/ratch/batch.rb', line 105 def exist? ; all?{ |path| FileTest.exist?(path) } ; end |
#exists? ⇒ Boolean
106 |
# File 'lib/ratch/batch.rb', line 106 def exists? ; all?{ |path| FileTest.exists?(path) } ; end |
#file! ⇒ Object
Limit list to files.
73 74 75 |
# File 'lib/ratch/batch.rb', line 73 def file! @file_list = @file_list.select{ |f| File.file?(f) } end |
#file? ⇒ Boolean
109 |
# File 'lib/ratch/batch.rb', line 109 def file? ; all?{ |path| FileTest.file?(path) } ; end |
#file_list ⇒ Object
Returns the the underlying FileList object.
39 40 41 |
# File 'lib/ratch/batch.rb', line 39 def file_list @file_list end |
#grpowned? ⇒ Boolean
112 |
# File 'lib/ratch/batch.rb', line 112 def grpowned? ; all?{ |path| FileTest.grpowned?(path) } ; end |
#identical?(other) ⇒ Boolean Also known as: compare_file, cmp
131 132 133 |
# File 'lib/ratch/batch.rb', line 131 def identical?(other) all?{ |path| FileTest.identical?(path, other) } end |
#install(dir, mode, options = {}) ⇒ Object
Install files to a directory with given mode. Unlike #cp, this will not copy the file if an up-to-date copy already exists.
266 267 268 269 270 |
# File 'lib/ratch/batch.rb', line 266 def install(dir, mode, ={}) #src = list.to_a #dest = localize(dest) fileutils.install(list, dir, mode, ) end |
#list ⇒ Object Also known as: filenames
Returns the list of files as Strings, rather than Pathname objects.
62 63 64 |
# File 'lib/ratch/batch.rb', line 62 def list @file_list.to_a end |
#ln(dir, options = {}) ⇒ Object Also known as: link
ln(list, destdir, options={})
193 194 195 196 197 |
# File 'lib/ratch/batch.rb', line 193 def ln(dir, ={}) #src = list.to_a #new = localize(new) fileutils.ln(list, dir, ) end |
#ln_s(dir, options = {}) ⇒ Object Also known as: symlink
ln_s(list, destdir, options={})
201 202 203 204 205 |
# File 'lib/ratch/batch.rb', line 201 def ln_s(dir, ={}) #src = list.to_a #new = localize(new) fileutils.ln_s(list, dir, ) end |
#ln_sf(dir, options = {}) ⇒ Object
208 209 210 211 212 |
# File 'lib/ratch/batch.rb', line 208 def ln_sf(dir, ={}) #src = list.to_a #new = localize(new) fileutils.ln_sf(list, dir, ) end |
#map_cp(options = {}, &block) ⇒ Object
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.
361 362 363 |
# File 'lib/ratch/batch.rb', line 361 def map_cp(={}, &block) map_send(:cp, ={}, &block) end |
#map_cp_r(options = {}, &block) ⇒ Object
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.
370 371 372 |
# File 'lib/ratch/batch.rb', line 370 def map_cp_r(={}, &block) map_send(:cp_r, ={}, &block) end |
#map_ln(options = {}, &block) ⇒ Object
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.
334 335 336 |
# File 'lib/ratch/batch.rb', line 334 def map_ln(={}, &block) map_send(:ln, ={}, &block) end |
#map_ln_s(options = {}, &block) ⇒ Object
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.
342 343 344 |
# File 'lib/ratch/batch.rb', line 342 def map_ln_s(={}, &block) map_send(:ln_s, ={}, &block) end |
#map_ln_sf(options = {}, &block) ⇒ Object
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.
352 353 354 |
# File 'lib/ratch/batch.rb', line 352 def map_ln_sf(={}, &block) map_send(:ln_sf, ={}, &block) end |
#map_mv(options = {}, &block) ⇒ Object
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.
326 327 328 |
# File 'lib/ratch/batch.rb', line 326 def map_mv(={}, &block) @file_list = map_send(:mv, ={}, &block) end |
#mkdir(options = {}) ⇒ Object
Make a directory for every entry.
167 168 169 170 171 172 173 174 |
# File 'lib/ratch/batch.rb', line 167 def mkdir(={}) list.each do |dir| if File.exist?(dir) raise Errno::EEXIST, "File exists - #{dir}" end end list.map{ |dir| fileutils.mkdir(dir, ) } end |
#mkdir_p(options = {}) ⇒ Object Also known as: mkpath
Make a directory for every entry.
177 178 179 180 181 182 183 184 |
# File 'lib/ratch/batch.rb', line 177 def mkdir_p(={}) 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, ) } end |
#mv(dir, options = {}) ⇒ Object Also known as: move
mv(list, dir, options={})
230 231 232 233 234 |
# File 'lib/ratch/batch.rb', line 230 def mv(dir, ={}) #src = list.to_a #dest = localize(dest) fileutils.mv(list, dir, ) end |
#noop? ⇒ Boolean
468 469 470 |
# File 'lib/ratch/batch.rb', line 468 def noop? @options[:noop] or @options[:dryrun] end |
#outofdate?(path) ⇒ Boolean
Is path out-of-date in comparsion to all files in batch.
458 459 460 |
# File 'lib/ratch/batch.rb', line 458 def outofdate?(path) fileutils.outofdate?(path, to_a) end |
#owned? ⇒ Boolean
116 |
# File 'lib/ratch/batch.rb', line 116 def owned? ; all?{ |path| FileTest.owned?(path) } ; end |
#pipe? ⇒ Boolean
108 |
# File 'lib/ratch/batch.rb', line 108 def pipe? ; all?{ |path| FileTest.pipe?(path) } ; end |
#pwd ⇒ Object
Present working directory. TODO: Does this make sense?
162 163 164 |
# File 'lib/ratch/batch.rb', line 162 def pwd @local end |
#readable? ⇒ Boolean
103 |
# File 'lib/ratch/batch.rb', line 103 def readable? ; all?{ |path| FileTest.readable?(path) } ; end |
#readable_real? ⇒ Boolean
127 |
# File 'lib/ratch/batch.rb', line 127 def readable_real? ; all?{ |path| FileTest.readable_real?(path) } ; end |
#relative? ⇒ Boolean
TODO: Will this work since all paths are localized?
122 |
# File 'lib/ratch/batch.rb', line 122 def relative? ; all?{ |path| FileTest.relative?(path) } ; end |
#rename(options = {}, &block) ⇒ Object
Convenient alias for #map_mv.
313 314 315 |
# File 'lib/ratch/batch.rb', line 313 def rename(={}, &block) map_mv(, &block) end |
#rm(options = {}) ⇒ Object Also known as: remove
238 239 240 241 |
# File 'lib/ratch/batch.rb', line 238 def rm(={}) list = list.to_a fileutils.rm(list, ) end |
#rm_f(options = {}) ⇒ Object
Remove, with force option.
253 254 255 256 |
# File 'lib/ratch/batch.rb', line 253 def rm_f(={}) #list = list.to_a fileutils.rm_f(list, ) end |
#rm_r(options = {}) ⇒ Object
Remove, recursively removing the contents of directories.
247 248 249 250 |
# File 'lib/ratch/batch.rb', line 247 def rm_r(={}) #list = list.to_a fileutils.rm_r(list, ) end |
#rm_rf(options = {}) ⇒ Object
Remove with force option, recursively removing the contents of directories.
259 260 261 262 |
# File 'lib/ratch/batch.rb', line 259 def rm_rf(={}) #list = list.to_a fileutils.rm_rf(list, ) end |
#rmdir(options = {}) ⇒ Object
Remove every directory.
188 189 190 |
# File 'lib/ratch/batch.rb', line 188 def rmdir(={}) list.map{ |dir| fileutils.rmdir(dir, ) } end |
#safe? ⇒ Boolean
119 |
# File 'lib/ratch/batch.rb', line 119 def safe? ; all?{ |path| FileTest.safe?(path) } ; end |
#select!(&block) ⇒ Object
Limit list to selection block.
83 84 85 86 |
# 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? ⇒ Boolean
113 |
# File 'lib/ratch/batch.rb', line 113 def setgid? ; all?{ |path| FileTest.setgid?(path) } ; end |
#setuid? ⇒ Boolean
114 |
# File 'lib/ratch/batch.rb', line 114 def setuid? ; all?{ |path| FileTest.setuid?(path) } ; end |
#size ⇒ Object
Returns the Integer size of the list of files.
49 50 51 |
# File 'lib/ratch/batch.rb', line 49 def size @file_list.size end |
#socket? ⇒ Boolean
115 |
# File 'lib/ratch/batch.rb', line 115 def socket? ; all?{ |path| FileTest.socket?(path) } ; end |
#stage(dir) ⇒ Object
Stage files. This is like #install but uses hardlinks.
305 306 307 308 309 310 |
# 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? ⇒ Boolean
110 |
# File 'lib/ratch/batch.rb', line 110 def sticky? ; all?{ |path| FileTest.sticky?(path) } ; end |
#symlink? ⇒ Boolean
102 |
# File 'lib/ratch/batch.rb', line 102 def symlink? ; all?{ |path| FileTest.symlink?(path) } ; end |
#to_a ⇒ Object Also known as: pathnames
Return the list of files as Pathname objects.
54 55 56 |
# File 'lib/ratch/batch.rb', line 54 def to_a @file_list.map{ |file| Pathname.new(file) } end |
#touch(options = {}) ⇒ Object
Touch each file.
299 300 301 302 |
# File 'lib/ratch/batch.rb', line 299 def touch(={}) #list = list.to_a fileutils.touch(list, ) end |
#uptodate?(path) ⇒ Boolean
Is path up-to-date in comparsion to all files in batch.
463 464 465 |
# File 'lib/ratch/batch.rb', line 463 def uptodate?(path) fileutils.uptodate?(path, to_a) end |
#verbose? ⇒ Boolean
473 474 475 |
# File 'lib/ratch/batch.rb', line 473 def verbose? @options[:verbose] or @options[:dryrun] end |
#writable? ⇒ Boolean
117 |
# File 'lib/ratch/batch.rb', line 117 def writable? ; all?{ |path| FileTest.writable?(path) } ; end |
#writable_real? ⇒ Boolean
125 |
# File 'lib/ratch/batch.rb', line 125 def writable_real? ; all?{ |path| FileTest.writable_real?(path) } ; end |
#zero? ⇒ Boolean
107 |
# File 'lib/ratch/batch.rb', line 107 def zero? ; all?{ |path| FileTest.zero?(path) } ; end |