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
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)
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)
85a850
32835c
    client = centpkg.cli.centpkgClient(config, name=program_name)
Brian Stinson 3edf28
    client.do_imports(site='centpkg')
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()