Bladeren bron

Make extra_preamble a file instead of code

Thomas Kluyver 11 jaren geleden
bovenliggende
commit
e536e24ce1
4 gewijzigde bestanden met toevoegingen van 29 en 8 verwijderingen
  1. 6 3
      doc/cfgfile.rst
  2. 1 2
      examples/pygtk/installer.cfg
  3. 13 1
      nsist/__init__.py
  4. 9 2
      nsist/configreader.py

+ 6 - 3
doc/cfgfile.rst

@@ -67,9 +67,12 @@ Application section
 
 
 .. describe:: extra_preamble (optional)
 .. describe:: extra_preamble (optional)
 
 
-   Any extra Python commands to be run before your code is launched, for example
-   to set environment variables needed by pygtk. This is only valid if you use
-   ``entry_point`` to specify how to launch your application.
+   Path to a file containing extra Python commands to be run before your code is
+   launched, for example  to set environment variables needed by pygtk. This is
+   only valid if you use ``entry_point`` to specify how to launch your application.
+   
+   If you use the Python API, this parameter can also be passed as a file-like
+   object, such as :class:`io.StringIO`.
 
 
 .. _shortcut_config:
 .. _shortcut_config:
 
 

+ 1 - 2
examples/pygtk/installer.cfg

@@ -2,8 +2,7 @@
 name=Hello World (PyGTK)
 name=Hello World (PyGTK)
 version=1.0
 version=1.0
 entry_point=helloworld:main
 entry_point=helloworld:main
-extra_preamble=os.environ['PATH'] += os.pathsep + os.path.join(pkgdir, 'gtk/lib') + \
-    os.pathsep + os.path.join(pkgdir, 'gtk/bin')
+extra_preamble=gtk_preamble.py
 
 
 [Python]
 [Python]
 version=2.7.7
 version=2.7.7

+ 13 - 1
nsist/__init__.py

@@ -1,6 +1,7 @@
 """Build NSIS installers for Python applications.
 """Build NSIS installers for Python applications.
 """
 """
 import errno
 import errno
+import io
 import logging
 import logging
 import ntpath
 import ntpath
 import operator
 import operator
@@ -200,8 +201,19 @@ if __name__ == '__main__':
                 if sc.get('entry_point'):
                 if sc.get('entry_point'):
                     sc['script'] = script = scname.replace(' ', '_') + '.launch.py' \
                     sc['script'] = script = scname.replace(' ', '_') + '.launch.py' \
                                                 + ('' if sc['console'] else 'w')
                                                 + ('' if sc['console'] else 'w')
+
+                    specified_preamble = sc.get('extra_preamble', None)
+                    if isinstance(specified_preamble, str):
+                        # Filename
+                        extra_preamble = io.open(specified_preamble, encoding='utf-8')
+                    elif specified_preamble is None:
+                        extra_preamble = io.StringIO()  # Empty
+                    else:
+                        # Passed a StringIO or similar object
+                        extra_preamble = specified_preamble
+
                     self.write_script(sc['entry_point'], pjoin(self.build_dir, script),
                     self.write_script(sc['entry_point'], pjoin(self.build_dir, script),
-                                      sc.get('extra_preamble', ''))
+                                      extra_preamble.read().rstrip())
                 else:
                 else:
                     shutil.copy2(sc['script'], self.build_dir)
                     shutil.copy2(sc['script'], self.build_dir)
 
 

+ 9 - 2
nsist/configreader.py

@@ -1,6 +1,7 @@
 #!/usr/bin/python3
 #!/usr/bin/python3
 
 
 import configparser
 import configparser
+import os.path
 
 
 class SectionValidator(object):
 class SectionValidator(object):
     def __init__(self, keys):
     def __init__(self, keys):
@@ -152,8 +153,14 @@ def read_shortcuts_config(cfg):
             sc2['icon'] = DEFAULT_ICON
             sc2['icon'] = DEFAULT_ICON
         sc2['console'] = sc.getboolean('console', fallback=False)
         sc2['console'] = sc.getboolean('console', fallback=False)
         sc2['parameters'] = sc.get('parameters', fallback='')
         sc2['parameters'] = sc.get('parameters', fallback='')
-        if 'extra_preamble' in sc2 and 'entry_point' not in sc2:
-            raise InvalidConfig('extra_preamble is only valid with entry_point')
+        if 'extra_preamble' in sc2:
+            if 'entry_point' not in sc2:
+                raise InvalidConfig('extra_preamble is only valid with entry_point')
+            preamb_file = sc2['extra_preamble']
+            if not os.path.isfile(preamb_file):
+                raise InvalidConfig('extra_preamble file %r does not exist' %
+                                                    preamb_file)
+
         shortcuts[name] = sc2
         shortcuts[name] = sc2
 
 
     for section in cfg.sections():
     for section in cfg.sections():