From df1b5ef4a296d3e962193471a8d663497292842a Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Wed, 20 Nov 2024 16:39:34 +0000 Subject: [PATCH] ssh: Fix folder pull on SSH connection Paramiko seems to have had a slight change in behavior that broke devlib: to save a remote command execution, we attempt to pull any path as file first, and then as a folder if the former failed. This is now broken as paramiko will create an empty destination file when trying to pull as a file. When we attempt again to pull as folder, the destination exists already (empty file) and we raise an exception. To fix that, make sure we cleanup any attempt after pulling as a file before trying again. --- devlib/utils/ssh.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/devlib/utils/ssh.py b/devlib/utils/ssh.py index 9fe4c61..81aa405 100644 --- a/devlib/utils/ssh.py +++ b/devlib/utils/ssh.py @@ -499,7 +499,18 @@ class SshConnection(SshConnectionBase): push(sftp, src, dst, callback) def _pull_file(self, sftp, src, dst, callback): - sftp.get(src, dst, callback=callback) + try: + sftp.get(src, dst, callback=callback) + except Exception as e: + # A file may have been created by Paramiko, but we want to clean + # that up, particularly if we tried to pull a folder and failed, + # otherwise this will make subsequent attempts at pulling the + # folder fail since the destination will exist. + try: + os.remove(dst) + except Exception: + pass + raise e def _pull_folder(self, sftp, src, dst, callback): os.makedirs(dst)