From b48c0232ebde1770311edbfcebba7caaa34f5d13 Mon Sep 17 00:00:00 2001 From: Anurag <130895173+Anurag0git@users.noreply.github.com> Date: Sun, 9 Feb 2025 22:35:44 -0800 Subject: [PATCH 1/2] Transport: SshTransport bug in method rename #6725 solved --- src/aiida/transports/plugins/ssh.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/aiida/transports/plugins/ssh.py b/src/aiida/transports/plugins/ssh.py index f5d0e7053f..23e86baa16 100644 --- a/src/aiida/transports/plugins/ssh.py +++ b/src/aiida/transports/plugins/ssh.py @@ -1368,16 +1368,13 @@ def rename(self, oldpath: TransportPath, newpath: TransportPath): oldpath = str(oldpath) newpath = str(newpath) - if not self.isfile(oldpath): - if not self.isdir(oldpath): + """The code now checks if oldpath exists and raises an OSError if not. A check is added to ensure that newpath doesn't already exist (using FileExistsError), to avoid unintentional overwriting.""" + + if not self.isfile(oldpath) and not self.isdir(oldpath): raise OSError(f'Source {oldpath} does not exist') - # TODO: this seems to be a bug (?) - # why to raise an OSError if the newpath does not exist? - # ofcourse newpath shouldn't exist, that's why we are renaming it! - # issue opened here: https://github.com/aiidateam/aiida-core/issues/6725 - if not self.isfile(newpath): - if not self.isdir(newpath): - raise OSError(f'Destination {newpath} does not exist') + + if self.isfile(newpath) or self.isdir(newpath): + raise FileExistsError(f'Destination {newpath}already exists') return self.sftp.rename(oldpath, newpath) From 93878749c90d5783ffa0bc11a7d4ac1c532e4b36 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 9 Feb 2025 17:11:22 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/aiida/transports/plugins/ssh.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aiida/transports/plugins/ssh.py b/src/aiida/transports/plugins/ssh.py index 23e86baa16..ae6852f73f 100644 --- a/src/aiida/transports/plugins/ssh.py +++ b/src/aiida/transports/plugins/ssh.py @@ -1371,10 +1371,10 @@ def rename(self, oldpath: TransportPath, newpath: TransportPath): """The code now checks if oldpath exists and raises an OSError if not. A check is added to ensure that newpath doesn't already exist (using FileExistsError), to avoid unintentional overwriting.""" if not self.isfile(oldpath) and not self.isdir(oldpath): - raise OSError(f'Source {oldpath} does not exist') + raise OSError(f'Source {oldpath} does not exist') if self.isfile(newpath) or self.isdir(newpath): - raise FileExistsError(f'Destination {newpath}already exists') + raise FileExistsError(f'Destination {newpath}already exists') return self.sftp.rename(oldpath, newpath)