提交 3221a1de 编写于 作者: L Lucas Meneghel Rodrigues

Merge pull request #658 from ruda/avocado_conf_cmd_line_option_lukas2

Add option to read a custom configuration from a file ǘ[v2]
...@@ -21,6 +21,7 @@ import sys ...@@ -21,6 +21,7 @@ import sys
import argparse import argparse
from avocado.core import tree from avocado.core import tree
from avocado.core import settings
from avocado.version import VERSION from avocado.version import VERSION
PROG = 'avocado' PROG = 'avocado'
...@@ -45,6 +46,8 @@ class Parser(object): ...@@ -45,6 +46,8 @@ class Parser(object):
self.application.add_argument('--plugins', action='store', self.application.add_argument('--plugins', action='store',
help='Load extra plugins from directory', help='Load extra plugins from directory',
dest='plugins_dir', default='') dest='plugins_dir', default='')
self.application.add_argument('--config', metavar='CONFIG_FILE',
help='Use custom configuration from a file')
def start(self): def start(self):
""" """
...@@ -55,6 +58,10 @@ class Parser(object): ...@@ -55,6 +58,10 @@ class Parser(object):
""" """
self.args, _ = self.application.parse_known_args() self.args, _ = self.application.parse_known_args()
# Load settings from file, if user provides one
if self.args.config is not None:
settings.settings.process_config_path(self.args.config)
# Use parent parsing to avoid breaking the output of --help option # Use parent parsing to avoid breaking the output of --help option
self.application = argparse.ArgumentParser(prog=PROG, self.application = argparse.ArgumentParser(prog=PROG,
description=DESCRIPTION, description=DESCRIPTION,
......
...@@ -166,30 +166,30 @@ class Settings(object): ...@@ -166,30 +166,30 @@ class Settings(object):
config_path_intree]) config_path_intree])
if config_intree: if config_intree:
# In this case, respect only the intree config # In this case, respect only the intree config
self._process_config_path(config_path_intree) self.process_config_path(config_path_intree)
if config_intree_extra: if config_intree_extra:
for extra_file in glob.glob(os.path.join(_config_path_intree_extra, '*.conf')): for extra_file in glob.glob(os.path.join(_config_path_intree_extra, '*.conf')):
self._process_config_path(extra_file) self.process_config_path(extra_file)
self.intree = True self.intree = True
else: else:
# In this case, load first the global config, then the # In this case, load first the global config, then the
# local config overrides the global one # local config overrides the global one
if config_system: if config_system:
self._process_config_path(config_path_system) self.process_config_path(config_path_system)
if config_system_extra: if config_system_extra:
for extra_file in glob.glob(os.path.join(_config_dir_system_extra, '*.conf')): for extra_file in glob.glob(os.path.join(_config_dir_system_extra, '*.conf')):
self._process_config_path(extra_file) self.process_config_path(extra_file)
if not config_local: if not config_local:
path.init_dir(_config_dir_local) path.init_dir(_config_dir_local)
with open(config_path_local, 'w') as config_local_fileobj: with open(config_path_local, 'w') as config_local_fileobj:
config_local_fileobj.write('# You can use this file to override configuration values from ' config_local_fileobj.write('# You can use this file to override configuration values from '
'%s and %s\n' % (config_path_system, _config_dir_system_extra)) '%s and %s\n' % (config_path_system, _config_dir_system_extra))
self._process_config_path(config_path_local) self.process_config_path(config_path_local)
else: else:
# Unittests # Unittests
self._process_config_path(config_path) self.process_config_path(config_path)
def _process_config_path(self, pth): def process_config_path(self, pth):
read_configs = self.config.read(pth) read_configs = self.config.read(pth)
if read_configs: if read_configs:
self.config_paths += read_configs self.config_paths += read_configs
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
SYNOPSIS SYNOPSIS
======== ========
avocado [-h] [-v] [--plugins PLUGINS_DIR] avocado [-h] [-v] [--plugins PLUGINS_DIR] [--config CONFIG_FILE]
{run,list,sysinfo,multiplex,plugins,datadir} ... {run,list,sysinfo,multiplex,plugins,datadir} ...
DESCRIPTION DESCRIPTION
...@@ -31,6 +31,7 @@ on them being loaded:: ...@@ -31,6 +31,7 @@ on them being loaded::
-h, --help show this help message and exit -h, --help show this help message and exit
-v, --version show program's version number and exit -v, --version show program's version number and exit
--plugins PLUGINS_DIR Load extra plugins from directory --plugins PLUGINS_DIR Load extra plugins from directory
--config CONFIG_FILE Use custom configuration from a file
Real use of avocado depends on running avocado subcommands. This a typical list Real use of avocado depends on running avocado subcommands. This a typical list
of avocado subcommands:: of avocado subcommands::
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册