From e5ce4ed3104f84fecae02e3867d80a7d79fe6010 Mon Sep 17 00:00:00 2001 From: Ed Bartosh Date: Sun, 11 Aug 2013 18:17:34 +0300 Subject: Implemented config file processing Configuration is taken from /etc/repa.conf and ~/.repa.conf, so it's possible to specify system defaults in /etc/repa.conf and user-related configuration options in ~/.repa.conf. ~/.repa.conf has higher priority and any option from /etc/repa.conf can be reset in ~/.repa.conf Configuration processing is implemented in a way that default values for command line parameters are taken from configuration file(s). Configuration section to use can be specified in command line too. Fixes: #1141 Change-Id: I632f22674c282021983bc6a496ef1fccc0da3137 Signed-off-by: Ed Bartosh Reviewed-on: https://otctools.jf.intel.com/review/5839 Tested-by: OTC Tools Tester Reviewed-by: Hasan Wan --- packaging/repa.spec | 1 + repa.conf | 9 +++++++ repa/group.py | 2 +- repa/list.py | 10 ++++--- repa/main.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++----- setup.py | 10 ++++--- 6 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 repa.conf diff --git a/packaging/repa.spec b/packaging/repa.spec index bea2d0a..176d0b0 100644 --- a/packaging/repa.spec +++ b/packaging/repa.spec @@ -35,6 +35,7 @@ rm -rf %{buildroot} %files %defattr(-,root,root,-) +%config %{_sysconfdir}/%{name}.conf %{_prefix}/share/doc/packages/%{name} %{python_sitelib}/%{name}-%{version}-*.egg-info %{python_sitelib}/%{name}-%{version}-*-nspkg.pth diff --git a/repa.conf b/repa.conf new file mode 100644 index 0000000..6e16037 --- /dev/null +++ b/repa.conf @@ -0,0 +1,9 @@ +[general] +#apiurl = +#apiuser = +#apipasswd = +list_regexp = .* + +[tizen.org] +apiurl = https://api.tizen.org + diff --git a/repa/group.py b/repa/group.py index 4d8140f..0c3089a 100755 --- a/repa/group.py +++ b/repa/group.py @@ -23,7 +23,7 @@ class Group(object): help = description @staticmethod - def add_arguments(parser): + def add_arguments(parser, _config): """Adds arguments to the parser. Called from [sub_]main.""" parser.add_argument('--comment', help='comment') diff --git a/repa/list.py b/repa/list.py index 6359502..e8c493b 100755 --- a/repa/list.py +++ b/repa/list.py @@ -23,9 +23,13 @@ class List(object): help = description @staticmethod - def add_arguments(parser): - """Adds arguments to the parser. Called from [sub_]main.""" - parser.add_argument('-regexp', default='.*', help='search regexp') + def add_arguments(parser, config): + """ + Add arguments to the parser. Called from [sub_]main. + Set defaults for arguments from config. + """ + parser.add_argument('-regexp', help='search regexp', + default=config.get('list_regexp', '.*')) def run(self, argv): """Command line entry point. Called from [sub_]main""" diff --git a/repa/main.py b/repa/main.py index 7dc63b8..1f1c402 100755 --- a/repa/main.py +++ b/repa/main.py @@ -13,13 +13,32 @@ Command line parsing, script entry point. import sys import pkg_resources +import ConfigParser +from os.path import expanduser from argparse import ArgumentParser +class RepaException(Exception): + """Custom repa exception. All repa modules should use it.""" + pass + + def parse_args(argv): - """Command line parsing.""" - parser = ArgumentParser(description='Release Engineering Process Assistant') + """ + Parse command line. Read config. + Set arguments to config values. + """ + # create parser to parse -section + parser = ArgumentParser(add_help=False) + parser.add_argument('-section', default='general', + help='config section to use') + # read config section, specified in command line + parsed, argv = parser.parse_known_args(argv) + config = read_config(section=parsed.section) + + # recreate parser to parse rest of the command line + parser = ArgumentParser(description='Release Engineering Process Assistant') # Define subcommands using setup.py entry points subparsers = parser.add_subparsers(help='sub-command help') for entry in pkg_resources.iter_entry_points(group='repa_commands'): @@ -28,16 +47,56 @@ def parse_args(argv): description=cmd.description, help=cmd.help) if hasattr(cmd, 'add_arguments'): - cmd.add_arguments(parser_cmd) + cmd.add_arguments(parser_cmd, config) parser_cmd.set_defaults(func=cmd.run) - return parser.parse_args(argv) + args = parser.parse_args(argv) + + return update_args(config, args) + + +def read_config(paths=('/etc/repa.conf', expanduser('~/.repa.conf')), + section='general', + mandatory=('apiurl', 'apiuser', 'apipasswd')): + """ + Read repa config. + Configuration is read from the set of files provided. + Optional section name can be specified to read options from + """ + conf = ConfigParser.RawConfigParser() + if not conf.read(paths): + raise RepaException("Configuration file not found") + if not conf.has_section(section): + raise RepaException("Section '%s' doesn't exist in config" % section) + + # read configuration from 'general' and then from specified section + # to have defaults set properly + config = dict(conf.items('general')) + if section != 'general': + config.update(conf.items(section)) + + # Check mandatory config options + missed = set(mandatory).difference(set(config.keys())) + if missed: + raise RepaException("Missing mandatory config options: %s" % \ + ','.join(missed)) + return config + +def update_args(config, args): + """Set configuration options as args attributes.""" + for key, val in config.iteritems(): + if not hasattr(args, key): + setattr(args, key, val) + return args def sub_main(argv, cmd): """Subcommand entry point.""" parser = ArgumentParser(description=cmd.description) - cmd.add_arguments(parser) - return cmd.run(parser.parse_args(argv)) + config = read_config() + cmd.add_arguments(parser, config) + args = parser.parse_args(argv) + return cmd.run(update_args(config, args)) + def main(argv=sys.argv[1:]): """Command line entry point.""" @@ -46,4 +105,7 @@ def main(argv=sys.argv[1:]): if __name__ == '__main__': - sys.exit(main(sys.argv[1:])) + try: + sys.exit(main(sys.argv[1:])) + except RepaException, error: + print >> sys.stderr, error diff --git a/setup.py b/setup.py index 6c38f90..a1f5e0f 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ Author: Ed Bartosh Setup module """ - +import os from setuptools import setup setup(name = "repa", @@ -18,10 +18,12 @@ setup(name = "repa", author_email = 'eduard.bartosh@intel.com', packages = ['repa'], namespace_packages = ['repa'], - data_files = [('share/doc/packages/repa/', ['README']),], + data_files = [ + ('share/doc/packages/repa/', ['README']), + (['/etc', 'etc'][bool(os.getenv('VIRTUAL_ENV'))], ['repa.conf'])], entry_points = { 'console_scripts': ['repa = repa.main:main'], - 'repa_commands': ['list = repa.list:List', - 'group = repa.group:Group'] + 'repa_commands': ['list = repa.list:List', + 'group = repa.group:Group'] } ) -- cgit v1.2.3