Sha256: 7c7422acb20c89b389ceb86943f6c9b2a7f28c47d6d3e1f5a98fb5d4c80b07cf

Contents?: true

Size: 1.4 KB

Versions: 114

Compression:

Stored size: 1.4 KB

Contents

require 'digest/md5'
require 'fileutils'

module MetricFu
  class MD5Tracker

    @@unchanged_md5s = []

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

      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

      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

      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

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

Version data entries

114 entries across 114 versions & 26 rubygems

Version Path
IPGlider-metric_fu-1.1.5.1.1 lib/base/md5_tracker.rb
IPGlider-metric_fu-1.1.5.1 lib/base/md5_tracker.rb
alexrothenberg-metric_fu-1.0.2 lib/base/md5_tracker.rb
cayblood-metric_fu-1.1.6 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.2 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.3.3 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.3.4 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.3.5 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.3.6 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.3.7 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.3.8 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.4.1 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.4.2 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.4.3 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.4.4 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.4.5 lib/base/md5_tracker.rb
edouard-metric_fu-1.0.4 lib/base/md5_tracker.rb
edouard-metric_fu-1.1.1 lib/base/md5_tracker.rb
gforces-metric_fu-1.1.4 lib/base/md5_tracker.rb
iain-metric_fu-0.1.0 lib/base/md5_tracker.rb