iOS打包上传蒲公英脚本

58 阅读2分钟

#!/usr/bin/env python
#coding=utf-8
#环境: UAT&SIT

import os
import requests
import webbrowser
import subprocess
import time
import smtplib
import json

# 路径信息
project_name     = 'XXX'                 # 项目名称
project_path     = '/Users/xxx/Desktop/XXX'       # 项目路径
export_directory = '/Users/xxx/Desktop'   # 输出的路径
exporrt_folder   = 'IPA'                    # 输出的文件夹


# 蒲公英app地址、USER_KEY、API_KEY,具体见蒲公英官网: 账户设置-->API信息  UAT
ipa_download_url = 'https://www.pgyer.com/xxx'
USER_KEY         = 'xxxxxxxxxx'
API_KEY          = 'xxxxxxxxxx'

class AutoArchive(object):
    
    def __init__(self):
        pass
    
    def clean(self):
        
        print("\n\n===========开始clean操作===========")
        start = time.time()
        clean_opt = 'xcodebuild clean -workspace %s/%s.xcworkspace -scheme %s -configuration Debug' % (project_path, project_name, project_name)
        clean_opt_run = subprocess.Popen(clean_opt, shell=True)
        clean_opt_run.wait()
        end = time.time()

        # clean 结果
        clean_result_code = clean_opt_run.returncode
        if clean_result_code != 0:
            print("===========clean失败,用时:%.2f秒===========" % (end - start))
        else:
            print("===========clean成功,用时:%.2f秒===========" % (end - start))
            self.archive()

    def archive(self):
        print("\n\n===========开始archive操作===========")

        subprocess.call(['rm', '-rf', '%s/%s' % (export_directory, exporrt_folder)])
        time.sleep(1)
        subprocess.call(['mkdir', '-p', '%s/%s' % (export_directory, exporrt_folder)])
        time.sleep(1)
     
        start = time.time()
        archive_opt = 'xcodebuild archive -workspace %s/%s.xcworkspace -scheme %s -configuration Debug -archivePath %s/%s' %(project_path, project_name, project_name, export_directory, exporrt_folder)
        archive_opt_run = subprocess.Popen(archive_opt, shell=True)
        archive_opt_run.wait()
        end = time.time()
        
        # archive 结果
        archive_result_code = archive_opt_run.returncode
        if archive_result_code != 0:
           print("===========archive失败,用时:%.2f秒===========" % (end - start))
        else:
           print("===========archive成功,用时:%.2f秒===========" % (end - start))
        
        # 导出IPA
        self.export()

    def export(self):
        print("\n\n===========开始export操作===========")
        start = time.time()
        export_opt = 'xcodebuild -exportArchive -archivePath %s/%s.xcarchive -exportPath %s/%s -exportOptionsPlist %s/ExportOptions.plist' % ( export_directory,exporrt_folder,export_directory, exporrt_folder,export_directory)
        export_opt_run = subprocess.Popen(export_opt, shell=True)
        export_opt_run.wait()
        end = time.time()
        
        # ipa导出结果
        export_result_code = export_opt_run.returncode
        if export_result_code != 0:
            print("===========导出IPA失败,用时:%.2f秒===========" % (end - start))
        else:
            print("===========导出IPA成功,用时:%.2f秒===========" % (end - start))
        
        # 删除archive.xcarchive文件
        subprocess.call(['rm', '-rf', '%s/%s.xcarchive' % (export_directory, exporrt_folder)])
        self.getToken('%s/%s/xxx.ipa' % (export_directory, exporrt_folder))
        
    def getToken(self, ipa_path):
        print("\n\n===========开始获取蒲公英Token===========")
        print("ipa_path:", ipa_path)
        url = 'https://www.pgyer.com/apiv2/app/getCOSToken'
        data = {
                    'uKey': USER_KEY,
                    '_api_key': API_KEY,
                    'buildType': 'ios',
            }
        r = requests.post(url, data=data)
        if r.status_code == 200:
            print(r)
            print("返回的文本格式响应数据为:", r.json()["data"])
            resData = r.json()["data"]
            self.upload(ipa_path, resData)



    def upload(self, ipa_path, token):
        
        print("\n\n===========开始上传蒲公英操作===========")
        if ipa_path:
            # 蒲公英操作API
            # https://www.pgyer.com/doc/api
            url = token["endpoint"]
            data = token["params"]
            # data["file"] = ipa_path
            files = {'file': open(ipa_path, 'rb')}
            r = requests.post(url, data=data, files=files)
            if r.status_code == 204:
                self.buildInfo(token["key"])
            else:
                print("上传失败", r.status_code)
        else:
            print("\n\n===========没有找到对应的ipa===========")
            return
    
    def buildInfo(self, key):
        time.sleep(5)
        print("\n\n===========开始验证===========")
        url = 'https://www.pgyer.com/apiv2/app/buildInfo'
        data = {
                    'buildKey': key,
                    '_api_key': API_KEY
            }
        r = requests.get(url, params=data)
        if r.status_code == 200:
            print("成功", r.text)
            if r.json()["code"] == 0:
                self.open_browser(self)
            else:
                print("正在处理", r.json()["message"])

    @staticmethod
        # 上传成功,通过浏览器打开蒲公英网站
    def open_browser(self):
        webbrowser.open(ipa_download_url, new=1, autoraise=True)



if __name__ == '__main__':
    archive = AutoArchive()
    archive.clean()