Sha256: 0d131b9245d2dabb3fe4479175e7924a5bb1b65099ed98671ed51069820ef636

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

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

require "checksummer_file"
require "json"
class Checksummer
  DEFAULT_SLEEP = 0.1
  
  def self.find(directory, options = nil)
    Kernel.send(:`, %(find #{directory} #{options ? "#{options} " : ""}-type f -printf "%p\t%T+\t%s\t%Y\t%l\n")).split("\n")
  end
  
  def self.run_for_args(args)
    if args.include?("--version")
      puts File.read(File.expand_path("../VERSION", File.dirname(__FILE__)))
      return
    end
    directory_or_file, checksum_to = args[0,2]
    sleep = sleep_from_args(args)
    if directory_or_file && checksum_to && File.directory?(checksum_to)
      lines = if directory_or_file == "--stdin"
        $stdin.readlines
      elsif File.directory?(directory_or_file)
        find(directory_or_file, args[2..-1].join(" "))
      else
        [directory_or_file]
      end
      checksum_many(lines, checksum_to, sleep)
      return true
    end
    puts usage
  end
  
  def self.sleep_from_args(args)
    if idx = args.index("--sleep")
      args.delete_at(idx)
      args.delete_at(idx).to_i / 1000.0
    else
      DEFAULT_SLEEP
    end
  end
  
  def self.checksum_many(lines, checksum_to, sleep = DEFAULT_SLEEP)
    started_at = Time.now
    total = lines.count
    lines.each_with_index do |line, index|
      extra = { :index => index + 1, :total => lines.count, :started_at => started_at.iso8601 }
      puts ChecksummerFile.from_line(line).checksum_to!(checksum_to).merge(extra).to_json
      $stdout.flush
      sleep sleep if !Range.new(0,7).include?(Time.now.hour)
    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

5 entries across 5 versions & 1 rubygems

Version Path
checksummer-0.2.6 lib/checksummer.rb
checksummer-0.2.5 lib/checksummer.rb
checksummer-0.2.4 lib/checksummer.rb
checksummer-0.2.3 lib/checksummer.rb
checksummer-0.2.2 lib/checksummer.rb