1
0
Cody Scott 8 жил өмнө
parent
commit
c9505a3c2b

+ 1 - 29
nsist/__init__.py

@@ -24,6 +24,7 @@ if os.name == 'nt':
 else:
     winreg = None
 
+from .configreader import get_installer_builder_args
 from .commands import prepare_bin_directory
 from .copymodules import copy_modules
 from .nsiswriter import NSISFileWriter
@@ -500,35 +501,6 @@ if __name__ == '__main__':
             if not exitcode:
                 logger.info('Installer written to %s', pjoin(self.build_dir, self.installer_name))
 
-
-def get_installer_builder_args(config):
-    def get_boolean(s):
-        if s.lower() in ('1', 'yes', 'true', 'on'):
-            return True
-        if s.lower() in ('0', 'no', 'false', 'off'):
-            return False
-        raise ValueError('ValueError: Not a boolean: {}'.format(s))
-
-    appcfg = config['Application']
-    args = {}
-    args['appname'] = appcfg['name'].strip()
-    args['version'] = appcfg['version'].strip()
-    args['publisher'] = appcfg['publisher'].strip() if 'publisher' in appcfg else None
-    args['icon'] = appcfg.get('icon', DEFAULT_ICON).strip()
-    args['packages'] = config.get('Include', 'packages', fallback='').strip().splitlines()
-    args['pypi_wheel_reqs'] = config.get('Include', 'pypi_wheels', fallback='').strip().splitlines()
-    args['extra_files'] = configreader.read_extra_files(config)
-    args['py_version'] = config.get('Python', 'version', fallback=DEFAULT_PY_VERSION).strip()
-    args['py_bitness'] = config.getint('Python', 'bitness', fallback=DEFAULT_BITNESS)
-    args['py_format'] = config.get('Python', 'format').strip() if 'format' in config['Python'] else None
-    inc_msvcrt = config.get('Python', 'include_msvcrt', fallback='True')
-    args['inc_msvcrt'] = get_boolean(inc_msvcrt.strip())
-    args['build_dir'] = config.get('Build', 'directory', fallback=DEFAULT_BUILD_DIR).strip()
-    args['installer_name'] = config.get('Build', 'installer_name') if 'installer_name' in config['Build'] else None
-    args['nsi_template'] = config.get('Build', 'nsi_template').strip() if 'nsi_template' in config['Build'] else None
-    args['exclude'] = config.get('Include', 'exclude', fallback='').strip().splitlines()
-    return args
-
 def main(argv=None):
     """Make an installer from the command line.
 

+ 40 - 7
nsist/configreader.py

@@ -11,12 +11,12 @@ class SectionValidator(object):
             key is mandatory
         """
         self.keys = keys
-    
+
     def check(self, config, section_name):
         """
         validates the section, if this is the correct validator for it
         returns True if this is the correct validator for this section
-        
+
         raises InvalidConfig if something inside the section is wrong
         """
         self._check_mandatory_fields(section_name, config[section_name])
@@ -33,7 +33,7 @@ class SectionValidator(object):
                                 section_name,
                                 key_name)
                     raise InvalidConfig(err_msg)
-    
+
     def _check_invalid_keys(self, section_name, section):
         for key in section:
             key_name = str(key)
@@ -116,12 +116,12 @@ def read_and_validate(config_file):
                        "be one of these: {1}").format(
                        section,
                        ', '.join(['"%s"' % n for n in valid_section_names]))
-            raise InvalidConfig(err_msg)    
+            raise InvalidConfig(err_msg)
     return config
 
 def read_extra_files(cfg):
     """Read the list of extra files from the config file.
-    
+
     Returns a list of 2-tuples: (file, destination_directory), which can be
     passed as the ``extra_files`` parameter to :class:`nsist.InstallerBuilder`.
     """
@@ -138,10 +138,10 @@ def read_extra_files(cfg):
 
 def read_shortcuts_config(cfg):
     """Read and verify the shortcut definitions from the config file.
-    
+
     There is one shortcut per 'Shortcut <name>' section, and one for the
     Application section.
-    
+
     Returns a dict of dicts with the fields from the shortcut sections.
     The optional 'icon' and 'console' fields will be filled with their
     default values if not supplied.
@@ -201,3 +201,36 @@ def read_commands_config(cfg):
                                     cc['extra_preamble'])
 
     return commands
+
+
+def get_installer_builder_args(config):
+    from . import (DEFAULT_BITNESS,
+                    DEFAULT_BUILD_DIR,
+                    DEFAULT_ICON,
+                    DEFAULT_PY_VERSION)
+    def get_boolean(s):
+        if s.lower() in ('1', 'yes', 'true', 'on'):
+            return True
+        if s.lower() in ('0', 'no', 'false', 'off'):
+            return False
+        raise ValueError('ValueError: Not a boolean: {}'.format(s))
+
+    appcfg = config['Application']
+    args = {}
+    args['appname'] = appcfg['name'].strip()
+    args['version'] = appcfg['version'].strip()
+    args['publisher'] = appcfg['publisher'].strip() if 'publisher' in appcfg else None
+    args['icon'] = appcfg.get('icon', DEFAULT_ICON).strip()
+    args['packages'] = config.get('Include', 'packages', fallback='').strip().splitlines()
+    args['pypi_wheel_reqs'] = config.get('Include', 'pypi_wheels', fallback='').strip().splitlines()
+    args['extra_files'] = read_extra_files(config)
+    args['py_version'] = config.get('Python', 'version', fallback=DEFAULT_PY_VERSION).strip()
+    args['py_bitness'] = config.getint('Python', 'bitness', fallback=DEFAULT_BITNESS)
+    args['py_format'] = config.get('Python', 'format').strip() if 'format' in config['Python'] else None
+    inc_msvcrt = config.get('Python', 'include_msvcrt', fallback='True')
+    args['inc_msvcrt'] = get_boolean(inc_msvcrt.strip())
+    args['build_dir'] = config.get('Build', 'directory', fallback=DEFAULT_BUILD_DIR).strip()
+    args['installer_name'] = config.get('Build', 'installer_name') if 'installer_name' in config['Build'] else None
+    args['nsi_template'] = config.get('Build', 'nsi_template').strip() if 'nsi_template' in config['Build'] else None
+    args['exclude'] = config.get('Include', 'exclude', fallback='').strip().splitlines()
+    return args

+ 2 - 6
nsist/tests/test_configuration_validator.py

@@ -4,7 +4,6 @@ import os
 from nose.tools import *
 
 from .. import configreader
-from .. import get_installer_builder_args
 
 
 DATA_FILES = os.path.join(os.path.dirname(__file__), 'data_files')
@@ -12,10 +11,7 @@ DATA_FILES = os.path.join(os.path.dirname(__file__), 'data_files')
 def test_valid_config():
     configfile = os.path.join(DATA_FILES, 'valid_config.cfg')
     config = configreader.read_and_validate(configfile)
-    print(config['Application'])
-    print('Application' in config)
-    print(config.has_section('Application'))
-    # assert False
+    assert config.has_section('Application')
 
 def test_valid_config_with_shortcut():
     configfile = os.path.join(DATA_FILES, 'valid_config_with_shortcut.cfg')
@@ -49,7 +45,7 @@ def test_valid_config_with_values_starting_on_new_line():
     assert config.get('Include', 'exclude') == '\nsomething'
     assert config.get('Include', 'files') == '\nLICENSE\ndata_files/'
 
-    args = get_installer_builder_args(config)
+    args = configreader.get_installer_builder_args(config)
     assert args['appname'] == 'My App'
     assert args['version'] == '1.0'
     assert args['publisher'] == 'Test'