lrossett / centos / centpkg

Forked from centos/centpkg a year ago
Clone
Pat Riehecky 85a850
'''
Pat Riehecky 85a850
    The main behavior of centpkg
Pat Riehecky 85a850
'''
Pat Riehecky 85a850
#
Pat Riehecky 85a850
# Author(s):
Pat Riehecky 85a850
#            Jesse Keating <jkeating@redhat.com>
Pat Riehecky 85a850
#            Pat Riehecky <riehecky@fnal.gov>
Pat Riehecky 85a850
#            Brian Stinson <bstinson@ksu.edu>
Pat Riehecky 85a850
#
Pat Riehecky 85a850
# This program is free software; you can redistribute it and/or modify it
Pat Riehecky 85a850
# under the terms of the GNU General Public License as published by the
Pat Riehecky 85a850
# Free Software Foundation; either version 2 of the License, or (at your
Pat Riehecky 85a850
# option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
Pat Riehecky 85a850
# the full text of the license.
Pat Riehecky 85a850
Pat Riehecky 85a850
import os
Pat Riehecky 85a850
import sys
Pat Riehecky 85a850
import logging
Pat Riehecky 85a850
Brian Stinson 3edf28
import six.moves.configparser as ConfigParser
Brian Stinson 3edf28
Pat Riehecky 85a850
import pyrpkg
Brian Stinson 3edf28
import pyrpkg.utils
Brian Stinson 3edf28
import centpkg.cli
Brian Stinson 3edf28
Pat Riehecky 85a850
32835c
def main():
Michal Konečný 0d832c
    """
Michal Konečný 0d832c
    Centpkg main.
Michal Konečný 0d832c
Michal Konečný 0d832c
    """
Brian Stinson 3edf28
    # Setup an argparser and parse the known commands to get the config file
Brian Stinson 3edf28
    program_name = os.path.basename(sys.argv[0])
Brian Stinson 3edf28
Brian Stinson 3edf28
    # Modified ArgumentParser provides parameter "allow_abbrev=False" (which affects processing
Brian Stinson 3edf28
    # of commandline arguments with common prefix). Generaly it is available since python3.6.
32835c
    
Brian Stinson 3edf28
    # This enables "allow_abbrev" for older python versions.
Brian Stinson 3edf28
    parser = pyrpkg.cli.ArgumentParser(add_help=False)
Michal Konečný 370301
32835c
    parser.add_argument('-C', '--config', help='Specify a config file to use',
32835c
                            default=f'/etc/rpkg/%s.conf' % program_name)
Pat Riehecky 85a850
Pat Riehecky 85a850
    (args, other) = parser.parse_known_args()
Pat Riehecky 85a850
Pat Riehecky 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']:
Pat Riehecky 85a850
        sys.stderr.write('Invalid config file %s\n' % args.config)
Pat Riehecky 85a850
        sys.exit(1)
Pat Riehecky 85a850
Brian Stinson 3edf28
    # Setup a configuration object and read config file data
Pat Riehecky 85a850
    config = ConfigParser.SafeConfigParser()
Pat Riehecky 85a850
    config.read(args.config)
Pat Riehecky 85a850
32835c
    client = centpkg.cli.centpkgClient(config, name=program_name)
Brian Stinson 3edf28
    client.do_imports(site='centpkg')
Pat Riehecky 85a850
    client.parse_cmdline()
Pat Riehecky 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
Pat Riehecky 85a850
    if not client.args.path:
Pat Riehecky 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?')
Pat Riehecky 85a850
            sys.exit(1)
Pat Riehecky 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).
Pat Riehecky 85a850
    log = pyrpkg.log
Pat Riehecky 85a850
    client.setupLogging(log)
Pat Riehecky 85a850
Pat Riehecky 85a850
    if client.args.v:
Pat Riehecky 85a850
        log.setLevel(logging.DEBUG)
Pat Riehecky 85a850
    elif client.args.q:
Pat Riehecky 85a850
        log.setLevel(logging.WARNING)
Pat Riehecky 85a850
    else:
Pat Riehecky 85a850
        log.setLevel(logging.INFO)
Pat Riehecky 85a850
Pat Riehecky 85a850
    # Run the necessary command
Pat Riehecky 85a850
    try:
Pat Riehecky 85a850
        sys.exit(client.args.command())
Pat Riehecky 85a850
    except KeyboardInterrupt:
Pat Riehecky 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)
Pat Riehecky 85a850
Brian Stinson 3edf28
Brian Stinson 3edf28
if __name__ == "__main__":
Pat Riehecky 85a850
    main()