Migration to the new mock testing approach (scripted sessions) showed
that there are frequent ftplib.FTP.cwd
calls in a row. The reason is
that every time _robust_ftp_command
def _robust_ftp_command(self, command, path, descend_deeply=False):
"""
Run an FTP command on a path. The return value of the method
is the return value of the command.
If `descend_deeply` is true (the default is false), descend
deeply, i. e. change the directory to the end of the path.
"""
# If we can't change to the yet-current directory, the code
# below won't work (see below), so in this case rather raise
# an exception than giving wrong results.
self._check_inaccessible_login_directory()
# Some FTP servers don't behave as expected if the directory
# portion of the path contains whitespace; some even yield
# strange results if the command isn't executed in the
# current directory. Therefore, change to the directory
# which contains the item to run the command on and invoke
# the command just there.
#
# Remember old working directory.
old_dir = self.getcwd()
try:
if descend_deeply:
# Invoke the command in (not: on) the deepest directory.
self.chdir(path)
# Workaround for some servers that give recursive
# listings when called with a dot as path; see issue <a href="/~sschwarzer/ftputil/33" title="~sschwarzer/ftputil#33: FTPHost.listdir fails for some servers if the path contains slashes">#33</a>,
# https://todo.sr.ht/~sschwarzer/ftputil/33
return command(self, "")
else:
# Invoke the command in the "next to last" directory.
head, tail = self.path.split(path)
self.chdir(head)
return command(self, tail)
finally:
self.chdir(old_dir)
is used, we have one cwd
call in _check_inaccessible_login_directory
(which should be only needed once after opening the session), a second
cwd
to descend into the possibly nested directory and finally a third
cwd
to restore the old directory after the "descent".
In the tests it looks like cwd /
is used all the time, but this is
only because the directory we start from is /
, so the directory into
which we "descend" and which we restore later in the method happen to be
the same.
Also keep in mind that most FTP sessions transfer files, and I imagine
the overall network traffic and hence the needed time for the file
transfers will outweigh the time for the cwd
calls.
As said in the description, I assume the problem is minor in relation to other network operations and therefore there's no need to do anything about this right now. Nevertheless I wanted to enter the ticket for later reference, especially the reasoning about the minor effect on performance.
Because of this, I'm closing the ticket right now. If you find by measurements that the repeated
cwd
calls are a performance problem for you, please feel free to reopen the ticket and describe your problem.