# -*- coding: utf-8 -*- import os import sys import platform import hashlib import json from versionIssue import versionIssue resSubFix = "../updateVersion/org_res/" hisSubFix = "../updateVersion/{}/version/history" ediSubFix = "../updateVersion/{}/version/edition" baseVersion = "1.0.0" skipFile = [ ".gitignore" ] skipSuffix = [ ".meta" ] skipFloder = [ ".svn", ".gradle", ".nomedia", ".idea", ".vscode", ".wing", "__pycache__" ] systemName = platform.system().lower() if( "darwin" == systemName ): skipFile.append( ".DS_Store" ) def validName( name, isFloder ): if isFloder: return name not in skipFloder else: if name in skipFile: return False for suffix in skipSuffix: if name.endswith( suffix ): return False return True def md5(fName): hash_md5 = hashlib.md5() with open(fName, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) return hash_md5.hexdigest() def browsFloder(floder): fileList = [] currentFiles = [] files = os.listdir(floder) files.sort() for file in files: childPath = os.path.join(floder, file) if os.path.isdir(childPath): if validName( file, True ): subFileList = browsFloder(childPath) fileList.extend(subFileList) else: if validName( file, False ): currentFiles.append(os.path.join(floder, file)) fileList.extend(currentFiles) return fileList def genFileMd5Dic(fileList): md5Dic = {} for file in fileList: digest = md5(file) md5Dic[file] = digest return md5Dic def getHistoryVersion(): resultList = [] historyRoot = hisSubFix files = os.listdir( historyRoot ) for file in files: if file.endswith( ".json" ): resultList.append( os.path.basename(file)[8:-5] ) return resultList def getNowVersion(): nowVI = versionIssue( baseVersion ) versionList = getHistoryVersion() for vr in versionList: curVI = versionIssue( vr ) if curVI.biggerThen(nowVI): nowVI = curVI return nowVI def genVersionJson( pathPreFix, version): startPos = len(pathPreFix) fileList = browsFloder(pathPreFix) fileList.sort() md5Dic = genFileMd5Dic(fileList) jsonStr = '{' + '"version":"{0}", "files":['.format( version ) isFirst = True for file in fileList: if not isFirst: jsonStr += "," else: isFirst = False jsonStr += '\n{"path"'+ ':"{0}","md5":"{1}"'.format( file[startPos:].replace("\\","/"), md5Dic[file] ) + '}' jsonStr += ']}' return jsonStr def getJsonPathByVersion(version): jsonFileName = "version_{}.json".format(version) return os.path.join( hisSubFix, jsonFileName ) def writeVersionFile(version): jsonFilePath = getJsonPathByVersion(version) jsonString = genVersionJson( resSubFix, version) with open(jsonFilePath, 'w', encoding='utf-8' ) as f: f.write(jsonString) def parseVersionFileToDic(version): jsonString = "" with open( getJsonPathByVersion(version), 'r', encoding='utf-8' ) as f: jsonString = f.read() dic = {} if 0 < len(jsonString): jsonRoot = json.loads( jsonString ) jsonList = jsonRoot.get("files") for fileItem in jsonList: dic[ fileItem.get("path") ] = fileItem.get("md5") return dic def genDiffJson(oldVersion, newVersion,enablePrint): oldDic = parseVersionFileToDic(oldVersion) newDic = parseVersionFileToDic(newVersion) jsonStr = '{' + '"newVersion":"{0}", "oldVersion":"{1}", "files":['.format( newVersion, oldVersion ) isFirst = True if enablePrint: print( u"========{}-{}========".format( oldVersion, newVersion ) ) for p in newDic: oldItemV = oldDic.get(p) newItemV = newDic.get(p) if oldItemV == None or oldItemV != newItemV: if enablePrint: if oldItemV == None: print( u"++新增:{}".format( p ) ) else: print( u"<>差异:{}".format( p ) ) if not isFirst: jsonStr += "," else: isFirst = False jsonStr += '\n{"path"'+ ':"{0}","md5":"{1}"'.format( p, newItemV ) + '}' jsonStr += ']}' return jsonStr def mkdir(path): if not os.path.exists(path): os.makedirs(path) def genDiffJsonVersionFile(version): diffPath = os.path.join(ediSubFix, version) mkdir(diffPath) lastVersionString = versionIssue(version).lastVersion().getVersion() versionList = getHistoryVersion() for versionItem in versionList: if version != versionItem: isLastVersion = ( lastVersionString == versionItem ) diffJsonString = genDiffJson(versionItem, version, isLastVersion ) jsonFilePath = os.path.join(diffPath, "{}-{}.json".format(versionItem, version)) with open(jsonFilePath, 'w', encoding='utf-8') as f: f.write(diffJsonString) def setPlatform(platform): global resSubFix global hisSubFix global ediSubFix resSubFix = os.path.join(sys.path[0], resSubFix ) hisSubFix = os.path.join(sys.path[0], hisSubFix.format(platform) ) ediSubFix = os.path.join(sys.path[0], ediSubFix.format(platform) ) if not os.path.exists(hisSubFix): os.makedirs(hisSubFix) if not os.path.exists(ediSubFix): os.makedirs(ediSubFix) def main(): platformValue = "default" if None != sys.argv: argvLength = len(sys.argv) for i in range(1, argvLength): findPos = sys.argv[i].find('Platform=') if -1 != findPos: platformValue = sys.argv[i][findPos+9:] setPlatform( platformValue ) version = None nowVersion = getNowVersion() if nowVersion.getVersion() == baseVersion: version = nowVersion.nextVersion() else: nextVersion = nowVersion.nextVersion() tip = u"""输入1或者2选择要生成版本:\n1. version {} (生成新版本)\n2. version {} (覆盖当前最高版本) """ print (tip.format(nextVersion.getVersion(), nowVersion.getVersion())) print (u'请输入:'), choice = input( "" ) if choice == "1": version = nextVersion elif choice == "2": version = nowVersion if version == None: print("version error") return print(u"""正在生成version {}""".format(version.getVersion())) writeVersionFile(version.getVersion()) genDiffJsonVersionFile(version.getVersion()) if __name__ == "__main__": main()