#!/usr/local/bin/ruby # upload.rb ver0.1 2001.4.25 by ippo # ver0.2 2001.4.27 by ippo +ftpup # # Usage: ruby update1.rb oldtime.list update.list newtime.list # # oldtime.list 前回時点file+更新時刻のlist-file # newtime.list 今回後のlist-file # update.list 新規+更新fileのlist-file。このlistのfileがupするlist require 'find' require 'net/ftp' include Net #require 'ftplib' #old-version flag_debug = true # for-debug ##### sub-routine? ##### def check_config(exp) # 設定項目指定不足 unless eval "defined? #{exp}" $stderr.puts "ERR; config parameter : #{exp} required" exit(1) end end ##### load config file ##### if ARGV[0] =~ /^-h/ $stderr.puts 'Usage; command (oldtime.list update.list newtime.list)' exit(1) end oldfile = "oldtime.list" oldfile = ARGV[0] if ARGV[0] != nil upfile = "update.list" upfile = ARGV[1] if ARGV[1] != nil newfile = "newtime.list" newfile = ARGV[2] if ARGV[2] != nil oldlist = [] newlist = [] uplist = [] ##### oldtime.list read ##### if File.exist?(oldfile) File.open(oldfile){ |f| f.each{ |line| if (line.chomp.strip.size != 0) && (line.strip !~ /^#/) name, time, opt = line.split("\t") depth = 0 name.gsub(/^(\s*)/) { depth = $1.size } name.gsub(/^(\s*)/,"") data = {} data[:name] = name.chomp.strip data[:time] = time.chomp data[:depth] = depth data[:opt] = opt oldlist.push(data) newlist.push(oldlist.size - 1) else newlist.push(line) end } } end update_comment = "#commnet:listupdate-" update_comment << Time.now.strftime("%Y.%m.%d %H:%M:%S") << "\n" newlist.push(update_comment) #p oldlist #exit ##### nowtime.list make ##### ##### local-dir file-listup ##### dir = LOCAL_DIR taillist = ASCIITAIL_LIST + BINARYTAIL_LIST tail = Regexp.new("\\.(" + taillist.join("|") + ")$") binary_tail = Regexp.new("\\.(" + BINARYTAIL_LIST.join("|") + ")$") #$stderr.print "CHK; file-listup " filelist = [] filelist = Dir.entries(dir).find_all{|file| file =~ tail}.sort #とりあえずいまは1-dirのみ。"Find.find(dir)"はそのうち nowlist = [] filelist.each{ |file| data = {} data[:name] = File.basename(file) data[:time] = File.mtime(file).strftime("%Y.%m.%d %H:%M:%S") data[:depth] = 0 nowlist.push(data) } def list_to_s(data) str = "" (0...data[:depth]).each { str << " " } str << data[:name] << "\t" << data[:time] if data[:opt] != nil str << "\t" << data[:opt] end str << "\n" return str end ##### update.list make ##### nowlist.each_index{ |j| data = {} data = nowlist[j] olddata = {} oldnum = nil flag = false #nowlistのかくfileをoldlistないでがいとうsearch oldlist.each_index{ |i| if oldlist[i][:opt] != "x" if oldlist[i][:name] =~ data[:name] flag = true olddata = oldlist[i] oldnum = i # oldlist.slice!(i) end end } # serach-hitしたらnowlistにcheckずみmark if flag # $stderr.puts "CHK; " << data[:name] # time-checkでuplistへ,newlistのtime-change if data[:time] > olddata[:time] uplist.push(data[:name]) olddata[:time] = data[:time] newlist[newlist.index(oldnum)] = list_to_s(olddata) end nowlist[j][:time] = "x" end } nowlist.each{ |data| if data[:time] != "x" newlist.push(list_to_s(data)) uplist.push(data[:name]) end } #p uplist #exit ##### newtime.list make ##### newlist.each_index { |j| # if newlist[j].to_s =~ /^\d+$/ # case newlist[j] # when Numeric # $stderr.puts newlist[j].type # if newlist[j].is_a? Numeric if newlist[j].type == Fixnum newlist[j] = list_to_s(oldlist[newlist[j]]) end } tmp = newlist.pop if tmp != update_comment newlist.push(tmp) end #p newlist #exit ################################## File.open(upfile, "w"){ |f| uplist.each { |data| f.puts data } } File.open(newfile, "w"){ |f| newlist.each { |data| f.print data } }