iOS自動(dòng)打包ipa(Python腳本)

系列

iOS自動(dòng)打包ipa(shell腳本)
iOS自動(dòng)打包ipa(Python腳本)

安裝Python庫

$ pip install requests

Python腳本

$ cd iOS項(xiàng)目目錄
$ vim build_using_python.py
#! /usr/bin/python
#coding=utf-8
# -*- coding:utf8 -*-

from optparse import OptionParser
import subprocess
import requests
import os

#configuration for iOS build setting
CODE_SIGN_IDENTITY = "iPhone Distribution:************.,Ltd"
PROVISIONING_PROFILE = "************"
CONFIGURATION = "Release"
SDK = "iphoneos"
WORKSPACE = "******.xcworkspace"
TARGET = "******"
SCHEME = "******"

# configuration for pgyer
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
    showNotification("任務(wù)完成!", "Upload Success!");
  else:
    print "Upload Fail!"
    print "Reason:"+jsonResult['message']
    showNotification("Upload failure!", "Reason:"+jsonResult['message']);

def uploadIpaToPgyer(ipaPath, comments):
    print "ipaPath:"+ipaPath
    files = {'file': open(ipaPath, 'rb')}
    headers = {'enctype':'multipart/form-data'}
    payload = {'uKey':USER_KEY,'_api_key':API_KEY,'updateDescription':comments}
    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
        showNotification("Upload failure!", 'HTTPError,Code:'+r.status_code);

def buildProject(project, target, output, comments):
  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, comments)
  cleanBuildDir("./build")

def buildWorkspace(workspace, scheme, output, comments):
  cleanWorkspace()
  process = subprocess.Popen("pwd", stdout=subprocess.PIPE)
  (stdoutdata, stderrdata) = process.communicate()
  buildDir = stdoutdata.strip() + '/build'
  cleanBuildDir(buildDir)
  showNotification("Clean Success!", "Clean Success! Then 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()
  showNotification("Build Success!", "Build Success! Then Sign.");
  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()
  showNotification("Sign Success!", "Sign Success! Then upload to pgyer.");
  uploadIpaToPgyer(output, comments)
  cleanBuildDir(buildDir)

def showNotification(title, subtitle):
  os.system("osascript -e 'display notification \"" + subtitle + "\" with title \"" + title + "\"'")

def cleanWorkspace():
  buildCmd = 'xcodebuild clean -workspace ' + WORKSPACE + ' -scheme ' + SCHEME + ' -configuration ' + CONFIGURATION
  print "clean workspace: " + buildCmd
  process = subprocess.Popen(buildCmd, shell = True)
  process.wait()

def xcbuild(options):
  # project = options.project
  # workspace = options.workspace
  # target = options.target
  # scheme = options.scheme
  # output = options.output
  # ipa輸出到桌面
  output = os.path.expanduser("~") + '/Desktop/' + TARGET + '.ipa'
  comments = options.comments

  if comments is None:
    print "請輸入更新日志"
  # elif project is None and workspace is None:
  #   pass
  # elif project is not None:
  #   buildProject(project, target, output, comments)
  else:
    buildWorkspace(WORKSPACE, SCHEME, output, comments)

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")
  parser.add_option("-m", "--comments", help="specify the upgrade comments", metavar="upgrade_comments")

  (options, args) = parser.parse_args()

  print "options: %s, args: %s" % (options, args)

  xcbuild(options)

if __name__ == '__main__':
  main()

參數(shù)說明

其中,CODE_SIGN_IDENTITY
為開發(fā)者證書標(biāo)識(shí)测蹲,可以在 Keychain Access -> Certificates-> 選中證書右鍵彈出菜單 -> Get Info -> Common Name 獲取,類似 iPhone Distribution: Company name Co. Ltd (xxxxxxxx9A)
, 包括括號(hào)內(nèi)的內(nèi)容鬼吵。

PROVISIONING_PROFILE
: 這個(gè)是 mobileprovision 文件的 identifier扣甲,獲取方式:
Xcode -> Preferences -> 選中申請開發(fā)者證書的 Apple ID -> 選中開發(fā)者證書 -> View Details… -> 根據(jù) Provisioning Profiles 的名字選中打包所需的 mobileprovision 文件 -> 右鍵菜單 -> Show in Finder -> 找到該文件后,除了該文件后綴名的字符串就是 PROVISIONING_PROFILE
字段的內(nèi)容齿椅。

打包

$ python build_using_python.py -m "更新日志"

注意:Xcode 8 不再用PROVISIONING_PROFILE琉挖,而是用PROVISIONING_PROFILE_SPECIFIER,它可以是teamID/Provisioning file名稱涣脚,也可以是Provisioning file ID


參考:iOS自動(dòng)打包并發(fā)布腳本示辈、githubCode Signing in Xcode 8

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末遣蚀,一起剝皮案震驚了整個(gè)濱河市矾麻,隨后出現(xiàn)的幾起案子纱耻,更是在濱河造成了極大的恐慌,老刑警劉巖险耀,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件弄喘,死亡現(xiàn)場離奇詭異,居然都是意外死亡甩牺,警方通過查閱死者的電腦和手機(jī)蘑志,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贬派,“玉大人急但,你說我怎么就攤上這事「惴Γ” “怎么了波桩?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長查描。 經(jīng)常有香客問我突委,道長,這世上最難降的妖魔是什么冬三? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任匀油,我火速辦了婚禮,結(jié)果婚禮上勾笆,老公的妹妹穿的比我還像新娘敌蚜。我一直安慰自己,他們只是感情好窝爪,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布弛车。 她就那樣靜靜地躺著,像睡著了一般蒲每。 火紅的嫁衣襯著肌膚如雪纷跛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天邀杏,我揣著相機(jī)與錄音贫奠,去河邊找鬼。 笑死望蜡,一個(gè)胖子當(dāng)著我的面吹牛唤崭,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播脖律,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼谢肾,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起匪凉,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤些侍,失蹤者是張志新(化名)和其女友劉穎匙铡,沒想到半個(gè)月后地沮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體躏尉,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡瞧预,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年谱净,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了弊决。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片噪舀。...
    茶點(diǎn)故事閱讀 39,773評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖飘诗,靈堂內(nèi)的尸體忽然破棺而出与倡,到底是詐尸還是另有隱情,我是刑警寧澤昆稿,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布纺座,位于F島的核電站,受9級特大地震影響溉潭,放射性物質(zhì)發(fā)生泄漏净响。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一喳瓣、第九天 我趴在偏房一處隱蔽的房頂上張望馋贤。 院中可真熱鬧,春花似錦畏陕、人聲如沸配乓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽犹芹。三九已至,卻和暖如春鞠绰,著一層夾襖步出監(jiān)牢的瞬間腰埂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工蜈膨, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留盐固,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓丈挟,卻偏偏與公主長得像,于是被迫代替她去往敵國和親志电。 傳聞我的和親對象是個(gè)殘疾皇子曙咽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容