Class: MetricFu::MD5Tracker

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

Constant Summary collapse

@@unchanged_md5s =
[]

Class Method Summary collapse

Class Method Details

.file_already_counted?(path_to_file) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/base/md5_tracker.rb', line 47

def file_already_counted?(path_to_file)
  return @@unchanged_md5s.include?(path_to_file)
end

.file_changed?(path_to_file, base_dir) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/base/md5_tracker.rb', line 29

def file_changed?(path_to_file, base_dir)
  orig_md5_file = md5_file(path_to_file, base_dir)
  return !!track(path_to_file, base_dir) unless File.exist?(orig_md5_file)

  current_md5 = ""
  file = File.open(orig_md5_file, 'r')
  file.each_line { |line| current_md5 << line }
  file.close
  current_md5.chomp!

  new_md5 = Digest::MD5.hexdigest(File.read(path_to_file))
  new_md5.chomp!

  @@unchanged_md5s << path_to_file if new_md5 == current_md5

  return new_md5 != current_md5
end

.md5_dir(path_to_file, base_dir) ⇒ Object



10
11
12
13
# File 'lib/base/md5_tracker.rb', line 10

def md5_dir(path_to_file, base_dir)
  File.join(base_dir,
            path_to_file.split('/')[0..-2].join('/'))
end

.md5_file(path_to_file, base_dir) ⇒ Object



15
16
17
18
# File 'lib/base/md5_tracker.rb', line 15

def md5_file(path_to_file, base_dir)
  File.join(md5_dir(path_to_file, base_dir),
            path_to_file.split('/').last.sub(/\.[a-z]+/, '.md5'))
end

.track(path_to_file, base_dir) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/base/md5_tracker.rb', line 20

def track(path_to_file, base_dir)
  md5 = Digest::MD5.hexdigest(File.read(path_to_file))
  FileUtils.mkdir_p(md5_dir(path_to_file, base_dir), :verbose => false)
  f = File.new(md5_file(path_to_file, base_dir), "w")
  f.puts(md5)
  f.close
  md5
end