File: buildPy2exe.py

package info (click to toggle)
syncplay 1.7.4%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,676 kB
  • sloc: python: 19,824; sh: 32; makefile: 16
file content (695 lines) | stat: -rwxr-xr-x 25,214 bytes parent folder | download
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
#!/usr/bin/env python3
#coding:utf8


# *** TROUBLESHOOTING ***
# 1) If you get the error "ImportError: No module named zope.interface" then add an empty __init__.py file to the PYTHONDIR/Lib/site-packages/zope directory
# 2) It is expected that you will have NSIS 3 NSIS from http://nsis.sourceforge.net installed.

import codecs
import sys
# try:
#     if (sys.version_info.major != 2) or (sys.version_info.minor < 7):
#         raise Exception("You must build Syncplay with Python 2.7!")
# except AttributeError:
#     import warnings
#     warnings.warn("You must build Syncplay with Python 2.7!")

from glob import glob
import os
import subprocess
from string import Template

from distutils.core import setup
try:
    from py2exe.build_exe import py2exe
except ImportError:
    from py2exe.distutils_buildexe import py2exe

import syncplay
from syncplay.messages import getMissingStrings, getMessage, getLanguages

missingStrings = getMissingStrings()
if missingStrings is not None and missingStrings != "":
    import warnings
    warnings.warn("MISSING/UNUSED STRINGS DETECTED:\n{}".format(missingStrings))

def get_nsis_path():
    bin_name = "makensis.exe"
    from winreg import HKEY_LOCAL_MACHINE as HKLM
    from winreg import KEY_READ, KEY_WOW64_32KEY, OpenKey, QueryValueEx

    try:
        nsisreg = OpenKey(HKLM, "Software\\NSIS", 0, KEY_READ | KEY_WOW64_32KEY)
        if QueryValueEx(nsisreg, "VersionMajor")[0] >= 3:
            return "{}\\{}".format(QueryValueEx(nsisreg, "")[0], bin_name)
        else:
            raise Exception("You must install NSIS 3 or later.")
    except WindowsError:
        return bin_name

NSIS_COMPILE = get_nsis_path()

OUT_DIR = "syncplay_v{}".format(syncplay.version)
SETUP_SCRIPT_PATH = "syncplay_setup.nsi"

languages = getLanguages()

def getLangTagFromNLF(lang):
    return "LANG_" + getMessage("installer-language-file", lang).upper().replace(".NLF","").replace("_","")


# Load languages
loadLanguageFileString = ""
for lang in languages:
    lineToAdd = "LoadLanguageFile \"$${{NSISDIR}}\\Contrib\\Language files\\{}\"".format(getMessage("installer-language-file", lang))
    loadLanguageFileString = loadLanguageFileString + "\r\n" + lineToAdd

# Add Version Keys
versionKeysString = ""
for lang in languages:
    languageIdent = getLangTagFromNLF(lang)
    lineToAdd = r"""  VIAddVersionKey /LANG=$${LANG_IDENT} "ProductName" "Syncplay"
  VIAddVersionKey /LANG=$${LANG_IDENT} "FileVersion" "$version.0"
  VIAddVersionKey /LANG=$${LANG_IDENT} "LegalCopyright" "Syncplay"
  VIAddVersionKey /LANG=$${LANG_IDENT} "FileDescription" "Syncplay"
  """.replace("LANG_IDENT", languageIdent)
    versionKeysString = versionKeysString + "\r\n" + lineToAdd

# Add Language Strings
languageString = ""
for lang in languages:
    languageIdent = getLangTagFromNLF(lang)

    # dict_dict = {'dict1':dict1, 'dicta':dicta, 'dict666':dict666}
    #
    # for name,dict_ in dict_dict.items():
    #     print 'the name of the dictionary is ', name
    #     print 'the dictionary looks like ', dict_

    langStringDict = {
        # "[NSIS key name]": "[messages_*.py key name]"
        "SyncplayLanguage": "LANGUAGE-TAG",
        "Associate": "installer-associate",
        "Shortcut": "installer-shortcut",
        "StartMenu": "installer-start-menu",
        "Desktop": "installer-desktop",
        "QuickLaunchBar": "installer-quick-launch-bar",
        "AutomaticUpdates": "installer-automatic-updates",
        "UninstConfig": "installer-uninstall-configuration"
    }
    for nsisKey, messageKey in langStringDict.items():
        nsisValue = getMessage(messageKey, lang)
        lineToAdd = "  LangString ^" + nsisKey + " $${" + languageIdent + "} \"" + nsisValue + "\""
        languageString = languageString + "\r\n" + lineToAdd
    languageString = languageString + "\r\n"

