Files @ 1d539bb18165
Branch filter:

Location: kallithea/kallithea/lib/paster_commands/common.py

Thomas De Schampheleire
cli: convert 'gearbox celeryd' into 'kallithea-cli celery-run'

Note:
- '--' is never explicitly present in the arguments when using Click.
The click parser will take care of '--' as separator between
dash-dash-arguments and positional arguments, following standard UNIX
conventions.
# -*- coding: utf-8 -*-
# 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
kallithea.lib.paster_commands.common
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Common code for gearbox commands.

This file was forked by the Kallithea project in July 2014.
Original author and date, and relevant copyright and licensing information is below:
:created_on: Apr 18, 2010
:author: marcink
:copyright: (c) 2013 RhodeCode GmbH, and others.
:license: GPLv3, see LICENSE.md for more details.
"""

import os
import sys
import logging.config

import paste.deploy
import gearbox.command

import kallithea.config.middleware
import kallithea.model.base
import kallithea.lib.utils2
import kallithea.lib.utils


def ask_ok(prompt, retries=4, complaint='Yes or no please!'):
    while True:
        ok = raw_input(prompt)
        if ok in ('y', 'ye', 'yes'):
            return True
        if ok in ('n', 'no', 'nop', 'nope'):
            return False
        retries = retries - 1
        if retries < 0:
            raise IOError
        print complaint


class BasePasterCommand(gearbox.command.Command):
    """
    Abstract Base Class for gearbox commands.
    """

    # override to control how much get_parser and run should do:
    takes_config_file = True
    requires_db_session = True
    config = None # set to the actual config object in run if takes_config_file is true

    def run(self, args):
        """
        Overrides Command.run

        If needed by the command, read config file and initialize database before running.
        """
        if self.takes_config_file:
            path_to_ini_file = os.path.realpath(args.config_file)
            self.config = paste.deploy.appconfig('config:' + path_to_ini_file)
            # TODO: also initialize kallithea.CONFIG?
            logging.config.fileConfig(path_to_ini_file)

            if self.requires_db_session:
                kallithea.config.middleware.make_app_without_logging(self.config.global_conf, **self.config.local_conf)
                # *now*, tg.config has been set and could be used ... but we just keep using self.config
                kallithea.lib.utils.setup_cache_regions(self.config)

        return super(BasePasterCommand, self).run(args)

    def get_parser(self, prog_name):
        parser = super(BasePasterCommand, self).get_parser(prog_name)

        if self.takes_config_file:
            parser.add_argument("-c", "--config",
                help='Kallithea .ini file with configuration of database etc',
                dest='config_file', required=True)

        return parser

    def error(self, msg, exitcode=1):
        """Write error message and exit"""
        sys.stderr.write('%s\n' % msg)
        raise SystemExit(exitcode)