#检查软件更新版本与旧版本之间的文件差异
#check two folder list the difference: input folderpath, output modification
#output:{'New':[],'Delete':[],'Modification':[]}
#!有同名文件
def ParsedPathList(PathList,WorkPath):
NameList=[]
for path in PathList:
NameList.append(path.split(WorkPath)[-1])
return NameList
def Nor2List(ListPathA,ListPathB,PathA,PathB):
NorList=[]
ComList=[]
ListA=ParsedPathList(ListPathA,PathA)
ListB=ParsedPathList(ListPathB,PathB)
ia=0
while(ia<len(ListA)):
ib=0
Lstart=len(ComList)
while(ib<len(ListB)):
if(ListA[ia]==ListB[ib]):
ComList.append(ListPathA[ia])
ib=ib+1
Lend=len(ComList)
if Lstart==Lend:
NorList.append(ListPathA[ia])
ia=ia+1
return NorList,ComList
#该函数返回PATH目录下所有的文件List
import os
def recursionFile(PATH):
if(PATH[-1]!='/'):
PWD=PATH+'/'
else:
PWD=PATH # 如果路径已经有有反斜杠就不用加了
Files=[]
#列出当前目录下文件和目录
CurrentDir=os.listdir(PATH)
#如果都是文件,则结束,否则分成folder和file,示性向量-1,1
for i in range (len(CurrentDir)):
#如果CurrentDir下的元素不是目录则查看文件类型,收集文件,若是目录则递归
if(os.path.isdir(PWD+CurrentDir[i])):
Files=Files+(recursionFile(PWD+CurrentDir[i]))
elif(os.path.isfile(PWD+CurrentDir[i])):
Files.append(PWD+CurrentDir[i])
return Files
import time
import hashlib
def CountExcutionTime(DifferenceByMD5):
def wrapper(*args, **kwargs):
startime=time.time()
res=DifferenceByMD5(*args, **kwargs)
#有返回数值函数使用装饰器,要将返回值一层层传出来
terminaltime=time.time()
runtime = terminaltime-startime
print("time is %d ms" %runtime)
return res
return wrapper
@CountExcutionTime
def DifferenceByMD5(NewFolder,OldFolder):
NewFolderList=recursionFile(NewFolder)
OldFolderList=recursionFile(OldFolder)
#Search string from new in old folder
NewNorList,ComListNew=Nor2List(NewFolderList,OldFolderList,NewFolder,OldFolder)
OldNorList,ComListOld=Nor2List(OldFolderList,NewFolderList,OldFolder,NewFolder)
Modification=[]
Equal=[]
NewNameList=ParsedPathList(ComListNew,NewFolder)
OldNameList=ParsedPathList(ComListOld,OldFolder)
newi=0
while(newi<len(NewNameList)):
NewFileName=NewNameList[newi]
oldi=0
while(oldi<len(OldNameList)):
OldFileName=OldNameList[oldi]
if (NewFileName==OldFileName):
NewFilePath=ComListNew[newi]
OldFilePath=ComListOld[oldi]
NewMD5=hashlib.md5(open(NewFilePath,'rb').read()).hexdigest()
OldMD5=hashlib.md5(open(OldFilePath,'rb').read()).hexdigest()
if NewMD5!=OldMD5:
Modification.append(NewFilePath)
else:
Equal.append(NewFilePath)
oldi+=1
newi+=1
return {'Modify':Modification,'New Create':NewNorList,'Delete Old':OldNorList,'Equal':Equal}
#Code example:
WorkPath='D:/Script/PDKVersion/1'
NewFolder=WorkPath+'/'+'software_v1.0.1'
OldFolder=WorkPath+'/'+'software_v1.0.0'
res=DifferenceByMD5(NewFolder,OldFolder)