view bin/cek.rb @ 537:351bd965bc1f

Update some syntax files.
author edogawaconan <me@myconan.net>
date Mon, 05 Jan 2015 21:47:44 +0900
parents a0f4a3e15322
children
line wrap: on
line source

#!/usr/bin/env ruby
require 'zlib'

class Cek
  CRC32_BLOCK = 2 ** 20

  attr_accessor :files

  def crc32(filepath)
    file = File.open filepath, 'rb'
    crc32 = 0
    while (line = file.read(CRC32_BLOCK)) do
      crc32 = Zlib.crc32(line, crc32)
    end
    file.close

    format "%08X", crc32.to_i
  end

  def print_result
    results = Hash.new(0)
    files.each do |f|
      time_start = Time.now
      if File.readable?(f) && File.file?(f) then
        hash = crc32 f
        size = File.size f
        /(\[|\()(?<filename_hash>\p{XDigit}{8})(\]|\))/ =~ f
        result =
          if filename_hash
            if filename_hash == hash
              :ok
            else
              :fail
            end
          else
            :missing_hash
          end
        output = hash
        case result
          when :ok then output << " - OK!"
          when :fail then output << " - ERROR - should be #{filename_hash}"
        end
      else
        result = :unreadable
        output = "not a file or unreadable"
      end
      process_time = Time.now - time_start
      size ||= 0
      size_mb = size.to_f / 1_000_000
      speed = (size_mb.to_f / process_time)
      results[result] += 1
      puts "#{f}: #{output} (#{format "%.2f", size_mb} MB / #{format "%.2f", process_time} s / #{format "%.2f", speed} MB/s)"
    end
    puts ("-" * 50)
    puts "Files ok: #{results[:ok]}"
    puts "Files broken: #{results[:fail]}"
    puts "Files without crc information: #{results[:missing_hash]}"
    puts "Files unreadable or not a file: #{results[:unreadable]}"
  end

  def initialize(*files)
    self.files = files.flatten
    @stats = {}
  end

  def self.run(*files)
    new(files).print_result
  end
end

Cek.run(ARGV) if __FILE__ == $0