Module: Inimit

Defined in:
lib/inimit.rb

Overview

TODO: lenient option to only fail on mismatch

Defined Under Namespace

Classes: Opts

Constant Summary collapse

VERSION =
'0.0.3'

Class Method Summary collapse

Class Method Details

.abort(msg) ⇒ Object



176
177
178
179
# File 'lib/inimit.rb', line 176

def self.abort msg
  warn Paint[msg, :yellow]
  exit 2
end

.compare(source, target, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/inimit.rb', line 67

def self.compare source, target, options = {}

  source, target = File.expand_path(source), File.expand_path(target)

  puts
  puts "Comparing files in #{source} to #{target}."
  puts unless options[:mismatch_only]

  Dir.chdir source
  total, match, unknown, mismatch = 0, 0, 0, 0

  Find.find '.' do |file|

    # skip directories
    next unless File.file? file
    human_file = file.sub /^\.\//, ''
    total += 1

    source_file = File.join source, file
    target_file = File.join target, file

    if !File.file? target_file
      unknown += 1
      puts %/#{Paint["not found", :yellow]} #{human_file}/
      next
    end

    source_sha1 = self.hash source_file, options
    target_sha1 = self.hash target_file, options

    if source_sha1 == target_sha1
      match += 1
      unless options[:mismatch_only]
        print Paint[self.hash_notice(source_sha1, target_sha1, options), :green]
        puts " #{human_file}..."
      end
    else
      mismatch += 1
      print Paint[self.hash_notice(source_sha1, target_sha1, options), :red]
      puts " #{human_file}..."
    end
  end

  puts

  notice = "Checked #{total} files: "
  notice << Paint["#{match} identical", :green]
  notice << ", "
  notice << Paint["#{unknown} not found", :yellow]
  notice << ", "
  notice << Paint["#{mismatch} different", :red]

  puts notice
  puts

  exit 1 if unknown >= 1 or mismatch >= 1
end

.compare_files(source, target, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/inimit.rb', line 125

def self.compare_files source, target, options = {}

  source_sha1, target_sha1 = self.hash(source, options), self.hash(target, options)

  puts

  if source_sha1 == target_sha1
    msg = "Files are identical"
    msg << " (#{self.hash_notice source_sha1, target_sha1, options})" if options[:verbose] >= 1
    msg << "."
    puts Paint[msg, :green]
    puts
  else
    msg = "The contents of the files do not match"
    msg << " (#{self.hash_notice source_sha1, target_sha1, options})" if options[:verbose] >= 1
    msg << "."
    puts Paint[msg, :red]
    puts
    exit 1
  end
end

.hash(file, options = {}) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/inimit.rb', line 162

def self.hash file, options = {}
  dig = case options[:digest]
  when 'md5'; Digest::MD5.new
  when 'sha256'; Digest::SHA256.new
  when 'sha384'; Digest::SHA384.new
  when 'sha512'; Digest::SHA512.new
  else; Digest::SHA1.new
  end
  File.open file, 'rb' do |io|
    dig.update io.readpartial(4096) while !io.eof
  end
  dig.hexdigest
end

.hash_notice(h1, h2, options = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/inimit.rb', line 147

def self.hash_notice h1, h2, options = {}
  if h1 == h2
    case options[:verbose]
    when 1; "#{h1[0,7]} == #{h2[0,7]}"
    when 2; "#{h1} == #{h2}"
    else; "identical"
    end
  else
    case options[:verbose]
    when 1..2; "#{h1} != #{h2}"
    else; "different"
    end
  end
end

.run(*args) ⇒ Object



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
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/inimit.rb', line 17

def self.run *args

  options = {
    :verbose => 0,
    :mismatch_only => false
  }
  Opts.new do |opts|
    opts.banner = "#{Paint[opts.program_name, :bold]} recursively compares files.\n\nUsage:\n    inimit [OPTION]... [SOURCE] TARGET\n"
    opts.version = VERSION
    opts.on('-d', '--digest ALGORITHM', 'Use ALGORITHM to hash file contents (available: md5, sha1, sha256, sha384, sha512)') do |digest|
      options[:digest] = digest
    end
    opts.on('-m', '--mismatch', 'Only show files when missing or the content does not match') do
      options[:mismatch_only] = true
    end
    opts.on('-v', '--verbose', 'Increase verbosity (once: show hash prefixes, twice: show full hashes)') do
      options[:verbose] += 1
    end
    opts.on('-h', '--help', 'Show this help and exit'){ puts opts; exit 0 }
    opts.on('-u', '--usage', 'Show this help and exit'){ puts opts; exit 0 }
  end.parse! args

  # check number of args
  self.abort "A target directory must be given" if args.length < 1
  self.abort "Only one source and target must be given (got #{args.length} arguments)" if args.length > 2

  source, target = case args.length
  when 1
    [ '.', args.shift ]
  when 2
    [ args.shift, args.shift ]
  end

  if !File.exists?(source)
    self.abort "Unknown file or directory #{source}"
  elsif !File.file?(source) and !File.directory?(source)
    self.abort "Source #{source} is neither a file nor a directory"
  elsif File.file?(source) and !File.file?(target)
    self.abort "Source #{source} is a file, target #{target} is not"
  elsif File.directory?(source) and !File.directory?(target)
    self.abort "Source #{source} is a directory, target #{target} is not"
  end

  if File.directory? source
    self.compare source, target, options
  else
    self.compare_files source, target, options
  end
end