changeset 6065:e0f31c7d0f5e

paster: split paster specifics out of kallithea.lib.utils BasePasterCommand and ask_ok are only useful in a Paster/command-line context, and can thus be removed from the already overly cluttered main utils module. (The new common.py has been added to Mercurial as a copy of utils.py, preserving its file history, but creating a somewhat bewildering diff.)
author Søren Løvborg <sorenl@unity3d.com>
date Thu, 28 Jul 2016 14:21:08 +0200
parents 9a35244c35b6
children e8565d50d064
files kallithea/lib/celerypylons/commands.py kallithea/lib/db_manage.py kallithea/lib/paster_commands/cache_keys.py kallithea/lib/paster_commands/cleanup.py kallithea/lib/paster_commands/common.py kallithea/lib/paster_commands/ishell.py kallithea/lib/paster_commands/make_index.py kallithea/lib/paster_commands/make_rcextensions.py kallithea/lib/paster_commands/repo_scan.py kallithea/lib/paster_commands/update_repoinfo.py kallithea/lib/utils.py
diffstat 11 files changed, 126 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/celerypylons/commands.py	Thu Apr 07 17:53:51 2016 +0200
+++ b/kallithea/lib/celerypylons/commands.py	Thu Jul 28 14:21:08 2016 +0200
@@ -1,7 +1,8 @@
 # -*- coding: utf-8 -*-
 
 import kallithea
-from kallithea.lib.utils import BasePasterCommand, Command, load_rcextensions
+from kallithea.lib.paster_commands.common import BasePasterCommand
+from kallithea.lib.utils import Command, load_rcextensions
 from celery.app import app_or_default
 from celery.bin import camqadm, celerybeat, celeryd, celeryev
 
--- a/kallithea/lib/db_manage.py	Thu Apr 07 17:53:51 2016 +0200
+++ b/kallithea/lib/db_manage.py	Thu Jul 28 14:21:08 2016 +0200
@@ -37,8 +37,8 @@
 import alembic.command
 
 from kallithea import __dbversion__, __py_version__, EXTERN_TYPE_INTERNAL
+from kallithea.lib.paster_commands.common import ask_ok
 from kallithea.model.user import UserModel
-from kallithea.lib.utils import ask_ok
 from kallithea.model import init_model
 from kallithea.model.db import User, Permission, Ui, \
     Setting, UserToPerm, RepoGroup, \
--- a/kallithea/lib/paster_commands/cache_keys.py	Thu Apr 07 17:53:51 2016 +0200
+++ b/kallithea/lib/paster_commands/cache_keys.py	Thu Jul 28 14:21:08 2016 +0200
@@ -30,8 +30,8 @@
 import os
 import sys
 
+from kallithea.lib.paster_commands.common import BasePasterCommand
 from kallithea.model.meta import Session
-from kallithea.lib.utils import BasePasterCommand
 from kallithea.lib.utils2 import safe_str
 from kallithea.model.db import CacheInvalidation
 
--- a/kallithea/lib/paster_commands/cleanup.py	Thu Apr 07 17:53:51 2016 +0200
+++ b/kallithea/lib/paster_commands/cleanup.py	Thu Jul 28 14:21:08 2016 +0200
@@ -33,7 +33,8 @@
 import shutil
 import datetime
 
