Can bzr remember ftp-passwords?
Chris Niekel
chris at niekel.net
Fri Apr 6 20:19:57 BST 2007
On Thu, Apr 05, 2007 at 06:02:45AM +1000, Martin Pool wrote:
> I don't think there is a way to do it at present. There is Python
> support for a netrc file, so we could use that to get it pretty
> easily, I would think.
It was indeed very easy. The hardest part was figuring out how to use bzr,
because I'm not used to it yet. Here's my patch, hopefully someone can
integrate it.
Regards,
Chris Niekel
=== modified file 'bzrlib/transport/ftp.py'
--- bzrlib/transport/ftp.py 2007-03-13 17:00:20 +0000
+++ bzrlib/transport/ftp.py 2007-04-06 15:46:11 +0000
@@ -36,6 +36,7 @@
import time
import random
from warnings import warn
+import netrc
from bzrlib import (
errors,
@@ -55,6 +56,18 @@
class FtpPathError(errors.PathError):
"""FTP failed for path: %(path)s%(extra)s"""
+def _find_stored_password(hostname, username):
+ try:
+ r = netrc.netrc().authenticators(hostname)
+ if r and username == r[0]:
+ password = r[2]
+ mutter('using password from netrc')
+ return password
+ else:
+ return None
+ except netrc.NetrcParseError:
+ return None
_FTP_cache = {}
def _find_FTP(hostname, port, username, password, is_active):
@@ -67,9 +80,13 @@
conn.connect(host=hostname, port=port)
if username and username != 'anonymous' and not password:
- password = bzrlib.ui.ui_factory.get_password(
- prompt='FTP %(user)s@%(host)s password',
- user=username, host=hostname)
+ stored_password = _find_stored_password(hostname, username)
+ if stored_password:
+ password = stored_password
+ else:
+ password = bzrlib.ui.ui_factory.get_password(
+ prompt='FTP %(user)s@%(host)s password',
+ user=username, host=hostname)
conn.login(user=username, passwd=password)
conn.set_pasv(not is_active)
More information about the bazaar
mailing list