时间: 2020-08-23|24次围观|0 条评论

脚本代码如下:

#!/usr/bin/python# -*- coding:UTF-8 -*-import osimport sysimport time# 发邮件所用from email import encodersfrom email.header import Headerfrom email.mime.text import MIMETextfrom email.utils import parseaddr, formataddrimport smtplib# 需要配置分割线 ===================================================================# fir tokenfir_api_token = '34d6f526c9fdcf9afe90753cdb9bb837' #firm的api tokendownload_address = "https://fir.im/xxxxxxxxx"      #firm 下载地址# pgyerpgyer_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'        #schemeisDistribution = 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,Releasedef configuration():    if isDistribution:        return 'Release'    else:        return 'Debug'# 编译成功后.xcarchive所在目录archive_dir = project_path + '/archive'# 打包后ipa存储目录targerIPA_dir = project_path + '/ipaDir'#CA certificate#发布包相关的plistDistributionExportFileName = "Distribution_ExportOptions.plist"#测试包相关的plistDeveloperExportFileName = "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()))##上传到firdef 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() # 发送完毕后退出smtpdef 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>
iOS python自动化出包脚本插图
  • 其中plist文件中的method参数有如下几个方法:{app-store, ad-hoc, enterprise, development}

附:我的博客地址

文章转载于:https://www.jianshu.com/p/0b7257de79b8

原著是一个有趣的人,若有侵权,请通知删除

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《iOS python自动化出包脚本
   

还没有人抢沙发呢~