Skip to content

Commit 0143982

Browse files
authored
Make it possible to scp send and recv files without recursion where target path is a directory - copy to directory with same filename as source filename
Added tests for scp_recv and send with recursion off where target path is dir. Resolves #183 (#213)
1 parent 070672d commit 0143982

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

pssh/clients/native/single.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ def scp_recv(self, remote_file, local_file, recurse=False, sftp=None,
505505
return self._scp_recv_dir(file_list, remote_file,
506506
local_file, sftp,
507507
encoding=encoding)
508+
elif local_file.endswith('/'):
509+
remote_filename = remote_file.rsplit('/')[-1]
510+
local_file += remote_filename
508511
destination = os.path.join(os.path.sep, os.path.sep.join(
509512
[_dir for _dir in local_file.split('/')
510513
if _dir][:-1]))
@@ -577,6 +580,9 @@ def scp_send(self, local_file, remote_file, recurse=False, sftp=None):
577580
self._eagain(sftp.stat, destination)
578581
except (SFTPHandleError, SFTPProtocolError):
579582
self.mkdir(sftp, destination)
583+
elif remote_file.endswith('/'):
584+
local_filename = local_file.rsplit('/')[-1]
585+
remote_file += local_filename
580586
self._scp_send(local_file, remote_file)
581587
logger.info("SCP local file %s to remote destination %s:%s",
582588
local_file, self.host, remote_file)

tests/native/test_single_client.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,85 @@ def test_scp_send_large_file(self):
434434
os.unlink(_path)
435435
except Exception:
436436
pass
437+
438+
def test_scp_send_dir_target(self):
439+
cur_dir = os.path.dirname(__file__)
440+
file_name = 'file1'
441+
file_path_from = os.path.sep.join([cur_dir, file_name])
442+
file_copy_to_dirpath = os.path.expanduser('~/')
443+
file_copy_to_abs = file_copy_to_dirpath + file_name
444+
for _path in (file_path_from, file_copy_to_abs):
445+
try:
446+
os.unlink(_path)
447+
except OSError:
448+
pass
449+
try:
450+
with open(file_path_from, 'wb') as fh:
451+
fh.write(b"adsfasldkfjabafj")
452+
self.client.scp_send(file_path_from, file_copy_to_dirpath)
453+
self.assertTrue(os.path.isfile(file_copy_to_abs))
454+
finally:
455+
for _path in (file_path_from, file_copy_to_abs):
456+
try:
457+
os.unlink(_path)
458+
except OSError:
459+
pass
460+
# Relative path
461+
file_copy_to_dirpath = './'
462+
for _path in (file_path_from, file_copy_to_abs):
463+
try:
464+
os.unlink(_path)
465+
except OSError:
466+
pass
467+
try:
468+
with open(file_path_from, 'wb') as fh:
469+
fh.write(b"adsfasldkfjabafj")
470+
self.client.scp_send(file_path_from, file_copy_to_dirpath)
471+
self.assertTrue(os.path.isfile(file_copy_to_abs))
472+
finally:
473+
for _path in (file_path_from, file_copy_to_abs):
474+
try:
475+
os.unlink(_path)
476+
except OSError:
477+
pass
478+
479+
def test_scp_recv_dir_target(self):
480+
cur_dir = os.path.dirname(__file__)
481+
file_name = 'file1'
482+
file_path_from = os.path.sep.join([cur_dir, file_name])
483+
file_copy_to_dirpath = os.path.expanduser('~/')
484+
file_copy_to_abs = file_copy_to_dirpath + file_name
485+
for _path in (file_path_from, file_copy_to_abs):
486+
try:
487+
os.unlink(_path)
488+
except OSError:
489+
pass
490+
try:
491+
with open(file_path_from, 'wb') as fh:
492+
fh.write(b"adsfasldkfjabafj")
493+
self.client.scp_recv(file_path_from, file_copy_to_dirpath)
494+
self.assertTrue(os.path.isfile(file_copy_to_abs))
495+
finally:
496+
for _path in (file_path_from, file_copy_to_abs):
497+
try:
498+
os.unlink(_path)
499+
except OSError:
500+
pass
501+
# Relative path
502+
file_copy_to_dirpath = './'
503+
for _path in (file_path_from, file_copy_to_abs):
504+
try:
505+
os.unlink(_path)
506+
except OSError:
507+
pass
508+
try:
509+
with open(file_path_from, 'wb') as fh:
510+
fh.write(b"adsfasldkfjabafj")
511+
self.client.scp_send(file_path_from, file_copy_to_dirpath)
512+
self.assertTrue(os.path.isfile(file_copy_to_abs))
513+
finally:
514+
for _path in (file_path_from, file_copy_to_abs):
515+
try:
516+
os.unlink(_path)
517+
except OSError:
518+
pass

0 commit comments

Comments
 (0)