Class: Ratch::Batch

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/ratch/batch.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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

#localObject (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

Returns:

  • (Boolean)


123
# File 'lib/ratch/batch.rb', line 123

def absolute?   ; all?{ |path| FileTest.absolute?(path)   } ; end

#blockdev?Boolean

Returns:

  • (Boolean)


111
# File 'lib/ratch/batch.rb', line 111

def blockdev?   ; all?{ |path| FileTest.blockdev?(path)   } ; end

#byte_sizeObject 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

Returns:

  • (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, options={})
  #list = list.to_a
  fileutils.chmod(mode, list, options)
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, options={})
  #list = list.to_a
  fileutils.chmod_r(mode, list, options)
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, options={})
  #list = list.to_a
  fileutils.chown(user, group, list, options)
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, options={})
  #list = list.to_a
  fileutils.chown_r(user, group, list, options)
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, options={})
  #src  = list.to_a
  #dest = localize(dest)
  fileutils.cp(list, dir, options)
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, options={})
  #src  = list.to_a
  #dest = localize(dest)
  fileutils.cp_r(list, dir, options)
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?

Returns:

  • (Boolean)


101
# File 'lib/ratch/batch.rb', line 101

def directory?  ; all?{ |path| FileTest.directory?(path)  } ; end

#dryrun?Boolean

Returns:

  • (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

#entriesObject

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

Returns:

  • (Boolean)


118
# File 'lib/ratch/batch.rb', line 118

def executable? ; all?{ |path| FileTest.executable?(path) } ; end

#executable_real?Boolean

Returns:

  • (Boolean)


126
# File 'lib/ratch/batch.rb', line 126

def executable_real? ; all?{ |path| FileTest.executable_real?(path) } ; end

#exist?Boolean

Returns:

  • (Boolean)


105
# File 'lib/ratch/batch.rb', line 105

def exist?      ; all?{ |path| FileTest.exist?(path)      } ; end

#exists?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


109
# File 'lib/ratch/batch.rb', line 109

def file?       ; all?{ |path| FileTest.file?(path)       } ; end

#file_listObject

Returns the the underlying FileList object.



39
40
41
# File 'lib/ratch/batch.rb', line 39

def file_list
  @file_list
end

#grpowned?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


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, options={})
  #src = list.to_a
  #dest = localize(dest)
  fileutils.install(list, dir, mode, options)
end

#listObject 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, options={})
  #src = list.to_a
  #new = localize(new)
  fileutils.ln(list, dir, options)
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, options={})
  #src = list.to_a
  #new = localize(new)
  fileutils.ln_s(list, dir, options)
end

#ln_sf(dir, options = {}) ⇒ Object



208
209
210
211
212
# 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) ⇒ 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(options={}, &block)
  map_send(:cp, options={}, &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(options={}, &block)
  map_send(:cp_r, options={}, &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(options={}, &block)
  map_send(:ln, options={}, &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(options={}, &block)
  map_send(:ln_s, options={}, &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(options={}, &block)
  map_send(:ln_sf, options={}, &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(options={}, &block)
  @file_list = map_send(:mv, options={}, &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(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 = {}) ⇒ 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(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(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, options={})
  #src  = list.to_a
  #dest = localize(dest)
  fileutils.mv(list, dir, options)
end

#noop?Boolean

Returns:

  • (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.

Returns:

  • (Boolean)


458
459
460
# File 'lib/ratch/batch.rb', line 458

def outofdate?(path)
  fileutils.outofdate?(path, to_a)
end

#owned?Boolean

Returns:

  • (Boolean)


116
# File 'lib/ratch/batch.rb', line 116

def owned?      ; all?{ |path| FileTest.owned?(path)      } ; end

#pipe?Boolean

Returns:

  • (Boolean)


108
# File 'lib/ratch/batch.rb', line 108

def pipe?       ; all?{ |path| FileTest.pipe?(path)       } ; end

#pwdObject

Present working directory. TODO: Does this make sense?



162
163
164
# File 'lib/ratch/batch.rb', line 162

def pwd
  @local
end

#readable?Boolean

Returns:

  • (Boolean)


103
# File 'lib/ratch/batch.rb', line 103

def readable?   ; all?{ |path| FileTest.readable?(path)   } ; end

#readable_real?Boolean

Returns:

  • (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?

Returns:

  • (Boolean)


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(options={}, &block)
  map_mv(options, &block)
end

#rm(options = {}) ⇒ Object Also known as: remove



238
239
240
241
# File 'lib/ratch/batch.rb', line 238

def rm(options={})
  list = list.to_a
  fileutils.rm(list, options)
end

#rm_f(options = {}) ⇒ Object

Remove, with force option.



253
254
255
256
# File 'lib/ratch/batch.rb', line 253

def rm_f(options={})
  #list = list.to_a
  fileutils.rm_f(list, options)
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(options={})
  #list = list.to_a
  fileutils.rm_r(list, options)
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(options={})
  #list = list.to_a
  fileutils.rm_rf(list, options)
end

#rmdir(options = {}) ⇒ Object

Remove every directory.



188
189
190
# File 'lib/ratch/batch.rb', line 188

def rmdir(options={})
  list.map{ |dir| fileutils.rmdir(dir, options) }
end

#safe?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


113
# File 'lib/ratch/batch.rb', line 113

def setgid?     ; all?{ |path| FileTest.setgid?(path)     } ; end

#setuid?Boolean

Returns:

  • (Boolean)


114
# File 'lib/ratch/batch.rb', line 114

def setuid?     ; all?{ |path| FileTest.setuid?(path)     } ; end

#sizeObject

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

Returns:

  • (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

Returns:

  • (Boolean)


110
# File 'lib/ratch/batch.rb', line 110

def sticky?     ; all?{ |path| FileTest.sticky?(path)     } ; end

#symlink?Boolean

Returns:

  • (Boolean)


102
# File 'lib/ratch/batch.rb', line 102

def symlink?    ; all?{ |path| FileTest.symlink?(path)    } ; end

#to_aObject 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(options={})
  #list = list.to_a
  fileutils.touch(list, options)
end

#uptodate?(path) ⇒ Boolean

Is path up-to-date in comparsion to all files in batch.

Returns:

  • (Boolean)


463
464
465
# File 'lib/ratch/batch.rb', line 463

def uptodate?(path)
  fileutils.uptodate?(path, to_a)
end

#verbose?Boolean

Returns:

  • (Boolean)


473
474
475
# File 'lib/ratch/batch.rb', line 473

def verbose?
  @options[:verbose] or @options[:dryrun]
end

#writable?Boolean

Returns:

  • (Boolean)


117
# File 'lib/ratch/batch.rb', line 117

def writable?   ; all?{ |path| FileTest.writable?(path)   } ; end

#writable_real?Boolean

Returns:

  • (Boolean)


125
# File 'lib/ratch/batch.rb', line 125

def writable_real?   ; all?{ |path| FileTest.writable_real?(path)   } ; end

#zero?Boolean

Returns:

  • (Boolean)


107
# File 'lib/ratch/batch.rb', line 107

def zero?       ; all?{ |path| FileTest.zero?(path)       } ; end