Commits

Mitsuhiro Koga  committed b748a0e

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.

  • Participants
  • Parent commits 27fec8b

Comments (0)

Files changed (3)

File hgsubversion/svnrepo.py

     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 += (
         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 creds
-    return callback
-

File hgsubversion/svnwrap/subvertpy_wrapper.py

         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
 
     return fn
 
+_prompt = None
+def prompt_callback(callback):
+    global _prompt
+    _prompt = callback
+
 _svntypes = {
     subvertpy.NODE_DIR: 'd',
     subvertpy.NODE_FILE: 'f',
             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:
+
+        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()
             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:

File hgsubversion/svnwrap/svn_swig_wrapper.py

     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
+_prompt = None
+def prompt_callback(callback):
+    global _prompt
+    _prompt = 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
+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
         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 = {