-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_missing_checksums.rb
executable file
·54 lines (42 loc) · 1.13 KB
/
add_missing_checksums.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env ruby
$LOAD_PATH << File.dirname(File.realpath(__FILE__)) + '/lib'
require 'file_pair_reader'
require 'digest/sha1'
if ARGV.include?('-h') || ARGV.include?('--help') || ARGV.empty?
puts "Adds checksums to records that don't have them."
puts "Usage: #{$0} [--dry-run] index-file.idx [...]"
exit(0)
end
dry_run = false
index_files = []
until ARGV.empty?
arg = ARGV.shift
if arg == '--dry-run'
dry_run = true
elsif arg.end_with?('.idx')
index_files << arg
else
raise "Unrecognized file extension for #{arg}"
end
end
total_records = 0
missing_checksums = 0
index_files.each do |index_file|
puts "Processing #{index_file}"
FilePairReader.open(index_file) do |reader|
records = []
reader.each_record { |r| records << r }
reader.rewind_and_truncate!
records.each do |record|
total_records += 1
unless record.sha1_checksum
missing_checksums += 1
record.sha1_checksum = record.calculate_data_checksum
end
unless dry_run
reader.write!(record)
end
end
end
end
puts "Checksums added to #{missing_checksums} of #{total_records} records"