#!/usr/local/bin/ruby # upload.rb ver0.1 2001.4.25 by ippo # ver0.2 2001.4.27 by ippo +ftpup # # Usage: ruby upload.rb sample.cfg # # 指定ローカルフォルダ以下、階層的に指定拡張子ファイルをチェック、 # 指定リモートで階層に準じて同名ファイルをチェック、 # 更新日付を比較し、新しい方だけリモートへ転送 # *)リモートに存在しなければ新規ファイル、新規フォルダ作成 # *)各指定項目は別ファイルに記述、引数に指定して読込 # ?)私の好みとしては、ローカルに更新日付テーブルを持っといて、 #   それを参照しながらという形態が好きだが。 # 謝)net/ftpについて指摘して頂いたnotさんに感謝。 # =begin ##### note: こういう内容のsample.cfgファイルが必要、引数指定。##### HOST = 'XXX.XXXXX.ne.jp' # ftpアドレス USER = 'XXXNNNNN' # アカウント名 PASSWD = 'XXXXXXXX' # パスワード REMOTE_DIR = 'homepage/' # 向こうのディレクトリ LOCAL_DIR = '.' # こちらのディレクトリ ASCIITAIL_LIST = ["htm", "txt"] # UPしたいファイルの拡張子 BINARYTAIL_LIST = ["jpg", "png"] # UPしたいファイルの拡張子:バイ UPLOAD_LIST = 'XXX.list' # UPたいファイルのlistファイル(省略可) =end 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].nil? $stderr.puts "ERR; please set config-file." exit(1) end load(ARGV[0]) ##### 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 = [] if defined? UPLOAD_LIST $stderr.print "-from:" << UPLOAD_LIST << "- " File.open(UPLOAD_LIST) { |f| f.each { |data| filelist.push(data.chomp) $stderr.print "." } } else Find.find(dir) { |data| if File.ftype(data) =~ "file" filelist.push(data) if data =~ tail end $stderr.print "." } end $stderr.print "\n" $stderr.puts "CHK; filelist" if defined? flag_debug $stderr.p filelist if defined? flag_debug ##### remote-dir file-upload ##### host = HOST user = USER passwd = PASSWD dir = REMOTE_DIR $stderr.puts "CHK; remote ftp connection ..." ftp = FTP.open(host, user, passwd) $stderr.puts "CHK; remote-dir ..." flag = 1 begin ftp.chdir(dir) $stderr.puts "CHK; change dir : #{dir}" rescue if flag == 1 flag = 0 fulldir = '' dir.split('/').each { |nowdir| fulldir += (nowdir) ftp.mkdir(fulldir) $stderr.puts "CHK; making dir : #{fulldir}" fulldir += '/' } retry else $stderr.puts "ERR; cannot down remote-dir #{dir}" exit(1) end end $stderr.puts "CHK; remote-file ..." filelist.each { |file| $stderr.puts "CHK; #{file}\n" if defined? flag_debug ##### time check ##### ltime = File.mtime(file) ltime_str = ltime.strftime("%Y.%m.%d %H:%M:%S") begin rtime = ftp.mtime(file).localtime rtime_str = rtime.strftime("%Y.%m.%d %H:%M:%S") rescue rtime = 0 rtime_str = "new-file " end $stderr.puts "local #{ltime}" if defined? flag_debug $stderr.puts "remote #{rtime}" if defined? flag_debug ##### upload ##### if ltime > rtime flag = 1 begin if file =~ binary_tail $stderr.print "CHK; b-upload; #{file} : #{rtime_str} -> #{ltime_str} " ftp.putbinaryfile(file, file, 4096) { |data| $stderr.print "." $stderr.flush } else $stderr.print "CHK; a-upload; #{file} : #{rtime_str} -> #{ltime_str} " i = 0 ftp.puttextfile(file, file) { |line| if i % 128 == 0 $stderr.print "." $stderr.flush end i += 1 } end $stderr.print "\n" $stderr.flush rescue if flag == 1 flag = 0 dirlist = file.split('/') # dirlist.pop fulldir = '' dirlist.each { |nowdir| fulldir += (nowdir) ftp.mkdir(fulldir) $stderr.puts "CHK; making dir test : #{fulldir}" fulldir += '/' } else $stderr.puts "ERR; cannot upload #{file}" exit(1) end retry end end } $stderr.puts "CHK; upload end" ftp.close