Created
August 31, 2009 04:54
-
-
Save kaichen/178292 to your computer and use it in GitHub Desktop.
This file contains 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 | |
require 'ftools' | |
def get_sub_dirs_in_dir(dir) | |
`find #{dir} -type d`.split("\n") - [dir] | |
end | |
def get_sub_dirs_in_current_dir | |
get_sub_dirs_in_dir Dir.pwd | |
end | |
def get_files_in_dir(dir, base = false) | |
list =`find #{dir} -type f`.split("\n") | |
list = list.map { |f| File.basename(f) } if base | |
list | |
end | |
def ask_source_files(array, msg) | |
puts msg | |
array.each_with_index do |f, index| | |
puts " (#{index}) - #{f}" | |
end | |
array[gets.chomp.to_i] | |
end | |
def copy_missing_file_to_dir(src, dir) | |
puts "** copy #{src} to #{dir}, confirm?(y/N)" | |
loop do | |
break if gets.chomp =~ /y|yes/i | |
end | |
File::copy(src, dir, true) | |
end | |
if __FILE__ == $0 && ARGV.size == 0 | |
base_dir = Dir.pwd | |
puts "BASE DIR - #{base_dir}" | |
subdirs = get_sub_dirs_in_current_dir | |
files_index, subdirs_hash = Hash.new, Hash.new | |
subdirs.each do |sdir| | |
subdirs_hash[sdir] = get_files_in_dir(sdir) | |
get_files_in_dir(sdir).each do |f| | |
fname = File.basename f | |
files_index[fname] ||= [] | |
files_index[fname] << File.join(sdir, fname) | |
end | |
end | |
all_files = files_index.keys | |
subdirs.each do |sdir| | |
missings = all_files - get_files_in_dir(sdir, true) | |
missings.each do |mf| | |
if files_index[mf].size > 1 | |
msg = "** Please select a source file of #{mf} to #{sdir}" | |
src = ask_source_files(files_index[mf], msg) | |
end | |
src ||= files_index[mf].first | |
copy_missing_file_to_dir(src, sdir) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment