diff --git a/mercurial_keyring.py b/mercurial_keyring.py --- a/mercurial_keyring.py +++ b/mercurial_keyring.py @@ -51,12 +51,17 @@ backend (by default keyring guesses, usu you get KDE Wallet under KDE, and Gnome Keyring under Gnome or Unity). ''' -import urllib2 -import smtplib import socket import os import sys import re +if sys.version_info[0] < 3: + from urllib2 import ( + HTTPPasswordMgrWithDefaultRealm, AbstractBasicAuthHandler, AbstractDigestAuthHandler) +else: + from urllib.request import ( + HTTPPasswordMgrWithDefaultRealm, AbstractBasicAuthHandler, AbstractDigestAuthHandler) +import smtplib from mercurial import util, sslutil, error from mercurial.i18n import _ @@ -215,7 +220,8 @@ class PasswordStore(object): password = keyring.get_password(KEYRING_SERVICE, pwdkey) except Exception as err: ui = uimod.ui() - ui.warn(_("keyring: keyring backend doesn't seem to work, password can not be restored. Falling back to prompts. Error details: %s\n") % str(err)) + ui.warn(_("keyring: keyring backend doesn't seem to work, password can not be restored. Falling back to prompts. Error details: %s\n"), + err) return '' # Reverse recoding from next routine if isinstance(password, unicode): @@ -234,7 +240,8 @@ class PasswordStore(object): KEYRING_SERVICE, pwdkey, password) except Exception as err: ui = uimod.ui() - ui.warn(_("keyring: keyring backend doesn't seem to work, password was not saved. Error details: %s\n") % str(err)) + ui.warn(_("keyring: keyring backend doesn't seem to work, password was not saved. Error details: %s\n"), + err) password_store = PasswordStore() @@ -322,17 +329,17 @@ class HTTPPasswordHandler(object): parsed_url, url_user, url_passwd = self.unpack_url(authuri) base_url = str(parsed_url) - ui.debug(_('keyring: base url: %s, url user: %s, url pwd: %s\n') % - (base_url, url_user or '', url_passwd and '******' or '')) + ui.debug(_('keyring: base url: %s, url user: %s, url pwd: %s\n'), + base_url, url_user or '', url_passwd and '******' or '') # Extract username (or password) stored directly in url if url_user and url_passwd: return url_user, url_passwd, self.SRC_URL, base_url # Extract data from urllib (in case it was already stored) - if isinstance(pwmgr, urllib2.HTTPPasswordMgrWithDefaultRealm): + if isinstance(pwmgr, HTTPPasswordMgrWithDefaultRealm): urllib_user, urllib_pwd = \ - urllib2.HTTPPasswordMgrWithDefaultRealm.find_user_password( + HTTPPasswordMgrWithDefaultRealm.find_user_password( pwmgr, realm, authuri) else: urllib_user, urllib_pwd = pwmgr.passwddb.find_user_password( @@ -364,7 +371,8 @@ class HTTPPasswordHandler(object): # Load from keyring. if actual_user: - ui.debug(_("keyring: looking for password (user %s, url %s)\n") % (actual_user, keyring_url)) + ui.debug(_("keyring: looking for password (user %s, url %s)\n"), + actual_user, keyring_url) keyring_pwd = password_store.get_http_password(keyring_url, actual_user) if keyring_pwd: return actual_user, keyring_pwd, self.SRC_KEYRING, keyring_url @@ -381,10 +389,10 @@ class HTTPPasswordHandler(object): ui.status(_("keyring: username not specified in hgrc (or in url). Password will not be saved.\n")) ui.write(_("http authorization required\n")) - ui.status(_("realm: %s\n") % realm) - ui.status(_("url: %s\n") % url) + ui.status(_("realm: %s\n"), realm) + ui.status(_("url: %s\n"), url) if user: - ui.write(_("user: %s (fixed in hgrc or url)\n" % user)) + ui.write(_("user: %s (fixed in hgrc or url)\n", user)) else: user = ui.prompt(_("user:"), default=None) pwd = ui.getpass(_("password: ")) @@ -418,7 +426,7 @@ class HTTPPasswordHandler(object): # It is done only if username is permanently set. # Otherwise we won't be able to find the password so it # does not make much sense to preserve it - _debug(ui, _("Saving password for %s to keyring") % user) + _debug(ui, _("Saving password for %s to keyring"), user) try: password_store.set_http_password(final_url, user, pwd) except Exception as e: @@ -575,7 +583,7 @@ def find_user_password(self, realm, auth return self._pwd_handler.find_auth(self, realm, authuri, req) -@monkeypatch_method(urllib2.AbstractBasicAuthHandler, "http_error_auth_reqed") +@monkeypatch_method(AbstractBasicAuthHandler, "http_error_auth_reqed") def basic_http_error_auth_reqed(self, authreq, host, req, headers): """Preserves current HTTP request so it can be consulted in find_user_password above""" @@ -586,7 +594,7 @@ def basic_http_error_auth_reqed(self, au self.passwd._http_req = None -@monkeypatch_method(urllib2.AbstractDigestAuthHandler, "http_error_auth_reqed") +@monkeypatch_method(AbstractDigestAuthHandler, "http_error_auth_reqed") def digest_http_error_auth_reqed(self, authreq, host, req, headers): """Preserves current HTTP request so it can be consulted in find_user_password above""" @@ -617,13 +625,14 @@ def try_smtp_login(ui, smtp_obj, usernam if not password: return False try: - ui.note(_('(authenticating to mail server as %s)\n') % - (username)) + ui.note(_('(authenticating to mail server as %s)\n'), + username) smtp_obj.login(username, password) return True except smtplib.SMTPException as inst: if inst.smtp_code == 535: - ui.status(_("SMTP login failed: %s\n\n") % inst.smtp_error) + ui.status(_("SMTP login failed: %s\n\n"), + inst.smtp_error) return False else: raise error.Abort(inst) @@ -685,8 +694,8 @@ def keyring_supported_smtp(ui, username) else: defaultport = 25 mailport = util.getport(ui.config('smtp', 'port', defaultport)) - ui.note(_('sending mail: smtp host %s, port %s\n') % - (mailhost, mailport)) + ui.note(_('sending mail: smtp host %s, port %s\n'), + mailhost, mailport) s.connect(host=mailhost, port=mailport) if starttls: ui.note(_('(using starttls)\n')) @@ -753,7 +762,7 @@ cmdtable = {} command = meu.command(cmdtable) -@command('keyring_check', +@command(b'keyring_check', [], _("keyring_check [PATH]"), optionalrepo=True) @@ -788,22 +797,23 @@ def cmd_keyring_check(ui, repo, *path_ar for name, url in paths: if not is_http_path(url): if path_args: - ui.status(_(" %s: non-http path (%s)\n") % (name, url)) + ui.status(_(" %s: non-http path (%s)\n"), + name, url) continue user, pwd, source, final_url = handler.get_credentials( make_passwordmgr(ui), name, url) if pwd: - ui.status(_(" %s: password available, source: %s, bound to user %s, url %s\n") % ( - name, source, user, final_url)) + ui.status(_(" %s: password available, source: %s, bound to user %s, url %s\n"), + name, source, user, final_url) elif user: - ui.status(_(" %s: password not available, once entered, will be bound to user %s, url %s\n") % ( - name, user, final_url)) + ui.status(_(" %s: password not available, once entered, will be bound to user %s, url %s\n"), + name, user, final_url) else: - ui.status(_(" %s: password not available, user unknown, url %s\n") % ( - name, final_url)) + ui.status(_(" %s: password not available, user unknown, url %s\n"), + name, final_url) -@command('keyring_clear', +@command(b'keyring_clear', [], _('hg keyring_clear PATH-OR-ALIAS'), optionalrepo=True) @@ -820,7 +830,8 @@ def cmd_keyring_clear(ui, repo, path, ** path_url = url break if not is_http_path(path_url): - ui.status(_("%s is not a http path (and %s can't be resolved as path alias)\n") % (path, path_url)) + ui.status(_("%s is not a http path (and %s can't be resolved as path alias)\n"), + path, path_url) return handler = HTTPPasswordHandler() @@ -828,20 +839,21 @@ def cmd_keyring_clear(ui, repo, path, ** user, pwd, source, final_url = handler.get_credentials( make_passwordmgr(ui), path, path_url) if not user: - ui.status(_("Username not configured for url %s\n") % final_url) + ui.status(_("Username not configured for url %s\n"), + final_url) return if not pwd: - ui.status(_("No password is saved for user %s, url %s\n") % ( - user, final_url)) + ui.status(_("No password is saved for user %s, url %s\n"), + user, final_url) return if source != handler.SRC_KEYRING: - ui.status(_("Password for user %s, url %s is saved in %s, not in keyring\n") % ( - user, final_url, source)) + ui.status(_("Password for user %s, url %s is saved in %s, not in keyring\n"), + user, final_url, source) password_store.clear_http_password(final_url, user) - ui.status(_("Password removed for user %s, url %s\n") % ( - user, final_url)) + ui.status(_("Password removed for user %s, url %s\n"), + user, final_url) buglink = 'https://bitbucket.org/Mekk/mercurial_keyring/issues'