# Add Language Pushs
languagePushString = ""
for lang in languages:
    languageIdent = getLangTagFromNLF(lang)
    languagePushString = languagePushString + "Push $${" + languageIdent + "}\r\n"
    languagePushString = languagePushString + "Push '" + getMessage("LANGUAGE", lang) + "'\r\n"

NSIS_SCRIPT_TEMPLATE = r"""
  !include LogicLib.nsh
  !include nsDialogs.nsh
  !include FileFunc.nsh

""" + loadLanguageFileString + r"""
 
  Unicode true

  Name "Syncplay $version"
  OutFile "Syncplay-$version-Setup.exe"
  InstallDir $$PROGRAMFILES\Syncplay
  RequestExecutionLevel admin
  ManifestDPIAware false
  XPStyle on
  Icon syncplay\resources\icon.ico ;Change DIR
  SetCompressor /SOLID lzma

  VIProductVersion "$version.0"
  
  """ + versionKeysString + languageString + r"""
  ; Remove text to save space
  LangString ^ClickInstall $${LANG_GERMAN} " "

  PageEx license
    LicenseData syncplay\resources\license.rtf
  PageExEnd
  Page custom DirectoryCustom DirectoryCustomLeave
  Page instFiles

  UninstPage custom un.installConfirm un.installConfirmLeave
  UninstPage instFiles

  Var Dialog
  Var Icon_Syncplay
  Var Icon_Syncplay_Handle
  ;Var CheckBox_Associate
  Var CheckBox_AutomaticUpdates
  Var CheckBox_StartMenuShortcut
  Var CheckBox_DesktopShortcut
  Var CheckBox_QuickLaunchShortcut
  ;Var CheckBox_Associate_State
  Var CheckBox_AutomaticUpdates_State
  Var CheckBox_StartMenuShortcut_State
  Var CheckBox_DesktopShortcut_State
  Var CheckBox_QuickLaunchShortcut_State
  Var Button_Browse
  Var Directory
  Var GroupBox_DirSub
  Var Label_Text
  Var Label_Shortcut
  Var Label_Size
  Var Label_Space
  Var Text_Directory

  Var Uninst_Dialog
  Var Uninst_Icon
  Var Uninst_Icon_Handle
  Var Uninst_Label_Directory
  Var Uninst_Label_Text
  Var Uninst_Text_Directory
  Var Uninst_CheckBox_Config
  Var Uninst_CheckBox_Config_State

  Var Size
  Var SizeHex
  Var AvailibleSpace
  Var AvailibleSpaceGiB
  Var Drive
  Var VLC_Directory

  ;!macro APP_ASSOCIATE EXT FileCLASS DESCRIPTION COMMANDTEXT COMMAND
  ;  WriteRegStr HKCR ".$${EXT}" "" "$${FileCLASS}"
  ;  WriteRegStr HKCR "$${FileCLASS}" "" `$${DESCRIPTION}`
  ;  WriteRegStr HKCR "$${FileCLASS}\shell" "" "open"
  ;  WriteRegStr HKCR "$${FileCLASS}\shell\open" "" `$${COMMANDTEXT}`
  ;  WriteRegStr HKCR "$${FileCLASS}\shell\open\command" "" `$${COMMAND}`
  ;!macroend

  !macro APP_UNASSOCIATE EXT FileCLASS
    ; Backup the previously associated File class
    ReadRegStr $$R0 HKCR ".$${EXT}" `$${FileCLASS}_backup`
    WriteRegStr HKCR ".$${EXT}" "" "$$R0"
    DeleteRegKey HKCR `$${FileCLASS}`
  !macroend

  ;!macro ASSOCIATE EXT
  ;  !insertmacro APP_ASSOCIATE "$${EXT}" "Syncplay.$${EXT}" "$$INSTDIR\Syncplay.exe,%1%" \
  ;  "Open with Syncplay" "$$INSTDIR\Syncplay.exe $$\"%1$$\""
  ;!macroend

  !macro UNASSOCIATE EXT
    !insertmacro APP_UNASSOCIATE "$${EXT}" "Syncplay.$${EXT}"
  !macroend

  ;Prevents from running more than one instance of installer and sets default state of checkboxes
  Function .onInit
    System::Call 'kernel32::CreateMutexA(i 0, i 0, t "SyncplayMutex") i .r1 ?e'
    Pop $$R0
    StrCmp $$R0 0 +3
    MessageBox MB_OK|MB_ICONEXCLAMATION "The installer is already running."
      Abort

    ;StrCpy $$CheckBox_Associate_State $${BST_CHECKED}
    StrCpy $$CheckBox_StartMenuShortcut_State $${BST_CHECKED}

    Call GetSize
    Call DriveSpace

    $${GetParameters} $$0
    ClearErrors
    $${GetOptions} $$0 "/LANG=" $$0
    $${IfNot} $${Errors}
    $${AndIf} $$0 <> 0
      StrCpy $$LANGUAGE $$0
    $${Else}
      Call Language
    $${EndIf}
  FunctionEnd

  ;Language selection dialog
  Function Language
    Push ""
    """ + languagePushString + r"""
    Push A ; A means auto count languages
    LangDLL::LangDialog "Language Selection" "Please select the language of Syncplay and the installer"
    Pop $$LANGUAGE
    StrCmp $$LANGUAGE "cancel" 0 +2
      Abort
  FunctionEnd

  Function DirectoryCustom

    nsDialogs::Create 1018
    Pop $$Dialog

    GetFunctionAddress $$R8 DirectoryCustomLeave
    nsDialogs::OnBack $$R8

    $${NSD_CreateIcon} 0u 0u 22u 20u ""
    Pop $$Icon_Syncplay
    $${NSD_SetIconFromInstaller} $$Icon_Syncplay $$Icon_Syncplay_Handle

    $${NSD_CreateLabel} 25u 0u 241u 34u "$$(^DirText)"
    Pop $$Label_Text

    $${NSD_CreateText} 8u 38u 187u 12u "$$INSTDIR"
    Pop $$Text_Directory
    $${NSD_SetFocus} $$Text_Directory

    $${NSD_CreateBrowseButton} 202u 37u 55u 14u "$$(^BrowseBtn)"
    Pop $$Button_Browse
    $${NSD_OnClick} $$Button_Browse DirectoryBrowseDialog

    $${NSD_CreateGroupBox} 1u 27u 264u 30u "$$(^DirSubText)"
    Pop $$GroupBox_DirSub

    $${NSD_CreateLabel} 0u 122u 132 8u "$$(^SpaceRequired)$$SizeMB"
    Pop $$Label_Size

    $${NSD_CreateLabel} 321u 122u 132 8u "$$(^SpaceAvailable)$$AvailibleSpaceGiB.$$AvailibleSpaceGB"
    Pop $$Label_Space

    ;$${NSD_CreateCheckBox} 8u 59u 187u 10u "$$(^Associate)"
    ;Pop $$CheckBox_Associate

    $${NSD_CreateCheckBox} 8u 72u 250u 10u "$$(^AutomaticUpdates)"
    Pop $$CheckBox_AutomaticUpdates
    $${NSD_Check} $$CheckBox_AutomaticUpdates

    $${NSD_CreateLabel} 8u 95u 187u 10u "$$(^Shortcut)"
    Pop $$Label_Shortcut

    $${NSD_CreateCheckbox} 8u 105u 70u 10u "$$(^StartMenu)"
    Pop $$CheckBox_StartMenuShortcut

    $${NSD_CreateCheckbox} 78u 105u 70u 10u "$$(^Desktop)"
    Pop $$CheckBox_DesktopShortcut

    $${NSD_CreateCheckbox} 158u 105u 130u 10u "$$(^QuickLaunchBar)"
    Pop $$CheckBox_QuickLaunchShortcut

    ;$${If} $$CheckBox_Associate_State == $${BST_CHECKED}
    ;  $${NSD_Check} $$CheckBox_Associate
    ;$${EndIf}


    $${If} $$CheckBox_StartMenuShortcut_State == $${BST_CHECKED}
      $${NSD_Check} $$CheckBox_StartMenuShortcut
    $${EndIf}

    $${If} $$CheckBox_DesktopShortcut_State == $${BST_CHECKED}
      $${NSD_Check} $$CheckBox_DesktopShortcut
    $${EndIf}

    $${If} $$CheckBox_QuickLaunchShortcut_State == $${BST_CHECKED}
      $${NSD_Check} $$CheckBox_QuickLaunchShortcut
    $${EndIf}

    $${If} $$CheckBox_AutomaticUpdates_State == $${BST_CHECKED}
      $${NSD_Check} $$CheckBox_AutomaticUpdates
    $${EndIf}

    nsDialogs::Show

    $${NSD_FreeIcon} $$Icon_Syncplay_Handle

  FunctionEnd

  Function DirectoryCustomLeave
    $${NSD_GetText} $$Text_Directory $$INSTDIR
    ;$${NSD_GetState} $$CheckBox_Associate $$CheckBox_Associate_State
    $${NSD_GetState} $$CheckBox_AutomaticUpdates $$CheckBox_AutomaticUpdates_State
    $${NSD_GetState} $$CheckBox_StartMenuShortcut $$CheckBox_StartMenuShortcut_State
    $${NSD_GetState} $$CheckBox_DesktopShortcut $$CheckBox_DesktopShortcut_State
    $${NSD_GetState} $$CheckBox_QuickLaunchShortcut $$CheckBox_QuickLaunchShortcut_State
  FunctionEnd

  Function DirectoryBrowseDialog
    nsDialogs::SelectFolderDialog $$(^DirBrowseText)
    Pop $$Directory
    $${If} $$Directory != error
    StrCpy $$INSTDIR $$Directory
    $${NSD_SetText} $$Text_Directory $$INSTDIR
    Call DriveSpace
    $${NSD_SetText} $$Label_Space "$$(^SpaceAvailable)$$AvailibleSpaceGiB.$$AvailibleSpaceGB"
    $${EndIf}
    Abort
  FunctionEnd

  Function GetSize
    StrCpy $$Size "$totalSize"
    IntOp $$Size $$Size / 1024
    IntFmt $$SizeHex "0x%08X" $$Size
    IntOp $$Size $$Size / 1024
  FunctionEnd

  ;Calculates Free Space on HDD
  Function DriveSpace
    StrCpy $$Drive $$INSTDIR 1
    $${DriveSpace} "$$Drive:\" "/D=F /S=M" $$AvailibleSpace
    IntOp $$AvailibleSpaceGiB $$AvailibleSpace / 1024
    IntOp $$AvailibleSpace $$AvailibleSpace % 1024
    IntOp $$AvailibleSpace $$AvailibleSpace / 102
  FunctionEnd

  Function InstallOptions
    ;$${If} $$CheckBox_Associate_State == $${BST_CHECKED}
    ;  Call Associate
    ;  DetailPrint "Associated Syncplay with multimedia files"
    ;$${EndIf}

    $${If} $$CheckBox_StartMenuShortcut_State == $${BST_CHECKED}
      CreateDirectory $$SMPROGRAMS\Syncplay
      SetOutPath "$$INSTDIR"
      CreateShortCut "$$SMPROGRAMS\Syncplay\Syncplay.lnk" "$$INSTDIR\Syncplay.exe" ""
      CreateShortCut "$$SMPROGRAMS\Syncplay\Syncplay Server.lnk" "$$INSTDIR\syncplayServer.exe" ""
      CreateShortCut "$$SMPROGRAMS\Syncplay\Uninstall.lnk" "$$INSTDIR\Uninstall.exe" ""
      WriteINIStr "$$SMPROGRAMS\Syncplay\SyncplayWebsite.url" "InternetShortcut" "URL" "https://syncplay.pl"
    $${EndIf}

    $${If} $$CheckBox_DesktopShortcut_State == $${BST_CHECKED}
      SetOutPath "$$INSTDIR"
      CreateShortCut "$$DESKTOP\Syncplay.lnk" "$$INSTDIR\Syncplay.exe" ""
    $${EndIf}

    $${If} $$CheckBox_QuickLaunchShortcut_State == $${BST_CHECKED}
      SetOutPath "$$INSTDIR"
      CreateShortCut "$$QUICKLAUNCH\Syncplay.lnk" "$$INSTDIR\Syncplay.exe" ""
    $${EndIf}
  FunctionEnd

  ;Associates extensions with Syncplay
  ;Function Associate
  ;  !insertmacro ASSOCIATE avi
  ;  !insertmacro ASSOCIATE mpg
  ;  !insertmacro ASSOCIATE mpeg
  ;  !insertmacro ASSOCIATE mpe
  ;  !insertmacro ASSOCIATE m1v
  ;  !insertmacro ASSOCIATE m2v
  ;  !insertmacro ASSOCIATE mpv2
  ;  !insertmacro ASSOCIATE mp2v
  ;  !insertmacro ASSOCIATE mkv
  ;  !insertmacro ASSOCIATE mp4
  ;  !insertmacro ASSOCIATE m4v
  ;  !insertmacro ASSOCIATE mp4v
  ;  !insertmacro ASSOCIATE 3gp
  ;  !insertmacro ASSOCIATE 3gpp
  ;  !insertmacro ASSOCIATE 3g2
  ;  !insertmacro ASSOCIATE 3pg2
  ;  !insertmacro ASSOCIATE flv
  ;  !insertmacro ASSOCIATE f4v
  ;  !insertmacro ASSOCIATE rm
  ;  !insertmacro ASSOCIATE wmv
  ;  !insertmacro ASSOCIATE swf
  ;  !insertmacro ASSOCIATE rmvb
  ;  !insertmacro ASSOCIATE divx
  ;  !insertmacro ASSOCIATE amv
  ;FunctionEnd

  Function WriteRegistry
    Call GetSize
    WriteRegStr HKLM SOFTWARE\Syncplay "Install_Dir" "$$INSTDIR"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "DisplayName" "Syncplay"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "InstallLocation" "$$INSTDIR"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "UninstallString" '"$$INSTDIR\uninstall.exe"'
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "DisplayIcon" "$$INSTDIR\resources\icon.ico"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "Publisher" "Syncplay"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "DisplayVersion" "$version"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "URLInfoAbout" "https://syncplay.pl/"
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "NoModify" 1
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "NoRepair" 1
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "EstimatedSize" "$$SizeHex"
    WriteINIStr $$APPDATA\syncplay.ini general language $$(^SyncplayLanguage)
    $${If} $$CheckBox_AutomaticUpdates_State == $${BST_CHECKED}
        WriteINIStr $$APPDATA\syncplay.ini general CheckForUpdatesAutomatically "True"
    $${Else}
        WriteINIStr $$APPDATA\syncplay.ini general CheckForUpdatesAutomatically "False"
    $${EndIf}
  FunctionEnd

  Function un.installConfirm
    nsDialogs::Create 1018
    Pop $$Uninst_Dialog

    $${NSD_CreateIcon} 0u 1u 22u 20u ""
    Pop $$Uninst_Icon
    $${NSD_SetIconFromInstaller} $$Uninst_Icon $$Uninst_Icon_Handle

    $${NSD_CreateLabel} 0u 45u 55u 8u "$$(^UninstallingSubText)"
    Pop $$Uninst_Label_Directory

    $${NSD_CreateLabel} 25u 0u 241u 34u "$$(^UninstallingText)"
    Pop $$Uninst_Label_Text

    ReadRegStr $$INSTDIR HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "InstallLocation"
    $${NSD_CreateText} 56u 43u 209u 12u "$$INSTDIR"
    Pop $$Uninst_Text_Directory
    EnableWindow $$Uninst_Text_Directory 0

    $${NSD_CreateCheckBox} 0u 60u 250u 10u "$$(^UninstConfig)"
    Pop $$Uninst_CheckBox_Config


    nsDialogs::Show
    $${NSD_FreeIcon} $$Uninst_Icon_Handle
  FunctionEnd

  Function un.installConfirmLeave
    $${NSD_GetState} $$Uninst_CheckBox_Config $$Uninst_CheckBox_Config_State
  FunctionEnd

  Function un.AssociateDel
    !insertmacro UNASSOCIATE avi
    !insertmacro UNASSOCIATE mpg
    !insertmacro UNASSOCIATE mpeg
    !insertmacro UNASSOCIATE mpe
    !insertmacro UNASSOCIATE m1v
    !insertmacro UNASSOCIATE m2v
    !insertmacro UNASSOCIATE mpv2
    !insertmacro UNASSOCIATE mp2v
    !insertmacro UNASSOCIATE mkv
    !insertmacro UNASSOCIATE mp4
    !insertmacro UNASSOCIATE m4v
    !insertmacro UNASSOCIATE mp4v
    !insertmacro UNASSOCIATE 3gp
    !insertmacro UNASSOCIATE 3gpp
    !insertmacro UNASSOCIATE 3g2
    !insertmacro UNASSOCIATE 3pg2
    !insertmacro UNASSOCIATE flv
    !insertmacro UNASSOCIATE f4v
    !insertmacro UNASSOCIATE rm
    !insertmacro UNASSOCIATE wmv
    !insertmacro UNASSOCIATE swf
    !insertmacro UNASSOCIATE rmvb
    !insertmacro UNASSOCIATE divx
    !insertmacro UNASSOCIATE amv
  FunctionEnd

  Function un.InstallOptions
    Delete $$SMPROGRAMS\Syncplay\Syncplay.lnk
    Delete "$$SMPROGRAMS\Syncplay\Syncplay Server.lnk"
    Delete $$SMPROGRAMS\Syncplay\Uninstall.lnk
    Delete $$SMPROGRAMS\Syncplay\SyncplayWebsite.url
    RMDir $$SMPROGRAMS\Syncplay
    Delete $$DESKTOP\Syncplay.lnk
    Delete $$QUICKLAUNCH\Syncplay.lnk
    ReadRegStr $$VLC_Directory HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay" "VLCInstallLocation"
    IfFileExists "$$VLC_Directory\lua\intf\syncplay.lua" 0 +2
    Delete $$VLC_Directory\lua\intf\syncplay.lua
  FunctionEnd

  Section "Install"
    SetOverwrite on
    SetOutPath $$INSTDIR
    WriteUninstaller uninstall.exe

    $installFiles

    Call InstallOptions
    Call WriteRegistry
  SectionEnd

  Section "Uninstall"
    Call un.AssociateDel
    Call un.InstallOptions
    $uninstallFiles
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Syncplay"
    DeleteRegKey HKLM SOFTWARE\Syncplay
    Delete $$INSTDIR\uninstall.exe
    RMDir $$INSTDIR\Syncplay\\resources\lua\intf
    RMDir $$INSTDIR\Syncplay\\resources\lua
    RMDir $$INSTDIR\Syncplay\\resources
    RMDir $$INSTDIR\resources
    RMDir $$INSTDIR\lib
    RMDir $$INSTDIR

    $${If} $$Uninst_CheckBox_Config_State == $${BST_CHECKED}
      IfFileExists "$$APPDATA\.syncplay" 0 +2
      Delete $$APPDATA\.syncplay
      IfFileExists "$$APPDATA\syncplay.ini" 0 +2
      Delete $$APPDATA\syncplay.ini
    $${EndIf}
  SectionEnd
"""

