iOS_Python_自动化打包

1 原理

Python只是一门语言,本身肯定不具备编译打包iOS程序的能力,只是用Python写一个脚本,调用xcode命令打包出ipa,然后可根据业务需求在脚本中实现将ipa上传至蒲公英、fir、appconnect,钉钉群消息通知。

2 代码实现

2.1 全局常量定义

首先定义了几个方便全局使用的常量

# 打包配置

CONFIGURATION = "VideoPre"

EXPORT_OPTIONS_PLIST = "/Users/wzz/Desktop/Git/PY_iOS_AutoBuild/exportOptions.plist"  

WORKSPACE = "/Users/wzz/Desktop/Git/ios_movie_searcher/video.xcworkspace"

PLIST_PATH = "/Users/wzz/Desktop/Git/ios_movie_searcher/video/Others/Info.plist"

TARGET = 'video'

SCHEME = 'video_pre'

# 输出路径

DATE = datetime.datetime.now().strftime('%Y-%m-%d_%H.%M.%S')

#DATE = "2020-07-30_13.13.50"

EXPORT_MAIN_DIRECTORY = "~/Desktop/ipa/" + SCHEME + DATE

ARCHIVEPATH = EXPORT_MAIN_DIRECTORY + "/%s.xcarchive" %(SCHEME)

IPAPATH = EXPORT_MAIN_DIRECTORY + "/%s.ipa" %(SCHEME)

# 蒲公英相关参数

PGYER_UPLOAD_URL = "http://www.pgyer.com/apiv1/app/upload"

DOWNLOAD_BASE_URL = "http://www.pgyer.com"

USER_KEY = "USER_KEY"   // 蒲公英网站上可以查到自己的USER_KEY和API_KEY

API_KEY = "API_KEY"

REMARK = ""

#钉钉上报

ACCESS_TOKEN = "ACCESS_TOKEN" // 钉钉群机器人token

2.2  调用xcode命令打包

def xcbuild():

        # 判断是 xcodeproj 还是  xcworkspace

        projectArg = "project"

        if WORKSPACE.find("xcworkspace")>=0:

            projectArg = "workspace"

            

        # 调用xcode命令进行ARCHIVE

        archiveCmd = 'xcodebuild -%s %s -scheme %s -configuration %s archive -archivePath %s -destination generic/platform=iOS' %(projectArg, WORKSPACE, SCHEME, CONFIGURATION, ARCHIVEPATH)

        process = subprocess.Popen(archiveCmd, shell=True)

        process.wait()

        

        # 判断打包结果 如果打包失败 删除ARCHIVE相关的内容

        archiveReturnCode = process.returncode

        if archiveReturnCode != 0:

            print "archive project %s failed" %(WORKSPACE)

            cleanCmd = "rm -r %s" %(ARCHIVEPATH)

            process = subprocess.Popen(cleanCmd, shell = True)

            process.wait()

            print "cleaned archiveFile: %s" %(ARCHIVEPATH)

        # 打包成功 使用ARCHIVE 导出IPA

        else:

            return archiveReturnCode

2.3 上传至蒲公英

# 上传蒲公英

def uploadIpaToPgyer():

    ipaPath = os.path.expanduser(IPAPATH)

    ipaPath = unicode(ipaPath, "utf-8")

    files = {'file': open(ipaPath, 'rb')}

    headers = {'enctype':'multipart/form-data'}

    payload = {'uKey':USER_KEY,'_api_key':API_KEY,'updateDescription':REMARK}

    print "uploading...."

    r = requests.post(PGYER_UPLOAD_URL, data = payload ,files=files,headers=headers)

    if r.status_code == requests.codes.ok:

        result = r.json()

        print "上传蒲公英成功"

        parserUploadResult(result)

    else:

        print 'HTTPError,Code:'+r.status_code

2.4 钉钉群通知

def parserUploadResult(jsonResult):

    ipaPath = os.path.expanduser(IPAPATH)

    ipaPath = unicode(ipaPath, "utf-8")

    version = analyze_ipa_with_plistlib(ipaPath)

    downUrl = DOWNLOAD_BASE_URL +"/"+jsonResult['data']['appShortcutUrl']

    headers = {'Content-Type': 'application/json'}

    payload = {

        "msgtype": "link",

        "link": {

            "title": SCHEME + " 已更新至 " + version + " csxc",

            "text": REMARK,

            "messageUrl": downUrl

        }

    }

    

    print payload

    print REMARK

    

    s = json.dumps(payload)

    r = requests.post("https://oapi.dingtalk.com/robot/send?access_token=" + ACCESS_TOKEN, data = s,headers = headers)

    if r.status_code == requests.codes.ok:

        print "通知成功"

# 分析IPA文件

def analyze_ipa_with_plistlib(ipa_path):

     ipa_file = zipfile.ZipFile(ipa_path)

     plist_path = find_plist_path(ipa_file)

     plist_data = ipa_file.read(plist_path)

     plist_root = biplist.readPlistFromString(plist_data)

     return plist_root['CFBundleShortVersionString'] + "_" + plist_root['CFBundleVersion']

def find_plist_path(zip_file):

     name_list = zip_file.namelist()

     # print name_list

     pattern = re.compile(r'Payload/[^/]*.app/Info.plist')

     for path in name_list:

         m = pattern.match(path)

         if m is not None:

             return m.group()

3 脚本使用

GitHub - 821985307/PY_iOS_AutoBuild

在githug上下载Python脚本 放在哪都行 不一定要放在工程目录下  

然后将脚本中定义的几个常量按自己的工程配置进行修改   

直接将py文件拖入到终端中  后面加上    -r  版本更新备注   回车就自动开始打包了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值