[PATCH 2 of 7 WIPv2] cli: initial introduction of 'kallithea-cli' command

Thomas De Schampheleire patrickdepinguin at gmail.com
Mon Oct 8 19:57:33 UTC 2018


# HG changeset patch
# User Thomas De Schampheleire <thomas.de_schampheleire at nokia.com>
# Date 1538510419 -7200
#      Tue Oct 02 22:00:19 2018 +0200
# Node ID a435806d3f32519ad5d15ed49cee298b58e9d86c
# Parent  52a3c6c437428b0ce8e3817ff37fba4903a0a427
cli: initial introduction of 'kallithea-cli' command

This commit adds a command 'kallithea-cli' that intends to replace the
existing set of 'gearbox' commands that relate to setting up kallithea.
Gearbox would still be used for 'gearbox serve', but other commands like
'make-config', 'setup-db', etc. would be converted to 'kallithea-cli'
commands.

The python package 'Click' is used to generate the CLI. Using decorators,
this package makes it very easy to generate a CLI out of simple methods.
See: http://click.pocoo.org/6/

Using Gearbox for custom commands is possible, but documentation on this
topic is limited. As the added value of Gearbox for that use case is not
clear, we can well switch to something else if it seems better/easier.

diff --git a/kallithea/bin/kallithea_cli.py b/kallithea/bin/kallithea_cli.py
new file mode 100644
--- /dev/null
+++ b/kallithea/bin/kallithea_cli.py
@@ -0,0 +1,20 @@
+# -*- 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/>.
+
+import click
+
+ at click.group()
+def cli():
+    """Various commands to set up a Kallithea instance."""
+    pass
diff --git a/kallithea/bin/kallithea_cli_util.py b/kallithea/bin/kallithea_cli_util.py
new file mode 100644
--- /dev/null
+++ b/kallithea/bin/kallithea_cli_util.py
@@ -0,0 +1,58 @@
+# -*- 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/>.
+
+import click
+import functools
+import os
+
+import kallithea
+import logging.config
+import paste.deploy
+
+def auto_setup_app(config_only=False):
+    """Decorator for commands that need the application to be set up.
+
+    A config_file argument will automatically be added to the command,
+    but the command method needs to accept a 'config_file' argument.
+
+    If config_only is set to True, only the configuration will be set up, the
+    application is not fully set up. The configuration is accessible as
+    kallithea.CONFIG.
+    If config_only is False, the application is fully initialized.
+    """
+    def auto_setup_app_decorator(func):
+        @functools.wraps(func)
+        def wrapper(config_file, *args, **kwargs):
+            path_to_ini_file = os.path.realpath(config_file)
+            config = kallithea.CONFIG = paste.deploy.appconfig('config:' + path_to_ini_file)
+            logging.config.fileConfig(path_to_ini_file)
+
+            if not config_only:
+                kallithea.config.middleware.make_app_without_logging(config.global_conf, **config.local_conf)
+                # *now*, tg.config has been set and could be used ... but we just keep using config
+                kallithea.lib.utils.setup_cache_regions(config)
+
+            return func(config_file, *args, **kwargs)
+        # add config file argument using click.argument decorator
+        return click.argument('config_file', type=click.Path())(wrapper)
+    return auto_setup_app_decorator
+
+def full_cmd_name():
+    """Return the full command being executed (without its options)"""
+    ctx = click.get_current_context()
+    cmd = [ctx.info_name]
+    while ctx.parent:
+        cmd.insert(0, ctx.parent.info_name)
+        ctx = ctx.parent
+    return ' '.join(cmd)
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -60,6 +60,7 @@ requirements = [
     "mercurial >= 4.1.1, < 4.8",
     "decorator >= 3.3.2, < 4.4",
     "Paste >= 2.0.3, < 3",
+    "Click >= 7.0, < 8",
 ]
 
 if sys.version_info < (2, 7):
@@ -151,6 +152,7 @@ setuptools.setup(
     kallithea-api =    kallithea.bin.kallithea_api:main
     kallithea-gist =   kallithea.bin.kallithea_gist:main
     kallithea-config = kallithea.bin.kallithea_config:main
+    kallithea-cli =    kallithea.bin.kallithea_cli:cli
 
     [paste.app_factory]
     main = kallithea.config.middleware:make_app


More information about the kallithea-general mailing list