Guest User

Untitled

a guest
Jan 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. require "pathname"
  2. require "digest/sha1"
  3. require "yaml"
  4. require 'trollop'
  5.  
  6. module DirScan
  7.  
  8. class Scanner
  9.  
  10. def initialize dir=Dir.pwd, options={:verbose => false}
  11. @dir = Pathname.new(dir).expand_path
  12. @file = Pathname.new('dirscan.yml').expand_path
  13. @verbose = options[:verbose]
  14. end
  15.  
  16. def scan
  17. puts "Scanning '#{@dir}'..."
  18. @contents = {}.tap do |contents|
  19. @dir.find do |f|
  20. contents[f.to_s] = sha1 f if f.file?
  21. end
  22. end
  23. end
  24.  
  25. def sha1 file
  26. Digest::SHA1.new.tap do |hash|
  27. File.open(file, 'r') do |fh|
  28. while buffer = fh.read(1024) do
  29. hash << buffer
  30. end
  31. end
  32. end.to_s
  33. end
  34.  
  35. def save
  36. puts "Saving '#{@file}'..."
  37. File.open(@file, 'w+') do |f|
  38. h = {
  39. :directory => @dir,
  40. :timestamp => Time.now,
  41. :contents => @contents
  42. }
  43. f.write h.to_yaml
  44. end
  45. end
  46.  
  47. def compare
  48. @snapshot = YAML.load_file @file
  49. a_hash = @snapshot[:contents]
  50. b_hash = scan
  51. a_hash.delete_if {|k, v| k =~ /[\/\\]dirscan.yml$/}
  52. b_hash.delete_if {|k, v| k =~ /[\/\\]dirscan.yml$/}
  53. @result = {:created => [], :updated => [], :deleted => [], :unchanged => []}
  54. a_hash.each do |a_key, a_value|
  55. b_value = b_hash.delete a_key
  56. case
  57. when !b_value then
  58. @result[:deleted] << a_key
  59. when b_value != a_value then
  60. @result[:updated] << a_key
  61. else
  62. @result[:unchanged] << a_key
  63. end
  64. end
  65. @result[:created] = b_hash.keys
  66. end
  67.  
  68. def report
  69. total = @result.values.map{|v| v.size}.inject :'+'
  70. puts "="*50
  71. puts "Total files: #{total}"
  72. puts "- Unchanged: #{@result[:unchanged].size}"
  73. puts
  74. puts "Differences since #{@snapshot[:timestamp]}:"
  75. puts "- Updated: #{@result[:updated].size}"
  76. show_files :updated
  77. puts "- Deleted: #{@result[:deleted].size}"
  78. show_files :deleted
  79. puts "- Created: #{@result[:created].size}"
  80. show_files :created
  81. puts "="*50
  82. end
  83.  
  84. protected
  85.  
  86. def show_files key
  87. return unless @verbose
  88. @result[key].each do |file|
  89. puts "\t#{file.gsub /^#{Regexp.escape @dir.to_s}/, ''}"
  90. end
  91. end
  92. end
  93. end
  94.  
  95. options = Trollop::options do
  96. opt :scan, "Scan directory", :default => Dir.pwd
  97. opt :compare, "Compare files", :default => false
  98. opt :write, "Save snapshot file", :default => false
  99. opt :verbose, "Display file names as well", :default => false
  100. end
  101.  
  102. scanner = DirScan::Scanner.new(options[:scan], :verbose => options[:verbose])
  103.  
  104. if options[:compare] then
  105. scanner.compare
  106. scanner.save if options[:write]
  107. scanner.report
  108. else
  109. scanner.scan
  110. scanner.save
  111. end
Add Comment
Please, Sign In to add comment