Mercurial > ec-dotfiles
comparison bin/cek.rb @ 522:a0f4a3e15322
Extend cek.rb to match main script.
| author | edogawaconan <me@myconan.net> |
|---|---|
| date | Sun, 29 Jun 2014 01:30:18 +0900 |
| parents | 324c5842af87 |
| children |
comparison
equal
deleted
inserted
replaced
| 521:fb2d37acca81 | 522:a0f4a3e15322 |
|---|---|
| 1 #!/usr/bin/env ruby | 1 #!/usr/bin/env ruby |
| 2 require 'zlib' | 2 require 'zlib' |
| 3 | 3 |
| 4 block = 1048576 | 4 class Cek |
| 5 ARGV.each do |filename| | 5 CRC32_BLOCK = 2 ** 20 |
| 6 crc = 0 | 6 |
| 7 file = File.open(filename, 'rb') | 7 attr_accessor :files |
| 8 currentbyte = 0 | 8 |
| 9 while (line = file.read(block)) do | 9 def crc32(filepath) |
| 10 crc = Zlib.crc32(line,crc) | 10 file = File.open filepath, 'rb' |
| 11 crc32 = 0 | |
| 12 while (line = file.read(CRC32_BLOCK)) do | |
| 13 crc32 = Zlib.crc32(line, crc32) | |
| 14 end | |
| 15 file.close | |
| 16 | |
| 17 format "%08X", crc32.to_i | |
| 11 end | 18 end |
| 12 file.close | 19 |
| 13 printf("%s %08X\n", filename, crc.to_i) | 20 def print_result |
| 21 results = Hash.new(0) | |
| 22 files.each do |f| | |
| 23 time_start = Time.now | |
| 24 if File.readable?(f) && File.file?(f) then | |
| 25 hash = crc32 f | |
| 26 size = File.size f | |
| 27 /(\[|\()(?<filename_hash>\p{XDigit}{8})(\]|\))/ =~ f | |
| 28 result = | |
| 29 if filename_hash | |
| 30 if filename_hash == hash | |
| 31 :ok | |
| 32 else | |
| 33 :fail | |
| 34 end | |
| 35 else | |
| 36 :missing_hash | |
| 37 end | |
| 38 output = hash | |
| 39 case result | |
| 40 when :ok then output << " - OK!" | |
| 41 when :fail then output << " - ERROR - should be #{filename_hash}" | |
| 42 end | |
| 43 else | |
| 44 result = :unreadable | |
| 45 output = "not a file or unreadable" | |
| 46 end | |
| 47 process_time = Time.now - time_start | |
| 48 size ||= 0 | |
| 49 size_mb = size.to_f / 1_000_000 | |
| 50 speed = (size_mb.to_f / process_time) | |
| 51 results[result] += 1 | |
| 52 puts "#{f}: #{output} (#{format "%.2f", size_mb} MB / #{format "%.2f", process_time} s / #{format "%.2f", speed} MB/s)" | |
| 53 end | |
| 54 puts ("-" * 50) | |
| 55 puts "Files ok: #{results[:ok]}" | |
| 56 puts "Files broken: #{results[:fail]}" | |
| 57 puts "Files without crc information: #{results[:missing_hash]}" | |
| 58 puts "Files unreadable or not a file: #{results[:unreadable]}" | |
| 59 end | |
| 60 | |
| 61 def initialize(*files) | |
| 62 self.files = files.flatten | |
| 63 @stats = {} | |
| 64 end | |
| 65 | |
| 66 def self.run(*files) | |
| 67 new(files).print_result | |
| 68 end | |
| 14 end | 69 end |
| 70 | |
| 71 Cek.run(ARGV) if __FILE__ == $0 |
