From: Thanh Ha Date: Mon, 30 Apr 2018 18:16:03 +0000 (-0400) Subject: Add configuration subsystem X-Git-Tag: v0.14.0~2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F73%2F10273%2F7;p=releng%2Flftools.git Add configuration subsystem Create a basic ini based configuration subsystem to be used in the future by lftools subsystems as a single point of pulling configuration from. Usage: lftools config get section [OPTION] lftools config set section option value Example: lftools config set global username zxiiro lftools config get global Issue: RELENG-915 Change-Id: I346662ae2c623538e8a6881574dad357a8ae4dee Signed-off-by: Thanh Ha --- diff --git a/docs/commands/config.rst b/docs/commands/config.rst new file mode 100644 index 00000000..341d80df --- /dev/null +++ b/docs/commands/config.rst @@ -0,0 +1,21 @@ +****** +Config +****** + +.. program-output:: lftools config --help + +Commands +======== + +.. contents:: Config Commands + :local: + +get +--- + +.. program-output:: lftools config get --help + +set +---- + +.. program-output:: lftools config set --help diff --git a/docs/commands/index.rst b/docs/commands/index.rst index c4bd35b5..86e449ba 100644 --- a/docs/commands/index.rst +++ b/docs/commands/index.rst @@ -8,6 +8,7 @@ bash. It supports the following commands. .. toctree:: :maxdepth: 2 + config deploy license nexus diff --git a/lftools/cli/__init__.py b/lftools/cli/__init__.py index ff844ffb..635941af 100644 --- a/lftools/cli/__init__.py +++ b/lftools/cli/__init__.py @@ -13,6 +13,7 @@ __author__ = 'Thanh Ha' import click +from lftools.cli.config import config_sys from lftools.cli.deploy import deploy from lftools.cli.jenkins import jenkins_cli from lftools.cli.license import license @@ -29,6 +30,7 @@ def cli(ctx): pass +cli.add_command(config_sys) cli.add_command(deploy) cli.add_command(jenkins_cli, name='jenkins') cli.add_command(license) diff --git a/lftools/cli/config.py b/lftools/cli/config.py new file mode 100644 index 00000000..c671c8ce --- /dev/null +++ b/lftools/cli/config.py @@ -0,0 +1,52 @@ +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""CLI interface for config subsystem.""" + +__author__ = 'Thanh Ha' + + +import click + +from lftools import config + + +@click.group(name='config') +@click.pass_context +def config_sys(ctx): + """Configuration subsystem.""" + pass + + +@click.command(name='get') +@click.argument('section', type=str) +@click.argument('option', type=str, required=False) +@click.pass_context +def get_setting(ctx, section, option): + """Print section or setting from config file.""" + result = config.get_setting(section, option) + if isinstance(result, list): + for i in result: + print('{}: {}'.format(i, config.get_setting(section, i))) + else: + print(result) + + +@click.command(name='set') +@click.argument('section') +@click.argument('option') +@click.argument('value') +@click.pass_context +def set_setting(ctx, section, option, value): + """Set a setting in the config file.""" + config.set_setting(section, option, value) + + +config_sys.add_command(get_setting) +config_sys.add_command(set_setting) diff --git a/lftools/config.py b/lftools/config.py new file mode 100644 index 00000000..c6abc9af --- /dev/null +++ b/lftools/config.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""Configuration subsystem for lftools.""" + +__author__ = 'Thanh Ha' + +import sys + +from six.moves import configparser +from xdg import XDG_CONFIG_HOME + +LFTOOLS_CONFIG_FILE = '/'.join([XDG_CONFIG_HOME, 'lftools', 'lftools.ini']) + + +def get_config(): + """Get the config object.""" + config = configparser.ConfigParser() + config.read(LFTOOLS_CONFIG_FILE) + return config + + +def get_setting(section, option=None): + """Get a configuration from a section.""" + config = get_config() + + if option: + try: + return config.get(section, option) + except configparser.NoOptionError: + print('ERROR: Config option does not exist.') + sys.exit(1) + except configparser.NoSectionError: + print('ERROR: Config section does not exist.') + sys.exit(1) + + else: + try: + return config.options(section) + except configparser.NoSectionError: + print('ERROR: Config section does not exist.') + sys.exit(1) + + +def set_setting(section, option, value): + """Save a configuration setting to config file.""" + config = get_config() + config.set(section, option, value) + + with open(LFTOOLS_CONFIG_FILE, 'w') as configfile: + config.write(configfile) diff --git a/requirements.txt b/requirements.txt index 2015e890..3df89f7b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,10 @@ click pyyaml requests~=2.18.0 setuptools>=36.5.0 +six~=1.11.0 python-jenkins~=1.1.0 +xdg~=1.0.7;python_version<'3' +xdg~=3.0.0;python_version>='3' # workarounds to prevent upstream from breaking us netifaces==0.10.5