Class: FileFilter

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(template) ⇒ FileFilter

Returns a new instance of FileFilter.



224
225
226
# File 'lib/inplace.rb', line 224

def initialize(template)
  @formatter = Formatter.new(template)
end

Class Method Details

.make_tmpfile_for(outfile) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/inplace.rb', line 305

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

Returns:

  • (Boolean)


228
229
230
# File 'lib/inplace.rb', line 228

def destructive?
  @formatter.arity == 1
end

#filter(origfile, infile, outfile) ⇒ Object



236
237
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
263
264
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
291
292
293
294
295
# File 'lib/inplace.rb', line 236

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



232
233
234
# File 'lib/inplace.rb', line 232

def filter!(origfile, file)
  filter(origfile, file, file)
end

#tmpfile?(file) ⇒ Boolean

Returns:

  • (Boolean)


299
300
301
# File 'lib/inplace.rb', line 299

def tmpfile?(file)
  @@tmpfiles.include?(file)
end