Module: AdvFileUtils
- Included in:
- FileUtils
- Defined in:
- lib/advanced-fileutils.rb
Defined Under Namespace
Classes: CommandError, Error, FileLockError
Constant Summary collapse
- OPT_TABLE =
Hash table to hold command options.
{}
Class Method Summary collapse
-
.append(filename, *data_and_options, &block) ⇒ Object
Options: verbose, noop, force, backup.
-
.edit(filename, *data_and_options) ⇒ Object
Options: verbose, noop, force, backup.
-
.replace(filename, *data_and_options) ⇒ Object
Options: verbose, noop, force, backup, lockfile, retry.
-
.sh ⇒ Object
Options: verbose, noop.
-
.shell ⇒ Object
Options: verbose, noop.
-
.system(*command_and_options) ⇒ Object
Options: verbose, noop.
-
.truncate(filename, *data_and_options, &block) ⇒ Object
Options: verbose, noop, force, backup.
Instance Method Summary collapse
-
#append(filename, *data_and_options, &block) ⇒ Object
Options: verbose, noop, force, backup.
-
#edit(filename, *data_and_options) ⇒ Object
Options: verbose, noop, force, backup.
-
#replace(filename, *data_and_options) ⇒ Object
Options: verbose, noop, force, backup, lockfile, retry.
-
#system(*command_and_options) ⇒ Object
(also: #sh, #shell)
Options: verbose, noop.
-
#truncate(filename, *data_and_options, &block) ⇒ Object
Options: verbose, noop, force, backup.
Class Method Details
.append(filename, *data_and_options, &block) ⇒ Object
Options: verbose, noop, force, backup
Append the given data
to the file named by filename
.
If called with a block then the File object is yielded to the block for appending data intead of the data being passed as an argument.
AdvFileUtils.append('data.log', "some data for log entry\n")
AdvFileUtils.append('data.log') { |f| f.puts "some data for log entry" }
175 176 177 |
# File 'lib/advanced-fileutils.rb', line 175 def append (filename, *, &block) generic_write 'a', filename, *, &block end |
.edit(filename, *data_and_options) ⇒ Object
Options: verbose, noop, force, backup
Invoke an external editor to edit some text or a file.
edit(filename, )
edit(filename, data, )
edit(nil, data, )
Return values
true, if successful and file was edited
false, if successful and file was not edited
nil, if successful and file was not saved
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/advanced-fileutils.rb', line 265 def edit (filename, *) data, = *() editor = ENV.has_key?('VISUAL') ? ENV['VISUAL'] : ENV.has_key?('EDITOR') ? ENV['EDITOR'] : 'vi' file_stat = File.stat(filename) file_checksum = SHA1.file(filename) system(editor, filename, ) proc_status = $? if [:noop] return true elsif proc_status.success? return nil if file_stat == File.stat(filename) return false if file_checksum == SHA1.file(filename) return true elsif proc_status.signaled? raise AdvFileUtils::CommandError.new("editor terminated on signal #{proc_status.termsig}") else raise AdvFileUtils::CommandError.new("editor had non-zero exit code #{proc_status.exitstatus}") end end |
.replace(filename, *data_and_options) ⇒ Object
Options: verbose, noop, force, backup, lockfile, retry
Edit a file, but open a temporary lockfile instead and move it in place after editting is complete.
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/advanced-fileutils.rb', line 303 def replace (filename, *) data, = *() lockfile = [:lockfile] ? [:lockfile] : "#{filename}.lock" begin if not [:noop] fd = IO.sysopen(lockfile, IO::WRONLY | IO::CREAT | IO::EXCL, 0700) f = IO.new(fd, 'w') hook_write(f, lockfile) if block_given? and [:verbose] else f = StringIO.new hook_write(f, lockfile, :rewind) if block_given? and [:verbose] end file_stat = File.stat(filename) rescue nil if block_given? $stderr.puts "cat /dev/null > #{Escape.shell_single_word(lockfile)}" if [:verbose] yield f else $stderr.puts AdvFileUtils.__send__(:write_echo_message, data, '>', lockfile) if [:verbose] f.write(data) end f.close if file_stat FileUtils.chown(file_stat.uid.to_s, file_stat.gid.to_s, lockfile, ) FileUtils.chmod(file_stat.mode & 07777, lockfile, ) end FileUtils.mv(lockfile, filename, ) ensure f.close if f and not f.closed? begin File.delete(lockfile) if fd rescue Errno::ENOENT end end end |
.sh ⇒ Object
Options: verbose, noop
An alternative to Kernel.system that accepts options for verbosity and dry runs.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/advanced-fileutils.rb', line 238 def system (*) if [-1].respond_to?(:has_key?) command = [0...-1] = [-1] else command = = {} end raise ArgumentError.new('wrong number of arguments') if command.empty? if [:verbose] if command.size == 1 $stderr.puts command[0] else $stderr.puts command.collect { |word| Escape.shell_single_word word }.join(' ') end end if not [:noop] Kernel.system(*command) end end |
.shell ⇒ Object
Options: verbose, noop
An alternative to Kernel.system that accepts options for verbosity and dry runs.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/advanced-fileutils.rb', line 242 def system (*) if [-1].respond_to?(:has_key?) command = [0...-1] = [-1] else command = = {} end raise ArgumentError.new('wrong number of arguments') if command.empty? if [:verbose] if command.size == 1 $stderr.puts command[0] else $stderr.puts command.collect { |word| Escape.shell_single_word word }.join(' ') end end if not [:noop] Kernel.system(*command) end end |
.system(*command_and_options) ⇒ Object
Options: verbose, noop
An alternative to Kernel.system that accepts options for verbosity and dry runs.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/advanced-fileutils.rb', line 210 def system (*) if [-1].respond_to?(:has_key?) command = [0...-1] = [-1] else command = = {} end raise ArgumentError.new('wrong number of arguments') if command.empty? if [:verbose] if command.size == 1 $stderr.puts command[0] else $stderr.puts command.collect { |word| Escape.shell_single_word word }.join(' ') end end if not [:noop] Kernel.system(*command) end end |
.truncate(filename, *data_and_options, &block) ⇒ Object
Options: verbose, noop, force, backup
Replace the given data
in the file named by filename
.
If called with a block then the File object is yielded to the block for writing data intead of the data being passed as an argument.
AdvFileUtils.truncate('data.log', "some data\n")
AdvFileUtils.truncate('data.log') { |f| f.puts "some data" }
195 196 197 |
# File 'lib/advanced-fileutils.rb', line 195 def truncate (filename, *, &block) generic_write 'w', filename, *, &block end |
Instance Method Details
#append(filename, *data_and_options, &block) ⇒ Object
Options: verbose, noop, force, backup
Append the given data
to the file named by filename
.
If called with a block then the File object is yielded to the block for appending data intead of the data being passed as an argument.
AdvFileUtils.append('data.log', "some data for log entry\n")
AdvFileUtils.append('data.log') { |f| f.puts "some data for log entry" }
175 176 177 |
# File 'lib/advanced-fileutils.rb', line 175 def append (filename, *, &block) generic_write 'a', filename, *, &block end |
#edit(filename, *data_and_options) ⇒ Object
Options: verbose, noop, force, backup
Invoke an external editor to edit some text or a file.
edit(filename, )
edit(filename, data, )
edit(nil, data, )
Return values
true, if successful and file was edited
false, if successful and file was not edited
nil, if successful and file was not saved
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/advanced-fileutils.rb', line 265 def edit (filename, *) data, = *() editor = ENV.has_key?('VISUAL') ? ENV['VISUAL'] : ENV.has_key?('EDITOR') ? ENV['EDITOR'] : 'vi' file_stat = File.stat(filename) file_checksum = SHA1.file(filename) system(editor, filename, ) proc_status = $? if [:noop] return true elsif proc_status.success? return nil if file_stat == File.stat(filename) return false if file_checksum == SHA1.file(filename) return true elsif proc_status.signaled? raise AdvFileUtils::CommandError.new("editor terminated on signal #{proc_status.termsig}") else raise AdvFileUtils::CommandError.new("editor had non-zero exit code #{proc_status.exitstatus}") end end |
#replace(filename, *data_and_options) ⇒ Object
Options: verbose, noop, force, backup, lockfile, retry
Edit a file, but open a temporary lockfile instead and move it in place after editting is complete.
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/advanced-fileutils.rb', line 303 def replace (filename, *) data, = *() lockfile = [:lockfile] ? [:lockfile] : "#{filename}.lock" begin if not [:noop] fd = IO.sysopen(lockfile, IO::WRONLY | IO::CREAT | IO::EXCL, 0700) f = IO.new(fd, 'w') hook_write(f, lockfile) if block_given? and [:verbose] else f = StringIO.new hook_write(f, lockfile, :rewind) if block_given? and [:verbose] end file_stat = File.stat(filename) rescue nil if block_given? $stderr.puts "cat /dev/null > #{Escape.shell_single_word(lockfile)}" if [:verbose] yield f else $stderr.puts AdvFileUtils.__send__(:write_echo_message, data, '>', lockfile) if [:verbose] f.write(data) end f.close if file_stat FileUtils.chown(file_stat.uid.to_s, file_stat.gid.to_s, lockfile, ) FileUtils.chmod(file_stat.mode & 07777, lockfile, ) end FileUtils.mv(lockfile, filename, ) ensure f.close if f and not f.closed? begin File.delete(lockfile) if fd rescue Errno::ENOENT end end end |
#system(*command_and_options) ⇒ Object Also known as: sh, shell
Options: verbose, noop
An alternative to Kernel.system that accepts options for verbosity and dry runs.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/advanced-fileutils.rb', line 210 def system (*) if [-1].respond_to?(:has_key?) command = [0...-1] = [-1] else command = = {} end raise ArgumentError.new('wrong number of arguments') if command.empty? if [:verbose] if command.size == 1 $stderr.puts command[0] else $stderr.puts command.collect { |word| Escape.shell_single_word word }.join(' ') end end if not [:noop] Kernel.system(*command) end end |
#truncate(filename, *data_and_options, &block) ⇒ Object
Options: verbose, noop, force, backup
Replace the given data
in the file named by filename
.
If called with a block then the File object is yielded to the block for writing data intead of the data being passed as an argument.
AdvFileUtils.truncate('data.log', "some data\n")
AdvFileUtils.truncate('data.log') { |f| f.puts "some data" }
195 196 197 |
# File 'lib/advanced-fileutils.rb', line 195 def truncate (filename, *, &block) generic_write 'w', filename, *, &block end |