-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_keyboard_cmcm_release.py
executable file
·914 lines (719 loc) · 30.7 KB
/
build_keyboard_cmcm_release.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
#coding:utf-8
import sys
import os
import traceback
import subprocess
import shutil
import datetime
import codecs
import glob,re
import time
import urllib2
import time
import json
import mimetypes
#蒲公英应用上传地址
url = 'https://qiniu-storage.pgyer.com/apiv1/app/upload'
#蒲公英提供的 用户Key
uKey = '151ddfce6c0202ac9db7514d33a50688'
#蒲公英提供的 API Key
_api_key = '292e6f251062573501a4f6f85c3e0f55'
#安装应用时需要输入的密码,这个可不填
installPassword = '123456'
#处理 蒲公英 上传结果
def handle_resule(result):
json_result = json.loads(result)
if json_result['code'] is 0:
print '*******文件上传成功****'
print json_result
def _encode_multipart(params_dict):
boundary = '----------%s' % hex(int(time.time() * 1000))
data = []
for k, v in params_dict.items():
data.append('--%s' % boundary)
if hasattr(v, 'read'):
filename = getattr(v, 'name', '')
content = v.read()
decoded_content = content.decode('ISO-8859-1')
data.append('Content-Disposition: form-data; name="%s"; filename="kangda.ipa"' % k)
data.append('Content-Type: application/octet-stream\r\n')
data.append(decoded_content)
else:
data.append('Content-Disposition: form-data; name="%s"\r\n' % k)
data.append(v if isinstance(v, str) else v.decode('utf-8'))
data.append('--%s--\r\n' % boundary)
return '\r\n'.join(data), boundary
def generate_installation(template_file, out_file, parameters):
if not os.path.exists(template_file):
return False
if len(parameters.keys()) == 0:
return False
lines = []
with codecs.open(template_file, u"r", u"utf-8") as inf:
for line in inf:
for key in parameters.keys():
line = line.replace(u"{%s}" % key, parameters[key])
lines.append(line)
if len(lines) == 0:
return False
with codecs.open(out_file, u"w", u"utf-8") as outf:
outf.writelines(lines)
return True
def copyfile_to_ftp(local_path, ftp_path):
import ftplib
import urlparse
u = urlparse.urlparse(ftp_path)
s = ftplib.FTP()
if u.port:
s.connect(u.hostname, u.port)
else:
s.connect(u.hostname, 21)
if u.username:
if u.password:
s.login(u.username, u.password)
else:
s.login(u.username)
else:
s.login()
if len(u.path) > 0:
remote_path = u.path
else:
remote_path = '/'
s.cwd(remote_path)
file_name = local_path.split('/')[-1]
s.storbinary('STOR ' + file_name, open(local_path, 'rb'))
def copydir_to_ftp(local_path, ftp_path, ftp_dirname):
import ftplib
import urlparse
u = urlparse.urlparse(ftp_path)
s = ftplib.FTP()
if u.port:
s.connect(u.hostname, u.port)
else:
s.connect(u.hostname, 21)
if u.username:
if u.password:
s.login(u.username, u.password)
else:
s.login(u.username)
else:
s.login()
if len(u.path) > 0:
remote_path = u.path
else:
remote_path = '/'
s.cwd(remote_path)
if ftp_dirname != '':
print "<ftp>mkdir: " + remote_path + '/' + ftp_dirname
dirarray = ftp_dirname.split('/')
for dirname in dirarray:
try:
s.cwd(dirname)
except Exception, e:
s.mkd(dirname)
s.cwd(dirname)
for root, dirs, files in os.walk(local_path):
remot_root = remote_path + '/' + ftp_dirname + root[len(local_path):].replace('\\','/')
print "<ftp>cwd: " + remot_root
s.cwd(remot_root)
print s.pwd()
for name in files:
fpath = root + '/' + name
print "<ftp>upload: " + remot_root + '/' + name
s.storbinary('STOR '+ name, open(fpath, 'rb'))
for name in dirs:
print "<ftp>mkdir: " + remot_root + '/' + name
s.mkd(name)
def generate_adhoc(t):
if (u"adhoc_install" in t) and (u"adhoc_url" in t):
adhoc_path = os.path.join(t[u'project_path'], u'const.txt')
if not os.path.exists(adhoc_path):
return False
params = {}
for line in open(adhoc_path, 'r'):
line = line.strip()
if len(line) == 0:
continue
offset = line.find('=')
value = line[offset + 1:]
key = line[:offset]
params[key] = value
print "%s = \"%s\"" %(key, value)
# 处理adhoc文件名格式
adhoc_url = t[u"adhoc_url"]
for k in t.keys():
adhoc_url = adhoc_url.replace(u"$(%s)" % k, t[k])
print "adhoc_url:", adhoc_url
# 在当前目录生成var.txt
import codecs
adhoc_var = os.path.join(t[u"project_path"], u"var.txt")
with codecs.open(adhoc_var, u"w", u"utf-8") as f:
lines = [
u"url=%s\n" % adhoc_url,
u"bundle_version=%s\n" % t[u"build_version"]
]
f.writelines(lines)
params[u'url'] = adhoc_url
params[u'bundle_version'] = t[u'build_version']
if generate_installation(u"template.plist", u"install.plist", params):
adhoc_install = t[u'adhoc_install']
if adhoc_install.startswith('ftp://'):
copyfile_to_ftp(os.path.join(t[u'project_path'], u'install.plist'), adhoc_install)
copyfile_to_ftp(os.path.join(t[u'project_path'], u'var.txt'), adhoc_install)
else:
file_path = os.path.join(adhoc_install, u"install.plist")
if os.path.exists(file_path):
shutil.rmtree(file_path)
shutil.copyfile(adhoc_var, file_path)
file_path = os.path.join(adhoc_install, u"var.txt")
if os.path.exists(file_path):
shutil.rmtree(file_path)
shutil.copyfile(adhoc_var, os.path.join(adhoc_install, u"install.plist"))
def get_provision_uuid(filepath):
import re
data = open(filepath,'rb').read()
rr = re.compile(r'<key>UUID</key>\n\t*<string>([A-Za-z0-9\-]+)</string>',re.M).search(data)
#rr = re.compile(r'<key>Name</key>\n\t*<string>([\w]+)</string>',re.M).search(data)
if not rr:
print "Invalid provision file: %s"%filepath
sys.exit(1)
return rr.group(1)
def get_provision_name(filepath):
import re
data = open(filepath,'rb').read()
#rr = re.compile(r'<key>UUID</key>\n\t*<string>([A-Za-z0-9\-]+)</string>',re.M).search(data)
rr = re.compile(r'<key>Name</key>\n\t*<string>([^<]+)</string>',re.M).search(data)
if not rr:
print "Invalid provision file name: %s"%filepath
sys.exit(1)
return rr.group(1)
def zip_dir_content(dir_path, outpath):
retcode = 0
old_cwd = os.getcwd()
os.chdir( dir_path )
print 'old_cwd:'+old_cwd
print 'cur_dir:'+dir_path
print 'dir_path--->'+dir_path;
lstfiles = glob.glob(dir_path+'/*.*');
print lstfiles;
cmdline = 'zip -v -r -y "%s" *'%outpath
print cmdline
retcode = os.system(cmdline)
os.chdir( old_cwd )
return (retcode == 0)
def update_bundle_info(t):
print '...update_bundle_info...'
# print t
project_path = t[u'project_path']
bundle_version = t[u'build_version']
print project_path;
print bundle_version;
if 'info_plist3' in t:
print 'info_plist in t'
info_plist_path = os.path.join(project_path, t[u'info_plist3'])
print 'info.plist = %s' %(info_plist_path)
import plistlib
info = plistlib.readPlist(info_plist_path)
info[u'CFBundleVersion'] = bundle_version
print 'CFBundleVersion = %s' %(info[u'CFBundleVersion'])
version_part = bundle_version.split('.')
info[u'CFBundleShortVersionString'] = version_part[0] + '.' + version_part[1] + '.' + version_part[2]
print 'CFBundleShortVersionString = %s' % (info[u'CFBundleShortVersionString'])
if 'bundle_id' in t:
info[u'CFBundleIdentifier'] = t[u'bundle_id']
print 'CFBundleIdentifier = %s' % (t[u'bundle_id'])
plistlib.writePlist(info, info_plist_path)
if 'info_plist2' in t:
print 'info_plist in t'
info_plist_path = os.path.join(project_path, t[u'info_plist2'])
print 'info.plist = %s' %(info_plist_path)
import plistlib
info = plistlib.readPlist(info_plist_path)
info[u'CFBundleVersion'] = bundle_version
print 'CFBundleVersion = %s' %(info[u'CFBundleVersion'])
version_part = bundle_version.split('.')
info[u'CFBundleShortVersionString'] = version_part[0] + '.' + version_part[1] + '.' + version_part[2]
print 'CFBundleShortVersionString = %s' % (info[u'CFBundleShortVersionString'])
if 'bundle_id' in t:
info[u'CFBundleIdentifier'] = t[u'bundle_id']
print 'CFBundleIdentifier = %s' % (t[u'bundle_id'])
plistlib.writePlist(info, info_plist_path)
if 'info_plist' in t:
print 'info_plist in t'
info_plist_path = os.path.join(project_path, t[u'info_plist'])
print 'info.plist = %s' %(info_plist_path)
import plistlib
info = plistlib.readPlist(info_plist_path)
info[u'CFBundleVersion'] = bundle_version
print 'CFBundleVersion = %s' %(info[u'CFBundleVersion'])
version_part = bundle_version.split('.')
info[u'CFBundleShortVersionString'] = version_part[0] + '.' + version_part[1] + '.' + version_part[2]
print 'CFBundleShortVersionString = %s' % (info[u'CFBundleShortVersionString'])
if 'bundle_id' in t:
info[u'CFBundleIdentifier'] = t[u'bundle_id']
print 'CFBundleIdentifier = %s' % (t[u'bundle_id'])
plistlib.writePlist(info, info_plist_path)
return True
for f in os.listdir(project_path):
if f.endswith(u'-Info.plist'):
info_plist_path = os.path.join(project_path, f)
print 'info.plist = %s' %(info_plist_path)
t[u'info_plist'] = info_plist_path
import plistlib
info = plistlib.readPlist(info_plist_path)
info[u'CFBundleVersion'] = bundle_version
print 'CFBundleVersion = %s' %(info[u'CFBundleVersion'])
version_part = bundle_version.split('.')
info[u'CFBundleShortVersionString'] = version_part[0] + '.' + version_part[1] + '.' + version_part[2]
print 'CFBundleShortVersionString = %s' % (info[u'CFBundleShortVersionString'])
if 'bundle_id' in t:
info[u'CFBundleIdentifier'] = t[u'bundle_id']
print 'CFBundleIdentifier = %s' % (t[u'bundle_id'])
plistlib.writePlist(info, info_plist_path)
return True
return False
def archive_xcode_proj(t, proj, config, target, xcconfig, scheme, archivePath):
cmd_line = u'xcodebuild clean build '
if proj:
cmd_line = cmd_line + u'archive -workspace "%s" '%proj
if config:
cmd_line = cmd_line + u'-configuration "%s" '%config
if xcconfig:
cmd_line = cmd_line + u'-xcconfig "%s" '%xcconfig
if scheme:
cmd_line = cmd_line + u'-scheme "%s" '%scheme
if archivePath:
cmd_line = cmd_line + u'-archivePath "%s" '%archivePath
cmd_line = cmd_line + u'-sdk iphoneos '
print "archive_xcode_proj command " + cmd_line
return 0 == os.system(cmd_line)
# return 1
def export_xcarchive(archivePath, exportPath, exportProvisioningProfile):
cmd_line = u'xcodebuild -exportArchive '
if archivePath:
cmd_line = cmd_line + u'-archivePath "%s" '%archivePath
if exportPath:
cmd_line = cmd_line + u'-exportPath "%s" '%exportPath
if exportProvisioningProfile:
cmd_line = cmd_line + u'-exportOptionsPlist "%s" '%exportProvisioningProfile
# cmd_line = cmd_line + u'-exportFormat ipa '
print "export_xcarchive command " + cmd_line
return 0 == os.system(cmd_line)
def build_task(t):
project_name = None
config = None
target = None
project_name = t[u'project_name']
config = t[u'build_config']
if u'build_target' in t:
target = t[u'build_target']
build_result = update_bundle_info(t)
if not build_result:
print "update_bundle_info failed"
sys.exit(1)
print "Process provisionging profiles..."
buildconfig_root = os.path.join(t[u'project_path'], 'buildconfig')
print "Process adhoc provisioning profiles..."
user_provision_root = os.path.expanduser('~/Library/MobileDevice/Provisioning Profiles')
if os.path.exists(buildconfig_root) and os.path.isdir(buildconfig_root):
#处理buildconfig_cheetah对应的provision文件 iOS Development
# Developmentprovision_filename = t[u'build_config'] + '.mobileprovision'
Developmentprovision_filename = 'singDevelopment.mobileprovision'
Developmentprovision_filepath = os.path.join(buildconfig_root, Developmentprovision_filename)
if os.path.exists(Developmentprovision_filepath):
assert(not os.path.isdir(Developmentprovision_filepath))
Developmentprovision_uuid = get_provision_uuid(Developmentprovision_filepath)
Developmentprovision_uuid_name = Developmentprovision_uuid + '.mobileprovision'
shutil.copyfile(Developmentprovision_filepath, os.path.join(user_provision_root, Developmentprovision_uuid_name))
adhoc_provision_filename = 'adhocDistribution.mobileprovision'
adhoc_provision_filepath = os.path.join(buildconfig_root, adhoc_provision_filename)
if os.path.exists(adhoc_provision_filepath):
assert(not os.path.isdir(adhoc_provision_filepath))
adhoc_provision_uuid = get_provision_uuid(adhoc_provision_filepath)
adhoc_provision_uuid_name = adhoc_provision_uuid + '.mobileprovision'
adhoc_provision_name = get_provision_name(adhoc_provision_filepath)
print '%s provision: %s -> %s'%(t[u'build_config'], adhoc_provision_filepath, adhoc_provision_uuid_name)
shutil.copyfile(adhoc_provision_filepath, os.path.join(user_provision_root, adhoc_provision_uuid_name))
t[u'build_config_adhoc_provision_path'] = os.path.join(user_provision_root, adhoc_provision_uuid_name)
assert(not os.path.isdir(adhoc_provision_filepath))
t[u'adhoc_provision_fullpath'] = adhoc_provision_filepath
print 'adhoc provision: %s'%(adhoc_provision_filepath)
# if True:
# return
# 编译
print "Build the project..."
old_cwd = os.getcwd()
os.chdir( t[u'project_path'] )
build_dir = os.path.join(t[u'project_path'], u'build')
t[u'build_dir'] = build_dir
archiveFilesPath = os.path.join(build_dir, u'archivefiles.xcarchive')
build_result = archive_xcode_proj(t, project_name + '.xcworkspace', config, target, None,project_name,archiveFilesPath)
os.chdir( old_cwd )
if not build_result:
sys.exit(1)
archive_name = t[u'archive_name_format']
for k in t.keys():
archive_name = archive_name.replace('$(%s)'%k,t[k])
t[u"archive_name_format"] = archive_name
# #创建存档包
t[u'tmp_path'] = os.tempnam()
tmp_archive_path = os.path.join( t[u'tmp_path'], archive_name )
os.makedirs(tmp_archive_path)
tmp_archive_zip_path = os.path.join(tmp_archive_path, 'archive.zip')
if not zip_dir_content(archiveFilesPath, tmp_archive_zip_path):
print 'zip build_result_path failed'
sys.exit(1)
shutil.copyfile(tmp_archive_zip_path,os.path.join(build_dir, 'archive.zip'))
app_path = None
tmp_build_result_path = os.path.join(archiveFilesPath, u'Products/Applications')
for f in os.listdir(tmp_build_result_path):
if os.path.splitext(f)[1] == '.app':
app_path = os.path.join(tmp_build_result_path, f)
break
if not app_path:
print 'app not found'
sys.exit(1)
print "app_path = " + app_path
#创建adhoc_ipa
if u'adhoc_ipa_name_format' in t:
adhoc_ipa_name_format = t[u'adhoc_ipa_name_format']
for k in t.keys():
adhoc_ipa_name_format = adhoc_ipa_name_format.replace('$(%s)'%k,t[k])
t[u'adhoc_ipa_name_format'] = adhoc_ipa_name_format
build_result = export_xcarchive(archiveFilesPath, build_dir, os.path.join(buildconfig_root, "adhoc.plist"))
print t[u'adhoc_ipa_name_format']
if not build_result:
sys.exit(1)
os.path.join(build_dir, project_name) +".ipa"
shutil.move(os.path.join(build_dir, project_name) +".ipa",os.path.join(build_dir, adhoc_ipa_name_format))
shutil.rmtree(archiveFilesPath)
# if True:
# return
archive_root = t[u'archive_root']
if archive_root.startswith('ftp://'):
copydir_to_ftp( build_dir, archive_root, archive_name )
else:
shutil.copytree(build_dir, os.path.join(archive_root, archive_name), True)
# generate_adhoc(t)
shutil.rmtree(t[u'tmp_path'])
#上传蒲公英
#请求参数字典
params = {
'uKey': uKey,
'_api_key': _api_key,
'file': open(os.path.join(build_dir, adhoc_ipa_name_format), 'rb'),
'publishRange': '2',
'password': installPassword
}
coded_params, boundary = _encode_multipart(params)
req = urllib2.Request(url, coded_params.encode('ISO-8859-1'))
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
try:
print '*******开始文件上传****'
resp = urllib2.urlopen(req)
body = resp.read().decode('utf-8')
handle_resule(body)
except urllib2.HTTPError as e:
print(e.fp.read())
def build_cmcm_task(t):
t[u'groupid'] = 'group.com.cheetah.keyboard.theme.emoji';
t[u'teamid'] = 'VRGS6G42JR';
modify_Groupid(t)
modify_teamid(t)
project_name = None
config = None
target = None
scheme = t[u'scheme']
project_name = t[u'project_name']
config = t[u'build_config']
if u'build_target' in t:
target = t[u'build_target']
build_result = update_bundle_info(t)
if not build_result:
print "update_bundle_info failed"
sys.exit(1)
print "Process provisionging profiles..."
buildconfig_root = os.path.join(t[u'project_path'], 'buildconfigRelease')
print "Process adhoc provisioning profiles..."
user_provision_root = os.path.expanduser('~/Library/MobileDevice/Provisioning Profiles')
array = ('adhoccomcheetahkeyboardthemeemoji.mobileprovision','adhoccomcheetahkeyboardthemeemojiExtension.mobileprovision','adhoccomcheetahkeyboardthemeemojiiMessageE.mobileprovision','comcheetahkeyboardthemeemoji.mobileprovision','comcheetahkeyboardthemeemojiExtension.mobileprovision','comcheetahkeyboardthemeemojiiMessageE.mobileprovision')
if os.path.exists(buildconfig_root) and os.path.isdir(buildconfig_root):
for prefile in array:
filepath = os.path.join(buildconfig_root, prefile)
print filepath
if os.path.exists(filepath):
assert(not os.path.isdir(filepath))
uuid = get_provision_uuid(filepath)
uuid_name = uuid + '.mobileprovision'
shutil.copyfile(filepath, os.path.join(user_provision_root, uuid_name))
# 编译
print "Build the project..."
old_cwd = os.getcwd()
os.chdir( t[u'project_path'] )
build_dir = os.path.join(t[u'project_path'], u'build')
t[u'build_dir'] = build_dir
archiveFilesPath = os.path.join(build_dir, u'archivefiles.xcarchive')
build_result = archive_xcode_proj(t, project_name + '.xcworkspace', config, target, None,scheme,archiveFilesPath)
os.chdir( old_cwd )
if not build_result:
sys.exit(1)
archive_name = t[u'archive_name_format']
for k in t.keys():
archive_name = archive_name.replace('$(%s)'%k,t[k])
t[u"archive_name_format"] = archive_name
#创建存档包
t[u'tmp_path'] = os.tempnam()
tmp_archive_path = os.path.join( t[u'tmp_path'], archive_name )
os.makedirs(tmp_archive_path)
tmp_archive_zip_path = os.path.join(tmp_archive_path, 'archive.zip')
if not zip_dir_content(os.path.join(archiveFilesPath, 'dSYMs' ), tmp_archive_zip_path):
print 'zip build_result_path failed'
sys.exit(1)
shutil.copyfile(tmp_archive_zip_path,os.path.join(build_dir, 'archive.zip'))
app_path = None
tmp_build_result_path = os.path.join(archiveFilesPath, u'Products/Applications')
for f in os.listdir(tmp_build_result_path):
if os.path.splitext(f)[1] == '.app':
app_path = os.path.join(tmp_build_result_path, f)
break
if not app_path:
print 'app not found'
sys.exit(1)
print "app_path = " + app_path
#创建adhoc_ipa
if u'adhoc_ipa_name_format' in t:
adhoc_ipa_name_format = t[u'adhoc_ipa_name_format']
for k in t.keys():
adhoc_ipa_name_format = adhoc_ipa_name_format.replace('$(%s)'%k,t[k])
t[u'adhoc_ipa_name_format'] = adhoc_ipa_name_format
build_result = export_xcarchive(archiveFilesPath, build_dir, os.path.join(buildconfig_root, "ad-hoc.plist"))
print t[u'adhoc_ipa_name_format']
if not build_result:
sys.exit(1)
shutil.move(os.path.join(build_dir, scheme) +".ipa",os.path.join(build_dir, adhoc_ipa_name_format))
#创建app_ipa
if u'dist_ipa_name_format' in t:
dist_ipa_name_format = t[u'dist_ipa_name_format']
for k in t.keys():
dist_ipa_name_format = dist_ipa_name_format.replace('$(%s)'%k,t[k])
t[u'dist_ipa_name_format'] = dist_ipa_name_format
build_result = export_xcarchive(archiveFilesPath, build_dir, os.path.join(buildconfig_root, "app-store.plist"))
print t[u'dist_ipa_name_format']
if not build_result:
sys.exit(1)
shutil.move(os.path.join(build_dir, scheme) +".ipa",os.path.join(build_dir, dist_ipa_name_format))
shutil.rmtree(archiveFilesPath)
#上传蒲公英
#请求参数字典
params = {
'uKey': uKey,
'_api_key': _api_key,
'file': open(os.path.join(build_dir, adhoc_ipa_name_format), 'rb'),
'publishRange': '2',
'password': installPassword
}
coded_params, boundary = _encode_multipart(params)
req = urllib2.Request(url, coded_params.encode('ISO-8859-1'))
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
try:
print '*******蒲公英开始文件上传****'
resp = urllib2.urlopen(req)
body = resp.read().decode('utf-8')
handle_resule(body)
except urllib2.HTTPError as e:
print(e.fp.read())
# if True:
# return
bundle_version = t[u'build_version']
version_part = bundle_version.split('.')
versiondir= version_part[0] + '.' + version_part[1] + '.' + version_part[2]
archive_root = t[u'archive_root']
if archive_root.startswith('ftp://'):
print '*******ftp开始文件上传****'
copydir_to_ftp( build_dir, archive_root, versiondir+"/"+ archive_name )
print '*******ftp文件上传结束****'
else:
shutil.copytree(build_dir, os.path.join(archive_root, archive_name), True)
# generate_adhoc(t)
shutil.rmtree(t[u'tmp_path'])
shutil.rmtree(build_dir)
def modify_teamid(t):
project_name = t[u'project_name']
filename = project_name + ".xcodeproj/project.pbxproj"
build_pattern = r'''DEVELOPMENT_TEAM[\s]*=[\s]*([A-Za-z0-9]*);'''
rawString = open(filename, "rb").read()
newString = re.sub(build_pattern, lambda m : 'DEVELOPMENT_TEAM = '+t[u'teamid']+';', rawString)
build_pattern = r'''DevelopmentTeam[\s]*=[\s]*([A-Za-z0-9]*);'''
newString = re.sub(build_pattern, lambda m :'DevelopmentTeam = '+t[u'teamid']+';', newString)
fw = open(filename, "wb")
if fw == None:
raise ("file can't write")
fw.write(newString);
fw.close()
def modify_Groupid(t):
filename = './KeyboardKit/Config/CMGroupDataManager.m'
build_pattern = r'''static NSString \* kGroupId(.)*'''
rawString = open(filename, "rb").read()
newString = re.sub(build_pattern, lambda m : 'static NSString * kGroupId = @"'+t[u'groupid']+'";', rawString)
fw = open(filename, "wb")
if fw == None:
raise ("file can't write")
fw.write(newString);
fw.close()
def buildEnterprise_task(t):
t[u'groupid'] = 'group.com.cheetah.keyboard';
t[u'teamid'] = 'RKFT92L7M9';
modify_Groupid(t)
modify_teamid(t)
project_name = None
config = None
target = None
scheme = t[u'scheme']
project_name = t[u'project_name']
config = t[u'build_config']
if u'build_target' in t:
target = t[u'build_target']
build_result = update_bundle_info(t)
if not build_result:
print "update_bundle_info failed"
sys.exit(1)
print "Process provisionging profiles..."
buildconfig_root = os.path.join(t[u'project_path'], 'buildconfigEnterprise')
print "Process adhoc provisioning profiles..."
user_provision_root = os.path.expanduser('~/Library/MobileDevice/Provisioning Profiles')
array = ('com cheetah keyboard Extension.mobileprovision','com cheetah keyboard.mobileprovision','XC com cheetah keyboard Extension.mobileprovision','XC com cheetah keyboard.mobileprovision')
if os.path.exists(buildconfig_root) and os.path.isdir(buildconfig_root):
for prefile in array:
filepath = os.path.join(buildconfig_root, prefile)
print filepath
if os.path.exists(filepath):
assert(not os.path.isdir(filepath))
uuid = get_provision_uuid(filepath)
uuid_name = uuid + '.mobileprovision'
shutil.copyfile(filepath, os.path.join(user_provision_root, uuid_name))
# 编译
print "Build the project..."
old_cwd = os.getcwd()
os.chdir( t[u'project_path'] )
build_dir = os.path.join(t[u'project_path'], u'build')
t[u'build_dir'] = build_dir
archiveFilesPath = os.path.join(build_dir, u'archivefiles.xcarchive')
build_result = archive_xcode_proj(t, project_name + '.xcworkspace', config, target, None,scheme,archiveFilesPath)
os.chdir( old_cwd )
if not build_result:
sys.exit(1)
archive_name = t[u'archive_name_format']
for k in t.keys():
archive_name = archive_name.replace('$(%s)'%k,t[k])
t[u"archive_name_format"] = archive_name
#创建存档包
t[u'tmp_path'] = os.tempnam()
tmp_archive_path = os.path.join( t[u'tmp_path'], archive_name )
os.makedirs(tmp_archive_path)
tmp_archive_zip_path = os.path.join(tmp_archive_path, 'archive.zip')
if not zip_dir_content(os.path.join(archiveFilesPath, 'dSYMs' ), tmp_archive_zip_path):
print 'zip build_result_path failed'
sys.exit(1)
shutil.copyfile(tmp_archive_zip_path,os.path.join(build_dir, 'archive.zip'))
app_path = None
tmp_build_result_path = os.path.join(archiveFilesPath, u'Products/Applications')
for f in os.listdir(tmp_build_result_path):
if os.path.splitext(f)[1] == '.app':
app_path = os.path.join(tmp_build_result_path, f)
break
if not app_path:
print 'app not found'
sys.exit(1)
print "app_path = " + app_path
#创建appStore_ipa
if u'enterprise_ipa_name_format' in t:
enterprise_ipa_name_format = t[u'enterprise_ipa_name_format']
for k in t.keys():
enterprise_ipa_name_format = enterprise_ipa_name_format.replace('$(%s)'%k,t[k])
t[u'enterprise_ipa_name_format'] = enterprise_ipa_name_format
build_result = export_xcarchive(archiveFilesPath, build_dir, os.path.join(buildconfig_root, "cmEnterprise.plist"))
print t[u'enterprise_ipa_name_format']
if not build_result:
sys.exit(1)
shutil.move(os.path.join(build_dir, scheme) +".ipa",os.path.join(build_dir, enterprise_ipa_name_format))
shutil.rmtree(archiveFilesPath)
#上传蒲公英
#请求参数字典
params = {
'uKey': uKey,
'_api_key': _api_key,
'file': open(os.path.join(build_dir, enterprise_ipa_name_format), 'rb'),
'publishRange': '2',
'password': installPassword
}
coded_params, boundary = _encode_multipart(params)
req = urllib2.Request(url, coded_params.encode('ISO-8859-1'))
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
try:
print '*******蒲公英开始文件上传****'
resp = urllib2.urlopen(req)
body = resp.read().decode('utf-8')
handle_resule(body)
except urllib2.HTTPError as e:
print(e.fp.read())
# if True:
# return
archive_root = t[u'archive_root']
if archive_root.startswith('ftp://'):
print '*******ftp开始文件上传****'
copydir_to_ftp( build_dir, archive_root, archive_name )
print '*******ftp文件上传结束****'
else:
shutil.copytree(build_dir, os.path.join(archive_root, archive_name), True)
# generate_adhoc(t)
shutil.rmtree(t[u'tmp_path'])
def main():
_module_fullname = os.path.abspath( sys.modules[__name__].__file__ )
_module_dir = os.path.dirname(_module_fullname)
buildcfg = os.path.join(_module_dir, u'buildcfg')
if not os.path.exists(buildcfg):
print 'error: %s not exists'%buildcfg
sys.exit(1)
print "Analyzing input parameters:"
t = {}
for l in open(buildcfg, 'r'):
l = l.strip()
if len(l) == 0:
continue
off = l.find('=')
v = l[off+1:]
k = l[:off]
t[k] = v
print "%s = \"%s\"" %(k, v)
print "Checking parameters..."
assert( u'project_path' in t )
assert( u'project_name' in t )
assert( u'build_config' in t )
#assert( u'app_version' in t )
#assert( u'build_revision' in t )
#assert( u'build_number' in t )
assert( u'archive_root' in t )
assert( u'archive_name_format' in t )
#assert( u'dist_ipa_name_format' in t )
#assert( u'adhoc_ipa_name_format' in t )
if (u'dist_ipa_name_format' not in t) and( u'adhoc_ipa_name_format' not in t ):
if u'ipa_name_format' in t:
t[u'adhoc_ipa_name_format'] = t[u'ipa_name_format']
assert( u'build_time' in t )
assert( u'build_version' in t )
if len(t[u'build_version'].split('.')) != 4:
print 'error: build_version format error. build_version=%s'%t[u'build_version']
sys.exit(1)
build_cmcm_task(t)
# try:
# shutil.rmtree(t[u'project_path'] +"/build")
# build_task(t)
# finally:
# if u'build_config_provision_path' in t:
# print 'delete %s'%t[u'build_config_provision_path']
# os.remove(t[u'build_config_provision_path'])
# if u'build_config_adhoc_provision_path' in t:
# print 'delete %s'%t[u'build_config_adhoc_provision_path']
# os.remove(t[u'build_config_adhoc_provision_path'])
if __name__ == u'__main__':
main()