iOS开发之 使用python自动化打包

一般使用企业证书打包的朋友可能会想到使用脚本进行打包,因为打包的次数相对普通开发者比较多,So本人自己网搜一堆Python(注:本人python一窍不通)并改成所需脚本,基本可以满足打包所需要求。

打包命令

直接上代码:

#-*-coding:utf-8-*-

#!/usr/bin/python

import sys

reload(sys)

sys.setdefaultencoding('utf-8')

from optparse import OptionParser

import subprocess

#import requests

import os

import shutil

import commands

#渠道文件

channellist        = ['渠道1','渠道2','....']

#渠道文件存放路径

channelpath        = 'MultiChannels'

#渠道文件下的资源文件

filenamelist        = ['Info.plist','58 - Settings.png','80 - Spotlight.png','120 - iPhone.png']

#项目中被替换的资源文件

filepathlist        = ['IosClient/Info.plist','IosClient/Assets.xcassets/AppIcon.appiconset/58 - Settings.png','IosClient/Assets.xcassets/AppIcon.appiconset/80 - Spotlight.png','IosClient/Assets.xcassets/AppIcon.appiconset/120 - iPhone.png']

#项目名称

projectname        = 'IosClient'

#导出ipa文件存放路径

OUTPUT = "/Users/xxxxx/Desktop/xxxxxx(文件夹名)"

#注:本人项目是cocoapods管理,因此此处路径是xcwordsapce,否则是xcodepro路径

WORKSAPCE = "/Users/xxxxxx/Desktop/xxxx/xxxx.xcworkspace"

#此处与项目保持一致

TARGET = "IosClient"

#同上

SCHEME = "IosClient"

#项目的配置信息

#configuration for iOS build setting

#CODE_SIGN_IDENTITY = "iPhone Distribution: xxxxxxxx Co. Ltd (xxxxxxx9A)"

#PROVISIONING_PROFILE = "xxxxxxxxxx-xxxxx-xxxxx-xxxx-xxxxxxxxxxxx"

#CONFIGURATION = "Release"

#SDK = "iphoneos"

#上传蒲公英

# configuration for pgyer

#PGYER_UPLOAD_URL = "http://www.pgyer.com/apiv1/app/upload"

#DOWNLOAD_BASE_URL = "http://www.pgyer.com"

#USER_KEY = "5994aef875878ee001ab35900803da74"

#API_KEY = "390700ad2e15a298225e0210161438a3"

def cleanBuildDir(buildDir):

cleanCmd = "rm -r %s" %(buildDir)

process = subprocess.Popen(cleanCmd, shell = True)

process.wait()

print "cleaned buildDir: %s" %(buildDir)

# 替换文件

def replaceflies(channel):

workspacepath = os.getcwd()

srcpath = os.path.join(workspacepath, channelpath)

srcpath = os.path.join(srcpath, channel)

despath = os.path.join(workspacepath, projectname)

# 替换资源

for filename in filenamelist:

index = filenamelist.index(filename)

filepath = filepathlist[index]

srcfullpath = os.path.join(srcpath, filename)

desfullpath = os.path.join(despath, filepath)

if os.path.exists(srcfullpath):

shutil.copy(srcfullpath, desfullpath)

print "Copy '" + srcfullpath + "' to '" + desfullpath + "'."

else:

print "'" + srcfullpath + "'" + "不存在"

#def buildProject(project, target, output):

#    if len(CODE_SIGN_IDENTITY) and len(PROVISIONING_PROFILE):

#        buildCmd = 'xcodebuild -project %s -target %s -sdk %s -configuration %s build CODE_SIGN_IDENTITY="%s" PROVISIONING_PROFILE="%s"' %(project, target, SDK, CONFIGURATION, CODE_SIGN_IDENTITY, PROVISIONING_PROFILE)

#        print buildCmd

#        pass

#    else:

#        buildCmd = 'xcodebuild -project %s -target %s -sdk %s -configuration %s' %(project, target, SDK, CONFIGURATION)

#        print buildCmd

#

#        process = subprocess.Popen(buildCmd, shell = True)

#        process.wait()

#

#        signApp = "./build/%s-iphoneos/%s.app" %(CONFIGURATION, target)

#        signCmd = "xcrun -sdk %s -v PackageApplication %s -o %s" %(SDK, signApp, output)

#        process = subprocess.Popen(signCmd, shell=True)

#        (stdoutdata, stderrdata) = process.communicate()

#

#        #        uploadIpaToPgyer(output)

#        cleanBuildDir("./build")

def buildWorkspace(workspace, scheme, output, i):

process = subprocess.Popen("pwd", stdout=subprocess.PIPE)

(stdoutdata, stderrdata) = process.communicate()

buildDir = stdoutdata.strip() + '/build'

print "buildDir: " + buildDir

if len(CODE_SIGN_IDENTITY) and len(PROVISIONING_PROFILE):

buildCmd = 'xcodebuild -workspace %s -scheme %s -sdk %s -configuration %s build CODE_SIGN_IDENTITY="%s" PROVISIONING_PROFILE="%s" SYMROOT=%s' %(workspace, scheme, SDK, CONFIGURATION,CODE_SIGN_IDENTITY, PROVISIONING_PROFILE, buildDir)

print buildCmd

pass

else:

buildCmd = 'xcodebuild -workspace %s -scheme %s -sdk %s -configuration %s SYMROOT=%s' %(workspace, scheme, SDK, CONFIGURATION, buildDir)

print buildCmd

process = subprocess.Popen(buildCmd, shell = True)

process.wait()

signApp = "./build/%s-iphoneos/%s.app" %(CONFIGURATION, scheme)

signCmd = "xcrun -sdk %s -v PackageApplication %s -o %s" %(SDK, signApp, output+"/"+str(i)+".ipa")

process = subprocess.Popen(signCmd, shell=True)

(stdoutdata, stderrdata) = process.communicate()

cleanBuildDir(buildDir)

def main(argvs):

if len(argvs) < 2:

print "至少输入一个渠道名称"

return

else:

for i in xrange(1,len(argvs)):

channel = argvs[i]

print len(argvs)

print argvs

if not channel in channellist:

print "没有此渠道名称"

else:

#                pass

replaceflies(channel)

buildWorkspace(WORKSAPCE, SCHEME, OUTPUT,i)

if __name__ == '__main__':

main(sys.argv)

------------------------------脚本结束-------------------------------------

调用方法:

终端命令输入:

1.把build.py 放在与项目文件同一级文件目录下

2. cd 项目存在的文件路径 

3.python build.py 渠道1 渠道2 

 大功告成就会发现 output路径下会出现1.ipa文件可自行选择上传。

你可能感兴趣的:(iOS开发之 使用python自动化打包)