changeset 965:b748a0eed09a

svnwrap: Refactor the svn_auth_ssl_server_trust_prompt to add other handler - Since there are five handler, change to SubversionPrompt class from svn_auth_ssl_server_trust_prompt(). - Moved out of the handler to check the callback function is non-None.
author Mitsuhiro Koga <shiena.jp@gmail.com>
date Wed, 24 Oct 2012 02:19:15 +0900
parents 27fec8bf9273
children 7561aef55a4b
files hgsubversion/svnrepo.py hgsubversion/svnwrap/subvertpy_wrapper.py hgsubversion/svnwrap/svn_swig_wrapper.py
diffstat 3 files changed, 46 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/svnrepo.py
+++ b/hgsubversion/svnrepo.py
@@ -195,11 +195,14 @@ def instance(ui, url, create):
     if create:
         raise hgutil.Abort('cannot create new remote Subversion repository')
 
-    svnwrap.ssl_server_trust_prompt_callback(svn_auth_ssl_server_trust_prompt(ui))
+    svnwrap.prompt_callback(SubversionPrompt(ui))
     return svnremoterepo(ui, url)
 
-def svn_auth_ssl_server_trust_prompt(ui):
-    def callback(realm, failures, cert_info, may_save, pool=None):
+class SubversionPrompt(object):
+    def __init__(self, ui):
+        self.ui = ui
+
+    def ssl_server_trust(self, realm, failures, cert_info, may_save, pool=None):
         msg = 'Error validating server certificate for \'%s\':\n' % (realm,)
         if failures & svnwrap.SSL_UNKNOWNCA:
             msg += (
@@ -233,14 +236,12 @@ def svn_auth_ssl_server_trust_prompt(ui)
         else:
             msg += '(R)eject or accept (t)emporarily? '
             choices = (('&Reject'), ('&Temporarily'))
-        choice = ui.promptchoice(msg, choices, default=0)
+        choice = self.ui.promptchoice(msg, choices, default=0)
         if choice == 1:
             creds = (failures, False)
         elif may_save and choice == 2:
             creds = (failures, True)
         else:
             creds = None
-
         return creds
-    return callback
 
--- a/hgsubversion/svnwrap/subvertpy_wrapper.py
+++ b/hgsubversion/svnwrap/subvertpy_wrapper.py
@@ -52,11 +52,6 @@ def version():
         svnvers += '-' + subversion_version[3]
     return (svnvers, 'Subvertpy ' + _versionstr(subvertpy.__version__))
 
-_ssl_server_trust_prompt_callback = None
-def ssl_server_trust_prompt_callback(callback):
-    global _ssl_server_trust_prompt_callback
-    _ssl_server_trust_prompt_callback = callback
-
 # exported values
 ERR_FS_CONFLICT = subvertpy.ERR_FS_CONFLICT
 ERR_FS_NOT_FOUND = subvertpy.ERR_FS_NOT_FOUND
@@ -83,6 +78,11 @@ def ieditor(fn):
 
     return fn
 
+_prompt = None
+def prompt_callback(callback):
+    global _prompt
+    _prompt = callback
+
 _svntypes = {
     subvertpy.NODE_DIR: 'd',
     subvertpy.NODE_FILE: 'f',
@@ -212,21 +212,15 @@ class SubversionRepo(object):
             return self.username or username, self.password or '', False
         def getuser(realm, may_save):
             return self.username or '', False
-        def svn_auth_ssl_server_trust_prompt(realm, failures, cert_info, may_save):
-            global _ssl_server_trust_prompt_callback
-            if _ssl_server_trust_prompt_callback:
-                ret = _ssl_server_trust_prompt_callback(realm, failures, cert_info, may_save)
-                if ret:
-                    creds = ret
-                else:
-                    # We need to reject the certificate, but subvertpy doesn't
-                    # handle None as a return value here, and requires
-                    # we instead return a tuple of (int, bool). Because of that,
-                    # we return (0, False) instead.
-                    creds = (0, False)
-            else:
-                creds = (0, False)
 
+        def ssl_server_trust(realm, failures, cert_info, may_save):
+            creds = _prompt.ssl_server_trust(realm, failures, cert_info, may_save)
+            if creds is None:
+                # We need to reject the certificate, but subvertpy doesn't
+                # handle None as a return value here, and requires
+                # we instead return a tuple of (int, bool). Because of that,
+                # we return (0, False) instead.
+                creds = (0, False)
             return creds
 
         providers = ra.get_platform_specific_client_providers()
@@ -238,8 +232,11 @@ class SubversionRepo(object):
             ra.get_ssl_server_trust_file_provider(),
             ra.get_username_prompt_provider(getuser, 0),
             ra.get_simple_prompt_provider(getpass, 0),
-            ra.get_ssl_server_trust_prompt_provider(svn_auth_ssl_server_trust_prompt),
         ]
+        if _prompt:
+            providers += [
+                ra.get_ssl_server_trust_prompt_provider(ssl_server_trust),
+            ]
 
         auth = ra.Auth(providers)
         if self.username:
--- a/hgsubversion/svnwrap/svn_swig_wrapper.py
+++ b/hgsubversion/svnwrap/svn_swig_wrapper.py
@@ -108,27 +108,23 @@ def user_pass_prompt(realm, default_user
     creds.password = getpass.getpass('Password for %s: ' % creds.username)
     return creds
 
-_ssl_server_trust_prompt_callback = None
-def ssl_server_trust_prompt_callback(callback):
-    global _ssl_server_trust_prompt_callback
-    _ssl_server_trust_prompt_callback = callback
-
-def _ssl_server_trust_prompt(realm, failures, cert_info, may_save, pool):
-    global _ssl_server_trust_prompt_callback
-    if _ssl_server_trust_prompt_callback:
-        cert = [
-                cert_info.hostname,
-                cert_info.fingerprint,
-                cert_info.valid_from,
-                cert_info.valid_until,
-                cert_info.issuer_dname,
-                ]
-        ret = _ssl_server_trust_prompt_callback(realm, failures, cert, may_save, pool)
-        if ret:
-            creds = core.svn_auth_cred_ssl_server_trust_t()
-            (creds.accepted_failures, creds.may_save) = ret
-        else:
-            creds = None
+_prompt = None
+def prompt_callback(callback):
+    global _prompt
+    _prompt = callback
+
+def _ssl_server_trust(realm, failures, cert_info, may_save, pool):
+    cert = [
+            cert_info.hostname,
+            cert_info.fingerprint,
+            cert_info.valid_from,
+            cert_info.valid_until,
+            cert_info.issuer_dname,
+            ]
+    ret = _prompt.ssl_server_trust(realm, failures, cert, may_save, pool)
+    if ret:
+        creds = core.svn_auth_cred_ssl_server_trust_t()
+        (creds.accepted_failures, creds.may_save) = ret
     else:
         creds = None
     return creds
@@ -176,9 +172,13 @@ def _create_auth_baton(pool, password_st
         client.get_ssl_client_cert_pw_file_provider(),
         client.get_ssl_server_trust_file_provider(),
         client.get_simple_prompt_provider(user_pass_prompt, 2),
-        client.get_ssl_server_trust_prompt_provider(_ssl_server_trust_prompt),
         ]
 
+    if _prompt:
+        providers += [
+            client.get_ssl_server_trust_prompt_provider(_ssl_server_trust),
+            ]
+
     return core.svn_auth_open(providers, pool)
 
 _svntypes = {