#!/usr/bin/env ruby #{{{ =begin updater by Lucas Oman (me@lucasoman.com) 2007-10-10 Purpose: By configuring urconf.yml in the current dir, modified files in the dir can be recursively uploaded to an FTP server based on the modified timestamp. Syntax: updater [-d] [-h] [-m] -d - Dry run. Show files that will be uploaded without actually uploading them. -h - Show "reminder" help -m - Make a template config file in the current dir =end #}}} require 'yaml' require 'net/ftp' def updateFiles(ftp,path)#{{{ Dir.open(path) do |d| d.each do |f| exempt = false if !$config["exempt"].nil? $config["exempt"].each do |e| exempt = true if f.match(e) end end next if (['.','..','urconf.yml'].include?(f) || exempt) fn = path+'/'+f if File.stat(fn).directory? updateFiles(ftp,fn) else if $config["lastupdate"].nil? || File.stat(fn).mtime > $config["lastupdate"] tofn = ($config['startdir'].nil? ? fn : $config['startdir'].sub(/\/$/,'')+fn.sub(/^\./,'')) print " uploading "+fn+" to "+tofn+"...\n" ftp.put(fn,fn) unless $dry end end end end end#}}} # argument stuff#{{{ def processArgs args = ARGV.join(' ').sub(/^-/,'').split(' -').collect{|a| a.split(' ') } $dry = argSet? args, 'd' $help = argSet? args, 'h' $makeconfig = argSet? args, 'm' end def argSet?(args,arg) !args.select{|a| a[0] == arg}.empty? end#}}} def openFtp#{{{ ftp = Net::FTP.new $config["host"] ftp.login $config["user"],$config["pass"] ftp.chdir $config["startdir"] unless $config["startdir"].nil? ftp end#}}} def closeFtp(ftp)#{{{ ftp.close end#}}} def updateConfig#{{{ $config["lastupdate"] = Time.now conf = File.new('urconf.yml','w') conf.write($config.to_yaml) conf.close end#}}} processArgs if $help print "updater [-d] [-h] [-m]\n" exit end if $makeconfig $config = {} $config['host'] = 'ftp.host.com' $config['user'] = 'username' $config['pass'] = 'password' $config['startdir'] = 'path/to/files' $config['exempt'] = ['.svn','Session.vim','.swp'] updateConfig print "Config created.\n" exit end $config = YAML::load File.open('urconf.yml') if $dry print "Dry run for updating files on "+$config["user"]+"@"+$config["host"]+"...\n" else print "Updating files on "+$config["user"]+"@"+$config["host"]+"...\n" end ftp = openFtp unless $dry updateFiles(ftp,'.') closeFtp(ftp) unless $dry updateConfig unless $dry print "... done.\n"