88
88
self.is_active = True
90
90
self.is_active = False
91
if from_transport is None:
92
self._FTP_instance = None
94
self._FTP_instance = from_transport._FTP_instance
96
92
def _get_FTP(self):
97
93
"""Return the ftplib.FTP instance for this object."""
98
if self._FTP_instance is None:
99
mutter("Constructing FTP instance against %r" %
100
((self._host, self._port, self._user, '********',
103
connection = ftplib.FTP()
104
connection.connect(host=self._host, port=self._port)
105
if self._user and self._user != 'anonymous' and \
107
self._password = bzrlib.ui.ui_factory.get_password(
108
prompt='FTP %(user)s@%(host)s password',
109
user=self._user, host=self._host)
110
connection.login(user=self._user, passwd=self._password)
111
connection.set_pasv(not self.is_active)
112
except ftplib.error_perm, e:
113
raise errors.TransportError(msg="Error setting up connection:"
114
" %s" % str(e), orig_error=e)
115
self._FTP_instance = connection
116
return self._FTP_instance
118
def _translate_perm_error(self, err, path, extra=None, unknown_exc=FtpPathError):
94
# Ensures that a connection is established
95
connection = self._get_connection()
96
if connection is None:
97
# First connection ever
98
connection, credentials = self._create_connection()
99
self._set_connection(connection, credentials)
102
def _create_connection(self, credentials=None):
103
"""Create a new connection with the provided credentials.
105
:param credentials: The credentials needed to establish the connection.
107
:return: The created connection and its associated credentials.
109
The credentials are only the password as it may have been entered
110
interactively by the user and may be different from the one provided
111
in base url at transport creation time.
113
if credentials is None:
114
password = self._password
116
password = credentials
118
mutter("Constructing FTP instance against %r" %
119
((self._host, self._port, self._user, '********',
122
connection = ftplib.FTP()
123
connection.connect(host=self._host, port=self._port)
124
if self._user and self._user != 'anonymous' and \
125
password is not None: # '' is a valid password
126
get_password = bzrlib.ui.ui_factory.get_password
127
password = get_password(prompt='FTP %(user)s@%(host)s password',
128
user=self._user, host=self._host)
129
connection.login(user=self._user, passwd=password)
130
connection.set_pasv(not self.is_active)
131
except ftplib.error_perm, e:
132
raise errors.TransportError(msg="Error setting up connection:"
133
" %s" % str(e), orig_error=e)
134
return connection, password
136
def _reconnect(self):
137
"""Create a new connection with the previously used credentials"""
138
credentials = self.get_credentials()
139
connection, credentials = self._create_connection(credentials)
140
self._set_connection(connection, credentials)
142
def _translate_perm_error(self, err, path, extra=None,
143
unknown_exc=FtpPathError):
119
144
"""Try to translate an ftplib.error_perm exception.
121
146
:param err: The error to translate into a bzr error