nsiswriter.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, 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.definitions = definitions or {}
  14. self.extra_files = []
  15. self.write_after_line = {
  16. ';EXTRA_FILES_INSTALL': self.write_extra_files_install,
  17. ';EXTRA_FILES_UNINSTALL': self.write_extra_files_uninstall,
  18. }
  19. if PY2:
  20. self.write_after_line.update({
  21. ';PYLAUNCHER_INSTALL': self.write_pylauncher_install,
  22. ';PYLAUNCHER_HELP': self.write_pylauncher_help})
  23. def write_definitions(self, f):
  24. """Write definition lines at the start of the file.
  25. :param f: A text-mode writable file handle
  26. """
  27. for name, value in self.definitions.items():
  28. f.write('!define {} "{}"\n'.format(name, value))
  29. def write_extra_files_install(self, f, indent):
  30. """Write the commands to install the list of extra files and directories.
  31. :param f: A text-mode writable file handle
  32. :param str indent: Leading space at this point in the file
  33. """
  34. for file, is_dir in self.extra_files:
  35. if is_dir:
  36. f.write(indent+'SetOutPath "$INSTDIR\{}"\n'.format(file))
  37. f.write(indent+'File /r "{}\*.*"\n'.format(file))
  38. f.write(indent+'SetOutPath "$INSTDIR"\n')
  39. else:
  40. f.write(indent+'File "{}"\n'.format(file))
  41. def write_extra_files_uninstall(self, f, indent):
  42. """Write the commands to uninstall the list of extra files and directories.
  43. :param f: A text-mode writable file handle
  44. :param str indent: Leading space at this point in the file
  45. """
  46. for file, is_dir in self.extra_files:
  47. if is_dir:
  48. f.write(indent+'RMDir /r "$INSTDIR\{}"\n'.format(file))
  49. else:
  50. f.write(indent+'Delete "$INSTDIR\{}"\n'.format(file))
  51. def write_pylauncher_install(self, f, indent):
  52. f.write(indent+"Section \"PyLauncher\" sec_pylauncher\n")
  53. f.write(indent+"File \"launchwin${ARCH_TAG}.msi\"\n")
  54. f.write(indent+"ExecWait 'msiexec /i "
  55. "\"$INSTDIR\launchwin${ARCH_TAG}.msi\" /qb ALLUSERS=1'\n")
  56. f.write(indent+"Delete $INSTDIR\launchwin${ARCH_TAG}.msi\n")
  57. f.write(indent+"SectionEnd\n")
  58. def write_pylauncher_help(self, f, indent):
  59. f.write(indent+"StrCmp $0 ${sec_pylauncher} 0 +2\n")
  60. f.write(indent+"SendMessage $R0 ${WM_SETTEXT} 0 "
  61. "\"STR:The Python launcher. \\\n")
  62. f.write(indent+"This is required for ${PRODUCT_NAME} to run.\"")
  63. def write(self, target):
  64. """Fill out the template and write the result to 'target'.
  65. :param str target: Path to the file to be written
  66. """
  67. with open(target, 'w') as fout, open(self.template_file) as fin:
  68. self.write_definitions(fout)
  69. for line in fin:
  70. fout.write(line)
  71. l = line.strip()
  72. if l in self.write_after_line:
  73. indent = re.match('\s*', line).group(0)
  74. self.write_after_line[l](fout, indent)