前幾天看到iOS群里有人分享一個自動打包生成ipa的python文件,雖然挺感興趣的,但是當(dāng)時工作挺忙就沒在意站绪,正好今天有時間,就研究了一下這東西怎么玩丽柿,中間有一些需要注意的地方恢准,在這里分享一下魂挂,python文章最下面貼出來。
確保python文件位置正確
- 把python文件放在項目的工程文件的目錄下馁筐。
- 打開終端涂召,進(jìn)入到工程文件的目錄下。
執(zhí)行python文件
命令如下
python autobuild.py -p Gomefxs.xcodeproj -s Gomefxs -t Gomefxs -o ~/Desktop/Gomefxs.ipa
這里要把工程名寫對敏沉,還有一個注意的地方果正,如果工程文件是.xcworkspace,那么命令中的-p應(yīng)該換成-w赦抖,python代碼中有這么一段代碼:
parser.add_option("-w", "--workspace", help="Build the workspace name.xcworkspace.", metavar="name.xcworkspace")
parser.add_option("-p", "--project", help="Build the project name.xcodeproj.", metavar="name.xcodeproj")
確保電腦中的python中有requests
當(dāng)我以為我的.ipa文件馬上就要出現(xiàn)在桌面的時候舱卡,終端里出現(xiàn)了一個我很愿意看到的東西,報錯了:
NO module named requests
報錯的代碼是第三行队萤,python代碼里面的第三行是這樣的:
import requests
作為python完全0知識儲備的我,也早就聽聞江湖中流傳這么一句話:import大法好矫钓。但是很不幸要尔,上面的終端中的信息已經(jīng)說了,我沒import到這個叫requests的東西(問了一下懂python的朋友新娜,這個東西叫模塊)赵辕。查了一下,終端里直接就能安裝模塊概龄,代碼如下:
easyinstall requests
然而还惠,終端又一次打擊了我:
command not found
又仔細(xì)看了一下,WTF私杜!命令沒敲對蚕键,應(yīng)該是easy_install,作為一個有著盡量不要CV信仰的碼農(nóng)衰粹,真是掉了無數(shù)次坑啊锣光,還是復(fù)制大法好。好了铝耻,那么換成正確的命令再來一次:
can't creat or remove files in install directory
WTF? 這又是什么誊爹?又回到網(wǎng)上查看了一下,原來這個命令需要root權(quán)限瓢捉,終端好像有直接切換到root用戶的命令频丘,但是由于工作不怎么用終端,所以就暫時用sudo解決吧:
sudo easy_install requests
這次終端終于出現(xiàn)我想要看到的東西了:
Searching for requests
Reading http://https://pypi.python.org/simple/requests/
然后等了沒幾秒泡态,直接提示超時了搂漠,這個命令我就不貼了(超時應(yīng)該是墻的原因,由于stackoverflow幾乎能解決我所有遇到的問題兽赁,所以我電腦一直沒翻墻状答,不過這是題外話)冷守。easy_install這個命令,不僅可以直接在終端里下載那幢模塊惊科,也可以安裝本地模塊文件拍摇,把http://https://pypi.python.org/simple/requests/ 打開,里面有兩種格式的文件馆截,上網(wǎng)查閱了一下充活,應(yīng)該下載.tar.gz后綴的,下載好了之后蜡娶,繼續(xù)嘗試easy_install混卵,不要忘了sudo,這次要easy_install下載好之后的模塊文件路徑:
sudo easy_install /Users/xinzhi/Desktop/requests-2.9.1.tar.gz
這次總算是安裝成功了窖张!
Installed /Library/Python/2.7/site-packages/requests-2.9.1-py2.7.egg
Processing dependencies for request==2.9.1
Finished processing dependencies for requests==2.9.1
這樣幕随,缺少的模塊,總算是安裝成功了宿接。再回頭執(zhí)行python文件:
python autobuild.py -p Gomefxs.xcodeproj -s Gomefxs -t Gomefxs -o ~/Desktop/Gomefxs.ipa
這個時候赘淮,終端開始瘋狂的log信息,log信息結(jié)束后睦霎,桌面上的.ipa文件也生成好了梢卸,大功告成。
python代碼
下面是python的代碼副女,把里面的CODE_SIGN_IDENTITY換成自己的證書蛤高,把PROVISIONING_PROFILE換成自己的描述文件,USER_KEY和API_KEY是蒲公英的東西碑幅,由于不用蒲公英戴陡,所以直接把這塊的信息刪掉了。需要改的地方就這四個枕赵。
from optparse import OptionParser
import subprocess
import requests
#configuration for iOS build setting
CODE_SIGN_IDENTITY = "iPhone Distribution: GOME Financial Holdings Investment CO., LTD"
PROVISIONING_PROFILE = "4bbd193f-7a2a-414f-9c00-4310817c14af"
CONFIGURATION = "Release"
SDK = "iphoneos"
# configuration for pgyer
#pgyer setting
PGYER_UPLOAD_URL = "http://www.pgyer.com/apiv1/app/upload"
DOWNLOAD_BASE_URL = "http://www.pgyer.com"
USER_KEY = ""
API_KEY = ""
def cleanBuildDir(buildDir):
cleanCmd = "rm -r %s" %(buildDir)
process = subprocess.Popen(cleanCmd, shell = True)
process.wait()
print "cleaned buildDir: %s" %(buildDir)
def parserUploadResult(jsonResult):
resultCode = jsonResult['code']
if resultCode == 0:
downUrl = DOWNLOAD_BASE_URL +"/"+jsonResult['data']['appShortcutUrl']
print "Upload Success"
print "DownUrl is:" + downUrl
else:
print "Upload Fail!"
print "Reason:"+jsonResult['message']
def uploadIpaToPgyer(ipaPath):
print "ipaPath:"+ipaPath
files = {'file': open(ipaPath, 'rb')}
headers = {'enctype':'multipart/form-data'}
payload = {'uKey':USER_KEY,'_api_key':API_KEY,'publishRange':'2','isPublishToPublic':'2', 'password':""}
print "uploading...."
r = requests.post(PGYER_UPLOAD_URL, data = payload ,files=files,headers=headers)
if r.status_code == requests.codes.ok:
result = r.json()
parserUploadResult(result)
else:
print 'HTTPError,Code:'+r.status_code
def buildProject(project, target, output):
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)
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):
process = subprocess.Popen("pwd", stdout=subprocess.PIPE)
(stdoutdata, stderrdata) = process.communicate()
buildDir = stdoutdata.strip() + '/build'
print "buildDir: " + buildDir
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)
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)
process = subprocess.Popen(signCmd, shell=True)
(stdoutdata, stderrdata) = process.communicate()
uploadIpaToPgyer(output)
cleanBuildDir(buildDir)
def xcbuild(options):
project = options.project
workspace = options.workspace
target = options.target
scheme = options.scheme
output = options.output
if project is None and workspace is None:
pass
elif project is not None:
buildProject(project, target, output)
elif workspace is not None:
buildWorkspace(workspace, scheme, output)
def main():
parser = OptionParser()
parser.add_option("-w", "--workspace", help="Build the workspace name.xcworkspace.", metavar="name.xcworkspace")
parser.add_option("-p", "--project", help="Build the project name.xcodeproj.", metavar="name.xcodeproj")
parser.add_option("-s", "--scheme", help="Build the scheme specified by schemename. Required if building a workspace.", metavar="schemename")
parser.add_option("-t", "--target", help="Build the target specified by targetname. Required if building a project.", metavar="targetname")
parser.add_option("-o", "--output", help="specify output filename", metavar="output_filename")
(options, args) = parser.parse_args()
print "options: %s, args: %s" % (options, args)
xcbuild(options)
if __name__ == '__main__':
main()
總結(jié)
其實python文件別人都是已經(jīng)寫好了猜欺,在配置的過程中花費(fèi)時間最長的算是安裝python的requests模塊了,這個python的實現(xiàn)原理網(wǎng)上有很多拷窜,我就不貼了开皿。