Skip to content

Instantly share code, notes, and snippets.

@takluyver
Created November 16, 2014 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takluyver/3990d1f9b681dad3e3ad to your computer and use it in GitHub Desktop.
Save takluyver/3990d1f9b681dad3e3ad to your computer and use it in GitHub Desktop.
Pynsist nsi template rewritten as Python
from nsist.nsigen import *
Document([
Comment('Definitions will be added above'),
Instruction('SetCompressor', 'lzma'),
Instruction('RequestExecutionLevel', 'admin'),
'',
Comment("Modern UI installer stuff"),
CompilerCommand("include", "MUI2.nsh"),
define("MUI_ABORTWARNING"),
define("MUI_ICON", "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"),
Comment("UI pages"),
insertmacro("MUI_PAGE_WELCOME"),
insertmacro("MUI_PAGE_COMPONENTS"),
insertmacro("MUI_PAGE_DIRECTORY"),
insertmacro("MUI_PAGE_INSTFILES"),
insertmacro("MUI_PAGE_FINISH"),
insertmacro("MUI_LANGUAGE", "English"),
Comment("MUI end ------"),
'',
Instruction("Name", "${PRODUCT_NAME} ${PRODUCT_VERSION}"),
Instruction("OutFile", "${INSTALLER_NAME}"),
Instruction("InstallDir", "$PROGRAMFILES${BITNESS}\${PRODUCT_NAME}"),
Instruction("ShowInstDetails", "show"),
'',
Section("-SETTINGS", contents=[
Instruction("SetOutPath", "$INSTDIR"),
Instruction("SetOverwrite", "ifnewer"),
]),
Section("Python ${PY_VERSION}", index_var="sec_py", contents=[
File("python-${PY_VERSION}${ARCH_TAG}.msi"),
Instruction("DetailPrint", "Installing Python ${PY_MAJOR_VERSION}, ${BITNESS} bit"),
Instruction("ExecWait", 'msiexec /i "$INSTDIR\python-${PY_VERSION}${ARCH_TAG}.msi" '+
'/qb ALLUSERS=1 TARGETDIR="$COMMONFILES${BITNESS}\Python\${PY_MAJOR_VERSION}"'),
Instruction("Delete", "$INSTDIR\python-${PY_VERSION}${ARCH_TAG}.msi"),
]),
Section("!${PRODUCT_NAME}", index_var="sec_app", contents=[
Instruction("SectionIn", "RO"),
Instruction("SetShellVarContext", "all"),
File("${PRODUCT_ICON}"),
Instruction("SetOutPath", "$INSTDIR\pkgs"),
File("/r", "pkgs\*.*"),
Instruction("SetOutPath", "$INSTDIR"),
Comment("INSTALL_FILES"),
Comment("INSTALL_DIRECTORIES"),
Comment("INSTALL_SHORTCUTS"),
Comment("Byte-compile Python files."),
DetailPrint("Byte-compiling Python modules..."),
Instruction("nsExec::ExecToLog", 'py -${PY_QUALIFIER} -m compileall -q "$INSTDIR\pkgs"'),
Instruction("WriteUninstaller", "$INSTDIR\\uninstall.exe"),
Comment(" Add ourselves to Add/remove programs"),
Instruction("WriteRegStr", "HKLM", r"Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}",
"DisplayName", "${PRODUCT_NAME}"),
Instruction("WriteRegStr", "HKLM", r"Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}",
"UninstallString", r'"$INSTDIR\uninstall.exe"'),
Instruction("WriteRegStr", "HKLM", r"Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}",
"InstallLocation", "$INSTDIR"),
Instruction("WriteRegStr", "HKLM", r"Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}",
"DisplayIcon", "$INSTDIR\${PRODUCT_ICON}"),
Instruction("WriteRegDWORD", "HKLM", r"Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}",
"NoModify", "1"),
Instruction("WriteRegDWORD", "HKLM", r"Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}",
"NoRepair", "1"),
]),
Section("Uninstall", contents=[
Instruction("SetShellVarContext", "all"),
Instruction("Delete", "$INSTDIR\\uninstall.exe"),
Instruction("Delete", "$INSTDIR\${PRODUCT_ICON}"),
Instruction("RMDir", "/r", "$INSTDIR\pkgs"),
Comment("UNINSTALL_FILES"),
Comment("UNINSTALL_DIRECTORIES"),
Comment("UNINSTALL_SHORTCUTS"),
Instruction("RMDir", "$INSTDIR"),
Instruction("DeleteRegKey", "HKLM", r"Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"),
]),
Comment(' Functions'),
'',
Function('.onMouseOverSection', [
Comment('Find which section the mouse is over, and set the corresponding description.'),
Instruction("FindWindow", "$R0", "#32770", "", "$HWNDPARENT"),
Instruction("GetDlgItem", "$R0", "$R0", "1043"), #description item (must be added to the UI)
'',
Conditional("StrCmp", "$0", "${sec_py}", ifbody=[
Instruction("SendMessage", "$R0", "${WM_SETTEXT}", "0",
"STR:The Python interpreter. This is required for ${PRODUCT_NAME} to run.")
]),
Comment(''),
Comment('PYLAUNCHER_HELP'),
Comment('------------------'),
'',
Conditional('StrCmp', "$0", "${sec_app}", ifbody=[
Instruction("SendMessage", "$R0", "${WM_SETTEXT}", "0", "STR:${PRODUCT_NAME}")
]),
]),
]).write("sample.nsi")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment