Created
July 28, 2012 20:56
-
-
Save adamhjk/3194758 to your computer and use it in GitHub Desktop.
djf-fsync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# ^ I added the shebang line, so you can run it like a program | |
require 'fileutils' | |
# I made check take two arguments, the src and the destination, so we can run it on | |
# any old directory we want. | |
def check(src, dest) | |
srcfiles = [] | |
destfiles = [] | |
# I broke these up a bit - using a regular expression to | |
# prune the top level directory off, so we always just wind up | |
# with the subdirectories, just like you were doing in your | |
# version. | |
Dir["#{src}/**/*"].each do |f| | |
unless File.directory?(f) | |
srcfiles << f.gsub!(/^#{src}/, "") | |
end | |
end | |
Dir["#{dest}/**/*"].each do |f| | |
unless File.directory?(f) | |
destfiles << f.gsub!(/^#{dest}/, "") | |
end | |
end | |
files2upload = srcfiles - destfiles | |
files2upload | |
end | |
# If you call the script with no args, use your | |
# homedir. Otherwise, take src and dest as the | |
# two arguments to the script | |
src = ARGV[0] || "/home/davidjaysonfoster/test2/" | |
dest = ARGV[1] || "/home/davidjaysonfoster/test/" | |
# We only call check once now! | |
files2upload = check(src, dest) | |
puts files2upload.inspect | |
if files2upload.length == 0 | |
puts "0 files to upload...goodbye." | |
exit | |
end | |
print "upload these #{files2upload.length} files? (yes/no) " | |
# Added STDIN here, for clarity - plus, a bare gets might | |
# actually get called against a different filehandle, if you | |
# aren't careful. | |
answer = STDIN.gets | |
if !answer.include?("yes") | |
puts "0 files transferred...goodbye." | |
exit | |
end | |
# Here we walk each file to upload, and | |
# we create the directory if we need it. | |
# File.dirname returns the directory part | |
# of the filesystem, rather than the full path. | |
files2upload.each do |f| | |
src_file = "#{src}/#{f}" | |
dest_file = "#{dest}/#{f}" | |
FileUtils.mkdir_p(File.dirname(dest_file)) | |
puts "Copied #{src_file} to #{dest_file}" | |
FileUtils.cp_r(src_file, dest_file, :preserve => true) | |
end | |
puts "#{files2upload.length} files uploaded...goodbye." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment