carlwgeorge / centos / centpkg

Forked from centos/centpkg 3 years ago
Clone
Blob Blame History Raw
'''
    The main behavior of centpkg
'''
#
# Author(s):
#            Jesse Keating <jkeating@redhat.com>
#            Pat Riehecky <riehecky@fnal.gov>
#            Brian Stinson <bstinson@ksu.edu>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.

import os
import sys
import logging

import six.moves.configparser as ConfigParser

import pyrpkg
import pyrpkg.utils
import centpkg.cli


def main():
    """
    Centpkg main.

    """
    program_name = os.path.basename(sys.argv[0])

    default_user_config_path = os.path.join(
        os.path.expanduser('~'), '.config', 'rpkg', '%s.conf' % program_name)

    # Setup an argparser and parse the known commands to get the config file

    # - use the custom ArgumentParser class from pyrpkg.cli and disable
    #   argument abbreviation to ensure that --user will be not treated as
    #   --user-config
    parser = pyrpkg.cli.ArgumentParser(add_help=False, allow_abbrev=False)

    parser.add_argument('-C', '--config', help='Specify a config file to use',
                        default='/etc/rpkg/%s.conf' % program_name)
    parser.add_argument('--user-config', help='Specify a user config file to use',
                        default=default_user_config_path)

    (args, other) = parser.parse_known_args()

    # Make sure we have a sane config file
    if not os.path.exists(args.config) and not other[-1] in ['--help', '-h', 'help']:
        sys.stderr.write('Invalid config file %s\n' % args.config)
        sys.exit(1)

    # Setup a configuration object and read config file data
    config = ConfigParser.SafeConfigParser()
    config.read(args.config)
    config.read(args.user_config)

    client = centpkg.cli.centpkgClient(config, name=program_name)
    client.do_imports(site='centpkg')
    client.parse_cmdline()

    # This is due to a difference argparse behavior to Python 2 version.
    # In Python 3, argparse will proceed to here without reporting
    # "too few arguments". Instead, client.args does not have attribute
    # command.
    if not hasattr(client.args, 'command'):
        client.parser.print_help()
        sys.exit(1)

    if not client.args.path:
        try:
            client.args.path = pyrpkg.utils.getcwd()
        except Exception:
            print('Could not get current path, have you deleted it?')
            sys.exit(1)

    # setup the logger -- This logger will take things of INFO or DEBUG and
    # log it to stdout.  Anything above that (WARN, ERROR, CRITICAL) will go
    # to stderr.  Normal operation will show anything INFO and above.
    # Quiet hides INFO, while Verbose exposes DEBUG.  In all cases WARN or
    # higher are exposed (via stderr).
    log = pyrpkg.log
    client.setupLogging(log)

    if client.args.v:
        log.setLevel(logging.DEBUG)
    elif client.args.q:
        log.setLevel(logging.WARNING)
    else:
        log.setLevel(logging.INFO)

    # Run the necessary command
    try:
        sys.exit(client.args.command())
    except KeyboardInterrupt:
        pass
    except Exception as e:
        if getattr(client.args, 'debug', False):
            raise
        log.error('Could not execute %s: %s' % (client.args.command.__name__, e))
        sys.exit(1)


if __name__ == "__main__":
    main()