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'):
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."""
if __name__ == '__main__':
- sys.exit(main(sys.argv[1:]))
+ try:
+ sys.exit(main(sys.argv[1:]))
+ except RepaException, error:
+ print >> sys.stderr, error
Setup module
"""
-
+import os
from setuptools import setup
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']
}
)