-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_build.py
190 lines (152 loc) · 7.3 KB
/
auto_build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/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.0'
BUILD = '17021803'
#要打包的TARGET名字
TARGET = 'MemoryChain'
#Info.plist路径
PLIST_PATH = "xxxxxx/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 = 'xxxxxx'
APPLEPWD = 'xxxxx'
# configuration for pgyer
PGYER_UPLOAD_URL = "http://www.pgyer.com/apiv1/app/upload"
DOWNLOAD_BASE_URL = "http://www.pgyer.com"
USER_KEY = "xxxxxx"
API_KEY = "xxxxx"
#设置从蒲公英下载应用时的密码
PYGER_PASSWORD = "xxxxx"
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 xcbuild(options):
project = options.project
workspace = options.workspace
if project is None and workspace is None:
pass
elif project is not None:
buildProject(project)
elif workspace is not None:
buildWorkspace(workspace)
#导出ipa文件
exportarchive = exportArchive()
print "~~~~~~~~~~~~~~~~是否上传到蒲公英~~~~~~~~~~~~~~~~"
print " 1 不上传 (默认)"
print " 2 上传 "
isuploadpgyer = raw_input("您的决定:")
if isuploadpgyer == "2" and exportarchive != "":
uploadIpaToPgyer(IPAPATH)
print "~~~~~~~~~~~~~~~~是否上传到AppStore~~~~~~~~~~~~~~~~"
print " 1 不上传 (默认)"
print " 2 上传 "
isuploadappstore = raw_input("您的决定:")
if isuploadappstore == '2':
uploadIpaToAppStore()
else:
print "~~~~~~~~~~~~~~~~是否删除archive文件~~~~~~~~~~~~~~~~"
print " 1 保留 (默认)"
print " 2 删除 "
iscleararchive = raw_input("您的决定:")
if iscleararchive == "2":
cleanArchiveFile()
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")
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))
xcbuild(options)
if __name__ == '__main__':
main()