Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FTPHook methods should not change working directory #35105

Merged
merged 11 commits into from
Oct 30, 2023
Merged

FTPHook methods should not change working directory #35105

merged 11 commits into from
Oct 30, 2023

Conversation

makrushin-evgenii
Copy link
Contributor

@makrushin-evgenii makrushin-evgenii commented Oct 21, 2023

This methods change current directory. And second call with same directory will raise no such file or directory error. So I add cwd call to change back to origin directory

closes: #35015


…ore_file methods, they no change working path anymore
@potiuk
Copy link
Member

potiuk commented Oct 21, 2023

This should be idone via try/finally or (better) following context_manager (and tests shoudl be fixed)

Comment on lines 93 to 96
current_path = conn.pwd()
conn.cwd(path)

files = conn.nlst()
conn.cwd(current_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here (https://pythontic.com/ftplib/ftp/nlst):

Suggested change
current_path = conn.pwd()
conn.cwd(path)
files = conn.nlst()
conn.cwd(current_path)
files = conn.nlst(path=path)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank a lot. It's look better. Cant commit your suggestion because of typo: nlst method has no path name argument

eb1c5cb

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hussein-awala can you see the changes you requested?

@@ -182,10 +185,12 @@ def write_to_file_with_progress(data):
callback = output_handle.write

remote_path, remote_file_name = os.path.split(remote_full_path)
current_path = conn.pwd()
conn.cwd(remote_path)
self.log.info("Retrieving file from FTP: %s", remote_full_path)
conn.retrbinary(f"RETR {remote_file_name}", callback, block_size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can provide the path of the file instead of its name to the command.

Copy link
Contributor Author

@makrushin-evgenii makrushin-evgenii Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. The old implementation confused me, I thought that this was impossible - so I tried to get around it by calling the cwd method again. Perhaps there was a reason for this approach?

86f92ca

@@ -214,8 +219,10 @@ def store_file(
else:
input_handle = local_full_path_or_buffer
remote_path, remote_file_name = os.path.split(remote_full_path)
current_path = conn.pwd()
conn.cwd(remote_path)
conn.storbinary(f"STOR {remote_file_name}", input_handle, block_size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here you can provide the path instead of the name too

Copy link
Contributor Author

@makrushin-evgenii makrushin-evgenii Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Commit with your suggestion:
86f92ca

@makrushin-evgenii
Copy link
Contributor Author

This should be idone via try/finally

I don't seem to understand your proposal. Are you expecting this?

remote_path, remote_file_name = os.path.split(remote_full_path)
current_path = conn.pwd()
try:
    conn.cwd(remote_path)
    conn.storbinary(f"STOR {remote_file_name}", input_handle, block_size)
finally:
    conn.cwd(current_path)

or (better) following context_manager

I can't understand what it means. Can you point to an example please?

@potiuk
Copy link
Member

potiuk commented Oct 22, 2023

Much nicer indeed (after fixing static checks). BTW. Highly recommend installing pre-commit, it will even fix your static checks for you automatically.

conn.cwd(remote_path)
conn.storbinary(f"STOR {remote_file_name}", input_handle, block_size)

conn.storbinary(f"STOR {remote_full_path}", input_handle, block_size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably already an issue previously; do we need to quote the path here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got ftplib.error_perm: 550 Failed to open file when surround path with quotes. But your proposal is understandable. Now there should be problems if there are spaces in the path

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to retrieve and store file to path with spaces without quoting - it works. Probably ftplib somehow handles this internally

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RFC does not allow quoting for STOR and simply STOR expects filename that ends with EOL so spaces are perfectly fine there:

https://www.w3.org/Protocols/rfc959/5_Declarative.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STOR <SP> <pathname> <CRLF>

@potiuk
Copy link
Member

potiuk commented Oct 24, 2023

@hussein-awala ?

Copy link
Member

@hussein-awala hussein-awala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good, all my previous comments were addressed. LGTM

@collinmcnulty
Copy link
Contributor

This is actually a change in behavior in list_directory. Now, the full filepath folder/filename is printed instead of just the filename

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FTPSHook.store_file() change directory
5 participants