升级列表自动生成脚本

一个自动生成升级文件列表的脚本,可以运行在WIN或者linux

要判断中间的版本号,取最大的

文件最终样式如下


productname:ABCikn7
version:2.23.81
uname:ABCikn7_2.23.81_20120101.upt
usize:3598290
dname:ABCikn7_2.23.81_20120101.dd5
dsize:33
url1:ftp://ftp.163.com/down/

productname:xx001A
version:4.0.4
uname:xx001A_4.0.4_20131114.upt
usize:14008320
dname:xx001A_4.0.4_20131114.dd5
dsize:40
url1:ftp://ftp.163.com/down/
url2:ftp://ftp.265.com/down/


#!/usr/bin/ruby
require 'net/ftp'
ip = ["1.2.3.4","2.11.12.2"] #server IP add
url = ["url1:ftp://ftp.163.com/down/","url2:ftp://ftp.265.com/down/"]
update_file = ["e:\\update.txt", "/tmp/update.dat"] #Win or Linux
file_hash = {}
text = ''
def os_family 
  case RUBY_PLATFORM 
    when /ix/i, /ux/i, /gnu/i, 
         /sysv/i, /solaris/i, 
         /sunos/i, /bsd/i 
      "unix" 
    when /win/i, /ming/i 
      "windows" 
    else 
      "other" 
  end 
end
if os_family == "unix"
    text_file = update_file[1]
elsif os_family == "windows"
    text_file = update_file[0]
else puts "unkown os"
    Process.exit(1)
end
def file_list(ip)
    #get ftp dir list
    files = []
    ip.size.times{|x|
        ftp = Net::FTP.open(ip[x])
        ftp.login('username','password')
        ftp.passive = true
        files << ftp.list('down')
        ftp.close
    }
    return files
end
def key_values(type,n,name_size) #get type_x.x.x_date.upt
    dd, up = '', ''
    if n == 1
        regexp = "#{type}_\\d+\\.\\d+\\.\\d+_\\d+."
    else
        regexp = "#{type}_\\d+\\."
    end
    2.times {
        name_size.each_key { |m|
            if /#{regexp}dd5/.match(m)
                dd = m.to_s
            elsif /#{regexp}upt/.match(m)
                up = m.to_s
            end
        }
    }
    return dd, name_size[dd], up, name_size[up]
end
def formatstr(dd,dd_size,up,up_size)   
    type, ver = '', ''
    tmp = []
    tmp = dd.split('_')
    type = tmp[0]
    ver = tmp[1]
msg =<<EOF
productname:#{type}
version:#{ver}
uname:#{up}
usize:#{up_size}
dname:#{dd}
dsize:#{dd_size}
EOF
return msg
end
def sorter(type,n,name_size)
    tmp = []
    a, a1, b, b1 = '', '', '', ''
    name_size.select {|x,y| tmp<<x if /#{type}_\d+\.\d+\.\d+_\d+\.upt/.match(x)}
    a = tmp[0].split('_')[1].split('.')
    a1 = "%d%04d%04d" % [a[0],a[1],a[2]]
    (n-1).times {
        b = tmp[n-1].split('_')[1].split('.')
        b1 = "%d%04d%04d" % [b[0],b[1],b[2]]
        if a1.to_i < b1.to_i
            a1 = b1
        end
    }
    return type + "_" + a1[-9..0].to_i.to_s + "." + a1[-8..-5].to_i.to_s + "." + a1[-4..-1].to_i.to_s
end
def update_text_create(list,x,url) 
    type = {}
    tmp_hash = {}
    log = ''
    name_size = {}
    list.each {|a|
        #get type hash
        tmp_array = a.split(' ')
        if /^\.|^\.\./.match(tmp_array[8]).nil? and /^total/.match(tmp_array[0]).nil?
            if type.has_key?(tmp_array[8].split('_')[0])
                type[tmp_array[8].split('_')[0]] = type[tmp_array[8].split('_')[0]] + 1
            else
                type[tmp_array[8].split('_')[0]] = 1
            end
            name_size[tmp_array[8]] = tmp_array[4]
        end
    }
        #get type
        type.each{|b,c|
            if c/2 == 1 #only one type
                dd5, dd_size, upt, upt_size = key_values(b,c/2,name_size)
                tmp_hash[formatstr(dd5, dd_size, upt, upt_size)] = url[x]
            else   
                dd5, dd_size, upt, upt_size = key_values(sorter(b,c/2,name_size),c/2,name_size)
                tmp_hash[formatstr(dd5, dd_size, upt, upt_size)] = url[x]         
            end
        }
        return tmp_hash
end
list = file_list(ip)
list.size.times {|x|
   file_hash[x] = update_text_create(list[x],x,url)
}
file_hash[0].each {|x,y|
    if file_hash[1].has_key?(x)
        file_hash[0][x] = y + "\n" + file_hash[1][x]
        file_hash[1].delete(x)
    end
}
def writefile(str,filename)
aFile = File.new(filename,"w")
       aFile.puts str
aFile.close
end
file_hash[0].merge(file_hash[1]).each {|x,y|
    text = text + "\n" + x + y + "\n"
}
writefile(text,text_file)


你可能感兴趣的:(Ruby,版本号判断)