Просмотр исходного кода

Start using Jinja2 for templating

Thomas Kluyver 10 лет назад
Родитель
Сommit
2ce1063345
5 измененных файлов с 122 добавлено и 116 удалено
  1. 1 1
      nsist/__init__.py
  2. 30 99
      nsist/nsiswriter.py
  3. 70 15
      nsist/pyapp.nsi
  4. 20 0
      nsist/pyapp_w_pylauncher.nsi
  5. 1 1
      setup.py

+ 1 - 1
nsist/__init__.py

@@ -34,7 +34,7 @@ logger = logging.getLogger(__name__)
 _PKGDIR = os.path.abspath(os.path.dirname(__file__))
 DEFAULT_PY_VERSION = '2.7.9' if PY2 else '3.4.2'
 DEFAULT_BUILD_DIR = pjoin('build', 'nsis')
-DEFAULT_NSI_TEMPLATE = pjoin(_PKGDIR, 'template.nsi')
+DEFAULT_NSI_TEMPLATE = 'pyapp_w_launcher.nsi' if PY2 else 'pyapp.nsi'
 DEFAULT_ICON = pjoin(_PKGDIR, 'glossyorb.ico')
 if os.name == 'nt' and sys.maxsize == (2**63)-1:
     DEFAULT_BITNESS = 64

+ 30 - 99
nsist/nsiswriter.py

@@ -1,9 +1,14 @@
 import itertools
 import operator
+import os
 import ntpath
 import re
 import sys
 
+import jinja2
+
+_PKGDIR = os.path.abspath(os.path.dirname(__file__))
+
 PY2 = sys.version_info[0] == 2
 
 
