includehandler: removed sys.exit(), expanded error checks

The application should not be stopped from this code, removed `sys.exit`
and added exceptions to improve error handling.

Also added some more checks that were missing.

Signed-off-by: Claudius Heine <ch@denx.de>
This commit is contained in:
Claudius Heine 2017-07-05 16:27:39 +02:00 committed by Daniel Wagner
parent 2be4007113
commit 7ef96e732a

View File

@ -26,7 +26,6 @@
""" """
import os import os
import sys
import collections import collections
import functools import functools
import logging import logging
@ -43,6 +42,13 @@ __license__ = 'MIT'
__copyright__ = 'Copyright (c) Siemens AG, 2017' __copyright__ = 'Copyright (c) Siemens AG, 2017'
class LoadConfigException(Exception):
"""
Class for exceptions that appear while loading the configuration file.
"""
pass
def load_config(filename): def load_config(filename):
""" """
Load the configuration file and test if version is supported. Load the configuration file and test if version is supported.
@ -58,21 +64,30 @@ def load_config(filename):
with open(filename, 'rb') as fds: with open(filename, 'rb') as fds:
config = yaml.safe_load(fds) config = yaml.safe_load(fds)
else: else:
logging.error('Config file extension not recognized: %s', raise LoadConfigException('Config file extension not recognized',
filename) filename)
sys.exit(1)
file_version_string = config.get('header', {}).get('version', None) try:
header = config.get('header', {})
except AttributeError:
raise LoadConfigException('Config does not contain a dictionary',
filename)
if file_version_string is None: if not header:
logging.error('Version missing: %s', filename) raise LoadConfigException('Header missing or empty', filename)
sys.exit(1)
try:
file_version_string = header.get('version', None)
except AttributeError:
raise LoadConfigException('Header is not a dictionary', filename)
if not file_version_string:
raise LoadConfigException('Version missing or empty', filename)
try: try:
if not isinstance(file_version_string, str): if not isinstance(file_version_string, str):
logging.error('Version has to be a string: %s', raise LoadConfigException('Version has to be a string',
filename) filename)
sys.exit(1)
file_version = StrictVersion() file_version = StrictVersion()
file_version.parse(file_version_string) file_version.parse(file_version_string)
@ -88,13 +103,12 @@ def load_config(filename):
file_version.version = tuple(list(file_version.version[:2]) + [0]) file_version.version = tuple(list(file_version.version[:2]) + [0])
if file_version < lower_version or kas_version < file_version: if file_version < lower_version or kas_version < file_version:
logging.error('This version of kas is compatible with version %s ' raise LoadConfigException('This version of kas is compatible with '
'to %s, file has version %s: %s', 'version {} to {}, file has version {}'
lower_version, kas_version, file_version, filename) .format(lower_version, kas_version,
sys.exit(1) file_version), filename)
except ValueError: except ValueError:
logging.exception('Not expected version format: %s', filename) raise LoadConfigException('Not expected version format', filename)
raise
return config return config
@ -128,7 +142,7 @@ class IncludeHandler(object):
# pylint: disable=no-self-use,unused-argument # pylint: disable=no-self-use,unused-argument
logging.error('get_config is not implemented') logging.error('get_config is not implemented')
sys.exit(1) raise NotImplementedError()
class GlobalIncludes(IncludeHandler): class GlobalIncludes(IncludeHandler):