Class: FileFilter
- Inherits:
-
Object
- Object
- FileFilter
- Defined in:
- lib/inplace.rb
Defined Under Namespace
Classes: Formatter
Constant Summary collapse
- TMPNAME_BASE =
MYNAME.tr('.', '-')
- @@tmpfiles =
Set.new
Class Method Summary collapse
Instance Method Summary collapse
- #destructive? ⇒ Boolean
- #filter(origfile, infile, outfile) ⇒ Object
- #filter!(origfile, file) ⇒ Object
-
#initialize(template) ⇒ FileFilter
constructor
A new instance of FileFilter.
- #tmpfile?(file) ⇒ Boolean
Constructor Details
#initialize(template) ⇒ FileFilter
Returns a new instance of FileFilter.
255 256 257 258 259 |
# File 'lib/inplace.rb', line 255 def initialize(template) raise ArgumentError, "empty command" if template.empty? @formatter = Formatter.new(template) end |
Class Method Details
.make_tmpfile_for(outfile) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/inplace.rb', line 338 def self.make_tmpfile_for(outfile) if m = File.basename(outfile).match(/(\..+)$/) ext = m[1] else ext = '' end if $same_directory tmpf = Tempfile.new([TMPNAME_BASE, ext], File.dirname(outfile)) else tmpf = Tempfile.new([TMPNAME_BASE, ext]) end tmpf.close path = tmpf.path @@tmpfiles << path return path end |
Instance Method Details
#destructive? ⇒ Boolean
261 262 263 |
# File 'lib/inplace.rb', line 261 def destructive? @formatter.arity == 1 end |
#filter(origfile, infile, outfile) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 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 |
# File 'lib/inplace.rb', line 269 def filter(origfile, infile, outfile) if !File.exist?(infile) flunk origfile, "file not found" end outfile_is_original = !tmpfile?(outfile) outfile_stat = File.lstat(outfile) if outfile_stat.symlink? $dereference or flunk origfile, "symlink" begin outfile = Pathname.new(outfile).realpath.to_s outfile_stat = File.lstat(outfile) rescue => e flunk origfile, "symlink unresolvable: %s", e end end outfile_stat.file? or flunk origfile, "symlink to a non-regular file" $force || outfile_stat.writable? or flunk origfile, "symlink to a read-only file" tmpfile = FileFilter.make_tmpfile_for(outfile) if destructive? debug "cp(%s, %s)", infile, tmpfile FileUtils.cp(infile, tmpfile) command = @formatter.format(origfile, tmpfile) else command = @formatter.format(origfile, infile, tmpfile) end if run(command) File.file?(tmpfile) or flunk origfile, "output file removed" !$accept_empty && File.zero?(tmpfile) and flunk origfile, "empty output" outfile_is_original && FileUtils.identical?(origfile, tmpfile) and flunk origfile, "unchanged" stat = File.stat(infile) newsize = File.size(tmpfile) if $dry_run uninterruptible { replace(tmpfile, outfile, stat) } newsize = File.size(outfile) unless $dry_run info "%s: edited (%d bytes -> %d bytes)", origfile, stat.size, newsize else flunk origfile, "command exited with %d", $?.exitstatus end end |
#filter!(origfile, file) ⇒ Object
265 266 267 |
# File 'lib/inplace.rb', line 265 def filter!(origfile, file) filter(origfile, file, file) end |
#tmpfile?(file) ⇒ Boolean
332 333 334 |
# File 'lib/inplace.rb', line 332 def tmpfile?(file) @@tmpfiles.include?(file) end |