GEEK: Ruby scripts to compare the contents of directory to svn
A pair of functions I wrote that compare the contents of a configuration directory on a remote server with the expected contents in SVN. compareDir is the first draft and compareDirTwo is the second draft. compareDirTwo compares all of the files in all the directories below, whereas the compareDir only goes one level and doesn't compare the files in the topmost level. The compare flag will cause the function to compare the contents of the files, in addition to checking for existence. It assumes that all files are text files. Pasted here to remind me how I wrote the compareDir function, because I'm deleting it and it has some tricks in it that I want to remember.
def compareDir(host, location, svndir, actualdir, *rest) puts "Now comparing the #{actualdir} on #{host}, #{location} with the directory in svn: #{svndir}" status = "pass" differences = "" result = "" linebreak = "="*80 + "\n" puts message = "host: #{host}\n" puts "\n" svndirlisting = %x[ svn list #{svndir}] tunnel = SSHTunnel.new(host, location) actualdir_listing = tunnel.remoteExecute("find #{actualdir} -type d -maxdepth 1 -mindepth 1").to_a actualdir_listing.each do |dir| puts linebreak puts "dir: #{dir}\n" message += linebreak message += "dir: #{dir}\n" dirname = dir.chomp.split(/\//)[3] file_list = tunnel.remoteExecute("find #{actualdir}/#{dirname} -maxdepth 1 -mindepth 1").to_a file_list.each do |file| filename = file.chomp.split(/\//)[4] svnfilename = "#{svndir}/#{dirname}/#{filename}" svnfile = %x[ svn cat #{svnfilename} ] actualfile = tunnel.remoteExecute("cat #{file}") differences = stringDiff(svnfile, actualfile) if differences != "" puts result = "FAILED:\n" status = "fail" else puts result = "PASSED:\n" end puts filenames = "svn: #{svnfilename} \nactual: #{file}\n" puts differences message += result message += filenames message += differences end end tunnel.destroySSHTunnel # puts message if status != "pass" flunk end end
def compareDirTwo(host, location, svndir, actualdir, compare="true", *rest) puts "Now comparing the #{actualdir} on #{host}, #{location} with the directory in svn: #{svndir}" status = "pass" differences = "" result = "" linebreak = "="*80 + "\n" puts message = "host: #{host}\n" puts "\n" puts "svnlist:\n" svn = %x[ svn -R list #{svndir}].to_a # Get a list of all files/directories from svn. Remove the trailing / from directories. Prepend the base directory. svnall = svn.collect { |name| actualdir + "/" + name.chomp.gsub(/\/$/,'') } svnall.map { |file| puts "#{file}" } puts linebreak puts "svnfiles:\n" # Get a list of only files from svn svnfiles = svn.delete_if { |name| name =~ %r[(.*)/$] } svnfiles.map { |file| puts "#{file}" } puts linebreak tunnel = SSHTunnel.new(host, location) actual = tunnel.remoteExecute("find #{actualdir}").to_a.collect { |name| name.chomp } actual = actual.delete_if { |name| name == "#{actualdir}" } puts "actual:\n" actual.map { |file| puts "#{file}" } if compare: svnfiles.each do |file| svnfilename = "#{svndir}/#{file}" svnfile = %x[ svn cat #{svnfilename} ] actualfile = tunnel.remoteExecute("cat #{actualdir}/#{file}") differences = stringDiff(svnfile, actualfile) if differences != "" puts result = "FAILED:\n" status = "fail" else puts result = "PASSED:\n" end puts filenames = "svn: #{svnfilename} \ractual: #{actualdir}/#{file}\r" puts differences message += result message += filenames message += differences end end tunnel.destroySSHTunnel differences = actual - svnall if differences != []: puts "These files are in the #{actualdir} directory on #{host}, #{location} but are not in svn:\n", differences message += "FAILED:\n" status = "fail" end return status, message, differences end
Post a Comment
You must be logged in to post a comment.