changeset 8454:d1521d421610 stable

vcs: extract get_urllib_request_handlers out of {hg,git}/repository.py Avoid some duplication between hg and git backends. A subsequent commit will need bugfixes in this area.
author Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
date Wed, 22 Jul 2020 21:48:53 +0200
parents 0bca9e828db2
children d727e81e0097
files kallithea/lib/vcs/backends/git/repository.py kallithea/lib/vcs/backends/hg/repository.py kallithea/lib/vcs/utils/helpers.py
diffstat 3 files changed, 22 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/vcs/backends/git/repository.py	Thu Jun 11 20:50:26 2020 +0200
+++ b/kallithea/lib/vcs/backends/git/repository.py	Wed Jul 22 21:48:53 2020 +0200
@@ -19,7 +19,6 @@
 import urllib.request
 from collections import OrderedDict
 
-import mercurial.url  # import httpbasicauthhandler, httpdigestauthhandler
 import mercurial.util  # import url as hg_url
 from dulwich.config import ConfigFile
 from dulwich.objects import Tag
@@ -31,6 +30,7 @@
 from kallithea.lib.vcs.exceptions import (BranchDoesNotExistError, ChangesetDoesNotExistError, EmptyRepositoryError, RepositoryError, TagAlreadyExistError,
                                           TagDoesNotExistError)
 from kallithea.lib.vcs.utils import ascii_str, date_fromtimestamp, makedate, safe_bytes, safe_str
+from kallithea.lib.vcs.utils.helpers import get_urllib_request_handlers
 from kallithea.lib.vcs.utils.lazy import LazyProperty
 from kallithea.lib.vcs.utils.paths import abspath, get_user_home
 
@@ -168,23 +168,14 @@
         if '+' in url[:url.find('://')]:
             url = url[url.find('+') + 1:]
 
-        handlers = []
         url_obj = mercurial.util.url(safe_bytes(url))
-        test_uri, authinfo = url_obj.authinfo()
+        test_uri, handlers = get_urllib_request_handlers(url_obj)
         if not test_uri.endswith(b'info/refs'):
             test_uri = test_uri.rstrip(b'/') + b'/info/refs'
 
         url_obj.passwd = b'*****'
         cleaned_uri = str(url_obj)
 
-        if authinfo:
-            # create a password manager
-            passmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
-            passmgr.add_password(*authinfo)
-
-            handlers.extend((mercurial.url.httpbasicauthhandler(passmgr),
-                             mercurial.url.httpdigestauthhandler(passmgr)))
-
         o = urllib.request.build_opener(*handlers)
         o.addheaders = [('User-Agent', 'git/1.7.8.0')]  # fake some git
 
--- a/kallithea/lib/vcs/backends/hg/repository.py	Thu Jun 11 20:50:26 2020 +0200
+++ b/kallithea/lib/vcs/backends/hg/repository.py	Wed Jul 22 21:48:53 2020 +0200
@@ -33,13 +33,13 @@
 import mercurial.sshpeer
 import mercurial.tags
 import mercurial.ui
-import mercurial.url
 import mercurial.util
 
 from kallithea.lib.vcs.backends.base import BaseRepository, CollectionGenerator
 from kallithea.lib.vcs.exceptions import (BranchDoesNotExistError, ChangesetDoesNotExistError, EmptyRepositoryError, RepositoryError, TagAlreadyExistError,
                                           TagDoesNotExistError, VCSError)
 from kallithea.lib.vcs.utils import ascii_str, author_email, author_name, date_fromtimestamp, makedate, safe_bytes, safe_str
+from kallithea.lib.vcs.utils.helpers import get_urllib_request_handlers
 from kallithea.lib.vcs.utils.lazy import LazyProperty
 from kallithea.lib.vcs.utils.paths import abspath
 
@@ -308,20 +308,12 @@
         if b'+' in url[:url.find(b'://')]:
             url_prefix, url = url.split(b'+', 1)
 
-        handlers = []
         url_obj = mercurial.util.url(url)
-        test_uri, authinfo = url_obj.authinfo()
+        test_uri, handlers = get_urllib_request_handlers(url_obj)
+
         url_obj.passwd = b'*****'
         cleaned_uri = str(url_obj)
 
-        if authinfo:
-            # create a password manager
-            passmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
-            passmgr.add_password(*authinfo)
-
-            handlers.extend((mercurial.url.httpbasicauthhandler(passmgr),
-                             mercurial.url.httpdigestauthhandler(passmgr)))
-
         o = urllib.request.build_opener(*handlers)
         o.addheaders = [('Content-Type', 'application/mercurial-0.1'),
                         ('Accept', 'application/mercurial-0.1')]
--- a/kallithea/lib/vcs/utils/helpers.py	Thu Jun 11 20:50:26 2020 +0200
+++ b/kallithea/lib/vcs/utils/helpers.py	Wed Jul 22 21:48:53 2020 +0200
@@ -6,6 +6,9 @@
 import os
 import re
 import time
+import urllib.request
+
+import mercurial.url
 
 from kallithea.lib.vcs.exceptions import RepositoryError, VCSError
 from kallithea.lib.vcs.utils.paths import abspath
@@ -217,3 +220,17 @@
     for attr in attrs:
         data[attr] = getattr(obj, attr)
     return data
+
+def get_urllib_request_handlers(url_obj):
+    handlers = []
+    test_uri, authinfo = url_obj.authinfo()
+
+    if authinfo:
+        # create a password manager
+        passmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
+        passmgr.add_password(*authinfo)
+
+        handlers.extend((mercurial.url.httpbasicauthhandler(passmgr),
+                         mercurial.url.httpdigestauthhandler(passmgr)))
+
+    return test_uri, handlers