Advertisement
Guest User

Untitled

a guest
May 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # -*- coding: utf-8 -*-
  4.  
  5. require 'fileutils'
  6.  
  7. SKIP_FILES = [
  8. '.DS_Store',
  9. '.sass-cache',
  10. 'node_modules'
  11. ]
  12.  
  13. def self.fix(path)
  14. path.chomp('/').gsub(/([&';()\s])/, '\\\\\1')
  15. end
  16.  
  17. def self.get_children(path)
  18. # because Dir.glob does not work well with sshfs mounts (at least via OSXFUSE)
  19. raw_children = `ls -A #{fix(path)}`
  20. raw_children.split("\n").map do |child_file|
  21. child_file
  22. end
  23. end
  24.  
  25. def self.process(source, destination)
  26. children = get_children(source)
  27.  
  28. children.each do |child|
  29. next if SKIP_FILES.include?(child)
  30.  
  31. source_child = File.join(source, child)
  32. destination_child = File.join(destination, child)
  33.  
  34. name_match = File.exists?(destination_child)
  35.  
  36. if name_match
  37. size_match = File.directory?(source_child) || File.size(source_child) == File.size(destination_child)
  38. else
  39. size_match = false
  40. end
  41.  
  42. match = name_match && size_match
  43.  
  44. if match
  45. if File.directory?(source_child)
  46. puts "processing: #{fix(source_child)}"
  47.  
  48. process(source_child, destination_child)
  49. end
  50. else
  51. if File.symlink?(source_child)
  52. symlink = File.readlink(source_child)
  53. puts "copy link: #{fix(source_child)} -> #{symlink}"
  54. FileUtils.ln_sf(symlink, destination_child)
  55. else
  56. puts "copy: #{fix(source_child)} to #{fix(destination)}"
  57. FileUtils.cp_r(source_child, destination, preserve: true)
  58. end
  59. end
  60. end
  61. end
  62.  
  63. if ARGV.size < 2
  64. puts 'You MUST specify both source and destination directories!'
  65. exit!
  66. elsif ARGV.size > 2
  67. puts 'You must ONLY specify source and destination directories!'
  68. exit!
  69. elsif !(File.exists?(ARGV[0]) && File.exists?(ARGV[1]))
  70. puts 'You MUST specify source and destination directories that actually exist!'
  71. exit!
  72. end
  73.  
  74. source = ARGV[0]
  75. destination = ARGV[1]
  76.  
  77. # start now
  78. start_time = Time.now.to_s
  79.  
  80. puts "Copying files from '#{source}' to '#{destination}'"
  81.  
  82. puts 'This may take several minutes'
  83.  
  84. process(source, destination)
  85.  
  86. puts "Started at: #{start_time}\tFinished at: #{Time.now.to_s}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement