python 自动化打包程序exe安装文件
需求: 实现自动化打包exe
项目说明:
引擎cocos-js 3.16版
打包流程说明:
初始化项目Sln -> 编译C++工程 -> 压缩图片资源 -> js转化jsc -> 删除VS生成多余文件 -> 加密 -> 删除jsc -> 生成exe安装包
环境变量:
因为需要使用vs工具的环境变量初始化批处理。这里配置vs安装路径(具体到Tools)到环境变量path(D:\xxx\Common7\Tools)
(或者python自己获取)
依赖工具:
InnoSetup
具体实现(主要)代码:
class PackagePc(object):
def __init__(self, args):
self.workPath = WORK_PATH
self.vsDevCmd = "VsDevCmd.bat"
self.compileCommand = ""
self.compilePath = ""
self.issPath = ""
self.packConfig = utils.parse_json(os.path.join(self.workPath, "package.json"))
self.projectName = self.packConfig.get("projectName")
self.projectSlnPath = utils.flat_path(os.path.join(self.workPath, ‘../../../frameworks/runtime-src/proj.win32/‘))
self.compilePath = "{}{}".format(self.packConfig.get("compileType"), ".win32")
self.assetsPath = "{}{}{}".format(self.projectSlnPath, "\\", self.compilePath)
self.useLessFileList = [‘.pdb‘, ‘.ilk‘, ‘.lib‘, ‘.log‘, ‘.obj‘, ‘.exp‘, ‘.js‘, ‘.idb‘, ".res"]
def buildPcPack(self):
#初始化项目Sln
self.initProjectSln()
#编译C++工程
self.compileCPlus()
#压缩图片资源
self.doPngQuant()
#js转化jsc
self.compileJsFile()
#删除VS生成多余文件
self.removeUselessFile(self.useLessFileList)
#加密
self.doEncrypt()
#删除jsc
self.removeUselessFile([‘.jsc‘])
#生成exe安装包
self.setUpISS()
# 初始化编译命令
def initProjectSln(self):
slnName = self.packConfig.get("sln")
if not slnName:
raise_known_error(‘没有配置项目sln名‘, KnownError.ERROR_WRONG_CONFIG)
self.compileCommand = "{}{}{}{}{}{}".format("devenv ", self.projectSlnPath , "\\" , slnName, ".sln", " /rebuild release")
# 编译C++工程
def compileCPlus(self):
print(u"----------开始编译C++工程----------")
utils.run_shell("%s & %s" % (self.vsDevCmd, self.compileCommand))
# 压缩图片资源
def doPngQuant(self):
print(u"----------开始压缩图片资源----------")
excludeDict = {"exclude_files": ["gg_s9_res"], "exclude_folders": ["games", "src/libs/images/native/native_s9_res"]}
quant = png_quant.PngQuant(self.assetsPath, excludeDict)
quant.compress_images_in_path()
#压缩js文件
def compileJsFile(self):
print(u"----------开始js转化jsc----------")
if not os.path.exists(self.assetsPath):
print("compileJsFile path not found =" + self.assetsPath)
return
compilePath = utils.flat_path(os.path.join(self.workPath, ‘../../../frameworks/cocos2d-x/tools/cocos2d-console/bin/cocos‘))
compileCmd = "%s jscompile -s %s -d %s" % (compilePath, self.assetsPath, self.assetsPath)
utils.run_shell(compileCmd)
#资源加密
def doEncrypt(self):
print(u"----------开始资源加密----------")
ASSETS_ENCRYPT_EXCLUDE_CFG = ["games/mj_common/images/card_packer/*.png"]
encrypt = ResEncrypt(self.assetsPath, self.assetsPath, False, ASSETS_ENCRYPT_EXCLUDE_CFG, True, False)
encrypt.do_encrypt()
#删除项目中无用的文件
def removeUselessFile(self, fileList):
print(u"----------开始删除VS生成多余文件----------")for parent, dirNames, fileNames in os.walk(self.assetsPath):
for fileFullName in fileNames:
if os.path.splitext(fileFullName)[1] in fileList:
print("file %s"%(os.path.join(parent, fileFullName)))
os.remove(os.path.join(parent, fileFullName))
#生成exe安装包
def setUpISS(self):
print(u"----------开始生成exe安装包----------")
issPath = utils.flat_path(os.path.join(self.workPath, ‘../../../tools/hall/PackPC/‘))
devCmd = "{}{}".format("cd ", os.path.join(issPath, "InnoSetup"))
issCmd = "{}{}".format("iscc ", os.path.join(issPath, "build.iss"))
self.issPath = os.path.join(issPath, "build.iss")
if not os.path.exists(self.issPath):
raise_known_error("找不到ISS文件", KnownError.ERROR_OTHERS)
self.modifyISS(issPath, False)
utils.run_shell("%s & %s & %s" % (devCmd, "ISCC.exe", issCmd))
self.modifyISS(issPath, True)
#修改、还原Inno Setup打包配置
def modifyISS(self, issPath, isReduction):
with open(self.issPath, "rb") as f:
data = f.read()
f.close()
cfgVersion = self.packConfig.get("version").decode(‘utf8‘).encode(‘gb2312‘)
if isReduction:
data = data.replace(cfgVersion, "GameVersion")
data = data.replace(self.assetsPath, "PCProjectPath")
data = data.replace(issPath, "IcoPath")
data = data.replace("xxxxxPC" + cfgVersion + "-Setup", "OutputFilename")
else:
data = data.replace("GameVersion", cfgVersion)#修改版本号
data = data.replace("PCProjectPath", self.assetsPath)#修改项目路径名
data = data.replace("IcoPath", issPath)#修改ICO路径
data = data.replace("OutputFilename","xxxxxPC" + cfgVersion + "-Setup")
with open(self.issPath, "wb") as f:
f.write(data)
f.close()(python新手,如有改进之处,还麻烦指出)