#!/usr/bin/env python # -*- coding:utf-8 -*- #./autobuild.py -p youproject.xcodeproj #./autobuild.py -w youproject.xcworkspace import argparse import subprocess import requests import os import datetime #configuration for iOS build setting CONFIGURATION = "Release" EXPORT_OPTIONS_PLIST = "exportOptions.plist" #发布版本号 VERSION = '1.0.6' BUILD = '1' #要打包的TARGET名字 TARGET = 'SolarLamp' #Info.plist路径 PLIST_PATH = "../SolarLamp/SolarLampShare/Info.plist" #存放路径以时间命令 DATE = datetime.datetime.now().strftime('%Y-%m-%d_%H.%M.%S') #会在桌面创建输出ipa文件的目录 EXPORT_MAIN_DIRECTORY = "~/Desktop/" + TARGET + DATE #xcarchive文件路径(含有dsym),后续查找BUG用途 ARCHIVEPATH = EXPORT_MAIN_DIRECTORY + "/%s%s.xcarchive" %(TARGET,VERSION) #ipa路径 IPAPATH = EXPORT_MAIN_DIRECTORY + "/%s.ipa" %(TARGET) #苹果开发者账号 APPLEID = 'zhnansong@hotmail.com' APPLEPWD = 'Tw060538' # configuration for pgyer PGYER_UPLOAD_URL = "http://www.pgyer.com/apiv1/app/upload" DOWNLOAD_BASE_URL = "http://www.pgyer.com" USER_KEY = "dc86bc578930052ae83ca5866dd2ea26" API_KEY = "0ff3943f4ddb2a4e52cab07e51f705a7" #设置从蒲公英下载应用时的密码 PYGER_PASSWORD = "" def cleanArchiveFile(): cleanCmd = "rm -r %s" %(ARCHIVEPATH) process = subprocess.Popen(cleanCmd, shell = True) process.wait() print "cleaned archiveFile: %s" %(ARCHIVEPATH) def uploadIpaToAppStore(): print "iPA上传中...." altoolPath = "/Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool" exportCmd = "%s --validate-app -f %s -u %s -p %s -t ios --output-format xml" % (altoolPath, IPAPATH, APPLEID,APPLEPWD) process = subprocess.Popen(exportCmd, shell=True) (stdoutdata, stderrdata) = process.communicate() validateResult = process.returncode if validateResult == 0: print '~~~~~~~~~~~~~~~~iPA验证通过~~~~~~~~~~~~~~~~' exportCmd = "%s --upload-app -f %s -u %s -p %s -t ios --output-format normal" % ( altoolPath, IPAPATH, APPLEID, APPLEPWD) process = subprocess.Popen(exportCmd, shell=True) (stdoutdata, stderrdata) = process.communicate() uploadresult = process.returncode if uploadresult == 0: print '~~~~~~~~~~~~~~~~iPA上传成功' else: print '~~~~~~~~~~~~~~~~iPA上传失败' else: print "~~~~~~~~~~~~~~~~iPA验证失败~~~~~~~~~~~~~~~~" def parserUploadResult(jsonResult): resultCode = jsonResult['code'] if resultCode == 0: downUrl = DOWNLOAD_BASE_URL +"/"+jsonResult['data']['appShortcutUrl'] print "Upload Success" print "DownUrl is:" + downUrl else: print "Upload Fail!" print "Reason:"+jsonResult['message'] def uploadIpaToPgyer(ipaPath): print "ipaPath:"+ipaPath 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,'publishRange':'2','isPublishToPublic':'2', 'password':PYGER_PASSWORD} print "uploading...." r = requests.post(PGYER_UPLOAD_URL, data = payload ,files=files,headers=headers) if r.status_code == requests.codes.ok: result = r.json() parserUploadResult(result) else: print 'HTTPError,Code:'+r.status_code def exportArchive(): exportCmd = "xcodebuild -exportArchive -archivePath %s -exportPath %s -exportOptionsPlist %s" %(ARCHIVEPATH, EXPORT_MAIN_DIRECTORY, EXPORT_OPTIONS_PLIST) process = subprocess.Popen(exportCmd, shell=True) (stdoutdata, stderrdata) = process.communicate() signReturnCode = process.returncode if signReturnCode != 0: print "export %s failed" %(TARGET) return "" else: return EXPORT_MAIN_DIRECTORY def buildProject(project): archiveCmd = 'xcodebuild -project %s -scheme %s -configuration %s archive -archivePath %s -destination generic/platform=iOS' %(project, TARGET, CONFIGURATION, ARCHIVEPATH) process = subprocess.Popen(archiveCmd, shell=True) process.wait() archiveReturnCode = process.returncode if archiveReturnCode != 0: print "archive project %s failed" %(project) cleanArchiveFile() def buildWorkspace(workspace): archiveCmd = 'xcodebuild -workspace %s -scheme %s -configuration %s archive -archivePath %s -destination generic/platform=iOS' %(workspace, TARGET, CONFIGURATION, ARCHIVEPATH) process = subprocess.Popen(archiveCmd, shell=True) process.wait() archiveReturnCode = process.returncode if archiveReturnCode != 0: print "archive workspace %s failed" %(workspace) cleanArchiveFile() def uploadIpaToAll(): # uploadIpaToPgyer(IPAPATH) uploadIpaToAppStore() def sendChoice(sendtype): if sendtype == "no": return elif sendtype == "pgy": uploadIpaToPgyer(IPAPATH) else: uploadIpaToAll() def select_option_run(options): project = options.project workspace = options.workspace command = options.command sendtype = options.sendtype if command == "build": if project is None and workspace is None: pass elif project is not None: buildProject(project) elif workspace is not None: buildWorkspace(workspace) exportarchive = exportArchive() sendChoice(sendtype) elif command == "send": sendChoice(sendtype) def main(): parser = argparse.ArgumentParser() parser.add_argument("-w", "--workspace", help="Build the workspace name.xcworkspace.", metavar="name.xcworkspace") parser.add_argument("-p", "--project", help="Build the project name.xcodeproj.", metavar="name.xcodeproj") parser.add_argument("-c", "--command", help="Select your arction command", metavar="build", default="build") parser.add_argument("-t", "--sendtype", help="Select your arction command", metavar="no", default="no") options = parser.parse_args() print "options: %s" % (options) os.system('/usr/libexec/PlistBuddy -c "Set:CFBundleShortVersionString %s" %s' % (VERSION,PLIST_PATH)) os.system('/usr/libexec/PlistBuddy -c "Set:CFBundleVersion %s" %s' % (BUILD, PLIST_PATH)) select_option_run(options) if __name__ == '__main__': main()