class NSISScript(object):
    def create(self):
        fileList, totalSize = self.getBuildDirContents(OUT_DIR)
        print("Total size eq: {}".format(totalSize))
        installFiles = self.prepareInstallListTemplate(fileList)
        uninstallFiles = self.prepareDeleteListTemplate(fileList)

        if os.path.isfile(SETUP_SCRIPT_PATH):
            raise RuntimeError("Cannot create setup script, file exists at {}".format(SETUP_SCRIPT_PATH))
        contents = Template(NSIS_SCRIPT_TEMPLATE).substitute(
            version=syncplay.version,
            uninstallFiles=uninstallFiles,
            installFiles=installFiles,
            totalSize=totalSize,
        )
        with codecs.open(SETUP_SCRIPT_PATH, "w", "utf-8-sig") as outfile:
            outfile.write(contents)

    def compile(self):
        if not os.path.isfile(NSIS_COMPILE):
            return "makensis.exe not found, won't create the installer"
        subproc = subprocess.Popen([NSIS_COMPILE, SETUP_SCRIPT_PATH], env=os.environ)
        subproc.communicate()
        retcode = subproc.returncode
        os.remove(SETUP_SCRIPT_PATH)
        if retcode:
            raise RuntimeError("NSIS compilation return code: %d" % retcode)

    def getBuildDirContents(self, path):
        fileList = {}
        totalSize = 0
        for root, _, files in os.walk(path):
            totalSize += sum(os.path.getsize(os.path.join(root, file_)) for file_ in files)
            for file_ in files:
                new_root = root.replace(OUT_DIR, "").strip("\\")
                if new_root not in fileList:
                    fileList[new_root] = []
                fileList[new_root].append(file_)
        return fileList, totalSize

    def prepareInstallListTemplate(self, fileList):
        create = []
        for dir_ in fileList.keys():
            create.append('SetOutPath "$INSTDIR\\{}"'.format(dir_))
            for file_ in fileList[dir_]:
                create.append('FILE "{}\\{}\\{}"'.format(OUT_DIR, dir_, file_))
        return "\n".join(create)

    def prepareDeleteListTemplate(self, fileList):
        delete = []
        for dir_ in fileList.keys():
            for file_ in fileList[dir_]:
                delete.append('DELETE "$INSTDIR\\{}\\{}"'.format(dir_, file_))
            delete.append('RMdir "$INSTDIR\\{}"'.format(file_))
        return "\n".join(delete)

