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