lrossett / centos / centpkg

Forked from centos/centpkg 3 years ago
Clone
b37d1c
import os
b37d1c
import sys
b37d1c
import argparse
b37d1c
import logging
b37d1c
import configparser
b37d1c
b37d1c
import pyrpkg.utils
1963a5
import pyrpkg
b37d1c
# import pyrpkg.cli
b37d1c
b37d1c
import centpkg.api.cli
0807ca
from ..api import lookaside
1963a5
1963a5
1963a5
class Commands(pyrpkg.Commands):
1963a5
    def __init__(self, *args, **kwargs):
1963a5
        """Init the object and some configuration details."""
1963a5
        super(Commands, self).__init__(*args, **kwargs)
1963a5
1963a5
    def lookasidecache(self):
1963a5
        """A helper to interact with the lookaside cache
1963a5
1963a5
        We override this because we need a different download path.
1963a5
        """
1963a5
        @pyrpkg.utils.cached_property
1963a5
        def lookasidecache(self):
1963a5
            return lookaside.CentOSLookasideCache(
1963a5
                self.lookasidehash, self.lookaside, self.lookaside_cgi)
b37d1c
b37d1c
b37d1c
def get_log(client):
b37d1c
    """
b37d1c
    Returns a logging level based on client params.
b37d1c
    :param client: pyrpkg.cli.cliClient
b37d1c
    :return: str
b37d1c
    """
b37d1c
    if client.args.v:
b37d1c
        return logging.DEBUG
b37d1c
    if client.args.q:
b37d1c
        return logging.WARNING
b37d1c
    return logging.INFO
b37d1c
b37d1c
b37d1c
def run(*args, **kwargs):
b37d1c
    """
b37d1c
    Runs centpkg command based on args and kwargs.
b37d1c
    It will use sys.argv as cli arguments if no args or kwargs are provided.
b37d1c
b37d1c
    :param args: list
b37d1c
    :param kwargs: dict
b37d1c
    :return: None
b37d1c
    """
0b5e9d
    cli_name = kwargs.get('cli_name', 'centpkg')
b37d1c
    parser = argparse.ArgumentParser(add_help=False)
1963a5
b37d1c
    parser.add_argument(
b37d1c
        '-C', '--config', help='Specify a config file to use',
0b5e9d
        default=f'/etc/{cli_name}/{cli_name}.conf')
b37d1c
    (args, other) = parser.parse_known_args()
b37d1c
b37d1c
    if not os.path.exists(args.config) and not other[-1] in ['--help', '-h']:
b37d1c
        sys.stderr.write(f'Invalid config file {cli_name} {args.config}\n')
b37d1c
        sys.exit(1)
b37d1c
b37d1c
    config = configparser.SafeConfigParser()
b37d1c
    config.read(args.config)
b37d1c
b37d1c
    client = centpkg.api.cli.CentPkgCli(config)
0b5e9d
    client.do_imports(cli_name)
b37d1c
    client.parse_cmdline()
b37d1c
b37d1c
    if not client.args.path:
b37d1c
        try:
b37d1c
            client.args.path = pyrpkg.utils.getcwd()
b37d1c
        except Exception:
b37d1c
            print('Could not get current path, have you deleted it?')
b37d1c
            sys.exit(1)
b37d1c
b37d1c
    log = pyrpkg.log
b37d1c
    client.setupLogging(log)
b37d1c
    log.setLevel(get_log(client))
b37d1c
b37d1c
    try:
b37d1c
        sys.exit(client.args.command())
b37d1c
    except KeyboardInterrupt:
b37d1c
        pass