赞
踩
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Used update game path to server
importos,re,sys,urllib,urllib2,hashlib,time,shutil,platform
defpost(status, type, info, err_info=""):
post_url = args["post_url"]
aid = args["aid"]
data = {"aid": aid,"status": status,"type": type,"info": info}
iferr_info:
data = {"aid": aid,"status": status,"type": type,"info": info,"err_info": err_info}
printdata
f = urllib2.urlopen(url=post_url, data=urllib.urlencode(data))
defmd5sum(file_name):
ifos.path.isfile(file_name):
f = open(file_name,'rb')
py_ver = sys.version[:3]
ifpy_ver =="2.4":
importmd5 as hashlib
else:
importhashlib
md5 = hashlib.md5(f.read()).hexdigest()
f.close()
returnmd5
else:
return0
defconfig(args,files):
try:
game = args["main_prefix"]
url ="http://208.asktao.com/autoupdate/%s/config.ini"% game
get = urllib.urlopen(url)
aa = get.readlines()
w = {}
foriinaa:
a = i.strip().split()[0]
if"["ina:
x = a.strip("[]")
w[x] = {}
continue
w[x][i.strip().split()[1].strip()] = i.strip().split()[0].strip()
forkeyinw:
ifkey == files:
returnw[key]
exceptException,e:
return0
classdown_start():
defwork(self,args):
aa = config(args,"path_md5")
game = args["main_prefix"]
ifaa ==0:
post(2,"read update config","Not find %s config file"% game)
sys.exit()
local ='/data/autoupdate/'
url ="http://208.asktao.com/autoupdate/%s/"% game
forfinaa:
md5_r = f.strip().split()[0]
pkg_name = f.strip().split()[1]
get = urllib.urlopen(os.path.join(url,pkg_name))
status = get.getcode()
ifstatus ==200:#验证MD5,MD5错误的话重新下载一次,再次错误就提示失败,退出程序
urllib.urlretrieve(os.path.join(url,pkg_name),os.path.join(local,pkg_name),)
md5_l = md5sum(os.path.join(local,pkg_name))
ifmd5_l == md5_r:
post(1,"down",pkg_name)
else:
urllib.urlretrieve(os.path.join(url,pkg_name),os.path.join(local,pkg_name),)
md5_l = md5sum(os.path.join(local,pkg_name))
ifmd5_l == md5_r:
post(1,"down",pkg_name)
else:
post(2,"down",pkg_name,"Download %s,MD5 not right"% pkg_name)
sys.exit()
else:
post(2,"down",pkg_name,"Not find %s path file"% pkg_name)
# 检查rsync服务是否启动
whileTrue:
pid = os.popen("ps auxww | grep 'rsync --daemon' | grep -v grep").read()
ifnotpid:
getso("/usr/bin/rsync --daemon")
continue
break
#全部完成,更新中心状态
post(1,"down","down_done")
classsync_start():
defrsync(self,update_pkg,args):
node_ip = args["node_ip"]
game = args["main_prefix"]
os_type = platform.system()
ifos_type =="Windows":
local ="C:\\update\\"
elifos_type =="Linux":
local ="/home/update/tmp/"
ifnotos.path.isdir(local):
os.mkdir(local)
forfinos.listdir(local):
ifos.path.isfile(f):
os.remove(os.path.join(local,f))
elifos.path.isdir(f):
shutil.rmtree(os.path.join(local,f))
aa = config(args,"path")
ifaa ==0:
post(2,"read update config","Not find %s config file"% game)
sys.exit()
forfinaa:
ifupdate_pkginf:
md5_r = aa[f]
pkg_name = f
ifos_type =="Windows":
sync ="rsync -avz %s::update/*%s* /cygdrive/c/update/"% (node_ip,update_pkg)
elifos_type =="Linux":
sync ="rsync -avz %s::update/*%s* /home/update/tmp/"% (node_ip,update_pkg)
result = os.popen(sync).readlines()
md5_l = md5sum(os.path.join(local,pkg_name))
ifmd5_l == md5_r:
post(1,"sync",pkg_name)
else:
ifos_type =="Windows":
sync ="rsync -avz %s::update/*%s* /cygdrive/c/update/"% (node_ip,update_pkg)
elifos_type =="Linux":
sync ="rsync -avz %s::update/*%s* /home/update/tmp/"% (node_ip,update_pkg)
result = os.popen(sync).readlines()
md5_l = md5sum(os.path.join(local,pkg_name))
ifmd5_l == md5_r:
post(1,"sync",pkg_name)
else:
post(2,"rsync",pkg_name,"sync Error,%s not find or file's md5 wrong"% pkg_name)
sys.exit()
defwork(self,args):
update_pkg = args["update_pkg"].split(',')
foriinrange(len(update_pkg)):
self.rsync(update_pkg[i],args)
post(1,"sync","sync_done")
classupdate_start():
defcopy(self,src, dst):
ifos.path.isdir(src):
base = os.path.basename(src)
ifos.path.exists(dst):
dst = os.path.join(dst, base)
ifnotos.path.exists(dst):
os.makedirs(dst)
names = os.listdir(src)
fornameinnames:
srcname = os.path.join(src, name)
self.copy(srcname, dst)
else:
shutil.copy2(src, dst)
defunrar(self,src,dst,args):
os_type = platform.system()
try:
ifnotos.path.exists(dst)ornotos.path.exists(src):
raiseException,"%s or %s not exist!"% (src, dst)
ifos_type =="Windows":
os.system(r'C:\Progra~1\WinRAR\rar x -o+ -inul %s %s'% (src, dst))
elifos_type =="Linux":
ifos.path.splitext(src)[1] ==".tgz":
os.system("tar -zxf %s -C %s"% (src, dst))
elifos.path.splitext(src)[1] ==".zip":
os.system("unzip -oq %s -d %s"% (src, dst))
return0
exceptException,e:
returne
defwork(self,args):
os_type = platform.system()
ifos_type =="Windows":
update ="C:\\update\\"
elifos_type =="Linux":
update ="/home/update/tmp/"
server_dir = {"tmcs":"C:\\Server\\","wd":"/home/asktao/"}
server = server_dir[args["main_prefix"]]
update_pkg = args["update_pkg"].split(',')
game = args["main_prefix"]
foriinrange(len(update_pkg)):
#####################解压更新包
fortgzinos.listdir(update):
ifupdate_pkg[i]intgz:
src = os.path.join(update,tgz)
r =self.unrar(src,update,args)
ifr ==0:
post(1,"update","%s unzip success"% src)
else:
post(2,"update",src,"unzip fail:%s"% r)
#####################拷贝更新文件到游戏目录
ser_list = os.listdir(server)
up_list = os.listdir(update)
fordirinup_list:
forlineinser_list:
filepath = os.path.join(update,dir+"\\")
serv = os.path.join(server,line+"\\")
ifdirinline:
self.copy(filepath,serv)
post(1,"update","%s files copy success"% line)
#####################验证重要文件MD5
aa = config(args,"files")
ifaa ==0:
post(2,"read update config","Not find %s config file"% game)
sys.exit()
forfinaa:
md5_r = aa[f]
pkg_name = f
ifos.path.exists(pkg_name):
md5_s = md5sum(pkg_name)
ifmd5_r == md5_s:
post(1,"update","%s md5 Ok,update success"% pkg_name)
else:
post(2,"update","%s md5 Error,update fail"% pkg_name)
sys.exit()
shutil.rmtree(update)
os.mkdir(update)
post(1,"update","All_done")
if__name__ =="__main__":
args = {"pack":"auto_update","func1":"down_start","url":"http://208.2222.com/manage/auto_update","post_url":"http://192.168.50.209/reg.php","update_pkg":"up,up","node_ip":"192.168.50.208","main_prefix":"tmcs","aid":"123"}
#down = down_start()
#down.work(args)
sync = sync_start()
sync.work(args)
update = update_start()
update.work(args)
'''''config.ini内容
[files]#需要验证MD5的重要文件绝对路径和MD5
15a6605156e29f68fdfd637e73a889d4 C:\Server\Line1\SAFlashPlayer.exe
15a6605156e29f68fdfd637e73a889d4 C:\Server\Line2\SAFlashPlayer.exe
[path]#更新包名字和MD5
32fcb8932799aa553db13b5b9b41e5e9 auto.rar
32fcb8932799aa553db13b5b9b41e5e9 update.rar
'''
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。