lrossett / centos / centpkg

Forked from centos/centpkg 3 years ago
Clone
85a850
'''
85a850
    The main behavior of centpkg
85a850
'''
85a850
#
85a850
# Author(s):
85a850
#            Jesse Keating <jkeating@redhat.com>
85a850
#            Pat Riehecky <riehecky@fnal.gov>
85a850
#            Brian Stinson <bstinson@ksu.edu>
85a850
#
85a850
# This program is free software; you can redistribute it and/or modify it
85a850
# under the terms of the GNU General Public License as published by the
85a850
# Free Software Foundation; either version 2 of the License, or (at your
85a850
# option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
85a850
# the full text of the license.
85a850
85a850
import os
85a850
import sys
85a850
import logging
85a850
Brian Stinson 3edf28
import six.moves.configparser as ConfigParser
Brian Stinson 3edf28
85a850
import pyrpkg
Brian Stinson 3edf28
import pyrpkg.utils
Brian Stinson 3edf28
import centpkg.cli
Brian Stinson 3edf28
85a850
794ab0
def main():
Michal Konečný 0d832c
    """
Michal Konečný 0d832c
    Centpkg main.
Michal Konečný 0d832c
Michal Konečný 0d832c
    """
Brian Stinson 3edf28
    program_name = os.path.basename(sys.argv[0])
c7c239
    is_sig = program_name.endswith('-sig')
Brian Stinson 3edf28
5b6d17
    default_user_config_path = os.path.join(
5b6d17
        os.path.expanduser('~'), '.config', 'rpkg', '%s.conf' % program_name)
5b6d17
5b6d17
    # Setup an argparser and parse the known commands to get the config file
5b6d17
5b6d17
    # - use the custom ArgumentParser class from pyrpkg.cli and disable
5b6d17
    #   argument abbreviation to ensure that --user will be not treated as
5b6d17
    #   --user-config
5b6d17
    parser = pyrpkg.cli.ArgumentParser(add_help=False, allow_abbrev=False)
Michal Konečný 370301
794ab0
    parser.add_argument('-C', '--config', help='Specify a config file to use',
5b6d17
                        default='/etc/rpkg/%s.conf' % program_name)
5b6d17
    parser.add_argument('--user-config', help='Specify a user config file to use',
5b6d17
                        default=default_user_config_path)
c7c239
    parser.add_argument('--lookaside-structure',
c7c239
                        help='set the lookaside cache structure type use',
c7c239
                        choices=['branch', 'hash'],
c7c239
                        default='hash')
85a850
85a850
    (args, other) = parser.parse_known_args()
85a850
85a850
    # Make sure we have a sane config file
Brian Stinson 3edf28
    if not os.path.exists(args.config) and not other[-1] in ['--help', '-h', 'help']:
85a850
        sys.stderr.write('Invalid config file %s\n' % args.config)
85a850
        sys.exit(1)
85a850
Brian Stinson 3edf28
    # Setup a configuration object and read config file data
85a850
    config = ConfigParser.SafeConfigParser()
85a850
    config.read(args.config)
5b6d17
    config.read(args.user_config)
c7c239
    if is_sig:
c7c239
        config[program_name]['lookaside_structure'] = args.lookaside_structure
85a850
c7c239
    client = centpkg.cli.centpkgClient(config, name=program_name)    
Brian Stinson 3edf28
    client.do_imports(site='centpkg')
c7c239
    client.update_argparser(is_sig=is_sig)
85a850
    client.parse_cmdline()
85a850
Brian Stinson 3edf28
    # This is due to a difference argparse behavior to Python 2 version.
Brian Stinson 3edf28
    # In Python 3, argparse will proceed to here without reporting
Brian Stinson 3edf28
    # "too few arguments". Instead, client.args does not have attribute
Brian Stinson 3edf28
    # command.
Brian Stinson 3edf28
    if not hasattr(client.args, 'command'):
Brian Stinson 3edf28
        client.parser.print_help()
Brian Stinson 3edf28
        sys.exit(1)
Brian Stinson 3edf28
85a850
    if not client.args.path:
85a850
        try:
Brian Stinson 3edf28
            client.args.path = pyrpkg.utils.getcwd()
Brian Stinson 3edf28
        except Exception:
Brian Stinson 3edf28
            print('Could not get current path, have you deleted it?')
85a850
            sys.exit(1)
85a850
Brian Stinson 3edf28
    # setup the logger -- This logger will take things of INFO or DEBUG and
Brian Stinson 3edf28
    # log it to stdout.  Anything above that (WARN, ERROR, CRITICAL) will go
Brian Stinson 3edf28
    # to stderr.  Normal operation will show anything INFO and above.
Brian Stinson 3edf28
    # Quiet hides INFO, while Verbose exposes DEBUG.  In all cases WARN or
Brian Stinson 3edf28
    # higher are exposed (via stderr).
85a850
    log = pyrpkg.log
85a850
    client.setupLogging(log)
85a850
85a850
    if client.args.v:
85a850
        log.setLevel(logging.DEBUG)
85a850
    elif client.args.q:
85a850
        log.setLevel(logging.WARNING)
85a850
    else:
85a850
        log.setLevel(logging.INFO)
85a850
85a850
    # Run the necessary command
85a850
    try:
85a850
        sys.exit(client.args.command())
85a850
    except KeyboardInterrupt:
85a850
        pass
Brian Stinson 3edf28
    except Exception as e:
Brian Stinson 3edf28
        if getattr(client.args, 'debug', False):
Brian Stinson 3edf28
            raise
Brian Stinson 13c432
        log.error('Could not execute %s: %s' % (client.args.command.__name__, e))
Brian Stinson 13c432
        sys.exit(1)
85a850
Brian Stinson 3edf28
Brian Stinson 3edf28
if __name__ == "__main__":
85a850
    main()