Browse Source

Allow extra preamble, e.g. to set environment variables, before loading user code

Thomas Kluyver 11 năm trước cách đây
mục cha
commit
9dd4bfbc52
3 tập tin đã thay đổi với 17 bổ sung3 xóa
  1. 7 0
      doc/cfgfile.rst
  2. 6 3
      nsist/__init__.py
  3. 4 0
      nsist/configreader.py

+ 7 - 0
doc/cfgfile.rst

@@ -65,6 +65,12 @@ Application section
    a console for the process. If ``false``, or not specified, they will use the
    ``pyw`` launcher, which doesn't create a console.
 
+.. 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.
+
 .. _shortcut_config:
 
 Shortcut sections
@@ -86,6 +92,7 @@ shortcuts by defining sections titled :samp:`Shortcut {Name}`. For example:
               console (optional)
               target (optional)
               parameters (optional)
+              extra_preamble (optional)
 
    These options all work the same way as in the Application section.
 

+ 6 - 3
nsist/__init__.py

@@ -163,11 +163,13 @@ def excepthook(etype, value, tb):
 if appdata:
     sys.excepthook = excepthook
 
+{extra_preamble}
+
 from {module} import {func}
 {func}()
 """
     
-    def write_script(self, entrypt, target):
+    def write_script(self, entrypt, target, extra_preamble=''):
         """Write a launcher script from a 'module:function' entry point
         
         python_version and bitness are used to write an appropriate shebang line
@@ -176,7 +178,7 @@ from {module} import {func}
         module, func = entrypt.split(":")
         with open(target, 'w') as f:
             f.write(self.SCRIPT_TEMPLATE.format(qualifier=self.py_qualifier,
-                                                module=module, func=func))
+                    module=module, func=func, extra_preamble=extra_preamble))
 
         pkg = module.split('.')[0]
         if pkg not in self.packages:
@@ -197,7 +199,8 @@ from {module} import {func}
                 if sc.get('entry_point'):
                     sc['script'] = script = scname.replace(' ', '_') + '.launch.py' \
                                                 + ('' if sc['console'] else 'w')
-                    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', ''))
                 else:
                     shutil.copy2(sc['script'], self.build_dir)
 

+ 4 - 0
nsist/configreader.py

@@ -59,6 +59,7 @@ CONFIG_VALIDATORS = {
         ('parameters', False),
         ('icon', False),
         ('console', False),
+        ('extra_preamble', False),
     ]),
     'Build': SectionValidator([
         ('directory', False),
@@ -80,6 +81,7 @@ CONFIG_VALIDATORS = {
         ('parameters', False),
         ('icon', False),
         ('console', False),
+        ('extra_preamble', False),
     ]),
 }
 
@@ -150,6 +152,8 @@ def read_shortcuts_config(cfg):
             sc2['icon'] = DEFAULT_ICON
         sc2['console'] = sc.getboolean('console', fallback=False)
         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')
         shortcuts[name] = sc2
 
     for section in cfg.sections():