-from kallithea.lib.utils import BasePasterCommand, ask_ok, REMOVED_REPO_PAT
+from kallithea.lib.paster_commands.common import ask_ok, BasePasterCommand
+from kallithea.lib.utils import REMOVED_REPO_PAT
 from kallithea.lib.utils2 import safe_str
 from kallithea.model.db import Ui
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/lib/paster_commands/common.py	Thu Jul 28 14:21:08 2016 +0200
@@ -0,0 +1,113 @@
+# -*- 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 Paster 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 logging
+
+import paste
+from paste.script.command import Command, BadCommand
+
+from kallithea.lib.utils import add_cache
+
+
+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(Command):
+    """
+    Abstract Base Class for paster commands.
+
+    The celery commands are somewhat aggressive about loading
+    celery.conf, and since our module sets the `CELERY_LOADER`
+    environment variable to our loader, we have to bootstrap a bit and
+    make sure we've had a chance to load the pylons config off of the
+    command line, otherwise everything fails.
+    """
+    min_args = 1
+    min_args_error = "Please provide a paster config file as an argument."
+    takes_config_file = 1
+    requires_config_file = True
+
+    def run(self, args):
+        """
+        Overrides Command.run
+
+        Checks for a config file argument and loads it.
+        """
+        if len(args) < self.min_args:
+            raise BadCommand(
+                self.min_args_error % {'min_args': self.min_args,
+                                       'actual_args': len(args)})
+
+        # Decrement because we're going to lob off the first argument.
+        # @@ This is hacky
+        self.min_args -= 1
+        self.bootstrap_config(args[0])
+        self.update_parser()
+        return super(BasePasterCommand, self).run(args[1:])
+
+    def update_parser(self):
+        """
+        Abstract method.  Allows for the class's parser to be updated
+        before the superclass's `run` method is called.  Necessary to
+        allow options/arguments to be passed through to the underlying
+        celery command.
+        """
+        raise NotImplementedError("Abstract Method.")
+
+    def bootstrap_config(self, conf):
+        """
+        Loads the pylons configuration.
+        """
+        from pylons import config as pylonsconfig
+
+        self.path_to_ini_file = os.path.realpath(conf)
+        conf = paste.deploy.appconfig('config:' + self.path_to_ini_file)
+        pylonsconfig.init_app(conf.global_conf, conf.local_conf)
+
+    def _init_session(self):
+        """
+        Inits SqlAlchemy Session
+        """
+        logging.config.fileConfig(self.path_to_ini_file)
+
+        from pylons import config
+        from kallithea.model import init_model
+        from kallithea.lib.utils2 import engine_from_config
+        add_cache(config)
+        engine = engine_from_config(config, 'sqlalchemy.db1.')
+        init_model(engine)
--- a/kallithea/lib/paster_commands/ishell.py	Thu Apr 07 17:53:51 2016 +0200
+++ b/kallithea/lib/paster_commands/ishell.py	Thu Jul 28 14:21:08 2016 +0200
@@ -29,7 +29,7 @@
 import os
 import sys
 
-from kallithea.lib.utils import BasePasterCommand
+from kallithea.lib.paster_commands.common import BasePasterCommand
 
 # Add location of top level folder to sys.path
 from os.path import dirname
--- a/kallithea/lib/paster_commands/make_index.py	Thu Apr 07 17:53:51 2016 +0200
+++ b/kallithea/lib/paster_commands/make_index.py	Thu Jul 28 14:21:08 2016 +0200
@@ -31,7 +31,8 @@
 
 from string import strip
 from kallithea.model.repo import RepoModel
-from kallithea.lib.utils import BasePasterCommand, load_rcextensions
+from kallithea.lib.paster_commands.common import BasePasterCommand
+from kallithea.lib.utils import load_rcextensions
 
 # Add location of top level folder to sys.path
 from os.path import dirname
--- a/kallithea/lib/paster_commands/make_rcextensions.py	Thu Apr 07 17:53:51 2016 +0200
+++ b/kallithea/lib/paster_commands/make_rcextensions.py	Thu Jul 28 14:21:08 2016 +0200
@@ -30,7 +30,7 @@
 import sys
 import pkg_resources
 
-from kallithea.lib.utils import BasePasterCommand, ask_ok
+from kallithea.lib.paster_commands.common import ask_ok, BasePasterCommand
 
 # Add location of top level folder to sys.path
 from os.path import dirname
