Class: HashThatTree::CompareMD5

Inherits:
Object
  • Object
show all
Defined in:
lib/compare.rb

Overview

This class accepts two folders and provides methods to iterate through them creating a hash of each file within and can display the results for analysis

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder1, folder2, options) ⇒ CompareMD5

initialize the class with the folders to be compared



18
19
20
21
22
23
24
# File 'lib/compare.rb', line 18

def initialize(folder1, folder2, options)
	   @folder1 = folder1
	   @folder2 = folder2
	   @format = options['output']
	   @filehash = Hash.new
	 validate
end

Instance Attribute Details

#folder1Object

path to folder containing files to hash



13
14
15
# File 'lib/compare.rb', line 13

def folder1
  @folder1
end

#folder2Object

path to folder containing files to hash to be compared to folder1



14
15
16
# File 'lib/compare.rb', line 14

def folder2
  @folder2
end

#formatObject

the format to output the results to. csv, html or json



15
16
17
# File 'lib/compare.rb', line 15

def format
  @format
end

Instance Method Details

#compareObject

Iterates through the folders and creates a FileHashResults object containing the results of the comparisson



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/compare.rb', line 43

def compare
	
	Dir.foreach(@folder1) do |item|
		begin
				next if item == '.' or item == '..'
				fullfilename = File.expand_path(@folder1, item)
				the_hash = Digest::MD5.hexdigest(File.read(File.join(File.expand_path(@folder1), item)))
				item = item.downcase
				filedata = FileHashResults.new(item, the_hash, nil)
				@filehash[item] = filedata
			rescue
			  #puts "Skipped:#{item.inspect}"
			end
	end

	Dir.foreach(@folder2) do |item|
	  begin
				next if item == '.' or item == '..'
				the_hash = Digest::MD5.hexdigest(File.read(File.join(@folder2, item)))
				item = item.downcase
        if(@filehash[item]==nil)
					filedata = FileHashResults.new(item, nil, the_hash)
					@filehash[item] = filedata
					next
				end
				@filehash[item.downcase].file_hash2 = the_hash
			rescue
        #puts "Skipped:#{item.inspect}"
      end	
	end
end

#display_resultsObject

print the contents of the FileHashResults object standard out in the format specified. Default is csv



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/compare.rb', line 76

def display_results()
 case @format
    when "csv"
      display_results_csv
    when "html"
      display_results_html
    when "json"
      display_results_json
    else
      display_results_csv
    end
end

#display_results_csvObject

Prints the results to standard out in the csv format specified.



90
91
92
93
# File 'lib/compare.rb', line 90

def display_results_csv
	puts "FileName,#{@folder1},#{@folder2},Are Equal"
	@filehash.each{ |key, value| puts "#{value.file_name},#{value.file_hash1},#{value.file_hash2}, #{value.file_hash1==value.file_hash2}" }
end

#display_results_htmlObject

Prints the results to standard out in the csv format specified.



96
97
98
99
100
101
102
# File 'lib/compare.rb', line 96

def display_results_html
output ="<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title></title></head><body>"
output.concat("<table><thead><tr><td>FileName</td></tr><tr><td>#{@folder1}</td></tr><tr><td>#{@folder2}</td></tr><tr><td>Equal</td></tr></thead>")
  @filehash.each{ |key, value| output.concat("<tbody><tr><td>#{value.file_name}</td></tr><tr><td>#{value.file_hash1}</td></tr><tr><td>#{value.file_hash2}</td></tr><tr><td>#{value.file_hash1==value.file_hash2}</td></tr>")}
  output.concat("</tbody></table></body></html>")
  puts output
end

#display_results_jsonObject

Prints the results to standard out in the csv format specified.



105
106
107
# File 'lib/compare.rb', line 105

def display_results_json
  puts @filehash 
end

#validateObject

Validates the input ensuring the arguments are both valid folders



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/compare.rb', line 28

def validate
	if(folder1==nil) || (folder1=="") || !Dir.exists?(folder1)
		puts "a valid folder path is required as argument 1"
		exit
	end
			
	if(folder2==nil) || (folder2=="")  || !Dir.exists?(folder2)
		puts "a valid folder path is required as argument 2"
		exit
	end
	
end