From 061f2d38c35c134a202e47d3f63b61ffda2965f8 Mon Sep 17 00:00:00 2001 From: Paul Brand Date: Tue, 22 Oct 2024 11:37:36 +0200 Subject: [PATCH 1/3] Removes duplicate "list_files_in_dir" call. --- src/SSHLibrary/library.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/SSHLibrary/library.py b/src/SSHLibrary/library.py index aba4026de..a2d3e33ba 100644 --- a/src/SSHLibrary/library.py +++ b/src/SSHLibrary/library.py @@ -2193,7 +2193,6 @@ def list_files_in_directory(self, path, pattern=None, absolute=False): files = self.current.list_files_in_dir(path, pattern, absolute) except SSHClientException as msg: raise RuntimeError(msg) - files = self.current.list_files_in_dir(path, pattern, absolute) self._log( "{0} file{1}:\n{2}".format( len(files), plural_or_not(files), "\n".join(files) From f5089c3abc90c6881a2f1d95d83df1c3daba3e98 Mon Sep 17 00:00:00 2001 From: Paul Brand Date: Tue, 26 Nov 2024 21:10:15 +0100 Subject: [PATCH 2/3] Adds keyword to remove file --- src/SSHLibrary/client.py | 23 +++++++++++++++++++++++ src/SSHLibrary/library.py | 13 +++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/SSHLibrary/client.py b/src/SSHLibrary/client.py index cf5cb4340..9d760c70a 100644 --- a/src/SSHLibrary/client.py +++ b/src/SSHLibrary/client.py @@ -1532,6 +1532,29 @@ def _put_file(self, source, destination, mode, newline, path_separator, scp_pres position += len(data) self._close_remote_file(remote_file) + def remove_file(self, source, path_separator='/'): + r"""Removes file(s) from the remote host. + + :param str source: Must be the path to an existing file on the remote + machine or a glob pattern. + Glob patterns, like '*' and '?', can be used in the source, in + which case all the matching files are removed. + + :param str path_separator: The path separator used for joining the + paths on the remote host. On Windows, this must be set as `\`. + The default is `/`, which is also the default on Linux-like systems. + """ + remote_files = self._get_get_file_sources(source, path_separator) + if not remote_files: + msg = "There were no remote files matching '%s'." % source + raise SSHClientException(msg) + for src in remote_files: + self._remove_file(src) + + def _remove_file(self, remote_path): + remote_path = remote_path.encode(self._encoding) + self._client.remove(remote_path) + def _list(self, path): path = path.encode(self._encoding) for item in self._client.listdir_attr(path): diff --git a/src/SSHLibrary/library.py b/src/SSHLibrary/library.py index a2d3e33ba..06fc12a3e 100644 --- a/src/SSHLibrary/library.py +++ b/src/SSHLibrary/library.py @@ -1997,6 +1997,19 @@ def put_file( scp_preserve_times, ) + @keyword(tags=("file",)) + def remove_file(self, source): + """Removes file(s) from the remote machine. + + ``source`` is a path on the remote machine. Both absolute paths and + paths relative to the current working directory are supported. + If the source contains wildcards explained in `glob patterns`, + all files matching it are removed. + + SCP is not supported. + """ + self._run_command(self.current.remove_file, source) + @keyword(tags=("file",)) def put_directory( self, From e1afa8e7a553460345919858349b91df005e40be Mon Sep 17 00:00:00 2001 From: Paul Brand Date: Fri, 20 Dec 2024 14:05:32 +0100 Subject: [PATCH 3/3] Revert change from other PR --- src/SSHLibrary/library.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SSHLibrary/library.py b/src/SSHLibrary/library.py index 06fc12a3e..440645065 100644 --- a/src/SSHLibrary/library.py +++ b/src/SSHLibrary/library.py @@ -2206,6 +2206,7 @@ def list_files_in_directory(self, path, pattern=None, absolute=False): files = self.current.list_files_in_dir(path, pattern, absolute) except SSHClientException as msg: raise RuntimeError(msg) + files = self.current.list_files_in_dir(path, pattern, absolute) self._log( "{0} file{1}:\n{2}".format( len(files), plural_or_not(files), "\n".join(files)