def pruneUnneededLibraries():
    from pathlib import Path
    cwd = os.getcwd()
    libDir = cwd + '\\' + OUT_DIR + '\\lib\\'
    unneededModules = ['PySide2.Qt3D*', 'PySide2.QtAxContainer.pyd', 'PySide2.QtCharts.pyd', 'PySide2.QtConcurrent.pyd',
                       'PySide2.QtDataVisualization.pyd', 'PySide2.QtHelp.pyd', 'PySide2.QtLocation.pyd',
                       'PySide2.QtMultimedia.pyd', 'PySide2.QtMultimediaWidgets.pyd', 'PySide2.QtOpenGL.pyd',
                       'PySide2.QtPositioning.pyd', 'PySide2.QtPrintSupport.pyd', 'PySide2.QtQml.pyd',
                       'PySide2.QtQuick.pyd', 'PySide2.QtQuickWidgets.pyd', 'PySide2.QtScxml.pyd', 'PySide2.QtSensors.pyd',
                       'PySide2.QtSql.pyd', 'PySide2.QtSvg.pyd', 'PySide2.QtTest.pyd', 'PySide2.QtTextToSpeech.pyd',
                       'PySide2.QtUiTools.pyd', 'PySide2.QtWebChannel.pyd', 'PySide2.QtWebEngine.pyd',
                       'PySide2.QtWebEngineCore.pyd', 'PySide2.QtWebEngineWidgets.pyd', 'PySide2.QtWebSockets.pyd',
                       'PySide2.QtWinExtras.pyd', 'PySide2.QtXml.pyd', 'PySide2.QtXmlPatterns.pyd']
    unneededLibs = ['Qt53D*', 'Qt5Charts.dll', 'Qt5Concurrent.dll', 'Qt5DataVisualization.dll', 'Qt5Gamepad.dll', 'Qt5Help.dll',
                    'Qt5Location.dll', 'Qt5Multimedia.dll', 'Qt5MultimediaWidgets.dll', 'Qt5OpenGL.dll', 'Qt5Positioning.dll',
                    'Qt5PrintSupport.dll', 'Qt5Quick.dll', 'Qt5QuickWidgets.dll', 'Qt5Scxml.dll', 'Qt5Sensors.dll', 'Qt5Sql.dll',
                    'Qt5Svg.dll', 'Qt5Test.dll', 'Qt5TextToSpeech.dll', 'Qt5WebChannel.dll', 'Qt5WebEngine.dll',
                    'Qt5WebEngineCore.dll', 'Qt5WebEngineWidgets.dll', 'Qt5WebSockets.dll', 'Qt5WinExtras.dll', 'Qt5Xml.dll',
                    'Qt5XmlPatterns.dll']
    windowsDLL = ['MSVCP140.dll', 'VCRUNTIME140.dll']
    deleteList = unneededModules + unneededLibs + windowsDLL
    deleteList.append('api-*')
    for filename in deleteList:
        for p in Path(libDir).glob(filename):
            p.unlink()

