Python3-打包脚本自动上传xcrun altool

2019-11-23  本文已影响0人  037e3257fa3b
随着Xcode11中删除Application Loader工具,我们想要自动上传IPA到苹果后台,可以使用xcrun altool。

功能介绍:
1.验证IPA包
2.上传IPA包
3.上传成功邮箱提示

import subprocess,os

import smtplib
from email.mime.text import MIMEText


#上传ipa包
def updateIpa(ipaPath,appleAccount,appleKey):

    if ipaPath == '':
        consoleError('no ipa file')
    #先验证
    # "xcrun altool --validate-app -f %s -t ios --apiKey %s --apiIssuer %s --verbose"
    # "xcrun altool --validate-app -f %s -t ios -u %s -p %s --verbose" % (ipaPath, appleAccount, appleKey)
    consoleInfo('start validate ipa file...')
    (status, output) = subprocess.getstatusoutput(
        "xcrun altool --validate-app -f %s -t ios -u %s -p %s --verbose" % (ipaPath, appleAccount, appleKey))
    if status == 0:
        consoleInfo('ipa validating success')
    else:
        consoleError("%d->%s"%(status,output))

    consoleInfo('start upload ipa file...')
    (status,output) = subprocess.getstatusoutput("xcrun altool --upload-app -f %s -t ios -u %s -p %s --verbose"%(ipaPath,appleAccount,appleKey))
    if status == 0:
        consoleInfo('ipa upload success')
        appInfoString = getIpaInformation(ipaPath)
        sendEmail('ipa包提交成功\n' + appInfoString)
    else:
        consoleError("%d->%s"%(status,output))


#获取ipa相关信息
def getIpaInformation(ipaPath):
    #文件名分离
    (filePath,tmpFileName) = os.path.split(ipaPath)
    (fileName,extension) = os.path.splitext(tmpFileName)
    unzipPath = '/tmp/' + fileName
    if not os.path.exists(unzipPath):
        os.makedirs(unzipPath)
    os.system('unzip -o %s -d %s'%(ipaPath,unzipPath))
    infoPlistPath = unzipPath + "/Payload/" + fileName + ".app/info.plist"
    consoleInfo('Info.plist路径:%s'%infoPlistPath)

    #
    bundleId = subprocess.getoutput(
        '/usr/libexec/PlistBuddy -c \"Print :CFBundleIdentifier\" {}'.format(infoPlistPath))
    versionStr = subprocess.getoutput(
        '/usr/libexec/PlistBuddy -c \"Print :CFBundleShortVersionString\" {}'.format(infoPlistPath))
    displayName = subprocess.getoutput(
        '/usr/libexec/PlistBuddy -c \"Print :CFBundleDisplayName\" {}'.format(infoPlistPath))
    return 'Bundle Id:%s\nVersion:%s\nApp Name:%s'%(bundleId,versionStr,displayName)

#打印常规信息
def consoleInfo(msg):
    print('\033[0;32;40mINFO:%s\033[0m' % msg)

#打印错误信息
def consoleError(msg,isStop = True):
    print('\033[0;31;40mERROR:%s\033[0m' % msg)
    if isStop:
        exit()

def sendEmail(msg):
    # 发送者邮箱如果是QQ邮箱则使用'smtp.qq.com'
    server = 'smtp.163.com'
#发送者邮箱密码
    sender = 'xxxx@163.com'
    password = 'xxxx'

#接受者邮箱
    receiver = ['xxx@qq.com']

    # 设置邮件内容
    message = MIMEText(msg,'plain', 'utf-8')
    message['Subject'] = 'iOS提交IPA文件提示邮件'
    message['From'] = sender
    message['To'] = ','.join(receiver)
    # 发送邮件
    try:
        smtp = smtplib.SMTP(server)
        # smtp.set_debuglevel(1)
        smtp.login(sender,password)
        smtp.sendmail(sender, receiver, message.as_string())
        smtp.quit()
        consoleInfo("send mail succeed")
    except Exception:
        consoleError("send mail error")

#选择文件
def chooseFilePath(startDir, chooseName, isFile=True):
    fileName = ('file' if isFile else 'folder')
    suffix = ('文件' if isFile else '目录')
    shellText = '#!/usr/bin/osascript \n\
on run argv \n\
    tell application "System Events" \n\
        activate \n\
            try \n\
                set strPath to POSIX file "{}/" \n\
                set f to choose {} with prompt "选择{}{}:" default location strPath \n\
                POSIX path of f \n\
            end \n\
    end tell \n\
end run'.format(startDir, fileName, chooseName, suffix)

    shellFilePath = '/tmp/choose_path.osascript'
    op = open(shellFilePath, 'w')
    op.write(shellText)
    op.close()

    ret = ''
    try:
        consoleInfo('开始选择{}{}'.format(chooseName, suffix))
        count = 0
        condition = 1
        while (condition):
            (status, output) = subprocess.getstatusoutput('osascript {}'.format(shellFilePath))
            if status != 0:
                consoleError('选择{}{}错误'.format(chooseName, suffix))
            ret = output.strip()
            if ret == '':
                count = count + 1
                consoleInfo('未选择,退出')
                if count >= 2:
                    condition = 0
                continue
            else:
                condition = 0

    finally:
        pass
    consoleInfo('选择的文件/文件夹路径:%s'%(ret))
    return ret




appleAccount = "xxx@qq.com"
appleScretKey = "xx-xx-xx-xx"


ipaPath = chooseFilePath(os.environ['HOME'],"ipa")
updateIpa(ipaPath,appleAccount,appleScretKey)
# getIpaInformation(ipaPath)
上一篇下一篇

猜你喜欢

热点阅读