From 3cea7b8f22e99e9a3be1e62558e2f2d6bfee84ab Mon Sep 17 00:00:00 2001 From: Panos Date: Tue, 23 Aug 2022 08:02:59 +0100 Subject: [PATCH 1/2] Moved command string parsing to separate function. Fixed issue with remote file path generation on copy_remote_file on windows clients when recurse is enabled. --- pssh/clients/base/single.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pssh/clients/base/single.py b/pssh/clients/base/single.py index 6229e0f4..c14f679d 100644 --- a/pssh/clients/base/single.py +++ b/pssh/clients/base/single.py @@ -540,23 +540,26 @@ def run_command(self, command, sudo=False, user=None, :rtype: :py:class:`pssh.output.HostOutput` """ # Fast path for no command substitution needed - if not sudo and not user and not shell: - _command = command - else: - _command = '' - if sudo and not user: - _command = 'sudo -S ' - elif user: - _command = 'sudo -u %s -S ' % (user,) - _shell = shell if shell else '$SHELL -c' - _command += "%s '%s'" % (_shell, command,) - _command = _command.encode(encoding) + _command = self._make_command(command, encoding, user, shell, sudo) with GTimeout(seconds=self.timeout): channel = self.execute(_command, use_pty=use_pty) _timeout = read_timeout if read_timeout else timeout host_out = self._make_host_output(channel, encoding, _timeout) return host_out + def _make_command(self, command, encoding, user, shell, sudo): + if not sudo and not user and not shell: + return command + _command = '' + if sudo and not user: + _command = 'sudo -S ' + elif user: + _command = 'sudo -u %s -S ' % (user,) + _shell = shell if shell else '$SHELL -c' + _command += "%s '%s'" % (_shell, command,) + _command = _command.encode(encoding) + return _command + def _eagain_write_errcode(self, write_func, data, eagain): data_len = len(data) total_written = 0 @@ -671,7 +674,7 @@ def _copy_remote_dir(self, file_list, remote_dir, local_dir, sftp, file_name = file_name.decode(encoding) if file_name in ('.', '..'): continue - remote_path = os.path.join(remote_dir, file_name) + remote_path = "/".join((remote_dir, file_name)) local_path = os.path.join(local_dir, file_name) self.copy_remote_file(remote_path, local_path, sftp=sftp, recurse=True, encoding=encoding) From 85f6a23b83f392a69bb8e560fc48240fa449823d Mon Sep 17 00:00:00 2001 From: Panos Date: Mon, 29 Aug 2022 10:31:18 +0100 Subject: [PATCH 2/2] Updated making command string --- pssh/clients/base/single.py | 4 ++-- tests/native/test_parallel_client.py | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pssh/clients/base/single.py b/pssh/clients/base/single.py index c14f679d..460bb814 100644 --- a/pssh/clients/base/single.py +++ b/pssh/clients/base/single.py @@ -539,7 +539,6 @@ def run_command(self, command, sudo=False, user=None, :rtype: :py:class:`pssh.output.HostOutput` """ - # Fast path for no command substitution needed _command = self._make_command(command, encoding, user, shell, sudo) with GTimeout(seconds=self.timeout): channel = self.execute(_command, use_pty=use_pty) @@ -548,8 +547,9 @@ def run_command(self, command, sudo=False, user=None, return host_out def _make_command(self, command, encoding, user, shell, sudo): + # Fast path for no command substitution needed if not sudo and not user and not shell: - return command + return command if isinstance(command, bytes) else command.encode(encoding) _command = '' if sudo and not user: _command = 'sudo -S ' diff --git a/tests/native/test_parallel_client.py b/tests/native/test_parallel_client.py index 72ea2cae..b62e18da 100644 --- a/tests/native/test_parallel_client.py +++ b/tests/native/test_parallel_client.py @@ -402,9 +402,8 @@ def test_pssh_copy_file(self): remote_filename = os.path.sep.join([remote_test_dir, remote_filepath]) remote_file_abspath = os.path.expanduser('~/' + remote_filename) remote_test_dir_abspath = os.path.expanduser('~/' + remote_test_dir) - test_file = open(local_filename, 'w') - test_file.writelines([test_file_data + os.linesep]) - test_file.close() + with open(local_filename, 'w') as test_file_fh: + test_file_fh.writelines([test_file_data + os.linesep]) cmds = client.copy_file(local_filename, remote_filename) cmds[0].get() try: