changeset 7674:5b551b189459

utils: refactor make_ui to always read from db, optionally also augmenting with hgrc content simplehg was the only user of reading hgrc content ... and it would rather just have everything at once. The implementation in utils is moved around and re-indented, but without significant changes.
author Mads Kiilerich <mads@kiilerich.com>
date Wed, 23 Jan 2019 00:03:40 +0100
parents 642847355a10
children c8239333853d
files kallithea/config/app_cfg.py kallithea/lib/hooks.py kallithea/lib/middleware/simplegit.py kallithea/lib/middleware/simplehg.py kallithea/lib/utils.py kallithea/model/db.py kallithea/model/repo.py kallithea/model/scm.py kallithea/model/validators.py
diffstat 9 files changed, 45 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/config/app_cfg.py	Wed Jan 23 03:52:13 2019 +0100
+++ b/kallithea/config/app_cfg.py	Wed Jan 23 00:03:40 2019 +0100
@@ -173,7 +173,7 @@
     load_rcextensions(root_path=config['here'])
 
     set_available_permissions(config)
-    repos_path = make_ui('db').configitems('paths')[0][1]
+    repos_path = make_ui().configitems('paths')[0][1]
     config['base_path'] = repos_path
     set_app_settings(config)
 
--- a/kallithea/lib/hooks.py	Wed Jan 23 03:52:13 2019 +0100
+++ b/kallithea/lib/hooks.py	Wed Jan 23 00:03:40 2019 +0100
@@ -342,7 +342,7 @@
         raise OSError('Repository %s not found in database'
                       % (safe_str(repo_path)))
 
-    baseui = make_ui('db')
+    baseui = make_ui()
     return baseui, repo
 
 
--- a/kallithea/lib/middleware/simplegit.py	Wed Jan 23 03:52:13 2019 +0100
+++ b/kallithea/lib/middleware/simplegit.py	Wed Jan 23 00:03:40 2019 +0100
@@ -118,7 +118,7 @@
 
         fix_PATH()
         log.debug('HOOKS extras is %s', extras)
-        baseui = make_ui('db')
+        baseui = make_ui()
         _set_extras(extras or {})
 
         try:
--- a/kallithea/lib/middleware/simplehg.py	Wed Jan 23 03:52:13 2019 +0100
+++ b/kallithea/lib/middleware/simplehg.py	Wed Jan 23 00:03:40 2019 +0100
@@ -39,7 +39,7 @@
 from kallithea.lib.utils2 import safe_str, safe_unicode, fix_PATH, get_server_url, \
     _set_extras
 from kallithea.lib.base import BaseVCSController
-from kallithea.lib.utils import make_ui, is_valid_repo, ui_sections
+from kallithea.lib.utils import make_ui, is_valid_repo
 from kallithea.lib.vcs.utils.hgcompat import RepoError, hgweb_mod
 
 log = logging.getLogger(__name__)
@@ -142,8 +142,7 @@
 
         fix_PATH()
         log.debug('HOOKS extras is %s', extras)
-        baseui = make_ui('db')
-        self._augment_hgrc(repo_path, baseui)
+        baseui = make_ui(repo_path=repo_path)
         _set_extras(extras or {})
 
         try:
@@ -237,11 +236,3 @@
 
         # Note: the client doesn't get the helpful error message
         raise HTTPBadRequest('Unable to detect pull/push action! Are you using non standard command or client?')
-
-    def _augment_hgrc(self, repo_path, baseui):
-        """Augment baseui with config settings from the repo_path repo"""
-        hgrc = os.path.join(repo_path, '.hg', 'hgrc')
-        repoui = make_ui('file', hgrc)
-        for section in ui_sections:
-            for k, v in repoui.configitems(section):
-                baseui.setconfig(section, k, v)
--- a/kallithea/lib/utils.py	Wed Jan 23 03:52:13 2019 +0100
+++ b/kallithea/lib/utils.py	Wed Jan 23 00:03:40 2019 +0100
@@ -315,15 +315,11 @@
                 'ui', 'web', ]
 
 
-def make_ui(read_from='file', path=None, clear_session=True):
+def make_ui(repo_path=None, clear_session=True):
     """
-    A function that will read python rc files or database
-    and make an mercurial ui object from read options
-
-    :param path: path to mercurial config file
-    :param read_from: read from 'file' or 'db'
+    Create an Mercurial 'ui' object based on database Ui settings, possibly
+    augmenting with content from a hgrc file.
     """
-
     baseui = ui.ui()
 
     # clean the baseui object
@@ -331,42 +327,39 @@
     baseui._ucfg = config.config()
     baseui._tcfg = config.config()
 
-    if read_from == 'file':
-        if not os.path.isfile(path):
-            log.debug('hgrc file is not present at %s, skipping...', path)
-            return baseui
-        log.debug('reading hgrc from %s', path)
-        cfg = config.config()
-        cfg.read(path)
-        for section in ui_sections:
-            for k, v in cfg.items(section):
-                log.debug('settings ui from file: [%s] %s=%s', section, k, v)
-                baseui.setconfig(safe_str(section), safe_str(k), safe_str(v))
-
-    elif read_from == 'db':
-        sa = meta.Session()
-        ret = sa.query(Ui).all()
+    sa = meta.Session()
+    for ui_ in sa.query(Ui).all():
+        if ui_.ui_active:
+            ui_val = '' if ui_.ui_value is None else safe_str(ui_.ui_value)
+            log.debug('config from db: [%s] %s=%r', ui_.ui_section,
+                      ui_.ui_key, ui_val)
+            baseui.setconfig(safe_str(ui_.ui_section), safe_str(ui_.ui_key),
+                             ui_val)
+    if clear_session:
+        meta.Session.remove()
 
