configreader.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/usr/bin/python3
  2. import configparser
  3. import os.path
  4. from pathlib import Path
  5. class SectionValidator(object):
  6. def __init__(self, keys):
  7. """
  8. keys
  9. list of tuples containing the names and whether the
  10. key is mandatory
  11. """
  12. self.keys = keys
  13. def check(self, config, section_name):
  14. """
  15. validates the section, if this is the correct validator for it
  16. returns True if this is the correct validator for this section
  17. raises InvalidConfig if something inside the section is wrong
  18. """
  19. self._check_mandatory_fields(section_name, config[section_name])
  20. self._check_invalid_keys(section_name, config[section_name])
  21. def _check_mandatory_fields(self, section_name, key):
  22. for key_name, mandatory in self.keys:
  23. if mandatory:
  24. try:
  25. key[key_name]
  26. except KeyError:
  27. err_msg = ("The section '{0}' must contain a "
  28. "key '{1}'!").format(
  29. section_name,
  30. key_name)
  31. raise InvalidConfig(err_msg)
  32. def _check_invalid_keys(self, section_name, section):
  33. for key in section:
  34. key_name = str(key)
  35. valid_key_names = [s[0] for s in self.keys]
  36. is_valid_key = key_name in valid_key_names
  37. if not is_valid_key:
  38. err_msg = ("'{0}' is not a valid key name for '{1}'. Must "
  39. "be one of these: {2}").format(
  40. key_name,
  41. section_name,
  42. ', '.join(valid_key_names))
  43. raise InvalidConfig(err_msg)
  44. # contains all configuration sections and keys
  45. # the keys are a tuple with their name and a boolean, which
  46. # tells us whether the option is mandatory
  47. CONFIG_VALIDATORS = {
  48. 'Application': SectionValidator([
  49. ('name', True),
  50. ('version', True),
  51. ('publisher', False),
  52. ('entry_point', False),
  53. ('script', False),
  54. ('target', False),
  55. ('parameters', False),
  56. ('icon', False),
  57. ('console', False),
  58. ('extra_preamble', False),
  59. ('license_file', False),
  60. ]),
  61. 'Build': SectionValidator([
  62. ('directory', False),
  63. ('installer_name', False),
  64. ('nsi_template', False),
  65. ]),
  66. 'Include': SectionValidator([
  67. ('packages', False),
  68. ('pypi_wheels', False),
  69. ('extra_wheel_sources', False),
  70. ('files', False),
  71. ('exclude', False),
  72. ('local_wheels', False)
  73. ]),
  74. 'Python': SectionValidator([
  75. ('version', False),
  76. ('bitness', False),
  77. ('format', False),
  78. ('include_msvcrt', False),
  79. ]),
  80. 'Shortcut': SectionValidator([
  81. ('entry_point', False),
  82. ('script', False),
  83. ('target', False),
  84. ('parameters', False),
  85. ('icon', False),
  86. ('console', False),
  87. ('extra_preamble', False),
  88. ]),
  89. 'Command': SectionValidator([
  90. ('entry_point', True),
  91. ('extra_preamble', False),
  92. ])
  93. }
  94. class InvalidConfig(ValueError):
  95. pass
  96. def read_and_validate(config_file):
  97. # Interpolation interferes with Windows-style environment variables, so
  98. # it's disabled for now.
  99. config = configparser.ConfigParser(interpolation=None)
  100. if config.read(config_file) == []:
  101. raise InvalidConfig("Config file not found: %r" % config_file)
  102. for section in config.sections():
  103. if section in CONFIG_VALIDATORS:
  104. CONFIG_VALIDATORS[section].check(config, section)
  105. elif section.startswith('Shortcut '):
  106. CONFIG_VALIDATORS['Shortcut'].check(config, section)
  107. elif section.startswith('Command '):
  108. CONFIG_VALIDATORS['Command'].check(config, section)
  109. else:
  110. valid_section_names = CONFIG_VALIDATORS.keys()
  111. err_msg = ("{0} is not a valid section header. Must "
  112. "be one of these: {1}").format(
  113. section,
  114. ', '.join(['"%s"' % n for n in valid_section_names]))
  115. raise InvalidConfig(err_msg)
  116. return config
  117. def read_extra_files(cfg):
  118. """Read the list of extra files from the config file.
  119. Returns a list of 2-tuples: (file, destination_directory), which can be
  120. passed as the ``extra_files`` parameter to :class:`nsist.InstallerBuilder`.
  121. """
  122. lines = cfg.get('Include', 'files', fallback='').splitlines()
  123. pairs = []
  124. for line in lines:
  125. if '>' in line:
  126. file, dest = line.rsplit('>', 1)
  127. pairs.append((file.strip(), dest.strip()))
  128. else:
  129. pairs.append((line, '$INSTDIR'))
  130. return pairs
  131. def read_shortcuts_config(cfg):
  132. """Read and verify the shortcut definitions from the config file.
  133. There is one shortcut per 'Shortcut <name>' section, and one for the
  134. Application section.
  135. Returns a dict of dicts with the fields from the shortcut sections.
  136. The optional 'icon' and 'console' fields will be filled with their
  137. default values if not supplied.
  138. """
  139. shortcuts = {}
  140. def _check_shortcut(name, sc, section):
  141. alternatives = ['entry_point', 'script', 'target']
  142. has_alternatives = sum(1 for k in alternatives if k in sc)
  143. if has_alternatives < 1:
  144. raise InvalidConfig('Section [{}] has none of {}.'.format(
  145. section, ', '.join(alternatives)))
  146. elif has_alternatives > 1:
  147. raise InvalidConfig('Section [{}] has more than one of {}.'.format(
  148. section, ', '.join(alternatives)))
  149. # Copy to a regular dict so it can hold a boolean value
  150. sc2 = dict(sc)
  151. if 'icon' not in sc2:
  152. from . import DEFAULT_ICON
  153. sc2['icon'] = DEFAULT_ICON
  154. sc2['console'] = sc.getboolean('console', fallback=False)
  155. sc2['parameters'] = sc.get('parameters', fallback='')
  156. if 'extra_preamble' in sc2:
  157. if 'entry_point' not in sc2:
  158. raise InvalidConfig('extra_preamble is only valid with entry_point')
  159. preamb_file = sc2['extra_preamble']
  160. if not os.path.isfile(preamb_file):
  161. raise InvalidConfig('extra_preamble file %r does not exist' %
  162. preamb_file)
  163. shortcuts[name] = sc2
  164. for section in cfg.sections():
  165. if section.startswith("Shortcut "):
  166. name = section[len("Shortcut "):]
  167. _check_shortcut(name, cfg[section], section)
  168. appcfg = cfg['Application']
  169. _check_shortcut(appcfg['name'], appcfg, 'Application')
  170. return shortcuts
  171. def read_commands_config(cfg):
  172. """Read and verify the command definitions from the config file.
  173. Returns a dict of dicts, keyed by command name, containing the values from
  174. the command sections of the config file.
  175. """
  176. commands = {}
  177. for section in cfg.sections():
  178. if section.startswith("Command "):
  179. name = section[len("Command "):]
  180. commands[name] = cc = dict(cfg[section])
  181. if ('extra_preamble' in cc) and \
  182. not os.path.isfile(cc['extra_preamble']):
  183. raise InvalidConfig('extra_preamble file %r does not exist' %
  184. cc['extra_preamble'])
  185. return commands
  186. def get_installer_builder_args(config):
  187. from . import (DEFAULT_BITNESS,
  188. DEFAULT_BUILD_DIR,
  189. DEFAULT_ICON,
  190. DEFAULT_PY_VERSION)
  191. appcfg = config['Application']
  192. args = {}
  193. args['appname'] = appcfg['name']
  194. args['version'] = appcfg['version']
  195. args['shortcuts'] = read_shortcuts_config(config)
  196. args['commands'] = read_commands_config(config)
  197. args['publisher'] = appcfg.get('publisher', None)
  198. args['icon'] = appcfg.get('icon', DEFAULT_ICON)
  199. args['license_file'] = appcfg.get('license_file', None)
  200. args['packages'] = config.get('Include', 'packages', fallback='').strip().splitlines()
  201. args['pypi_wheel_reqs'] = config.get('Include', 'pypi_wheels', fallback='').strip().splitlines()
  202. args['extra_wheel_sources'] = [Path(p) for p in
  203. config.get('Include', 'extra_wheel_sources', fallback='').strip().splitlines()
  204. ]
  205. args['extra_files'] = read_extra_files(config)
  206. args['py_version'] = config.get('Python', 'version', fallback=DEFAULT_PY_VERSION)
  207. args['py_bitness'] = config.getint('Python', 'bitness', fallback=DEFAULT_BITNESS)
  208. args['inc_msvcrt'] = config.getboolean('Python', 'include_msvcrt', fallback=True)
  209. args['build_dir'] = config.get('Build', 'directory', fallback=DEFAULT_BUILD_DIR)
  210. args['installer_name'] = config.get('Build', 'installer_name', fallback=None)
  211. args['nsi_template'] = config.get('Build', 'nsi_template', fallback=None)
  212. args['exclude'] = config.get('Include', 'exclude', fallback='').strip().splitlines()
  213. args['local_wheels'] = config.get('Include', 'local_wheels', fallback='').strip().splitlines()
  214. return args