def copyQtPlugins(paths):
    import shutil
    from PySide2 import QtCore
    basePath = QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.PluginsPath)
    basePath = basePath.replace('/', '\\')
    destBase = os.getcwd() + '\\' + OUT_DIR
    for elem in paths:
        elemDir, elemName = os.path.split(elem)
        source = basePath + '\\' + elem
        dest = destBase + '\\' + elem
        destDir = destBase + '\\' + elemDir
        os.makedirs(destDir, exist_ok=True)
        shutil.copy(source, dest)

class build_installer(py2exe):
    def run(self):
        py2exe.run(self)
        print('*** deleting unnecessary libraries and modules ***')
        pruneUnneededLibraries()
        print('*** copying qt plugins ***')
        copyQtPlugins(qt_plugins)
        script = NSISScript()
        script.create()
        print("*** compiling the NSIS setup script ***")
        script.compile()
        print("*** DONE ***")

guiIcons = glob('syncplay/resources/*.ico') + glob('syncplay/resources/*.png') +  ['syncplay/resources/spinner.mng']

resources = [
    "syncplay/resources/syncplayintf.lua",
    "syncplay/resources/license.rtf",
    "syncplay/resources/third-party-notices.txt"
]
resources.extend(guiIcons)
intf_resources = ["syncplay/resources/lua/intf/syncplay.lua"]