@@ -16,111 +21,37 @@ class NSISFileWriter(object):
         :param str template_file: Path to the .nsi template
         :param dict definitions: Mapping of name to value (values will be quoted)
         """
-        self.template_file = template_file
+        env = jinja2.Environment(loader=jinja2.FileSystemLoader([
+            _PKGDIR,
+            os.getcwd()
+        ]),
+        # Change template markers from {}, which NSIS uses, to [], which it
+        # doesn't much, so it's easier to distinguishing our templating from
+        # NSIS preprocessor variables.
+        block_start_string="[%",
+        block_end_string="%]",
+        variable_start_string="[[",
+        variable_end_string="]]",
+        comment_start_string="[#",
+        comment_end_string="#]",
+        )
+        self.template = env.get_template(template_file)
         self.installerbuilder = installerbuilder
-        self.definitions = definitions or {}
-        self.template_fields = {
-                ';INSTALL_FILES': self.files_install,
-                ';INSTALL_DIRECTORIES': self.dirs_install,
-                ';INSTALL_SHORTCUTS': self.shortcuts_install,
-                ';UNINSTALL_FILES': self.files_uninstall,
-                ';UNINSTALL_DIRECTORIES': self.dirs_uninstall,
-                ';UNINSTALL_SHORTCUTS': self.shortcuts_uninstall,
+        self.namespace = ns = {
+            'ib': installerbuilder,
+            'grouped_files': itertools.groupby(
+                   self.installerbuilder.install_files, operator.itemgetter(1)),
+            'icon': os.path.basename(installerbuilder.icon),
+            'arch_tag': '.amd64' if (installerbuilder.py_bitness==64) else '',
+            'pjoin': ntpath.join,
+            'single_shortcut': len(installerbuilder.shortcuts) == 1,
         }
-        if installerbuilder.py_version < '3.3':
-            self.template_fields.update({
-                ';PYLAUNCHER_INSTALL': self.pylauncher_install,
-                ';PYLAUNCHER_HELP': self.pylauncher_help})
 
     def write(self, target):
         """Fill out the template and write the result to 'target'.
         
         :param str target: Path to the file to be written
         """
-        with open(target, 'w') as fout, open(self.template_file) as fin:
-            self.write_definitions(fout)
-
-            for line in fin:
-                fout.write(line)
-                l = line.strip()
-                if l in self.template_fields:
-                    indent = re.match('\s*', line).group(0)
-                    for fillline in self.template_fields[l]():
-                        fout.write(indent+fillline+'\n')
-
-    def write_definitions(self, f):
-        """Write definition lines at the start of the file.
-        
-        :param f: A text-mode writable file handle
-        """
-        for name, value in self.definitions.items():
-            f.write('!define {} "{}"\n'.format(name, value))
-
-    # Template fillers
-    # ----------------
-
-    # These return an iterable of lines to fill after a given template field
-
-    def files_install(self):
-        for destination, group in itertools.groupby(
-                    self.installerbuilder.install_files, operator.itemgetter(1)):
-            yield 'SetOutPath "{}"'.format(destination)
-            for file, _ in group:
-                yield 'File "{}"'.format(file)
-        yield 'SetOutPath "$INSTDIR"'
-
-    def dirs_install(self):
-        for dir, destination in self.installerbuilder.install_dirs:
-            yield 'SetOutPath "{}"'.format(ntpath.join(destination, dir))
-            yield 'File /r "{}\*.*"'.format(dir)
-        yield 'SetOutPath "$INSTDIR"'
-    
-    def shortcuts_install(self):
-        shortcuts = self.installerbuilder.shortcuts
-        # The output path becomes the working directory for shortcuts.
-        yield 'SetOutPath "%HOMEDRIVE%\\%HOMEPATH%"'
-        if len(shortcuts) == 1:
-            scname, sc = next(iter(shortcuts.items()))
-            yield 'CreateShortCut "$SMPROGRAMS\{}.lnk" "{}" \'"$INSTDIR\{}"\' \\'.format(\
-                    scname, ('py' if sc['console'] else 'pyw'), sc['script'])
-            yield '    "$INSTDIR\{}"'.format(sc['icon'])
-            yield 'SetOutPath "$INSTDIR"'
-            return
-        
-        # Multiple shortcuts - make a folder
-        yield 'CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"'
-        for scname, sc in shortcuts.items():
-            yield 'CreateShortCut "$SMPROGRAMS\${{PRODUCT_NAME}}\{}.lnk" "{}" \\'.format(\
-                    scname, sc['target'])
-            yield '    \'{}\' "$INSTDIR\{}"'.format(sc['parameters'], sc['icon'])
-        yield 'SetOutPath "$INSTDIR"'
-
-    def files_uninstall(self):
-        for file, destination in self.installerbuilder.install_files:
-            yield 'Delete "{}"'.format(ntpath.join(destination, file))
-
-    def dirs_uninstall(self):
-        for dir, destination in self.installerbuilder.install_dirs:
-            yield 'RMDir /r "{}"'.format(ntpath.join(destination, dir))
-    
-    def shortcuts_uninstall(self):
-        shortcuts = self.installerbuilder.shortcuts
-        if len(shortcuts) == 1:
-            scname = next(iter(shortcuts))
-            yield 'Delete "$SMPROGRAMS\{}.lnk"'.format(scname)
-        else:
-            yield 'RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}"'
-
-    def pylauncher_install(self):
-        return ["Section \"PyLauncher\" sec_pylauncher",
-            "    File \"launchwin${ARCH_TAG}.msi\"",
-            "    ExecWait 'msiexec /i \"$INSTDIR\launchwin${ARCH_TAG}.msi\" /qb ALLUSERS=1'",
-            "    Delete $INSTDIR\launchwin${ARCH_TAG}.msi",
-            "SectionEnd",
-           ]
+        with open(target, 'w') as fout:
+            fout.write(self.template.render(self.namespace))
 
-    def pylauncher_help(self):
-        return ["StrCmp $0 ${sec_pylauncher} 0 +2",
-                "SendMessage $R0 ${WM_SETTEXT} 0 \"STR:The Python launcher. \\",
-                "    This is required for ${PRODUCT_NAME} to run.\"",
-               ]

+ 70 - 15
nsist/template.nsi → nsist/pyapp.nsi

@@ -1,10 +1,18 @@
-
-; Definitions will be added above
+!define PRODUCT_NAME "[[ib.appname]]"
+!define PRODUCT_VERSION "[[ib.version]]"
+!define PY_VERSION "[[ib.py_version]]"
+!define PY_MAJOR_VERSION "[[ib.py_major_version]]"
+!define PY_QUALIFIER "[[ib.py_qualifier]]"
+!define BITNESS "[[ib.py_bitness]]"
+!define ARCH_TAG "[[arch_tag]]"
+!define INSTALLER_NAME "[[ib.installer_name]]"
+!define PRODUCT_ICON "[[icon]]"
  
 SetCompressor lzma
 
 RequestExecutionLevel admin
 
+[% block modernui %]
 ; Modern UI installer stuff 
 !include "MUI2.nsh"
 !define MUI_ABORTWARNING
@@ -17,13 +25,14 @@ RequestExecutionLevel admin
 !insertmacro MUI_PAGE_INSTFILES
 !insertmacro MUI_PAGE_FINISH
 !insertmacro MUI_LANGUAGE "English"
-; MUI end ------
+[% endblock modernui %]
 
 Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
 OutFile "${INSTALLER_NAME}"
 InstallDir "$PROGRAMFILES${BITNESS}\${PRODUCT_NAME}"
 ShowInstDetails show
 
+[% block sections %]
 Section -SETTINGS
   SetOutPath "$INSTDIR"
   SetOverwrite ifnewer
@@ -37,9 +46,6 @@ Section "Python ${PY_VERSION}" sec_py
   Delete $INSTDIR\python-${PY_VERSION}${ARCH_TAG}.msi
 SectionEnd
 
-;PYLAUNCHER_INSTALL
-;------------------
-
 Section "!${PRODUCT_NAME}" sec_app
   SectionIn RO
   SetShellVarContext all
@@ -47,9 +53,41 @@ Section "!${PRODUCT_NAME}" sec_app
   SetOutPath "$INSTDIR\pkgs"
   File /r "pkgs\*.*"
   SetOutPath "$INSTDIR"
-  ;INSTALL_FILES
-  ;INSTALL_DIRECTORIES
-  ;INSTALL_SHORTCUTS
+  
+  ; Install files
+  [% for destination, group in grouped_files %]
+    SetOutPath "[[destination]]"
+    [% for file, _ in group %]
+      File "[[file]]"
+    [% endfor %]
+  [% endfor %]
+  
+  ; Install directories
+  [% for dir, destination in ib.install_dirs %]
+    SetOutPath "[[ pjoin(destination, dir) ]]"
+    File /r "[[dir]]\*.*"
+  [% endfor %]
+  
+  [% block install_shortcuts %]
+  ; Install shortcuts
+  ; The output path becomes the working directory for shortcuts
+  SetOutPath "%HOMEDRIVE%\%HOMEPATH%"
+  [% if single_shortcut %]
+    [% for scname, sc in ib.shortcuts.items() %]
+      CreateShortCut "$SMPROGRAMS\[[scname]].lnk" "[[sc['target'] ]]" \
+        '"$INSTDIR\[[ sc['parameters'] ]]"' "$INSTDIR\[[ sc['icon'] ]]"
+    [% endfor %]
+  [% else %]
+    [# Multiple shortcuts: create a directory for them #]
+    CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
+    [% for scname, sc in ib.shortcuts.items() %]
+      CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\[[scname]].lnk" "[[sc['target'] ]]" \
+        '"$INSTDIR\[[ sc['parameters'] ]]"' "$INSTDIR\[[ sc['icon'] ]]"
+    [% endfor %]
+  [% endif %]
+  SetOutPath "$INSTDIR"
+  [% endblock install_shortcuts %]
+  
   ; Byte-compile Python files.
   DetailPrint "Byte-compiling Python modules..."
   nsExec::ExecToLog 'py -${PY_QUALIFIER} -m compileall -q "$INSTDIR\pkgs"'
@@ -74,13 +112,30 @@ Section "Uninstall"
   Delete $INSTDIR\uninstall.exe
   Delete "$INSTDIR\${PRODUCT_ICON}"
   RMDir /r "$INSTDIR\pkgs"
-  ;UNINSTALL_FILES
-  ;UNINSTALL_DIRECTORIES
-  ;UNINSTALL_SHORTCUTS
+  ; Uninstall files
+  [% for file, destination in ib.install_files %]
+    Delete "[[pjoin(destination, file)]]"
+  [% endfor %]
+  ; Uninstall directories
+  [% for dir, destination in ib.install_dirs %]
+    RMDir /r "[[pjoin(destination, dir)]]"
+  [% endfor %]
+  [% block uninstall_shortcuts %]
+  ; Uninstall shortcuts
+  [% if single_shortcut %]
+    [% for scname in ib.shortcuts %]
+      Delete "$SMPROGRAMS\[[scname]].lnk"
+    [% endfor %]
+  [% else %]
+    RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}"
+  [% endif %]
+  [% endblock uninstall_shortcuts %]
   RMDir $INSTDIR
   DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
 SectionEnd
 
+[% endblock sections %]
+
 ; Functions
 
 Function .onMouseOverSection
@@ -88,13 +143,13 @@ Function .onMouseOverSection
     FindWindow $R0 "#32770" "" $HWNDPARENT
     GetDlgItem $R0 $R0 1043 ; description item (must be added to the UI)
 
+    [% block mouseover_messages %]
     StrCmp $0 ${sec_py} 0 +2
       SendMessage $R0 ${WM_SETTEXT} 0 "STR:The Python interpreter. \
             This is required for ${PRODUCT_NAME} to run."
-    ;
-    ;PYLAUNCHER_HELP
-    ;------------------
 
     StrCmp $0 ${sec_app} "" +2
       SendMessage $R0 ${WM_SETTEXT} 0 "STR:${PRODUCT_NAME}"
+    
+    [% endblock mouseover_messages %]
 FunctionEnd

+ 20 - 0
nsist/pyapp_w_pylauncher.nsi

@@ -0,0 +1,20 @@
+[% extends pyapp.nsi %]
+[# For Python 2, add the py/pyw Windows launcher. Python 3 includes it already. #]
+
+[% block sections %]
+[[ super() ]]
+
+Section "PyLauncher" sec_pylauncher
+    File "launchwin${ARCH_TAG}.msi",
+    ExecWait 'msiexec /i "$INSTDIR\launchwin${ARCH_TAG}.msi" /qb ALLUSERS=1'
+    Delete "$INSTDIR\launchwin${ARCH_TAG}.msi"
+SectionEnd
+[% endblock %]
+
+[% block mouseover_messages %]
+[[ super() ]]
+
+StrCmp $0 ${sec_app} "" +2
+  SendMessage $R0 ${WM_SETTEXT} 0 "STR:The Python launcher. \
+      This is required for ${PRODUCT_NAME} to run."
+[% endblock %]

+ 1 - 1
setup.py

@@ -55,7 +55,7 @@ setup(name='pynsist',
       author_email='thomas@kluyver.me.uk',
       url='https://github.com/takluyver/pynsist',
       packages=['nsist'],
-      package_data={'nsist': ['template.nsi',
+      package_data={'nsist': ['pyapp.nsi',
                               'glossyorb.ico',
                              ]
                     },