iOS python自動化出包腳本

腳本代碼以下:python

#!/usr/bin/python
# -*- coding:UTF-8 -*-

import os
import sys
import time

# 發郵件所用
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib

# 須要配置分割線 ===================================================================

# fir token
fir_api_token = '34d6f526c9fdcf9afe90753cdb9bb837' #firm的api token
download_address = "https://fir.im/xxxxxxxxx"      #firm 下載地址

# pgyer
pgyer_uKey         = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
pgyer_apiKey       = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
pgyer_appQRCodeURL = "http://www.pgyer.com/xxxxxxxxx"   # 下載地址
pgyer_password     = "12345"
pgyer_updateDescription = "test版本"                     # 更新描述

# 項目配置
project_Name = 'Unity-iPhone'  #工程名
scheme = 'Unity-iPhone'        #scheme
isDistribution = False         #生成dev包或者dis包類型
isWorkspace = False            #工程類型 pod工程 -workspace 普通工程 -project

# 項目根目錄
project_path = '/Users/yostar/Desktop/ProjectiOSTest'
#當前autoIpa.py 以及 plist 所在文件夾位置
#主執行文件的父級目錄
autoPythonRoot = sys.path[0]

# 發郵件相關信息
from_addr = '250***2914@qq.com'
password = 'plgke***pzbjdice'
smtp_host = 'smtp.qq.com'
to_addr = ['250***2914@qq.com', '1728***24@qq.com']

# 須要配置分割線 ===================================================================

# 編譯模式 Debug,Release
def configuration():
    if isDistribution:
        return 'Release'
    else:
        return 'Debug'

# 編譯成功後.xcarchive所在目錄
archive_dir = project_path + '/archive'
# 打包後ipa存儲目錄
targerIPA_dir = project_path + '/ipaDir'

#CA certificate
#發佈包相關的plist
DistributionExportFileName = "Distribution_ExportOptions.plist"

#測試包相關的plist
DeveloperExportFileName = "Develop_ExportOptions.plist"

#時間字符串
time_Tag = '%s'%(time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())))

#xcodebuild export ipa包命令時須要用到
def export_OptionsPlist():
    if isDistribution:
        return autoPythonRoot + '/' + DistributionExportFileName
    else:
        return autoPythonRoot + '/' + DeveloperExportFileName

#打包名字
def archiveName():
    return project_Name + '_' + time_Tag + '.xcarchive'

#archive地址
def archivePath():
    return '%s/%s'%(archive_dir, archiveName())

#ipa包名
def ipaFileName():
    return '%s_%s'%(project_Name, time_Tag)

#ipa導出地址
def exportPath():
    if isDistribution:
        return '%s/%s/%s'%(targerIPA_dir, 'Distribution', ipaFileName())
    else:
        return '%s/%s/%s'%(targerIPA_dir, 'development', ipaFileName())

# 清理項目
def clean_project():
    os.system('rm -rf %s'%(archive_dir))
    print(project_path + '******' + project_Name + '******' + '******' + scheme + '******' + configuration())
    if isWorkspace:
        os.system('cd %s; xcodebuild clean -workspace %s.xcworkspace -scheme %s -configuration %s'%(project_path, project_Name, scheme, configuration()))
    else:
        os.system('cd %s; xcodebuild clean -project %s.xcodeproj -scheme %s -configuration %s'%(project_path, project_Name, scheme, configuration()))


#archive 打包
def archive_project():
    print('======archive_project start')
    print(archiveName())
    if isWorkspace:
        os.system('cd %s; xcodebuild archive -workspace %s.xcworkspace -scheme %s -archivePath %s'%(project_path, project_Name, scheme, archivePath()))
    else:
        os.system('cd %s; xcodebuild archive -project %s.xcodeproj -scheme %s -archivePath %s'%(project_path, project_Name, scheme, archivePath()))
   

# 打包ipa 而且保存在桌面
def export_ipa():
    print('export_ipa start')
    print(ipaFileName())
    print(export_OptionsPlist())
    os.system('cd %s; xcodebuild -exportArchive -archivePath %s/ -exportOptionsPlist %s -exportPath %s'%(project_path, archivePath(), export_OptionsPlist(), exportPath()))

##上傳到fir
def upload_fir():
    p = exportPath() + '/' + scheme + '.ipa'
    if os.path.exists(p):
        print('watting===%s...上傳到fir'%p)
        # 直接使用fir 有問題 這裏使用了絕對地址 在終端經過 which fir 得到
        ret = os.system('fir publish %s -T %s'%(p, fir_api_token))
        print('watting...上傳結束')
        return True
    else:
        print('沒有找到IPA文件')
        return False

# 發郵件
def send_mail():
    msg = MIMEText('【%s】'%scheme + 'iOS 測試項目完成,請下載測試!若有問題,請聯繫iOS相關人員,咱們會及時解決,謝謝!', 'plain', 'utf-8') #發郵件內容
    msg['From'] = Header('自動打包系統<%s>' % from_addr, 'utf-8') #發件人
    msg['To'] = Header('測試人員', 'utf-8') #收件人
    msg['Subject'] = Header('【%s】'%scheme + 'iOS客戶端測試包構建完成, 構建時間:%s'%(time_Tag), 'utf-8').encode() #郵件主題
    
    try:
        server = smtplib.SMTP(smtp_host, 25)
        server.set_debuglevel(1)
        server.login(from_addr, password)
        server.sendmail(from_addr, to_addr, msg.as_string())
        print('郵件發送成功')
    except smtplib.SMTPException:
        print('Error:沒法發送郵件')
    finally:
        server.quit() # 發送完畢後退出smtp

def main():
    # 執行
    # 清理目錄
    clean_project()
    # 編譯coocaPods項目文件並 執行編譯目錄
    archive_project()
    # 導出ipa
    export_ipa()

    if not isDistribution:
        # 上傳fir
        success = upload_fir()
        # 發郵件
        if success:
            send_mail()

main()
複製代碼
  • 該腳本是把Distribution_ExportOptions.plistDevelop_ExportOptions.plist和腳本放在同一目錄裏面的
  • 該腳本是針對xcode 8及以上版本的,低版本會出包失敗
  • plist文件內容以下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>provisioningProfiles</key>
	<dict>
		<key>com.Y***ane</key>
		<string>azur***_dev</string>
	</dict>
	<key>method</key>
	<string>development</string>
	<key>signingCertificate</key>
	<string>iPhone Developer</string>
	<key>signingStyle</key>
	<string>manual</string>
	<key>teamID</key>
	<string>42***ZL</string>
	<key>compileBitcode</key>
	<false/>
	<key>uploadSymbols</key>
	<false/>
</dict>
</plist>
複製代碼

  • 其中plist文件中的method參數有以下幾個方法:{app-store, ad-hoc, enterprise, development}

附:個人博客地址git

相關文章
相關標籤/搜索