qt_plugins = ['platforms\\qwindows.dll', 'styles\\qwindowsvistastyle.dll']

common_info = dict(
    name='Syncplay',
    version=syncplay.version,
    author='Uriziel',
    author_email='dev@syncplay.pl',
    description='Syncplay',
)

info = dict(
    common_info,
    windows=[{
        "script": "syncplayClient.py",
        "icon_resources": [(1, "syncplay\\resources\\icon.ico")],
        'dest_base': "Syncplay"},
    ],
    console=['syncplayServer.py', {"script":"syncplayClient.py", "icon_resources":[(1, "syncplay\\resources\\icon.ico")], 'dest_base': "SyncplayConsole"}],

    options={
        'py2exe': {
            'dist_dir': OUT_DIR,
            'packages': 'PySide2, cffi, OpenSSL, certifi',
            'includes': 'twisted, sys, encodings, datetime, os, time, math, urllib, ast, unicodedata, _ssl, win32pipe, win32file, sqlite3',
            'excludes': 'venv, doctest, pdb, unittest, win32clipboard, win32pdh, win32security, win32trace, win32ui, winxpgui, win32process, tcl, tkinter',
            'dll_excludes': 'msvcr71.dll, MSVCP90.dll, POWRPROF.dll',
            'optimize': 2,
            'compressed': 1
        }
    },
    data_files=[("resources", resources), ("resources/lua/intf", intf_resources)],
    zipfile="lib/libsync.zip",
    cmdclass={"py2exe": build_installer},
)

sys.argv.extend(['py2exe'])
setup(**info)