-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdeploy.py
executable file
·873 lines (654 loc) · 34 KB
/
deploy.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
#!/usr/bin/env python3
# Copyright (C) 2021 Adrian Carpenter, et al
#
# This file is part of Pingnoo (https://github.com/nedrysoft/pingnoo)
#
# An open-source cross-platform traceroute analyser.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import argparse
import codecs
import datetime
import glob
import logging
import os
import platform
import re
import shutil
import string
import sys
import tempfile
import time
import zipfile
from pingnoo_support_python.common import *
from pingnoo_support_python.makedeb import deb_create
from pingnoo_support_python.makepkg import pkg_create
from pingnoo_support_python.makerpm import rpm_create
from pingnoo_support_python.msg_printer import msg_printer, MsgPrinterException
if sys.hexversion < 0x030600f0:
raise RuntimeError('requires python >= 3.6')
if sys.hexversion < 0x030800f0:
# Older python versions not liking our checkboxes and color
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
def notarize_file(filetonotarize, username, password):
# TODO: Move to new execute
uuidpattern = re.compile(r'RequestUUID\s=\s(?P<requestUUID>[a-f|0-9]{8}-[a-f|0-9]{4}-[a-f|0-9]{4}-[a-f|0-9]{4}-['
r'a-f|0-9]{12})\n')
statuspattern = re.compile(r'\s*Status:\s(?P<status>\w+)\n')
uploadid = str(int(time.time()))
resultcode, result = execute(f'xcrun altool --notarize-app --primary-bundle-id "com.nedrysoft.pingnoo.{uploadid}" '
f'-u {username} --password {password} --file {filetonotarize}')
if resultcode:
return False
match = uuidpattern.search(result)
if not match:
return False
requestid = match.groupdict()["requestUUID"]
if not requestid:
return False
while True:
resultcode, result = execute(f'xcrun altool --notarization-info {requestid} -u {username}'
f' --password {password}')
if not resultcode:
match = statuspattern.search(result)
if match:
currentstatus = match.groupdict()["status"]
if currentstatus == "in progress":
time.sleep(10)
continue
if currentstatus == "success":
break
if currentstatus == "invalid":
break
return currentstatus
def find_qt():
""" Common code between Linux and Darwin """
with msg_printer('Checking qtdir...'):
if platform.system() == "Windows":
raise MsgPrinterException("Windows shouldn't have called find_qt()!?!")
if args.qtdir and os.path.isfile(args.qtdir + '/bin/qmake'):
qtdir = args.qtdir
else:
qtdir = None
if qtdir is None:
qmake = which('qmake')
if qmake:
qtdir = parent(os.path.normpath(os.path.dirname(qmake)))
else:
qtdir = None
if (qtdir is None) or (not os.path.isdir(qtdir)):
raise MsgPrinterException('qt directory could not be found. (see --qtdir).')
return qtdir
# application entry point
parser = argparse.ArgumentParser(description='Pingnoo deployment tool')
parser.add_argument('--qtdir', type=str, nargs='?', help='path to qt')
parser.add_argument('--curlbin', type=str, nargs='?', help='path to curl binary')
if platform.system() == "Darwin":
parser.add_argument('--arch',
choices=['x86_64', 'arm64', 'universal'],
type=str,
default='x86_64',
nargs='?',
help='architecture type to deploy')
parser.add_argument('--universal',
action='store_true',
help='whether to deploy a universal binary or the target arch binary')
elif platform.system() == "Linux":
parser.add_argument('--arch',
choices=['x86', 'x86_64', 'armv7l'],
type=str,
default='x86_64',
nargs='?',
help='architecture type to deploy')
else:
parser.add_argument('--arch',
choices=['x86', 'x86_64', 'universal'],
type=str,
default='x86_64',
nargs='?',
help='architecture type to deploy')
parser.add_argument('--type',
choices=['release', 'debug'],
default='release',
type=str,
nargs='?',
help='type of build to deploy')
parser.add_argument('--cert', type=str, nargs='?', help='certificate id to sign with')
if platform.system() == "Linux":
parser.add_argument('--linuxdeployqt',
type=str,
default='tools/linuxdeployqt/linuxdeployqt-6-x86_64.AppImage',
nargs='?',
help='path to linuxdeployqt')
parser.add_argument('--appimagetool',
type=str,
default='tools/appimagetool/appimagetool-x86_64.AppImage',
nargs='?',
help='path to appimagetool')
parser.add_argument('--deb',
action='store_true',
help='generate deb package')
parser.add_argument('--rpm',
action='store_true',
help='generate rpm package')
parser.add_argument('--pkg',
action='store_true',
help='generate arch package')
parser.add_argument('--appimage',
action='store_true',
help='generate AppImage package')
parser.add_argument('--extra-packages',
type=str,
default=None,
nargs='?',
help='any extra packages that are required')
if platform.system() == "Windows":
parser.add_argument('--timeserver',
type=str,
default='http://time.certum.pl/',
nargs='?',
help='time server to use for signing')
parser.add_argument('--signtool',
type=str,
nargs='?',
default='tools\\smartcardtools\\x64\\ScSignTool.exe',
help='path to signing binary')
parser.add_argument('--pin',
type=str,
nargs='?',
default='',
help='pin when using scsigntool')
parser.add_argument('--portable',
action='store_true',
help='create portable zip')
parser.add_argument('--qtdir64', type=str, nargs='?', help='path to qt')
if platform.system() == "Darwin":
parser.add_argument('--appleid', type=str, nargs='?', help='apple id to use for notarization')
parser.add_argument('--password', type=str, nargs='?', help='password for apple id')
parser.add_argument('--version', type=str, nargs='?', help='version string', required=True)
parser.add_argument('--debugoutput', type=str, nargs='?', help='debugoutput string', required=False)
args = parser.parse_args()
build_arch = args.arch
build_type = args.type.capitalize()
build_version = args.version.replace("/", "-")
def _do_darwin():
""" MacOS / Darwin version """
def mac_sign_binary(filetosign, cert):
# TODO: Move to new execute
return execute(f'codesign --verify --timestamp -o runtime --force --sign "{cert}" "{filetosign}"')
# check for qt installation
qtdir = find_qt()
if args.universal:
target_arch = "universal"
else:
target_arch = args.arch
# remove previous deployment files and copy current binaries
with msg_printer('Setting up deployment directory...'):
rm_path('deployment')
os.makedirs('deployment')
rm_path(f'bin/{target_arch}/Deploy')
os.makedirs(f'bin/{target_arch}/Deploy')
if not os.path.isfile('tools/macdeployqtfix/macdeployqtfix.py'):
rm_path('tools/macdeployqtfix')
with msg_printer('Cloning macdeployqtfix...'):
execute('cd tools; git clone https://github.com/nedrysoft/macdeployqtfix.git',
fail_msg='unable to clone macdeployqtfix.')
if args.universal:
if not os.path.isfile('tools/makeuniversal/makeuniversal'):
rm_path('tools/makeuniversal')
with msg_printer('Cloning makeuniversal...'):
execute('cd tools;git clone https://github.com/nedrysoft/makeuniversal.git',
fail_msg='unable to clone makeuniversal.')
with msg_printer('Building makeuniversal...'):
execute(f'cd tools/makeuniversal;{qtdir}/bin/qmake;make', fail_msg='error building makeuniversal.')
with msg_printer('Running makeuniversal...'):
execute(f'tools/makeuniversal/makeuniversal bin/universal/Deploy/Pingnoo.app '
f'bin/x86_64/{build_type}/Pingnoo.app bin/arm64/{build_type}/Pingnoo.app',
fail_msg='error building makeuniversal.')
else:
shutil.copytree(f'bin/{build_arch}/{build_type}/Pingnoo.app',
f'bin/{build_arch}/Deploy/Pingnoo.app', symlinks=True)
target_arch = build_arch
# run standard qt deployment tool
with msg_printer('Running macdeployqt...'):
execute(f'{qtdir}/bin/macdeployqt bin/{target_arch}/Deploy/Pingnoo.app -no-strip',
fail_msg='there was a problem running macdeployqt.')
# remove the sql drivers that we don't use
with msg_printer('Removing unwanted qt plugins...'):
rm_file(f'bin/{target_arch}/Deploy/Pingnoo.app/Contents/PlugIns/sqldrivers/libqsqlodbc.dylib')
rm_file(f'bin/{target_arch}/Deploy/Pingnoo.app/Contents/PlugIns/sqldrivers/libqsqlpsql.dylib')
# run fixed qt deployment tool
if platform.system() == "Darwin":
with msg_printer('Running macdeployqtfix...'):
sys.path.insert(1, 'tools/macdeployqtfix')
import macdeployqtfix as fixdeploy
fixdeploy.GlobalConfig.qtpath = os.path.normpath(f'{qtdir}/bin')
fixdeploy.GlobalConfig.exepath = f'bin/{target_arch}/Deploy/Pingnoo.app'
fixdeploy.GlobalConfig.logger = logging.getLogger()
fixdeploy.GlobalConfig.logger.addHandler(logging.NullHandler())
if not fixdeploy.fix_main_binaries():
raise MsgPrinterException('there was a problem running macdeployqtfix.')
# sign the application
with msg_printer('Signing binaries...'):
for file in glob.glob(f'bin/{target_arch}/Deploy/Pingnoo.app/**/*.framework', recursive=True):
result_code, result_output = mac_sign_binary(file, args.cert)
if result_code:
raise MsgPrinterException(
f'there was a problem signing a file ({file}).\r\n\r\n{result_output}\r\n')
for file in glob.glob(f'bin/{target_arch}/Deploy/Pingnoo.app/**/*.dylib', recursive=True):
result_code, result_output = mac_sign_binary(file, args.cert)
if result_code:
raise MsgPrinterException(
f'there was a problem signing a file ({file}).\r\n\r\n{result_output}\r\n')
result_code, result_output = mac_sign_binary(f'bin/{target_arch}/Deploy/Pingnoo.app', args.cert)
if result_code:
raise MsgPrinterException(f'there was a problem signing a file (bin/{target_arch}/Deploy/Pingnoo.app).'
f'\r\n\r\n{result_output}\r\n')
# package the application into a zip file and notarize the application
with msg_printer('Creating zip archive...'):
execute(f'ditto '
f'-ck '
f'--sequesterRsrc '
f'--keepParent bin/{target_arch}/Deploy/Pingnoo.app '
f'bin/{target_arch}/Deploy/Pingnoo.zip', fail_msg='there was a problem generating the application zip.')
with msg_printer('Performing notarization of application binary...'):
status = notarize_file(f'bin/{target_arch}/Deploy/Pingnoo.zip', args.appleid, args.password)
if not status == "success":
raise MsgPrinterException(f'there was a problem notarizing the application ({status}).')
with msg_printer('Stapling notarization ticket to binary...'):
execute(f'xcrun stapler staple bin/{target_arch}/Deploy/Pingnoo.app',
'there was a problem stapling the ticket to application.')
with msg_printer('Creating installation dmg...'):
try:
import dmgbuild
execute('tiffutil '
'-cat '
'artwork/background.tiff artwork/[email protected] '
'-out '
'artwork/pingnoo_background.tiff',
fail_msg='there was a problem creating the combined tiff.')
defines = {'application_binary': f'bin/{target_arch}/Deploy/Pingnoo.app',
'background_image': 'artwork/pingnoo_background.tiff'}
dmgbuild.build_dmg(volume_name='Pingnoo',
filename=f'bin/{target_arch}/Deploy/Pingnoo.dmg',
settings_file='installer/dmg-settings.py',
defines=defines,
lookForHiDPI=True)
except Exception as err:
raise MsgPrinterException(f'there was a problem creating the dmg.\r\n\r\n'+str(err))
# sign the dmg and notarize it
with msg_printer('Signing dmg...'):
result_code, result_output = mac_sign_binary(f'./bin/{target_arch}/Deploy/Pingnoo.dmg', args.cert)
if result_code:
raise MsgPrinterException(f'there was a problem signing the dmg.\r\n\r\n{result_output}\r\n')
with msg_printer('Performing notarization of installation dmg...'):
status = notarize_file(f'bin/{target_arch}/Deploy/Pingnoo.dmg', args.appleid, args.password)
if not status == "success":
raise MsgPrinterException(f'there was a problem notarizing the dmg ({status}).')
with msg_printer('Stapling notarization ticket to dmg...'):
execute(f'xcrun stapler staple bin/{target_arch}/Deploy/Pingnoo.dmg',
fail_msg='there was a problem stapling the ticket to dmg.')
with msg_printer('Copying dmg to deployment directory...'):
build_filename = f'deployment/Pingnoo.{build_version}.{target_arch}.dmg'
shutil.copy2(f'bin/{target_arch}/Deploy/Pingnoo.dmg', build_filename)
print(f'\r\n' + Style.BRIGHT + Fore.CYAN + f'Disk Image at \"deployment/{build_filename}\" is ' +
Fore.GREEN + 'ready' + Fore.CYAN + ' for distribution.', flush=True)
def _do_linux():
""" Linux version """
if not any([args.appimage, args.deb, args.rpm, args.pkg]):
print('You must select at least one type of output: appimage, rpm, deb, pkg')
sys.exit(1)
with msg_printer('Checking for curl...'):
if args.curlbin and os.path.isfile(args.curlbin):
curl = args.curlbin
else:
curl = which('curl')
if not curl:
raise MsgPrinterException('curl could not be found. (see --curlbin).')
if args.appimage or args.deb:
# RPM building has its own Qt macros
qtdir = find_qt()
else:
qtdir = None
deployed_message = ""
linux_deploy_qt = args.linuxdeployqt
if args.appimage:
# here we check if we need linuxdeploy qt for the deployment, if so we check if it's been supplied or whether
# we need to download it. This if can be expanded to any other builds which require linuxdeployqt
if not linux_deploy_qt or not os.path.isfile(linux_deploy_qt):
with msg_printer('Downloading linuxdeployqt...'):
if os.path.exists('tools/linuxdeployqt'):
rm_path('tools/linuxdeployqt')
os.mkdir('tools/linuxdeployqt')
execute('cd tools/linuxdeployqt; '
'curl -LJO '
'https://github.com/probonopd/linuxdeployqt/releases/download/6/'
'linuxdeployqt-6-x86_64.AppImage',
fail_msg='unable to download linuxdeployqt.')
execute('chmod +x tools/linuxdeployqt/linuxdeployqt-6-x86_64.AppImage',
fail_msg='unable to set permissions on linuxdeployqt.')
linux_deploy_qt = 'tools/linuxdeployqt/linuxdeployqt-6-x86_64.AppImage'
if not os.path.isfile(linux_deploy_qt):
bad_msg("> No valid linuxdeployqt could be found.")
_, result_output = execute('ldd --version')
ldd_regex = re.compile(r"^ldd\s\(.*\)\s(?P<version>.*)$", re.MULTILINE)
match_result = ldd_regex.match(result_output)
if not match_result:
bad_msg("> Skipping AppImage deployment, unable to get glibc version.")
else:
glibc_version = float(match_result.group("version"))
if glibc_version > 2.23:
args.appimage = False # TODO: Set a flag and return errorlevel != 0?
bad_msg("> Skipping AppImage deployment, glibc is too new..")
if args.appimage:
appimage_tool = args.appimagetool
if not appimage_tool or not os.path.isfile(appimage_tool):
with msg_printer('Downloading appimagetool...'):
if not os.path.exists('tools/appimagetool'):
rm_path('tools/appimagetool')
os.mkdir('tools/appimagetool')
execute('cd tools/appimagetool; '
'curl -LJO '
'https://github.com/AppImage/AppImageKit/releases/download/continuous/'
'appimagetool-x86_64.AppImage', fail_msg='unable to download appimagetool.')
execute('chmod +x tools/appimagetool/appimagetool-x86_64.AppImage',
fail_msg='unable to set permissions on appimagetool.')
appimage_tool = 'tools/appimagetool/appimagetool-x86_64.AppImage'
if not os.path.isfile(appimage_tool):
bad_msg("> No valid appimagetool could be found.")
# remove previous deployment files and copy current binaries
with msg_printer('Setting up deployment directory...'):
rm_path(f'bin/{build_arch}/Deploy/AppImage/')
rm_path('deployment')
os.makedirs('deployment')
os.makedirs(f'bin/{build_arch}/Deploy/AppImage/usr/bin')
os.makedirs(f'bin/{build_arch}/Deploy/AppImage/usr/lib')
os.makedirs(f'bin/{build_arch}/Deploy/AppImage/usr/share/icons/hicolor/128x128/apps')
os.makedirs(f'bin/{build_arch}/Deploy/AppImage/usr/share/applications')
shutil.copy2(f'bin/{build_arch}/{build_type}/Pingnoo',
f'bin/{build_arch}/Deploy/AppImage/usr/bin')
shutil.copy2('installer/Pingnoo.png',
f'bin/{build_arch}/Deploy/AppImage/usr/share/icons/hicolor/128x128/apps')
shutil.copy2('installer/Pingnoo.desktop',
f'bin/{build_arch}/Deploy/AppImage/usr/share/applications')
shutil.copy2('installer/AppRun', f'bin/{build_arch}/Deploy/AppImage/')
shutil.copytree(f'bin/{build_arch}/{build_type}/Components',
f'bin/{build_arch}/Deploy/AppImage/Components', symlinks=True)
build_parts = args.version.split('-', 1)
if not int(len(build_parts)) == 2:
linux_build_version = "0.0.0"
else:
linux_build_version = build_parts[0][2:]
with open("installer/Pingnoo.desktop.in", 'r') as desktop_file:
desktop_template = string.Template(desktop_file.read())
# use control.in template to create the deb control file
desktop_file_content = desktop_template.substitute(
executable="Pingnoo",
icon="Pingnoo",
version=linux_build_version)
with open(f'bin/{build_arch}/Deploy/AppImage/usr/share/applications/Pingnoo.desktop', 'w') as desktop_file:
desktop_file.write(desktop_file_content)
for file in glob.glob(f'bin/{build_arch}/{build_type}/*.so'):
shutil.copy2(file, f'bin/{build_arch}/Deploy/AppImage/usr/lib')
# create the app dir
with msg_printer('Running linuxdeployqt...'):
execute(f'{linux_deploy_qt} '
f'\'bin/{build_arch}/Deploy/AppImage/usr/share/applications/Pingnoo.desktop\' '
f'-qmake=\'{qtdir}/bin/qmake\' '
f'-bundle-non-qt-libs '
f'-exclude-libs=\'libqsqlodbc,libqsqlpsql\'',
fail_msg='there was a problem running linuxdeployqt.')
# create the AppImage
sign_parameters = ''
if args.cert:
sign_parameters = f'-s --sign-key={args.cert} '
with msg_printer('Creating AppImage...'):
build_filename = f'Pingnoo.{build_version}.{build_arch}.AppImage'
execute(f'{appimage_tool} -g {sign_parameters} '
f'bin/{build_arch}/Deploy/AppImage \"deployment/{build_filename}\"',
fail_msg='there was a problem creating the AppImage.')
deployed_message += '\r\n' + Style.BRIGHT + Fore.CYAN + \
f'AppImage at \"deployment/{build_filename}\" is ' + Fore.GREEN + 'ready' + \
Fore.CYAN + ' for distribution.'
if args.deb:
write_msg('> Creating deb package...')
deb_arch = "all"
if args.arch == 'x86_64':
deb_arch = "amd64"
deb_version = build_version.replace('/', '.')
issue_parts = open('/etc/issue').readline().lower().strip().split(' ')
distro = issue_parts[0]
if distro == "ubuntu":
release_parts = issue_parts[1].split('.')
major = release_parts[0]
minor = release_parts[1]
deb_distro = f'{distro}{major}.{minor}'
elif distro == "debian":
release = issue_parts[2]
deb_distro = f'{distro}{release}'
elif distro == "raspbian":
release = issue_parts[2]
deb_distro = f'{distro}{release}'
else:
deb_distro = 'unknown'
version_parts = deb_version.split('-', 1)
build_filename = f'deployment/pingnoo_{deb_version}-{deb_distro}_{deb_arch}.deb'
if int(len(version_parts)) == 2:
deb_version = version_parts[0][2:]
try:
if deb_create(build_arch, build_type, deb_version, build_filename, args.cert, args.extra_packages):
raise RuntimeError("deb creation unknown error")
deployed_message += '\r\n' + Style.BRIGHT + Fore.CYAN + \
f'deb package at \"{build_filename}\" is ' + Fore.GREEN + 'ready' + Fore.CYAN + \
' for distribution.'
except Exception as err:
print(f"Failed building deb: {type(err).__name__}: {err}")
if args.rpm:
write_msg('> Creating rpm package...')
rpm_version = build_version.replace('/', '.')
rpm_release = 1 # TODO: CLI argument
rpm_name = rpm_create(build_arch, build_type, rpm_version, rpm_release, args.cert)
build_filename = f'bin/{build_arch}/Deploy/rpm/{rpm_name}'
deployed_message += '\r\n' + Style.BRIGHT + Fore.CYAN + \
f'rpm package at \"{build_filename}\" is ' + Fore.GREEN + 'ready' + Fore.CYAN + \
' for distribution.'
if args.pkg:
write_msg('> Creating pkg package...')
pkg_version = build_version.replace('/', '.')
pkg_create(build_arch, build_type, pkg_version, args.cert)
build_filename = f'deployment/pingnoo-{pkg_version}-1-{build_arch}.pkg.tar.zst'
deployed_message += '\r\n' + Style.BRIGHT + Fore.CYAN + \
f'pkg package at \"{build_filename}\" is ' + Fore.GREEN + 'ready' + Fore.CYAN + \
' for distribution.'
print(deployed_message, flush=True)
def file_matched(file, expressions):
for expression in expressions:
if re.match(expression, file):
return True
return False
def add_files_to_zip(zip_file, root_path, exclusions=None):
if exclusions is None:
exclusions = []
for root, dirs, files in os.walk(root_path):
for file in files:
if file_matched(file, exclusions):
continue
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, root_path)
zip_file.write(os.path.join(root, file), arcname=rel_path)
def _do_windows():
""" Windows version """
def win_sign_binary(signingtool, filetosign, certificate, timeserver, pin=None):
# TODO: Move to new execute
# FIXME: cert parameter was unused?
if pin:
pin = "-pin " + pin
if args.debugoutput:
print(f'sign command {signingtool} {pin} sign /fd sha256 /t {timeserver} /n {certificate} {filetosign}')
return execute(f'{signingtool} {pin} sign /fd sha256 /t {timeserver} /n {certificate} {filetosign}')
with msg_printer('Checking for curl...'):
if args.curlbin and os.path.isfile(args.curlbin):
curl = args.curlbin
else:
curl = which('curl.exe')
if not curl:
raise MsgPrinterException('curl could not be found. (see --curlbin).')
pin_code = None
if args.pin:
pin_code = args.pin
else:
if os.environ.get('PINGNOO_CERTIFICATE_PIN'):
pin_code = os.environ.get('PINGNOO_CERTIFICATE_PIN')
cert = None
if args.cert:
cert = args.cert
else:
if os.environ.get('PINGNOO_DEVELOPER_CERTIFICATE'):
cert = os.environ.get('PINGNOO_DEVELOPER_CERTIFICATE')
tempdir = os.path.normpath(tempfile.mkdtemp())
signtool = args.signtool
if cert:
if not os.path.exists(signtool):
with msg_printer('Downloading SmartCardTools...'):
execute(f'cd \"{tempdir}\" && \"{curl}\"'
f' -LJO https://www.mgtek.com/files/smartcardtools.zip',
fail_msg='unable to download SmartCardTools.')
execute(f'cd \"{tempdir}\" && \"{curl}\"'
f' -LJO ftp://ftp.info-zip.org/pub/infozip/win32/unz600xn.exe'
f' & unz600xn -jo unzip.exe',
fail_msg='unable to download info-zip tools.')
execute(f'\"{tempdir}\\unzip\" \"{tempdir}'
f'\\smartcardtools.zip\" -d tools\\smartcardtools',
fail_msg='unable to unzip SmartCardTools.')
signtool = 'tools\\smartcardtools\\x64\\ScSignTool.exe'
# if universal, then we run the deployment stages once for each arch, otherwise just once.
if args.arch == "universal":
architectures = ['x86_64', 'x86']
else:
architectures = [args.arch]
qtdirs = {'x86_64': args.qtdir64, 'x86': args.qtdir}
rm_path('deployment')
os.makedirs('deployment')
for current_build_arch in architectures:
if current_build_arch == "x86_64":
windows_arch = "x64"
else:
windows_arch = "x86"
# check for qt installation
with msg_printer(f'Checking {current_build_arch} qtdir...'):
if qtdirs[current_build_arch] and os.path.isfile(f'{qtdirs[current_build_arch]}\\bin\\windeployqt.exe'):
windeployqt = f'{qtdirs[current_build_arch]}\\bin\\windeployqt.exe'
else:
windeployqt = which('windeployqt')
if not windeployqt:
raise MsgPrinterException('qt could not be found. (see --qtdir).')
# remove previous deployment files and copy current binaries
with msg_printer(f'Setting up {current_build_arch} deployment directory...'):
deploy_dir = f'bin\\{current_build_arch}\\Deploy'
binary_dir = f'bin\\{current_build_arch}\\{build_type}'
extensions = ['.exe', '.dll']
rm_path(deploy_dir)
sign_list = []
os.makedirs(deploy_dir)
# TODO: Refactor with os.walk
for file in glob.glob(f'{binary_dir}\\**\\*', recursive=True):
_, extension = os.path.splitext(file)
if os.path.isdir(file):
os.makedirs(file.replace(binary_dir, deploy_dir, 1))
if extension in extensions:
dest_file = file.replace(binary_dir, deploy_dir, 1)
shutil.copy2(file, dest_file)
sign_list.append(dest_file)
files = []
for extension in extensions:
files += glob.glob(f'{deploy_dir}\\*{extension}')
if not files:
raise MsgPrinterException('no files could be found to deploy.')
# sign the application binaries
if cert:
with msg_printer(f'Signing {current_build_arch} binaries...'):
for file in sign_list:
result_code, result_output = win_sign_binary(signtool, file, cert, args.timeserver, pin_code)
if result_code:
raise MsgPrinterException(
f'there was a problem signing a file ({file}).\r\n\r\n{result_output}\r\n')
files_string = ' '.join(files)
# run windeployqt
with msg_printer(f'Deploying {current_build_arch} qt libraries...'):
execute(f'{windeployqt} --dir {deploy_dir} {files_string} -sql --{args.type}',
fail_msg='there was a problem running windeployqt.')
if args.portable:
with msg_printer(f'Creating {current_build_arch} portable edition...'):
zip_file = zipfile.ZipFile(
f'.\\deployment\\Pingnoo.{build_version}.windows-portable.{current_build_arch}.zip', "w")
# portable edition is detected by pingnoo if a folder named data exists in the same folder as the exe
zip_file_info = zipfile.ZipInfo("data/")
zip_file.writestr(zip_file_info, '')
# copy the deployed folder to the zip file
add_files_to_zip(zip_file, f'bin\\{current_build_arch}\\Deploy\\', ['vc_redist.*'])
# copy the visual studio redistributable to the zip
redist_dir = os.getenv('VCToolsRedistDir')
add_files_to_zip(zip_file, f'{redist_dir}\\{windows_arch}\\Microsoft.VC142.CRT')
# copy the universal crt files (doesn't appear to be needed - by for completeness)
sdk_dir = os.getenv('WindowsSdkDir')
sdk_version = os.getenv('WindowsSDKLibVersion')
add_files_to_zip(zip_file, f'{sdk_dir}\\Redist\\{sdk_version}\\ucrt\\DLLs\\{windows_arch}')
zip_file.close()
# run advanced installer
with msg_printer('Creating installer...'):
rm_file('installer\\PingnooBuild.aip')
# use python templating to set the pin in the aip file as it can't lookup an environment variable directly
with open("installer\\Pingnoo.aip", 'r') as installer_file:
installer_template = string.Template(installer_file.read())
installer_file_content = installer_template.substitute(pinCode=f'{pin_code}')
with open('installer\\PingnooBuild.aip', 'w') as out_installer_file:
out_installer_file.write(installer_file_content)
build_parts = args.version.split('-', 1)
if int(len(build_parts)) != 2:
win_build_version = "0.0.0"
else:
win_build_version = build_parts[0][2:]
build_filename = f'Pingnoo.{build_version}.exe'
execute(f'AdvancedInstaller.com /edit installer\\PingnooBuild.aip /SetVersion {win_build_version}',
fail_msg='there was a problem creating the installer.')
execute((
f'AdvancedInstaller.com /edit installer\\PingnooBuild.aip /SetPackageName '
f'"{build_filename}" -buildname MsiBuild'),
fail_msg='there was a problem creating the installer.')
execute(f'AdvancedInstaller.com /build installer\\PingnooBuild.aip',
fail_msg='there was a problem creating the installer.')
if args.cert:
with msg_printer('Signing installer...'):
result_code, result_output = win_sign_binary(signtool, f'deployment\\{build_filename}', args.cert,
args.timeserver,
args.pin)
if result_code:
raise MsgPrinterException(f'there was a problem signing the installer.\r\n\r\n{result_output}\r\n')
print(f'\r\n' + Style.BRIGHT + Fore.CYAN + f'Installer at \"deployment\\{build_filename}\" is ' +
Fore.GREEN + 'ready' + Fore.CYAN + ' for distribution.', flush=True)
print(Style.BRIGHT + 'Deployment process started at ' + str(datetime.datetime.now()) + '\r\n', flush=True)
start_time = time.time()
if platform.system() == "Windows":
_do_windows()
elif platform.system() == "Linux":
_do_linux()
elif platform.system() == "Darwin":
_do_darwin()
else:
raise RuntimeError("Unknown OS!")
end_time = time.time()
print(Style.BRIGHT + f'\r\nTotal time taken to perform deployment was ' +
timedelta(end_time - start_time) + '.', flush=True)
sys.exit(0)