Sha256: cbbbb81584d903fd61dced642fe26ffb581787849e50b7a0869fa62be130e663

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

$:<< File.expand_path(File.dirname(__FILE__))

require "checksummer_file"
class Checksummer
  attr_accessor :checksum_to, :paths
  
  def initialize(checksum_to)
    self.checksum_to = File.expand_path(checksum_to)
  end
  
  def checksum_directory(directory, find_options = nil)
    files = find(directory, find_options)
    total = files.length
    files.each_with_index do |file, i|
      status = file.checksum_to!(checksum_to)
      yield(status, file, i, total) if block_given?
    end
  end
  
  def find(directory, options = nil)
    Kernel.send(:`, %(find #{directory} #{options ? "#{options} " : ""}-type f -printf "%T+\t%s\t%Y\t%p\t%l\n")).split("\n").map do |line|
      ChecksummerFile.from_line(line)
    end
  end
  
  def self.run_for_args(args)
    directory, checksum_to = args[0,2]
    sleep = args.index("--sleep") ? args[args.index("--sleep") + 1].to_i / 1000.0 : 0.1
    if !directory || !File.directory?(directory) || !checksum_to || !File.directory?(checksum_to)
      puts usage
    else
      self.new(checksum_to).checksum_directory(directory, args[2..-1].join(" ")) do |status, file, index, total|
        puts "%s\t%0#{total.to_s.length}d/%d\t%s\t%s" % [Time.now.strftime("%Y-%m-%d %H:%M:%S"), index, total, status, file.path]
        $stdout.flush
        sleep sleep if !Range.new(0,7).include?(Time.now.hour)
      end
    end
  end
  
  def self.usage
    out = ["checksummer <directory_to_checksum> <directory_to_store_checksummed_files> [options]"]
    out << ["OPTIONS", "--sleep <sleep in ms>", "<any find option>"]
    out.join("\n")
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
checksummer-0.1.6 lib/checksummer.rb