def work2JPG(filename, isPng = False): filepath = FileHelper.realpath(filename) filedir = FileHelper.dirname(filepath) name = FileHelper.basename(filepath) os.chdir(tpDir) jpgCMD = """%s -quality 90 %s %s """ % (convertBin, filepath, filepath) os.system(jpgCMD) #return tmpfilename = FileHelper.join(filedir, hashlib.md5(name.encode('utf-8')).hexdigest()) isSuccess = True with open(tmpfilename, 'wb+') as tmpFile: try: tmpFile.write(b'MNG') rgbname = filepath FileHelper.writeWithSize(tmpFile, filepath) except Exception: Log.printDetailln ("error00 !!!", filename, "cannot convert.") isSuccess = False finally: pass if isSuccess: FileHelper.remove(filepath) FileHelper.rename(tmpfilename, filepath) return 5 else: return 2
def task_convertImage2(tid, taskTotSize, absFilePath, relativepath, imgPacker, taskSize): ret = imgPacker.convert(absFilePath) Log.printDetailln("转换[%d/%d]: %s" % (taskSize, taskTotSize, relativepath)) return ret == 0
def task_compileLuaJit2(tid, taskTotSize, absFilePath, relativepath, luaPacker, taskSize): ret = luaPacker.compile(absFilePath, relativepath) Log.printDetailln("编译[%d/%d]: %s" % (taskSize, taskTotSize, relativepath)) return ret == 0
def print(self): if self.empty(): return Log.printDetailln("%s.print:" % (self.name)) while not self.empty(): task = self.get() ret, *args = task.param Log.printDetailln('\t', ret, *args)
def runProcess(tid, taskQueue, errorQueue): try: while taskQueue.qsize() > 0: func, *args = taskQueue.get() if not func(tid, *args): errorQueue.put((func, *args)) except: t, v, tb = sys.exc_info() print(t, v) traceback.print_tb(tb) Log.printDetailln('线程异常,' + str(sys.exc_info())) finally: Log.printDetailln('线程结束, tid : ' + str(tid))
def runProcess(tid, taskQueue, errorQueue): try: while taskQueue.qsize() > 0: func, *args = taskQueue.get() if not func(tid, *args) : errorQueue.put((func, *args)) except : t, v, tb = sys.exc_info() print(t, v) traceback.print_tb(tb) Log.printDetailln('线程异常,' + str(sys.exc_info())) finally : Log.printDetailln('线程结束, tid : ' + str(tid))
def runThread(tid, taskQueue, errorQueue): try: while True: func, *args = taskQueue.get_nowait() if not func(tid, *args) : errorQueue.put((func, *args)) except queue.Empty: pass except : t, v, tb = sys.exc_info() print(t, v) traceback.print_tb(tb) Log.printDetailln('线程异常,' + str(sys.exc_info())) finally : Log.printDetailln('线程结束, tid : ' + str(tid))
def runThread(tid, taskQueue, errorQueue): try: while True: func, *args = taskQueue.get_nowait() if not func(tid, *args): errorQueue.put((func, *args)) except queue.Empty: pass except: t, v, tb = sys.exc_info() print(t, v) traceback.print_tb(tb) Log.printDetailln('线程异常,' + str(sys.exc_info())) finally: Log.printDetailln('线程结束, tid : ' + str(tid))
def _packimg_rgb_a8(tid, ctx, filePath): noneExt = os.path.splitext(filePath)[0] Log.printDetailln('exe packimg_data_single : ' + filePath) if ctx.platformType == Constant.PLATFORM_IOS : # IOS鎵撳寘 cmd = str(Config.CMD_PACKIMG_RGB_PVR).format(fileName=filePath, noneExtName=noneExt) retval = execCmd(cmd) else: # 瀹夊崜鎵撳寘 cmd = str(Config.CMD_PACKIMG_RGB_ETC).format(fileName=filePath, noneExtName=noneExt) retval = execCmd(cmd) if retval == 0 and re.match(r".*\.png", filePath) : cmd = str(Config.CMD_PACKIMG_A8).format(fileName=filePath, noneExtName=noneExt) retval = execCmd(cmd) os.remove(filePath) return retval == 0
def _packimg_rgb(tid, ctx, filePath): noneExt = os.path.splitext(filePath)[0] Log.printDetailln('exec packimg_data_single : ' + filePath) cmd = str(Config.CMD_CONVERT_COLOR_LEVEL).format(fileName=filePath) retval = execCmd(cmd) if retval != 0 : return False if ctx.platformType == Constant.PLATFORM_IOS : # IOS鎵撳寘 cmd = str(Config.CMD_PACKIMG_RGB_PVR).format(fileName=filePath, noneExtName=noneExt) retval = execCmd(cmd) else: # 瀹夊崜鎵撳寘 cmd = str(Config.CMD_PACKIMG_RGB_ETC).format(fileName=filePath, noneExtName=noneExt) retval = execCmd(cmd) os.remove(filePath) return retval == 0
def remove(absFilePath, realPath = ''): with open(absFilePath, "rb") as tmpFile: content = tmpFile.read() if len(content) < 3: return 0 if content[0:3] == b'\xef\xbb\xbf': content = content[3:] with open(absFilePath, "wb+") as tmpFile: tmpFile.write(content) if realPath != '': Log.printDetailln("[Bom] removed: %s" % (realPath)) return 0
def _packimg_rgb(tid, ctx, filePath): noneExt = os.path.splitext(filePath)[0] Log.printDetailln('exec packimg_data_single : ' + filePath) cmd = str(Config.CMD_CONVERT_COLOR_LEVEL).format(fileName=filePath) retval = execCmd(cmd) if retval != 0: return False if ctx.platformType == Constant.PLATFORM_IOS: # IOS鎵撳寘 cmd = str(Config.CMD_PACKIMG_RGB_PVR).format(fileName=filePath, noneExtName=noneExt) retval = execCmd(cmd) else: # 瀹夊崜鎵撳寘 cmd = str(Config.CMD_PACKIMG_RGB_ETC).format(fileName=filePath, noneExtName=noneExt) retval = execCmd(cmd) os.remove(filePath) return retval == 0
def _packimg_rgb_a8(tid, ctx, filePath): noneExt = os.path.splitext(filePath)[0] Log.printDetailln('exe packimg_data_single : ' + filePath) if ctx.platformType == Constant.PLATFORM_IOS: # IOS鎵撳寘 cmd = str(Config.CMD_PACKIMG_RGB_PVR).format(fileName=filePath, noneExtName=noneExt) retval = execCmd(cmd) else: # 瀹夊崜鎵撳寘 cmd = str(Config.CMD_PACKIMG_RGB_ETC).format(fileName=filePath, noneExtName=noneExt) retval = execCmd(cmd) if retval == 0 and re.match(r".*\.png", filePath): cmd = str(Config.CMD_PACKIMG_A8).format(fileName=filePath, noneExtName=noneExt) retval = execCmd(cmd) os.remove(filePath) return retval == 0
def work_file_GZIP(filename, nameType=1): filepath = os.path.realpath(filename) filedir = os.path.dirname(filepath) sys.stdout.flush() with open(filepath, 'rb') as tmpFile: tmpContent = tmpFile.read(3) if tmpContent[0:2] == b'\x1f\x8b': Log.printDetailln("Gzip File, pass.") return 0 os.chdir(tpDir) if not os.path.exists(filepath): Log.printDetailln("error !!!", filepath, "cannot convert.") return 2 gzip_cmd = gzipBin + filepath + " -n -f -9" os.system(gzip_cmd) if os.path.exists(filepath): os.remove(filepath) os.rename(filepath + ".gz", filepath) return 0
tstart = time.clock() for thread in self.threads: thread.join() tend = time.clock() self.elapse = tend - tstart def test_runable(tid, *args): Log.printDetailln('进程:' + str(tid), *args) return True if __name__ == '__main__': work = Work(count=os.cpu_count()) for i in range(0, 50): work.putTask(Task(target=test_runable, args=('41', '2', '3', ))) work.start() work.join() Log.printDetailln("Completed in %.3fs" % (work.elapse)) work.tasks.print() work.errs.print() pass
def start(self): if self.isOK: self.process() else: Log.printDetailln("路径错误, 请重试...")
def _removebom(tid, ctx, filePath): Log.printDetailln('UTF-8 remove bom : ' + filePath) cmd = str(Config.CMD_REMOVE_BOM).format(fileName=filePath) retval = execCmd(cmd) return retval == 0
def process(ctx=Context()): try: Log.printInfoln('==============开始打包==============') if not os.path.isdir(ctx.resPath): Log.printInfoln('源目录不存在, resPath : ' + ctx.resPath) return if os.path.exists(ctx.getOutputPath()): shutil.rmtree(ctx.getOutputPath(), True) Log.printDetailln('删除目录 : ' + ctx.getOutputPath()) if os.path.exists(ctx.getTempPath()): shutil.rmtree(ctx.getTempPath(), True) Log.printDetailln('删除目录 : ' + ctx.getTempPath()) # 不管新包还是旧包,release数据备份都不已md5命名,只有export里用md5命名 if os.path.exists(ctx.getExportPath()): shutil.rmtree(ctx.getExportPath(), True) Log.printDetailln('删除目录 : ' + ctx.getExportPath()) Log.printInfo('【1/4】<1/1> 扫描并计算MD5...') ctx.curScanner = FileScanner() ctx.curScanner.initFromRootPath(ctx.resPath) Log.printInfoln('完成') os.makedirs(ctx.getDataFolder(), exist_ok=True) # 路径比较的时候加入root前缀,然后判断是不是同一个文件,不需要直接判断相对路径 ctx.folderPackList = PackHelper.getFolderPackList(ctx.resPath) # 从output文件件到release文件夹的备份 if ctx.isNew or ctx.lastReleasePath == '': Log.printInfo('复制完整文件夹, from : ' + ctx.resPath + ", to : " + ctx.getOutputResPath() + ' ...') shutil.copytree(ctx.resPath, ctx.getOutputResPath()) Log.printInfoln('完成') if not PackProcess.process(ctx): return Log.printInfo('复制完整文件夹, from : ' + ctx.getOutputResPath() + ", to : " + ctx.getReleaseOutputPath() + ' ...') shutil.copytree(ctx.getOutputPath(), ctx.getReleaseOutputPath()) Log.printInfoln('完成') else: removeSet = set() if not compareAndPack(ctx, removeSet): return Log.printInfo('复制最近release版本 to : ' + ctx.getReleaseOutputPath() + ' ...') shutil.copytree(ctx.lastReleasePath, ctx.getReleaseOutputPath()) Log.printInfoln('完成') Log.printInfo('删除失效文件(' + str(len(removeSet)) + '个)...') # 删除removeSet中的文件和文件夹 for removeRelPath in removeSet: removeAbsPath = os.path.join( ctx.getReleaseOutputPath() + os.sep + 'res', removeRelPath) if not os.path.exists(removeAbsPath): continue if os.path.isfile(removeAbsPath): os.remove(removeAbsPath) elif os.path.isdir(removeAbsPath): shutil.rmtree(removeAbsPath) dir_util.copy_tree(ctx.getOutputPath(), ctx.getReleaseOutputPath()) Log.printInfoln('完成') # 生成xml-before Log.printInfo('生成Xml-before...') beforeXml = ctx.getReleaseOutputPath() + os.sep + Constant.BEFORE_XML XmlMgr.write(beforeXml, ctx.curScanner.fileList) Log.printInfoln('完成') # 生成xml-after Log.printInfo('生成Xml-after...') afterScanner = FileScanner() afterScanner.initFromRootPath(ctx.getReleaseOutputPath() + os.sep + 'res') afterXml = ctx.getReleaseOutputPath() + os.sep + Constant.AFTER_XML XmlMgr.write(afterXml, afterScanner.fileList) Log.printInfoln('完成') os.makedirs(ctx.getExportPath(), exist_ok=True) Log.printInfo('导出变更文件到export目录...') if ctx.isFull: for r, d, fileList in os.walk(ctx.getReleaseOutputPath() + os.sep + 'res'): for file in fileList: srcAbsPath = os.path.join(r, file) relativePath = os.path.relpath(srcAbsPath, ctx.getReleaseOutputPath()) targetPath = os.path.join( ctx.getExportPath(), relativePath + '_' + PackHelper.calcMd5(srcAbsPath)) os.makedirs(os.path.dirname(targetPath), exist_ok=True) shutil.copyfile(srcAbsPath, targetPath) else: for r, d, fileList in os.walk(ctx.getOutputPath()): for file in fileList: srcAbsPath = os.path.join(r, file) relativePath = os.path.relpath(srcAbsPath, ctx.getOutputPath()) targetPath = os.path.join( ctx.getExportPath(), relativePath + '_' + PackHelper.calcMd5(srcAbsPath)) os.makedirs(os.path.dirname(targetPath), exist_ok=True) shutil.copyfile(srcAbsPath, targetPath) xmlMd5 = PackHelper.calcMd5(afterXml) # 生成文件列表 shutil.copyfile(afterXml, ctx.getExportPath() + os.sep + xmlMd5) # 生成version文件 output = open(ctx.getExportPath() + os.sep + 'version.html', 'w') output.write('{\n') output.write('\tmd5:\"' + xmlMd5 + '\"\n') output.write('}') output.close() Log.printInfoln('完成') except: t, v, tb = sys.exc_info() print(t, v) traceback.print_tb(tb)
def task_convertMap2(tid, taskTotSize, absFilePath, relativepath, taskSize): ret = PackMap.convert(absFilePath) Log.printDetailln("转换[%d/%d]: %s" % (taskSize, taskTotSize, relativepath)) return ret == 0
def test_runable(tid, *args): Log.printDetailln('进程:' + str(tid), *args) return True
def work(self, filename): filepath = os.path.realpath(filename) for filtername in self.filters: if filepath.find(filtername) != -1: self.PassCnt = self.PassCnt + 1 self.passFiles.append(filename) return 2 with open(filepath, 'rb') as tmpFile: tmpContent = tmpFile.read(3) if tmpContent[0:2] == b'\x1f\x8b': Log.printDetailln("Gzip File, pass.") self.GzipCnt = self.GzipCnt + 1 return if tmpContent == b"MNG": Log.printDetailln("MNG File, pass.") self.MngCnt = self.MngCnt + 1 return if tmpContent == b"PKM": Log.printDetailln("PKM File, pass.") self.PkmCnt = self.PkmCnt + 1 return isAlphaJPG = False isTC4 = False isDTC4 = False for filtername in self.TC4Module: if filepath.find(filtername) != -1: isTC4 = True break for filtername in self.DTC4Module: if filepath.find(filtername) != -1: isDTC4 = True break for filtername in self.RGB_JPGModule: if filepath.find(filtername) != -1: isAlphaJPG = True break isPng = True if filename.find(".png") != -1: isPng = True elif filename.find(".jpg") != -1: isPng = False ret = 0 if self.isIOS: if isPng: ret = PackImg2Mng_PVR.convert(filepath, self.DTC4Module) if ret == 2: ret = PackImgJPG.convert(filepath, isPng) else: ret = PackImgJPG.convert(filepath, isPng) else: ret = PackImg2Mng_ETC.convert(filepath, isAlphaJPG) if ret == 1: Log.printDetailln("ocurr Error") self.passFiles.append(filepath) elif ret == 2: self.PassCnt = self.PassCnt + 1 self.passFiles.append(filepath) elif ret == 3: self.GzipCnt = self.GzipCnt + 1 elif ret == 4: self.PkmCnt = self.PkmCnt + 1 elif ret == 5: self.MngCnt = self.MngCnt + 1 return ret
def work_png(filename): filepath = FileHelper.realpath(filename) filedir = FileHelper.dirname(filepath) os.chdir(tpDir) isSaveTransFile = False isPng = True useGZIP = False if isPng: jpgCMD = """%s %s -background black %s """ % (convertBin, filepath, filepath.replace(".png", ".rgb.jpg")) alphaCMD = """%s %s -alpha extract %s """ % (convertBin, filepath, filepath.replace(".png", ".alpha.jpg")) try: os.system(jpgCMD) os.system(alphaCMD) except Exception: Log.printDetailln ("error33 !!!", filename, "cannot convert.") return 2 finally: pass tmpfilename = filepath.replace(".png", ".tmp") FileHelper.remove(tmpfilename) isSuccess = True with open(tmpfilename, 'wb+') as tmpFile: try: tmpFile.write(b'MNG') rgbname = filepath.replace(".png", ".rgb.jpg") FileHelper.writeWithSize(tmpFile, rgbname) alphaname = filepath.replace(".png", ".alpha.jpg") FileHelper.writeWithSize(tmpFile, alphaname) if not isSaveTransFile: FileHelper.remove(rgbname) FileHelper.remove(alphaname) except Exception: Log.printError() isSuccess = False pass finally: pass if isSuccess: if useGZIP: gzip_cmd = gzipBin + tmpfilename + " -n -f -9" os.system(gzip_cmd) FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename + ".gz", tmpfilename.replace(".tmp", ".png")) return 3 else: FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename, tmpfilename.replace(".tmp", ".png")) return 5 else: return 2
def work_file_ETC(filename, isAlphaJPG = False, isFast = False): filepath = FileHelper.realpath(filename) filedir = FileHelper.dirname(filepath) sys.stdout.flush() #preAlpha = needPreAplha(filedir) preAlpha = False preCMD = " -p " if not preAlpha: preCMD = "" os.chdir(toolsPath) isPng = True if filename.find(".png") != -1: isPng = True elif filename.find(".jpg") != -1: isPng = False else: return 2 if isFast: quality = 'etcfast' else: quality = 'etcslow' rgbCMD = """ %s -f ETC1 %s -q %s -i %s -o %s """ % (pvrTexToolBin, preCMD, quality, filepath, filepath.replace(".png", ".pvr")) alphaCMD = """%s %s -alpha extract %s """ % (convertBin, filepath, filepath.replace(".png", ".alpha.jpg")) alphaJPGCMD = """ %s -f ETC1 -q %s -i %s -o %s """ % (pvrTexToolBin, quality, filepath.replace(".png", ".alpha.jpg"), filepath.replace(".png", ".alpha.pvr")) try: if isPng: FileHelper.remove(filepath.replace(".png", ".pkm")) FileHelper.remove(filepath.replace(".png", "_alpha.pkm")) os.system(rgbCMD) os.system(alphaCMD) if not isAlphaJPG: os.system(alphaJPGCMD) FileHelper.rename(filepath.replace(".png", ".pvr"), filepath.replace(".png", ".pkm")) if isAlphaJPG: FileHelper.rename(filepath.replace(".png", ".alpha.jpg"), filepath.replace(".png", "_alpha.pkm")) else: FileHelper.rename(filepath.replace(".png", ".alpha.pvr"), filepath.replace(".png", "_alpha.pkm")) FileHelper.remove(filepath.replace(".png", ".alpha.jpg")) FileHelper.remove(filepath.replace(".png", ".alpha.pvr")) else: FileHelper.remove(filepath.replace(".jpg", ".pkm")) rgbCMD = """ %s -f ETC1 -p -q %s -i %s -o %s """ % (pvrTexToolBin, quality, filepath, filepath.replace(".jpg", ".pvr")) os.system(rgbCMD) FileHelper.rename(filepath.replace(".jpg", ".pvr"), filepath.replace(".jpg", ".pkm")) except Exception: t, v, tb = sys.exc_info() Log.printDetailln(t, v) pass finally: pass if isPng: tmpfilename = filepath.replace(".png", ".tmp") FileHelper.remove(tmpfilename) isSuccess = True with open(tmpfilename, 'wb+') as tmpFile: try: tmpFile.write(b'MNG') rgbname = filepath.replace(".png", ".pkm") alphaname = filepath.replace(".png", "_alpha.pkm") FileHelper.writeWithSize(tmpFile, rgbname) FileHelper.writeWithSize(tmpFile, alphaname) # if preAlpha: # tmpFile.write('p') # else: # tmpFile.write('P') if not isSaveTransFile: FileHelper.remove(rgbname) FileHelper.remove(alphaname) except Exception: Log.printError() isSuccess = False pass finally: pass if isSuccess: if isUseGzip: gzip_cmd = gzipBin + tmpfilename + " -n -f -9" os.system(gzip_cmd) FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename + ".gz", tmpfilename.replace(".tmp", ".png")) return 3 else: FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename, tmpfilename.replace(".tmp", ".png")) return 5 else: FileHelper.remove(tmpfilename) return 2 else: tmpfilename = filepath.replace(".jpg", ".pkm") if not FileHelper.exists(tmpfilename): Log.printDetailln ("error !!!", filepath, "cannot convert.") return 2 if isUseGzip: gzip_cmd = gzipBin + tmpfilename + " -n -f -9" os.system(gzip_cmd) FileHelper.remove(tmpfilename.replace(".pkm", ".jpg")) FileHelper.rename(tmpfilename + ".gz", tmpfilename.replace(".pkm", ".jpg")) return 3 else: FileHelper.remove(tmpfilename.replace(".pkm", ".jpg")) FileHelper.rename(tmpfilename, tmpfilename.replace(".pkm", ".jpg")) return 4
def process(self): tstart = time.clock() if not os.path.isdir(self.dir): if self._FileCat(os.path.realpath(self.dir)): self.file_tot_count = self.file_tot_count + 1 else: for r, d, fileList in os.walk(self.dir): for file in fileList: absFilePath = os.path.join(r, file) if self._FileCat(absFilePath): self.file_tot_count = self.file_tot_count + 1 Log.printDetailln("%d Files loaded Completed, Compiling..." % (self.file_tot_count)) self.work.start(self.file_tot_count) self.work.join() tend = time.clock() errs = self.work.errs if not errs.empty(): Log.printDetailln("This files ocurs ERROR:") while not errs.empty(): task = errs.get() absFilePath, realpath, *args = task.param Log.printDetailln('\t', realpath) Log.printDetailln("\n\nCompeleted in %.3fs." % (tend - tstart)) if len(self.imgPacker.passFiles) > 0: Log.printDetailln("Pass Files:") for passfile in self.imgPacker.passFiles: Log.printDetailln('\t' + passfile) return True
def compareAndPack(ctx, removeSet): if os.path.exists(ctx.getOutputPath()) : shutil.rmtree(ctx.getOutputPath(), True) Log.printInfo('扫描对比列表...') lastScanner = XmlMgr.read(ctx.lastReleasePath + os.sep + Constant.BEFORE_XML) # 遍历更新文件夹找出需要copy到工作区的文件 fileDatas = ctx.curScanner.getUpdateFileLists(lastScanner) copySet = set() for fileData in fileDatas : path = fileData.getAbsPath() relPath = fileData.relativePath # 打了大图的文件夹需要整个删除重来 for folderPath in PackHelper.getFolderPackList(ctx.resPath) : if str(path + os.sep).find(folderPath + os.sep) >= 0 : relPath = os.path.relpath(folderPath, ctx.resPath) removeSet.add(relPath) break; copySet.add(relPath) # 遍历删除的文件,找出大图文件有删除的重新打包 removeDatas = ctx.curScanner.getRemoveFileLists(lastScanner) for fileData in removeDatas : path = os.path.normpath(os.path.join(ctx.resPath, fileData.relativePath)) relPath = fileData.relativePath # 打大图的文件夹内有文件被删除,需要整个大图文件夹重新打包一遍 for folderPath in PackHelper.getFolderPackList(ctx.resPath) : if str(path + os.sep).find(folderPath + os.sep) >= 0 : relPath = os.path.relpath(folderPath, ctx.resPath) copySet.add(relPath) break; removeSet.add(relPath) if re.match(r'^.*\.((jpg)|(png)|(jpeg))$', relPath) : noneExtPath = os.path.splitext(relPath)[0] removeSet.add(noneExtPath + '.pvr') removeSet.add(noneExtPath + '._alpha.pvr') Log.printInfoln('完成') # 复制文件 os.makedirs(ctx.getOutputPath()) Log.printDetail('复制打包文件...') for relativePath in copySet : targetPath = ctx.getOutputResPath() + os.sep + relativePath; sourcePath = ctx.resPath + os.sep + relativePath; if not os.path.exists(sourcePath) : Log.printDetailln('复制失败, 文件或文件夹不存在, path : ' + sourcePath) continue if os.path.isdir(sourcePath) : shutil.copytree(sourcePath, targetPath) elif os.path.isfile(sourcePath) : os.makedirs(os.path.dirname(targetPath), exist_ok=True) shutil.copyfile(sourcePath, targetPath) else : Log.printDetailln('复制失败, path : ' + sourcePath) Log.printDetailln('完成') # 开始打包 Log.printDetailln('开始打包') if len(copySet) != 0 : if not PackProcess.process(ctx) : return False Log.printDetailln('打包完成') return True
def compareAndPack(ctx, removeSet): if os.path.exists(ctx.getOutputPath()): shutil.rmtree(ctx.getOutputPath(), True) Log.printInfo('扫描对比列表...') lastScanner = XmlMgr.read(ctx.lastReleasePath + os.sep + Constant.BEFORE_XML) # 遍历更新文件夹找出需要copy到工作区的文件 fileDatas = ctx.curScanner.getUpdateFileLists(lastScanner) copySet = set() for fileData in fileDatas: path = fileData.getAbsPath() relPath = fileData.relativePath # 打了大图的文件夹需要整个删除重来 for folderPath in PackHelper.getFolderPackList(ctx.resPath): if str(path + os.sep).find(folderPath + os.sep) >= 0: relPath = os.path.relpath(folderPath, ctx.resPath) removeSet.add(relPath) break copySet.add(relPath) # 遍历删除的文件,找出大图文件有删除的重新打包 removeDatas = ctx.curScanner.getRemoveFileLists(lastScanner) for fileData in removeDatas: path = os.path.normpath( os.path.join(ctx.resPath, fileData.relativePath)) relPath = fileData.relativePath # 打大图的文件夹内有文件被删除,需要整个大图文件夹重新打包一遍 for folderPath in PackHelper.getFolderPackList(ctx.resPath): if str(path + os.sep).find(folderPath + os.sep) >= 0: relPath = os.path.relpath(folderPath, ctx.resPath) copySet.add(relPath) break removeSet.add(relPath) if re.match(r'^.*\.((jpg)|(png)|(jpeg))$', relPath): noneExtPath = os.path.splitext(relPath)[0] removeSet.add(noneExtPath + '.pvr') removeSet.add(noneExtPath + '._alpha.pvr') Log.printInfoln('完成') # 复制文件 os.makedirs(ctx.getOutputPath()) Log.printDetail('复制打包文件...') for relativePath in copySet: targetPath = ctx.getOutputResPath() + os.sep + relativePath sourcePath = ctx.resPath + os.sep + relativePath if not os.path.exists(sourcePath): Log.printDetailln('复制失败, 文件或文件夹不存在, path : ' + sourcePath) continue if os.path.isdir(sourcePath): shutil.copytree(sourcePath, targetPath) elif os.path.isfile(sourcePath): os.makedirs(os.path.dirname(targetPath), exist_ok=True) shutil.copyfile(sourcePath, targetPath) else: Log.printDetailln('复制失败, path : ' + sourcePath) Log.printDetailln('完成') # 开始打包 Log.printDetailln('开始打包') if len(copySet) != 0: if not PackProcess.process(ctx): return False Log.printDetailln('打包完成') return True
def process(ctx=Context()): try: Log.printInfoln('==============开始打包==============') if not os.path.isdir(ctx.resPath) : Log.printInfoln('源目录不存在, resPath : ' + ctx.resPath) return; if os.path.exists(ctx.getOutputPath()) : shutil.rmtree(ctx.getOutputPath(), True) Log.printDetailln('删除目录 : ' + ctx.getOutputPath()) if os.path.exists(ctx.getTempPath()) : shutil.rmtree(ctx.getTempPath(), True) Log.printDetailln('删除目录 : ' + ctx.getTempPath()) # 不管新包还是旧包,release数据备份都不已md5命名,只有export里用md5命名 if os.path.exists(ctx.getExportPath()) : shutil.rmtree(ctx.getExportPath(), True) Log.printDetailln('删除目录 : ' + ctx.getExportPath()) Log.printInfo('【1/4】<1/1> 扫描并计算MD5...') ctx.curScanner = FileScanner() ctx.curScanner.initFromRootPath(ctx.resPath) Log.printInfoln('完成') os.makedirs(ctx.getDataFolder(), exist_ok=True) # 路径比较的时候加入root前缀,然后判断是不是同一个文件,不需要直接判断相对路径 ctx.folderPackList = PackHelper.getFolderPackList(ctx.resPath) # 从output文件件到release文件夹的备份 if ctx.isNew or ctx.lastReleasePath == '': Log.printInfo('复制完整文件夹, from : ' + ctx.resPath + ", to : " + ctx.getOutputResPath() + ' ...') shutil.copytree(ctx.resPath, ctx.getOutputResPath()) Log.printInfoln('完成') if not PackProcess.process(ctx) : return Log.printInfo('复制完整文件夹, from : ' + ctx.getOutputResPath() + ", to : " + ctx.getReleaseOutputPath() + ' ...') shutil.copytree(ctx.getOutputPath(), ctx.getReleaseOutputPath()) Log.printInfoln('完成') else : removeSet = set() if not compareAndPack(ctx, removeSet) : return Log.printInfo('复制最近release版本 to : ' + ctx.getReleaseOutputPath() + ' ...') shutil.copytree(ctx.lastReleasePath, ctx.getReleaseOutputPath()) Log.printInfoln('完成') Log.printInfo('删除失效文件(' + str(len(removeSet)) + '个)...') # 删除removeSet中的文件和文件夹 for removeRelPath in removeSet : removeAbsPath = os.path.join(ctx.getReleaseOutputPath() + os.sep + 'res', removeRelPath) if not os.path.exists(removeAbsPath) : continue if os.path.isfile(removeAbsPath) : os.remove(removeAbsPath) elif os.path.isdir(removeAbsPath) : shutil.rmtree(removeAbsPath) dir_util.copy_tree(ctx.getOutputPath(), ctx.getReleaseOutputPath()) Log.printInfoln('完成') # 生成xml-before Log.printInfo('生成Xml-before...') beforeXml = ctx.getReleaseOutputPath() + os.sep + Constant.BEFORE_XML XmlMgr.write(beforeXml, ctx.curScanner.fileList) Log.printInfoln('完成') # 生成xml-after Log.printInfo('生成Xml-after...') afterScanner = FileScanner() afterScanner.initFromRootPath(ctx.getReleaseOutputPath() + os.sep + 'res') afterXml = ctx.getReleaseOutputPath() + os.sep + Constant.AFTER_XML XmlMgr.write(afterXml, afterScanner.fileList) Log.printInfoln('完成') os.makedirs(ctx.getExportPath(), exist_ok=True) Log.printInfo('导出变更文件到export目录...') if ctx.isFull : for r, d, fileList in os.walk(ctx.getReleaseOutputPath() + os.sep + 'res') : for file in fileList : srcAbsPath = os.path.join(r, file) relativePath = os.path.relpath(srcAbsPath, ctx.getReleaseOutputPath()) targetPath = os.path.join(ctx.getExportPath(), relativePath + '_' + PackHelper.calcMd5(srcAbsPath)) os.makedirs(os.path.dirname(targetPath), exist_ok=True) shutil.copyfile(srcAbsPath, targetPath) else : for r, d, fileList in os.walk(ctx.getOutputPath()) : for file in fileList : srcAbsPath = os.path.join(r, file) relativePath = os.path.relpath(srcAbsPath, ctx.getOutputPath()) targetPath = os.path.join(ctx.getExportPath(), relativePath + '_' + PackHelper.calcMd5(srcAbsPath)) os.makedirs(os.path.dirname(targetPath), exist_ok=True) shutil.copyfile(srcAbsPath, targetPath) xmlMd5 = PackHelper.calcMd5(afterXml) # 生成文件列表 shutil.copyfile(afterXml, ctx.getExportPath() + os.sep + xmlMd5) # 生成version文件 output = open(ctx.getExportPath() + os.sep + 'version.html', 'w') output.write('{\n') output.write('\tmd5:\"' + xmlMd5 + '\"\n') output.write('}') output.close() Log.printInfoln('完成') except : t, v, tb = sys.exc_info() print(t, v) traceback.print_tb(tb)
def task_XXTeaEncode2(tid, taskTotSize, absFilePath, relativepath, taskSize): ret = PackXXTea.encode(absFilePath) Log.printDetailln("加密[%d/%d]: %s" % (taskSize, taskTotSize, relativepath)) return ret == 0
def work_file_PVR(filename, isDTC4Module = False, isTC4 = False): filepath = FileHelper.realpath(filename) filedir = FileHelper.dirname(filepath) sys.stdout.flush() os.chdir(toolsPath) isTC4 = True isAlphaJPG = False if isDTC4Module: isTC4 = False preCMD = " -p " preCMD = "" info = ImageInfo.size(filepath) # 只支持png纹理 if info[0] != 'PNG': return 2 width = info[1] height = info[2] # 只支持正方形2的幂纹理 if width & (width-1) != 0 or width != height: return 2 rgbCMD = """ %s -f PVRTC1_4_RGB %s -q pvrtcbest -i %s -o %s """ % (pvrTexToolBin, preCMD, filepath, filepath.replace(".png", ".pvr")) alphaCMD = """%s %s -alpha extract %s """ % (convertBin, filepath, filepath.replace(".png", ".alpha.jpg")) alphaJPGCMD = """ %s -f PVRTC1_4_RGB -q pvrtcbest -i %s -o %s """ % (pvrTexToolBin, filepath.replace(".png", ".alpha.jpg"), filepath.replace(".png", ".alpha.pvr")) if isTC4: rgbCMD = """ %s -f PVRTC1_4 %s -q pvrtcbest -i %s -o %s """ % (pvrTexToolBin, preCMD, filepath, filepath.replace(".png", ".pvr")) try: FileHelper.remove(filepath.replace(".png", ".pkm")) FileHelper.remove(filepath.replace(".png", "_alpha.pkm")) os.system(rgbCMD) if not isTC4: os.system(alphaCMD) if not isAlphaJPG and not isTC4: os.system(alphaJPGCMD) if not FileHelper.exists(filepath.replace(".png", ".pvr")): return 2 os.rename(filepath.replace(".png", ".pvr"), filepath.replace(".png", ".pkm")) if not isTC4: if not isAlphaJPG: os.rename(filepath.replace(".png", ".alpha.jpg"), filepath.replace(".png", "_alpha.pkm")) else: os.rename(filepath.replace(".png", ".alpha.pvr"), filepath.replace(".png", "_alpha.pkm")) FileHelper.remove(filepath.replace(".png", ".alpha.jpg")) FileHelper.remove(filepath.replace(".png", ".alpha.pvr")) except Exception: Log.printError() return 2 finally: pass tmpfilename = filepath.replace(".png", ".tmp") FileHelper.remove(tmpfilename) isSuccess = True with open(tmpfilename, 'wb+') as tmpFile: try: tmpFile.write(b'MNG') rgbname = filepath.replace(".png", ".pkm") statinfo = os.stat(rgbname) fileSize = statinfo.st_size tmpFile.write(pack("i", fileSize)) rgbfile = open(rgbname, "rb") tmpFile.write(rgbfile.read()) rgbfile.close() alphaname = filepath.replace(".png", "_alpha.pkm") if not isTC4: statinfo = os.stat(alphaname) fileSize = statinfo.st_size tmpFile.write(pack("i", fileSize)) alphafile = open(alphaname, "rb") tmpFile.write(alphafile.read()) alphafile.close() # if preAlpha: # tmpFile.write('p') # else: # tmpFile.write('P') if not isSaveTransFile: FileHelper.remove(rgbname) FileHelper.remove(alphaname) except Exception: t, v, tb = sys.exc_info() Log.printDetailln(t, v) isSuccess = False pass finally: pass if isSuccess: if isUseGzip: gzip_cmd = gzipBin + tmpfilename + " -n -f -9" os.system(gzip_cmd) FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename + ".gz", tmpfilename.replace(".tmp", ".png")) return 3 else: FileHelper.remove(tmpfilename.replace(".tmp", ".png")) FileHelper.rename(tmpfilename, tmpfilename.replace(".tmp", ".png")) return 5
tend = time.clock() self.elapse = tend - tstart if __name__ == '__main__': work = Work(count=4) def runable(tid, *args): Log.printDetailln('线程:' + str(tid), *args) return True for i in range(0, 50): work.putTask(Task(target=runable, args=( '41', '2', '3', ))) work.start() work.join() Log.printDetailln("Completed in %.3fs" % (work.elapse)) work.tasks.print() work.errs.print() pass
def runable(tid, *args): Log.printDetailln('线程:' + str(tid), *args) return True
def process(self): tstart = time.clock() if not os.path.isdir(self.dir): if self._FileCat(os.path.realpath(self.dir)): self.file_tot_count = self.file_tot_count + 1 else: for r, d, fileList in os.walk(self.dir) : for file in fileList : absFilePath = os.path.join(r, file) if self._FileCat(absFilePath): self.file_tot_count = self.file_tot_count + 1 Log.printDetailln("%d Files loaded Completed, Compiling..." % (self.file_tot_count)) self.work.start(self.file_tot_count) self.work.join() tend = time.clock() errs = self.work.errs if not errs.empty(): Log.printDetailln("This files ocurs ERROR:") while not errs.empty(): task = errs.get() absFilePath, realpath, *args = task.param Log.printDetailln('\t', realpath) Log.printDetailln("\n\nCompeleted in %.3fs." % (tend - tstart)) if len(self.imgPacker.passFiles) > 0 : Log.printDetailln("Pass Files:") for passfile in self.imgPacker.passFiles: Log.printDetailln('\t' + passfile) return True