Class: Rconftool::Processor

Inherits:
Object show all
Defined in:
lib/rconftool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = nil) ⇒ Processor

Parse command-line options and set the @o options hash



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/rconftool.rb', line 284

def initialize(argv=nil)
  require 'optparse'

  @o = { :strip_regexp => /\.dist\z/ }
  return unless argv
  opts = OptionParser.new do |opts|
    opts.banner = "rconftool version #{VERSION}"
    opts.separator "Usage: #{$0} [options]"
    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-n", "--noclobber", "Dummy run") do
      @o[:noclobber] = true
    end
    opts.on("-f", "--force", "Update files even if VERSION is same") do
      @o[:force] = true
    end
    opts.on("-q", "--quiet", "No progress reporting") do
      @o[:debug] = ""
    end
    opts.on("--targetdir DIR", "Where to write merged config files") do |dir|
      @o[:targetdir] = dir
    end
    opts.on("--olddir DIR", "If file does not exist in targetdir,",
            "try to merge from here") do |dir|
      @o[:olddir] = dir
    end
    opts.on("--[no-]recursive", "Traverse directories recursively") do |v|
      @o[:recursive] = v
    end
    opts.on("--strip-suffix FOO", "Remove suffix FOO from target filenames",
		"(default .dist)") do |suffix|
      @o[:strip_regexp] = /#{Regexp.escape(suffix)}\z/
    end
    opts.on("-a", "--add-suffix FOO", "Add suffix FOO to target filenames") do |suffix|
      @o[:add_suffix] = suffix
    end

    opts.on_tail("-?", "--help", "Show this message") do
      puts opts
      exit
    end
  end
  opts.parse!(argv)
end

Instance Attribute Details

#oObject (readonly)

Returns the value of attribute o.



280
281
282
# File 'lib/rconftool.rb', line 280

def o
  @o
end

Instance Method Details

#run(files) ⇒ Object

Process a list of files, [src1,src2,…]. If recursive mode has been enabled, then subdirectories of destdir are created as necessary when ‘src’ is a directory, and the mode/ownership of these newly created directories is copied from the original.



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/rconftool.rb', line 335

def run(files)
  done_work = false
  files.each do |f|
    if not File.directory?(f)
      dst = old = nil
      dst = @o[:targetdir] + File::SEPARATOR + File.basename(f) if @o[:targetdir]
      old = @o[:olddir]    + File::SEPARATOR + File.basename(f) if @o[:olddir]
      Rconftool::install(f, dst, old, @o)
    elsif not @o[:recursive]
      raise Errno::EISDIR, "#{f} (not copied). Need --recursive?"
    else
      Rconftool::recurse_dir(f) do |nf|
        src = f + File::SEPARATOR + nf
        dst = old = nil
        dst = @o[:targetdir] + File::SEPARATOR + nf if @o[:targetdir]
        old = @o[:olddir]    + File::SEPARATOR + nf if @o[:olddir]
        if File.directory?(src)
          if dst and not File.directory?(dst)
            orig = File.stat(src)
            Dir.mkdir(dst, orig.mode)
            begin
              File.chown(orig.uid, orig.gid, dst)
            rescue Errno::EPERM
            end
          end
        else
          Rconftool::install(src, dst, old, @o)
        end
      end
    end
    done_work = true
  end
  unless done_work
    $stderr.puts "Usage: #{$0} [options] src1 src2 ...\n"+
                 "Try #{$0} --help for more information\n"
    exit 1
  end
end