您的位置:首页 > 编程语言 > Python开发

python一键打包

2016-02-22 15:05 483 查看
# -*- coding: UTF-8 -*-

#android package

from Tkinter import *

import shutil,zipfile,os,_subprocess,time

#python f:/python/pack/package.py

exeFile = "Des_Enctypt_AndPIC.exe"

xmlFile = "AndroidManifest.xml"

#batFile

debatFile = "decompression.bat"

gebatFile = "generate.bat"

sibatFile = "certificate.bat"

cobatFile = "Com.bat"

zibatFile = "zipalign.bat"

targetFile = ["fonts","res","script"]

def getVersionName(versionCode):
versionCode = versionCode < 10 and versionCode or 0
versionName = "1.0.%d" % versionCode
return versionName

def mergeDir(srcDir,dstDir):
if not os.path.exists(dstDir): os.mkdir(dstDir)
for f in os.listdir(srcDir):
srcFile = os.path.join(srcDir,f)
dstFile = os.path.join(dstDir,f)
if os.path.isdir(srcFile):
mergeDir(srcFile,dstFile)
else:
open(dstFile,"wb").write(open(srcFile,"rb").read())

def speMerge(src,dst):
for k in targetFile:
srcDir = os.path.join(src,k)
dstDir = os.path.join(dst,k)
print "wait ... " + k + " ... encrypt"
mergeDir(srcDir,dstDir)

def speRemoveDir(src):
for k in targetFile:
srcDir = os.path.join(src,k)
shutil.rmtree(srcDir,True)

def callExe(exePath,exeFile):
handle = _subprocess.CreateProcess(os.path.join(exePath,exeFile),"",None,None,0,_subprocess.CREATE_NEW_CONSOLE,None,exePath,STARTUPINFO())
run = True
while run:
rc = _subprocess.WaitForSingleObject(handle[0], 1000)
if rc == _subprocess.WAIT_OBJECT_0: run = False

def modBat(batFile,fileName,apkPath):
goalFile = os.path.join(apkPath,batFile)
goalStr,goalLine = "",1
if batFile[0:2] == "de": 
goalStr = "apktool d %s\n" % fileName
goalLine = 4
elif batFile[0:2] == "ge": 
goalStr = "apktool b %s new.apk\n" % fileName[:-4]
goalLine = 5
modFile(goalFile,goalLine,goalStr)

def modFile(goalFile,goalLine,goalStr):
contents = []
line = 0
tempFile = open(goalFile).readlines()
for L in tempFile:
line += 1
if line == goalLine: L = goalStr
contents.append(L)
open(goalFile,"w").write("")
for L in contents:
open(goalFile,"a").write(L)

class STARTUPINFO:

    dwFlags = 0

    hStdInput = None

    hStdOutput = None

    hStdError = None

    wShowWindow = 0

top = Tk()

top.title("one key to package in android")

# 创建框架

frame1st = Frame(top)

frame1st.pack()

frame2st = Frame(top)

frame2st.pack()

frame3st = Frame(top)

frame3st.pack()

frame4st = Frame(top)

frame4st.pack()

frame5st = Frame(top)

frame5st.pack()

width = 60

# 1、显示结果 ——lab

Label(frame1st,text = "结果",fg = "red",font = 50).pack(side = LEFT)

strResult = "Show result here!\nPlease wait.It may cost 4 mins to 5 mins,\nwhen you click pack btn!"

labResult = Label(frame1st,text = strResult,width = width,height = 15,font = 50)

labResult.pack(side = LEFT)

# 2、资源文件的路径 ——输入框

Label(frame2st,text = "资源文件目录: ",fg = "red",font = 50).pack(side = LEFT)

entryDir = Entry(frame2st,width = width,font = 40,bd = 3)

entryDir.pack(side = LEFT)

dirPath = "E:/Client/trunk/samples/MyMainGame/Resources"

entryDir.insert(0,dirPath)

# 3、tools的路径 ——输入框

Label(frame3st,text = "tools目录: ",fg = "red",font = 50).pack(side = LEFT)

entryDirTools = Entry(frame3st,width = width,font = 40,bd = 3)

entryDirTools.pack(side = LEFT)

dirPathTools = "E:/Client/tools"

entryDirTools.insert(0,dirPathTools)

# 4、apk文件名 ——输入框

Label(frame4st,text = "apk文件名(母包): ",fg = "red",font = 50).pack(side = LEFT)

entryApkName = Entry(frame4st,width = width,font = 40,bd = 3)

entryApkName.pack(side = LEFT)

apkFileName = "111.apk"

entryApkName.insert(0,apkFileName)

# 5、版本号 ——输入框