-        hg_ui = ret
-        for ui_ in hg_ui:
-            if ui_.ui_active:
-                ui_val = '' if ui_.ui_value is None else safe_str(ui_.ui_value)
-                log.debug('settings ui from db: [%s] %s=%r', ui_.ui_section,
-                          ui_.ui_key, ui_val)
-                baseui.setconfig(safe_str(ui_.ui_section), safe_str(ui_.ui_key),
-                                 ui_val)
-        if clear_session:
-            meta.Session.remove()
+    # force set push_ssl requirement to False, Kallithea handles that
+    baseui.setconfig('web', 'push_ssl', False)
+    baseui.setconfig('web', 'allow_push', '*')
+    # prevent interactive questions for ssh password / passphrase
+    ssh = baseui.config('ui', 'ssh', default='ssh')
+    baseui.setconfig('ui', 'ssh', '%s -oBatchMode=yes -oIdentitiesOnly=yes' % ssh)
+    # push / pull hooks
+    baseui.setconfig('hooks', 'changegroup.kallithea_log_push_action', 'python:kallithea.lib.hooks.log_push_action')
+    baseui.setconfig('hooks', 'outgoing.kallithea_log_pull_action', 'python:kallithea.lib.hooks.log_pull_action')
 
-        # force set push_ssl requirement to False, Kallithea handles that
-        baseui.setconfig('web', 'push_ssl', False)
-        baseui.setconfig('web', 'allow_push', '*')
-        # prevent interactive questions for ssh password / passphrase
-        ssh = baseui.config('ui', 'ssh', default='ssh')
-        baseui.setconfig('ui', 'ssh', '%s -oBatchMode=yes -oIdentitiesOnly=yes' % ssh)
-        # push / pull hooks
-        baseui.setconfig('hooks', 'changegroup.kallithea_log_push_action', 'python:kallithea.lib.hooks.log_push_action')
-        baseui.setconfig('hooks', 'outgoing.kallithea_log_pull_action', 'python:kallithea.lib.hooks.log_pull_action')
+    if repo_path is not None:
+        hgrc_path = os.path.join(repo_path, '.hg', 'hgrc')
+        if os.path.isfile(hgrc_path):
+            log.debug('reading hgrc from %s', hgrc_path)
+            cfg = config.config()
+            cfg.read(hgrc_path)
+            for section in ui_sections:
+                for k, v in cfg.items(section):
+                    log.debug('config from file: [%s] %s=%s', section, k, v)
+                    baseui.setconfig(safe_str(section), safe_str(k), safe_str(v))
+        else:
+            log.debug('hgrc file is not present at %s, skipping...', hgrc_path)
 
     return baseui
 
--- a/kallithea/model/db.py	Wed Jan 23 03:52:13 2019 +0100
+++ b/kallithea/model/db.py	Wed Jan 23 00:03:40 2019 +0100
@@ -1196,7 +1196,7 @@
         Creates an db based ui object for this repository
         """
         from kallithea.lib.utils import make_ui
-        return make_ui('db', clear_session=False)
+        return make_ui(clear_session=False)
 
     @classmethod
     def is_valid(cls, repo_name):
--- a/kallithea/model/repo.py	Wed Jan 23 03:52:13 2019 +0100
+++ b/kallithea/model/repo.py	Wed Jan 23 00:03:40 2019 +0100
@@ -295,7 +295,7 @@
                 # clone_uri is modified - if given a value, check it is valid
                 if clone_uri != '':
                     # will raise exception on error
-                    is_valid_repo_uri(cur_repo.repo_type, clone_uri, make_ui('db', clear_session=False))
+                    is_valid_repo_uri(cur_repo.repo_type, clone_uri, make_ui(clear_session=False))
                 cur_repo.clone_uri = clone_uri
 
             if 'repo_name' in kwargs:
@@ -365,7 +365,7 @@
             new_repo.private = private
             if clone_uri:
                 # will raise exception on error
-                is_valid_repo_uri(repo_type, clone_uri, make_ui('db', clear_session=False))
+                is_valid_repo_uri(repo_type, clone_uri, make_ui(clear_session=False))
             new_repo.clone_uri = clone_uri
             new_repo.landing_rev = landing_rev
 
@@ -666,7 +666,7 @@
         backend = get_backend(repo_type)
 
         if repo_type == 'hg':
-            baseui = make_ui('db', clear_session=False)
+            baseui = make_ui(clear_session=False)
             # patch and reset hooks section of UI config to not run any
             # hooks on creating remote repo
             for k, v in baseui.configitems('hooks'):
--- a/kallithea/model/scm.py	Wed Jan 23 03:52:13 2019 +0100
+++ b/kallithea/model/scm.py	Wed Jan 23 00:03:40 2019 +0100
@@ -178,7 +178,7 @@
 
         log.info('scanning for repositories in %s', repos_path)
 
-        baseui = make_ui('db')
+        baseui = make_ui()
         repos = {}
 
         for name, path in get_filesystem_repos(repos_path):
--- a/kallithea/model/validators.py	Wed Jan 23 03:52:13 2019 +0100
+++ b/kallithea/model/validators.py	Wed Jan 23 00:03:40 2019 +0100
@@ -427,7 +427,7 @@
 
             if url and url != value.get('clone_uri_hidden'):
                 try:
-                    is_valid_repo_uri(repo_type, url, make_ui('db', clear_session=False))
+                    is_valid_repo_uri(repo_type, url, make_ui(clear_session=False))
                 except Exception:
                     log.exception('URL validation failed')
                     msg = self.message('clone_uri', state)