Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # -*- coding: utf-8 -*-
- require 'fileutils'
- SKIP_FILES = [
- '.DS_Store',
- '.sass-cache',
- 'node_modules'
- ]
- def self.fix(path)
- path.chomp('/').gsub(/([&';()\s])/, '\\\\\1')
- end
- def self.get_children(path)
- # because Dir.glob does not work well with sshfs mounts (at least via OSXFUSE)
- raw_children = `ls -A #{fix(path)}`
- raw_children.split("\n").map do |child_file|
- child_file
- end
- end
- def self.process(source, destination)
- children = get_children(source)
- children.each do |child|
- next if SKIP_FILES.include?(child)
- source_child = File.join(source, child)
- destination_child = File.join(destination, child)
- name_match = File.exists?(destination_child)
- if name_match
- size_match = File.directory?(source_child) || File.size(source_child) == File.size(destination_child)
- else
- size_match = false
- end
- match = name_match && size_match
- if match
- if File.directory?(source_child)
- puts "processing: #{fix(source_child)}"
- process(source_child, destination_child)
- end
- else
- if File.symlink?(source_child)
- symlink = File.readlink(source_child)
- puts "copy link: #{fix(source_child)} -> #{symlink}"
- FileUtils.ln_sf(symlink, destination_child)
- else
- puts "copy: #{fix(source_child)} to #{fix(destination)}"
- FileUtils.cp_r(source_child, destination, preserve: true)
- end
- end
- end
- end
- if ARGV.size < 2
- puts 'You MUST specify both source and destination directories!'
- exit!
- elsif ARGV.size > 2
- puts 'You must ONLY specify source and destination directories!'
- exit!
- elsif !(File.exists?(ARGV[0]) && File.exists?(ARGV[1]))
- puts 'You MUST specify source and destination directories that actually exist!'
- exit!
- end
- source = ARGV[0]
- destination = ARGV[1]
- # start now
- start_time = Time.now.to_s
- puts "Copying files from '#{source}' to '#{destination}'"
- puts 'This may take several minutes'
- process(source, destination)
- puts "Started at: #{start_time}\tFinished at: #{Time.now.to_s}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement