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

Add keyword to remove file(s) #469

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/SSHLibrary/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
13 changes: 13 additions & 0 deletions src/SSHLibrary/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down