mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-27 12:04:27 +01:00
cli-function: raise if the config file is given and does not exist
This commit is contained in:
@@ -147,15 +147,15 @@ class CliFunction:
|
|||||||
def _do_main(self, kwargs):
|
def _do_main(self, kwargs):
|
||||||
return self.main(**self._get_args(kwargs))
|
return self.main(**self._get_args(kwargs))
|
||||||
|
|
||||||
def __init__(self, config_file=None, description=None, extra_config_params=None):
|
def __init__(self, default_config_file=None, description=None, extra_config_params=None):
|
||||||
self._arguments = collections.OrderedDict()
|
self._arguments = collections.OrderedDict()
|
||||||
self._config_file = config_file
|
self._default_config_file = default_config_file
|
||||||
self._description = description
|
self._description = description
|
||||||
self.extra_config_params = extra_config_params
|
self.extra_config_params = extra_config_params
|
||||||
if self._config_file is not None:
|
if self._default_config_file is not None:
|
||||||
self.add_argument(
|
self.add_argument(
|
||||||
'--config-file',
|
'--config-file',
|
||||||
default=self._config_file,
|
default=self._default_config_file,
|
||||||
help='Path to the configuration file to use'
|
help='Path to the configuration file to use'
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -172,30 +172,35 @@ class CliFunction:
|
|||||||
args_with_defaults = kwargs.copy()
|
args_with_defaults = kwargs.copy()
|
||||||
# Add missing args from config file.
|
# Add missing args from config file.
|
||||||
config_file = None
|
config_file = None
|
||||||
|
args_given = {}
|
||||||
if 'config_file' in args_with_defaults and args_with_defaults['config_file'] is not None:
|
if 'config_file' in args_with_defaults and args_with_defaults['config_file'] is not None:
|
||||||
config_file = args_with_defaults['config_file']
|
config_file = args_with_defaults['config_file']
|
||||||
|
args_given['config_file'] = True
|
||||||
else:
|
else:
|
||||||
config_file = self._config_file
|
config_file = self._default_config_file
|
||||||
args_given = {}
|
args_given['config_file'] = False
|
||||||
for key in self._arguments:
|
for key in self._arguments:
|
||||||
args_given[key] = not (
|
args_given[key] = not (
|
||||||
not key in args_with_defaults or
|
not key in args_with_defaults or
|
||||||
args_with_defaults[key] is None or
|
args_with_defaults[key] is None or
|
||||||
self._arguments[key].nargs == '*' and args_with_defaults[key] == []
|
self._arguments[key].nargs == '*' and args_with_defaults[key] == []
|
||||||
)
|
)
|
||||||
if config_file is not None and os.path.exists(config_file):
|
if config_file is not None:
|
||||||
config_configs = {}
|
if os.path.exists(config_file):
|
||||||
config = imp.load_source('config', config_file)
|
config_configs = {}
|
||||||
if self.extra_config_params is None:
|
config = imp.load_source('config', config_file)
|
||||||
config.set_args(config_configs)
|
if self.extra_config_params is None:
|
||||||
else:
|
config.set_args(config_configs)
|
||||||
config.set_args(config_configs, self.extra_config_params)
|
else:
|
||||||
for key in config_configs:
|
config.set_args(config_configs, self.extra_config_params)
|
||||||
if key not in self._arguments:
|
for key in config_configs:
|
||||||
raise Exception('Unknown key in config file: ' + key)
|
if key not in self._arguments:
|
||||||
if not args_given[key]:
|
raise Exception('Unknown key in config file: ' + key)
|
||||||
args_with_defaults[key] = config_configs[key]
|
if not args_given[key]:
|
||||||
args_given[key] = True
|
args_with_defaults[key] = config_configs[key]
|
||||||
|
args_given[key] = True
|
||||||
|
elif args_given['config_file']:
|
||||||
|
raise Exception('Config file does not exist: ' + config_file)
|
||||||
# Add missing args from hard-coded defaults.
|
# Add missing args from hard-coded defaults.
|
||||||
for key in self._arguments:
|
for key in self._arguments:
|
||||||
argument = self._arguments[key]
|
argument = self._arguments[key]
|
||||||
@@ -329,7 +334,7 @@ if __name__ == '__main__':
|
|||||||
class OneCliFunction(CliFunction):
|
class OneCliFunction(CliFunction):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
config_file='cli_function_test_config.py',
|
default_config_file='cli_function_test_config.py',
|
||||||
description = '''\
|
description = '''\
|
||||||
Description of this
|
Description of this
|
||||||
amazing function!
|
amazing function!
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ consts['userland_out_exts'] = [
|
|||||||
consts['userland_executable_ext'],
|
consts['userland_executable_ext'],
|
||||||
consts['obj_ext'],
|
consts['obj_ext'],
|
||||||
]
|
]
|
||||||
consts['config_file'] = os.path.join(consts['data_dir'], 'config.py')
|
consts['default_config_file'] = os.path.join(consts['data_dir'], 'config.py')
|
||||||
consts['magic_fail_string'] = b'lkmc_test_fail'
|
consts['magic_fail_string'] = b'lkmc_test_fail'
|
||||||
consts['baremetal_lib_basename'] = 'lib'
|
consts['baremetal_lib_basename'] = 'lib'
|
||||||
consts['emulator_userland_only_short_to_long_dict'] = collections.OrderedDict([
|
consts['emulator_userland_only_short_to_long_dict'] = collections.OrderedDict([
|
||||||
@@ -156,7 +156,7 @@ class LkmcCliFunction(cli_function.CliFunction):
|
|||||||
:ptype defaults: Dict[str,Any]
|
:ptype defaults: Dict[str,Any]
|
||||||
:param defaults: override the default value of an argument
|
:param defaults: override the default value of an argument
|
||||||
'''
|
'''
|
||||||
kwargs['config_file'] = consts['config_file']
|
kwargs['default_config_file'] = consts['default_config_file']
|
||||||
kwargs['extra_config_params'] = os.path.basename(inspect.getfile(self.__class__))
|
kwargs['extra_config_params'] = os.path.basename(inspect.getfile(self.__class__))
|
||||||
if defaults is None:
|
if defaults is None:
|
||||||
defaults = {}
|
defaults = {}
|
||||||
|
|||||||
Reference in New Issue
Block a user