nsiswriter.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import re
  2. import sys
  3. PY2 = sys.version_info[0] == 2
  4. class NSISFileWriter(object):
  5. """Write an .nsi script file by filling in a template.
  6. """
  7. def __init__(self, template_file, installerbuilder, definitions=None):
  8. """Instantiate an NSISFileWriter
  9. :param str template_file: Path to the .nsi template
  10. :param dict definitions: Mapping of name to value (values will be quoted)
  11. """
  12. self.template_file = template_file
  13. self.installerbuilder = installerbuilder
  14. self.definitions = definitions or {}
  15. self.template_fields = {
  16. ';INSTALL_FILES': self.files_install,
  17. ';INSTALL_DIRECTORIES': self.dirs_install,
  18. ';INSTALL_SHORTCUTS': self.shortcuts_install,
  19. ';UNINSTALL_FILES': self.files_uninstall,
  20. ';UNINSTALL_DIRECTORIES': self.dirs_uninstall,
  21. ';UNINSTALL_SHORTCUTS': self.shortcuts_uninstall,
  22. }
  23. if PY2:
  24. self.template_fields.update({
  25. ';PYLAUNCHER_INSTALL': self.pylauncher_install,
  26. ';PYLAUNCHER_HELP': self.pylauncher_help})
  27. def write(self, target):
  28. """Fill out the template and write the result to 'target'.
  29. :param str target: Path to the file to be written
  30. """
  31. with open(target, 'w') as fout, open(self.template_file) as fin:
  32. self.write_definitions(fout)
  33. for line in fin:
  34. fout.write(line)
  35. l = line.strip()
  36. if l in self.template_fields:
  37. indent = re.match('\s*', line).group(0)
  38. for fillline in self.template_fields[l]():
  39. fout.write(indent+fillline+'\n')
  40. def write_definitions(self, f):
  41. """Write definition lines at the start of the file.
  42. :param f: A text-mode writable file handle
  43. """
  44. for name, value in self.definitions.items():
  45. f.write('!define {} "{}"\n'.format(name, value))
  46. # Template fillers
  47. # ----------------
  48. # These return an iterable of lines to fill after a given template field
  49. def files_install(self):
  50. for file in self.installerbuilder.install_files:
  51. yield 'File "{}"'.format(file)
  52. def dirs_install(self):
  53. for dir in self.installerbuilder.install_dirs:
  54. yield 'SetOutPath "$INSTDIR\{}"'.format(dir)
  55. yield 'File /r "{}\*.*"'.format(dir)
  56. yield 'SetOutPath "$INSTDIR"'
  57. def shortcuts_install(self):
  58. shortcuts = self.installerbuilder.shortcuts
  59. if len(shortcuts) == 1:
  60. scname, sc = next(iter(shortcuts.items()))
  61. # The output path becomes the working directory for shortcuts.
  62. yield 'SetOutPath "%HOMEDRIVE%\\%HOMEPATH%"'
  63. yield 'CreateShortCut "$SMPROGRAMS\{}.lnk" "{}" \'"$INSTDIR\{}"\' \\'.format(\
  64. scname, ('py' if sc['console'] else 'pyw'), sc['script'])
  65. yield ' "$INSTDIR\{}"'.format(sc['icon'])
  66. yield 'SetOutPath "$INSTDIR"
  67. return
  68. # Multiple shortcuts - make a folder
  69. yield 'CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"'
  70. for scname, sc in shortcuts.items():
  71. yield 'CreateShortCut "$SMPROGRAMS\${{PRODUCT_NAME}}\{}.lnk" "{}" \\'.format(\
  72. scname, ('py' if sc['console'] else 'pyw'))
  73. yield ' \'"$INSTDIR\{}"\' "$INSTDIR\{}"'.format(sc['script'], sc['icon'])
  74. def files_uninstall(self):
  75. for file in self.installerbuilder.install_files:
  76. yield 'Delete "$INSTDIR\{}"'.format(file)
  77. def dirs_uninstall(self):
  78. for dir in self.installerbuilder.install_dirs:
  79. yield 'RMDir /r "$INSTDIR\{}"'.format(dir)
  80. def shortcuts_uninstall(self):
  81. shortcuts = self.installerbuilder.shortcuts
  82. if len(shortcuts) == 1:
  83. scname = next(iter(shortcuts))
  84. yield 'Delete "$SMPROGRAMS\{}.lnk"'.format(scname)
  85. else:
  86. yield 'RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}"'
  87. def pylauncher_install(self):
  88. return ["Section \"PyLauncher\" sec_pylauncher",
  89. " File \"launchwin${ARCH_TAG}.msi\"",
  90. " ExecWait 'msiexec /i \"$INSTDIR\launchwin${ARCH_TAG}.msi\" /qb ALLUSERS=1'",
  91. " Delete $INSTDIR\launchwin${ARCH_TAG}.msi",
  92. "SectionEnd",
  93. ]
  94. def pylauncher_help(self):
  95. return ["StrCmp $0 ${sec_pylauncher} 0 +2",
  96. "SendMessage $R0 ${WM_SETTEXT} 0 \"STR:The Python launcher. \\",
  97. " This is required for ${PRODUCT_NAME} to run.\"",
  98. ]