Label(frame5st,text = "版本号(versionCode): ",fg = "red",font = 50).pack(side = LEFT)

entryVersionCode = Entry(frame5st,width = width,font = 40,bd = 3)

entryVersionCode.pack(side = LEFT)

versionCode = "1"

entryVersionCode.insert(0,versionCode)

# 6、pack按钮 ——btn

#dirFile

def packEvent():
srcPath = entryDir.get()
toolPath = entryDirTools.get()
apkFileName = entryApkName.get()
versionCode = entryVersionCode.get()

exePath = toolPath + "/DES_Enctypt/Exe"
apkPath = toolPath + "/AndroidPack/ApkTools"
signPath = toolPath + "/AndroidPack/Auto-sign"
zipPath = toolPath + "/AndroidPack/Zipalign"

dstPath = os.path.join(exePath,"sourFile")
pngPath = os.path.join(dstPath,"res")

apkTemp = "new.apk"
apkTemp2 = "new2.apk"

s = "脚本开始执行,共9步:"
print s.decode('utf-8').encode('gbk')

timeBegin = time.time()

#copy file to exe --1
print "1-- copy file to exe"
speMerge(srcPath,dstPath)

# 图片压缩 --1.1
print "1.1-- compress .png file"
os.chdir(exePath)
os.system(cobatFile)

#exe encrypt --2
print "2-- exe encrypt"
callExe(exePath,exeFile)
#merge outfile to srcfile --3
print "3-- merge outfile to srcfile"
fileTemp = os.path.join(exePath,"outFile")
speMerge(fileTemp,os.path.join(exePath,"sourFile"))
#删除中间文件
speRemoveDir(fileTemp)

#modefied bat --4
print "4-- modefied bat"
fileName = apkFileName
modBat(debatFile,fileName,apkPath)
modBat(gebatFile,fileName,apkPath)

#decompression mother package --5
print "5-- decompression mother package"
os.chdir(apkPath)
os.system(debatFile)

#move file to asserts --6
print "6-- move file to asserts"
fileTemp = os.path.join(exePath,"sourFile")
speMerge(fileTemp,os.path.join(apkPath,fileName[:-4]) + "/assets")
#删除中间文件
speRemoveDir(fileTemp)
#mod xmlfile
fileTemp = os.path.join(apkPath,apkFileName[:-4])
goalFile = os.path.join(fileTemp,xmlFile)
goalLine = 2
goalStr = '<manifest android:versionCode="%s" android:versionName="%s" android:installLocation="auto" package="com.shy.idlegame"\n' % (versionCode,getVersionName(versionCode))
modFile(goalFile,goalLine,goalStr)

#generate new package --7
print "7-- generate new package"
os.system(gebatFile)
#删除中间文件
shutil.rmtree(fileTemp,True)

#certificate --8
print "8-- certificate"
os.chdir(signPath)
apkFile = os.path.join(apkPath,apkTemp)
newApkFile = os.path.join(signPath,apkTemp)
open(newApkFile,"wb").write(open(apkFile,"rb").read())
#删除中间文件
os.remove(apkFile)
os.system(sibatFile)
#删除中间文件
os.remove(newApkFile)

#zipalign --9
print "9-- zipalign"
os.chdir(zipPath)
apkFile = os.path.join(signPath,apkTemp2)
newApkFile = os.path.join(zipPath,apkTemp2)
open(newApkFile,"wb").write(open(apkFile,"rb").read())
#删除中间文件
os.remove(apkFile)
os.system(zibatFile)
#删除中间文件
os.remove(newApkFile)

timeEnd = time.time()
cost = timeEnd - timeBegin
print "cost = ",cost

labResult["text"] = "恭喜,你成功啦,耗时" + str("%.2f"%(cost/60)) + "分!" + "\n————生成的新包————\n" + os.path.join(zipPath,"优化后发布包.apk")

btnPack = Button(top,command = packEvent,text = "打\t包",fg = "purple",font = 80)

btnPack.pack()

top.mainloop()

#解压zip文件

#copy zip to dstpath

# name = "proj.android_Original"

# srcZipFile = "E:/Client/trunk_new/samples/MyMainGame/" + name + ".zip"

# dstPath = "F:/Program Files"

# shutil.copy(srcZipFile,dstPath)

# fileName = os.path.basename(srcZipFile)

# dstFile = os.path.join(dstPath,fileName)

# z = zipfile.ZipFile(dstFile, "r")

# for k in z.namelist():

# if not k.endswith("/"):

# f = os.path.join(dstPath,k)

# direct = os.path.dirname(f)

# if not os.path.exists(direct): os.makedirs(direct)

# file(f, "wb").write(z.read(k))

# z.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 一键打包