Skip to content

Commit cba131e

Browse files
authored
Copy encoding fix (#313)
* Added unittest for remote dir copy supplying correct encoding when recursion is on - fix copy_remote_file issue with encoding when recurse is on * Updated changelog
1 parent 3130318 commit cba131e

File tree

5 files changed

+31
-47
lines changed

5 files changed

+31
-47
lines changed

Changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Changes
1010
* ``user`` keyword argument no longer required on Windows - exception is raised if user cannot be identified.
1111
* Removed deprecated since ``2.0.0`` functions and parameters.
1212

13+
Fixes
14+
-----
15+
16+
* ``copy_remote_file`` with recurse enabled would not use a provided encoding for sub-directories - #284
17+
1318
2.5.4
1419
+++++
1520

pssh/clients/base/parallel.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,7 @@ def join(self, output=None, consume_output=False, timeout=None):
335335
timeout, finished_output, unfinished_output,
336336
)
337337

338-
def _join(self, host_out, consume_output=False, timeout=None,
339-
encoding="utf-8"):
338+
def _join(self, host_out, consume_output=False, timeout=None):
340339
if host_out is None:
341340
return
342341
client = host_out.client

pssh/clients/base/single.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def _copy_remote_dir(self, file_list, remote_dir, local_dir, sftp,
630630
remote_path = os.path.join(remote_dir, file_name)
631631
local_path = os.path.join(local_dir, file_name)
632632
self.copy_remote_file(remote_path, local_path, sftp=sftp,
633-
recurse=True)
633+
recurse=True, encoding=encoding)
634634

635635
def _make_local_dir(self, dirpath):
636636
if os.path.exists(dirpath):

tests/native/test_parallel_client.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -487,50 +487,6 @@ def test_pssh_copy_file_per_host_args(self):
487487
os.unlink(remote_file_abspath)
488488
os.unlink(local_file_path)
489489

490-
def test_pssh_client_directory_relative_path(self):
491-
"""Tests copying multiple directories with SSH client. Copy all the files from
492-
local directory to server, then make sure they are all present."""
493-
client = ParallelSSHClient([self.host], port=self.port,
494-
pkey=self.user_key)
495-
test_file_data = 'test'
496-
local_test_path = 'directory_test'
497-
remote_test_path = 'directory_test_copied'
498-
dir_name = os.path.dirname(__file__)
499-
remote_test_path_rel = os.sep.join(
500-
(dir_name.replace(os.path.expanduser('~') + os.sep, ''),
501-
remote_test_path))
502-
remote_test_path_abs = os.sep.join((dir_name, remote_test_path))
503-
for path in [local_test_path, remote_test_path_abs]:
504-
try:
505-
shutil.rmtree(path)
506-
except OSError:
507-
pass
508-
os.mkdir(local_test_path)
509-
remote_file_paths = []
510-
for i in range(0, 10):
511-
local_file_path_dir = os.path.join(
512-
local_test_path, 'sub_dir1', 'sub_dir2', 'dir_foo' + str(i))
513-
os.makedirs(local_file_path_dir)
514-
local_file_path = os.path.join(local_file_path_dir, 'foo' + str(i))
515-
remote_file_path = os.path.join(
516-
remote_test_path, 'sub_dir1', 'sub_dir2', 'dir_foo' + str(i), 'foo' + str(i))
517-
remote_file_paths.append(
518-
os.sep.join((os.path.dirname(__file__), remote_file_path)))
519-
test_file = open(local_file_path, 'w')
520-
test_file.write(test_file_data)
521-
test_file.close()
522-
cmds = client.copy_file(local_test_path, remote_test_path_rel, recurse=True)
523-
try:
524-
joinall(cmds, raise_error=True)
525-
for path in remote_file_paths:
526-
self.assertTrue(os.path.isfile(path))
527-
finally:
528-
for _path in (local_test_path, remote_test_path_abs):
529-
try:
530-
shutil.rmtree(_path)
531-
except Exception:
532-
pass
533-
534490
def test_pssh_client_directory_abs_path(self):
535491
client = ParallelSSHClient([self.host], port=self.port,
536492
pkey=self.user_key)

tests/native/test_single_client.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import subprocess
2020
import shutil
2121
import tempfile
22+
from unittest.mock import MagicMock, call
2223
from hashlib import sha256
2324
from datetime import datetime
2425

@@ -935,6 +936,29 @@ def _disc():
935936
client._connect_init_session_retry(1)
936937
client.disconnect()
937938

939+
def test_copy_remote_dir_encoding(self):
940+
client = SSHClient(self.host, port=self.port,
941+
pkey=self.user_key,
942+
num_retries=1)
943+
remote_file_mock = MagicMock()
944+
suffix = b"\xbc"
945+
encoding = 'latin-1'
946+
encoded_fn = suffix.decode(encoding)
947+
file_list = [suffix + b"1", suffix + b"2"]
948+
client.copy_remote_file = remote_file_mock
949+
local_dir = (b"l_dir" + suffix).decode(encoding)
950+
remote_dir = (b"r_dir" + suffix).decode(encoding)
951+
client._copy_remote_dir(
952+
file_list, local_dir, remote_dir, None, encoding=encoding)
953+
call_args = [call(local_dir + "/" + file_list[0].decode(encoding),
954+
remote_dir + "/" + file_list[0].decode(encoding),
955+
recurse=True, sftp=None, encoding=encoding),
956+
call(local_dir + "/" + file_list[1].decode(encoding),
957+
remote_dir + "/" + file_list[1].decode(encoding),
958+
recurse=True, sftp=None, encoding=encoding)
959+
]
960+
self.assertListEqual(remote_file_mock.call_args_list, call_args)
961+
938962

939963
# TODO
940964
# * read output callback

0 commit comments

Comments
 (0)