说明:通过比较源目录和地址目录文件夹下的VXX,ZIP文件来确定版本号,如果版本号低,就从源目录copy zip文件到地址目录,并且解压,然后删除地址目录的旧的版本的zip文件。
import shutil
import os
import zipfile
#source :更新文件的目录
#dst:应用安装目录
class fileupdate:
def __init__(self,source,dst):
self.source=source
self.dst=dst
def check_version(self,source): #检查目标地址的版本号,并返回版本号,文件路径
version=-1
path="NA"
for i in os.listdir(source):
if os.path.splitext(i)[1]==".zip":
#print (i)
if version<int((i[1:3])):
version=int((i[1:3]))
path=i
return [version,path]
def check_update(self):
new_version,new_path=self.check_version(self.source)
old_version,old_path=self.check_version(self.dst)
if(new_version>old_version):
print ("当前版本号:V"+str(old_version)+"最新版本号:V"+str(new_version)+"需要更新版本")
return 1
else:
print ("已经是最新版本")
return 0
def update(self):
new_version,new_path=self.check_version(self.source)
old_version,old_path=self.check_version(self.dst)
shutil.copyfile(self.source+"\\"+new_path,self.dst+"\\"+new_path) #把源代码copy从source拷贝到dst
print("把源代码的ZIP文件解压缩")
f = zipfile.ZipFile(self.dst+"\\"+new_path,'r')
for file in f.namelist():
f.extract(file,self.dst)
print("删除旧的安装zip")
for i in os.listdir(self.dst):
if os.path.splitext(i)[1]==".zip":
if i!=new_path:
os.remove(self.dst+"\\"+i)
print("更新完成")
if __name__ == '__main__':
sourcepath="d:\\1"
dstpath="d:\\software"
myupdate=fileupdate(sourcepath,dstpath)
if(myupdate.check_update()==1):
myupdate.update()