--- a/kallithea/lib/paster_commands/repo_scan.py	Thu Apr 07 17:53:51 2016 +0200
+++ b/kallithea/lib/paster_commands/repo_scan.py	Thu Jul 28 14:21:08 2016 +0200
@@ -30,7 +30,8 @@
 import sys
 
 from kallithea.model.scm import ScmModel
-from kallithea.lib.utils import BasePasterCommand, repo2db_mapper
+from kallithea.lib.paster_commands.common import BasePasterCommand
+from kallithea.lib.utils import repo2db_mapper
 
 # Add location of top level folder to sys.path
 from os.path import dirname
--- a/kallithea/lib/paster_commands/update_repoinfo.py	Thu Apr 07 17:53:51 2016 +0200
+++ b/kallithea/lib/paster_commands/update_repoinfo.py	Thu Jul 28 14:21:08 2016 +0200
@@ -30,7 +30,7 @@
 import sys
 import string
 
-from kallithea.lib.utils import BasePasterCommand
+from kallithea.lib.paster_commands.common import BasePasterCommand
 from kallithea.lib.utils2 import safe_unicode
 from kallithea.model.db import Repository
 from kallithea.model.repo import RepoModel
--- a/kallithea/lib/utils.py	Thu Apr 07 17:53:51 2016 +0200
+++ b/kallithea/lib/utils.py	Thu Jul 28 14:21:08 2016 +0200
@@ -311,18 +311,6 @@
     return False
 
 
-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
-
 #propagated from mercurial documentation
 ui_sections = ['alias', 'auth',
                 'decode/encode', 'defaults',
@@ -725,75 +713,6 @@
     setup_package()
 
 
-#==============================================================================
-# PASTER COMMANDS
-#==============================================================================
-class BasePasterCommand(Command):
-    """
-    Abstract Base Class for paster commands.
-
-    The celery commands are somewhat aggressive about loading
-    celery.conf, and since our module sets the `CELERY_LOADER`
-    environment variable to our loader, we have to bootstrap a bit and
-    make sure we've had a chance to load the pylons config off of the
-    command line, otherwise everything fails.
-    """
-    min_args = 1
-    min_args_error = "Please provide a paster config file as an argument."
-    takes_config_file = 1
-    requires_config_file = True
-
-    def run(self, args):
-        """
-        Overrides Command.run
-
-        Checks for a config file argument and loads it.
-        """
-        if len(args) < self.min_args:
-            raise BadCommand(
-                self.min_args_error % {'min_args': self.min_args,
-                                       'actual_args': len(args)})
-
-        # Decrement because we're going to lob off the first argument.
-        # @@ This is hacky
-        self.min_args -= 1
-        self.bootstrap_config(args[0])
-        self.update_parser()
-        return super(BasePasterCommand, self).run(args[1:])
-
-    def update_parser(self):
-        """
-        Abstract method.  Allows for the class's parser to be updated
-        before the superclass's `run` method is called.  Necessary to
-        allow options/arguments to be passed through to the underlying
-        celery command.
-        """
-        raise NotImplementedError("Abstract Method.")
-
-    def bootstrap_config(self, conf):
-        """
-        Loads the pylons configuration.
-        """
-        from pylons import config as pylonsconfig
-
-        self.path_to_ini_file = os.path.realpath(conf)
-        conf = paste.deploy.appconfig('config:' + self.path_to_ini_file)
-        pylonsconfig.init_app(conf.global_conf, conf.local_conf)
-
-    def _init_session(self):
-        """
-        Inits SqlAlchemy Session
-        """
-        logging.config.fileConfig(self.path_to_ini_file)
-
-        from pylons import config
-        from kallithea.model import init_model
-        from kallithea.lib.utils2 import engine_from_config
-        add_cache(config)
-        engine = engine_from_config(config, 'sqlalchemy.db1.')
-        init_model(engine)
-
-
 def check_git_version():
     """
     Checks what version of git is installed in system, and issues a warning