Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e4a04ca
copy_file now runs recursively when directory paths are supplied.
Oct 24, 2015
13eae16
Added test for recursive use of copy_file.
Oct 24, 2015
700ffd5
Made code a bit more concise.
Oct 24, 2015
1d35cdb
Forgot to have test_ssh_client_directory clean up after itself.
Oct 24, 2015
b3b2e9f
copy_file now runs recursively when directory paths are supplied.
Oct 24, 2015
1c50939
Added test for recursive use of copy_file.
Oct 24, 2015
9c4bb70
Made code a bit more concise.
Oct 24, 2015
90a1b87
Forgot to have test_ssh_client_directory clean up after itself.
Oct 24, 2015
426f289
Merge branch 'sftp_recursive' of https://github.com/Caid11/parallel-s…
Oct 27, 2015
ca12de5
Broke directory copy logic into copy_dir; added recurse flag.
Oct 27, 2015
a3ae185
Updated test_ssh_client_directory to use recurse flag.
Oct 28, 2015
9faef2b
Made copy_file branching simpler; updated copy_file documentation.
Oct 28, 2015
2379b7a
copy_file now runs recursively when directory paths are supplied.
Oct 24, 2015
363e53d
Added test for recursive use of copy_file.
Oct 24, 2015
6d0022e
Made code a bit more concise.
Oct 24, 2015
4b38c14
Forgot to have test_ssh_client_directory clean up after itself.
Oct 24, 2015
655fba7
copy_file now runs recursively when directory paths are supplied.
Oct 24, 2015
6e09d52
Made code a bit more concise.
Oct 24, 2015
4af7aa0
Broke directory copy logic into copy_dir; added recurse flag.
Oct 27, 2015
ce4ae41
Updated test_ssh_client_directory to use recurse flag.
Oct 28, 2015
cac9ba2
Made copy_file branching simpler; updated copy_file documentation.
Oct 28, 2015
b912576
Merge branch 'sftp_recursive' of https://github.com/Caid11/parallel-s…
Oct 29, 2015
6ff45ee
Fixed messy merge.
Oct 29, 2015
a890f41
Forgot to enable recurse when recursively calling copy_file.
Oct 29, 2015
d14f719
ssh_client now throws an exception when given a directory and recurse…
Oct 30, 2015
8df29c9
update docstring comments for copy_file and _copy_dir
Nov 4, 2015
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
21 changes: 20 additions & 1 deletion pssh/ssh_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,16 @@ def mkdir(self, sftp, directory):
return self.mkdir(sftp, sub_dirs)
return True

def copy_file(self, local_file, remote_file):
def _copy_dir(self, local_dir, remote_dir):
"""Call copy_file on every file in the specified directory, copying
them to the specified remote directory."""
file_list = os.listdir(local_dir)
for file_name in file_list:
local_path = os.path.join(local_dir, file_name)
remote_path = os.path.join(remote_dir, file_name)
self.copy_file(local_path, remote_path, recurse=True)

def copy_file(self, local_file, remote_file, recurse=False):
"""Copy local file to host via SFTP/SCP

Copy is done natively using SFTP/SCP version 2 protocol, no scp command \
Expand All @@ -295,7 +304,17 @@ def copy_file(self, local_file, remote_file):
:type local_file: str
:param remote_file: Remote filepath on remote host to copy file to
:type remote_file: str
:param recurse: Whether or not to descend into directories recursively.
:type recurse: bool

:raises: :mod:'ValueError' when a directory is supplied to local_file \
and recurse is not set
"""
if os.path.isdir(local_file) and recurse:
return self._copy_dir(local_file, remote_file)
elif os.path.isdir(local_file) and not recurse:
raise ValueError("Recurse must be true if local_file is a "
"directory.")
sftp = self._make_sftp()
destination = [_dir for _dir in remote_file.split(os.path.sep)
if _dir][:-1][0]
Expand Down
44 changes: 44 additions & 0 deletions tests/test_ssh_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import gevent
import socket
import time
import shutil
import unittest
from pssh import SSHClient, ParallelSSHClient, UnknownHostException, AuthenticationException,\
logger, ConnectionErrorException, UnknownHostException, SSHException
Expand Down Expand Up @@ -141,6 +142,49 @@ def test_ssh_client_sftp(self):
os.rmdir(dirpath)
del client

def test_ssh_client_directory(self):
"""Tests copying directories with SSH client. Copy all the files from
local directory to server, then make sure they are all present."""
test_file_data = 'test'
local_test_path = 'directory_test'
remote_test_path = 'directory_test_copied'
os.mkdir(local_test_path)
remote_file_paths = []
for i in range(0, 10):
local_file_path = os.path.join(local_test_path, 'foo' + str(i))
remote_file_path = os.path.join(remote_test_path, 'foo' + str(i))
remote_file_paths.append(remote_file_path)
test_file = open(local_file_path, 'w')
test_file.write(test_file_data)
test_file.close()
client = SSHClient(self.host, port=self.listen_port,
pkey=self.user_key)
client.copy_file(local_test_path, remote_test_path, recurse=True)
for path in remote_file_paths:
self.assertTrue(os.path.isfile(path))
shutil.rmtree(local_test_path)
shutil.rmtree(remote_test_path)

def test_ssh_client_directory_no_recurse(self):
"""Tests copying directories with SSH client. Copy all the files from
local directory to server, then make sure they are all present."""
test_file_data = 'test'
local_test_path = 'directory_test'
remote_test_path = 'directory_test_copied'
os.mkdir(local_test_path)
remote_file_paths = []
for i in range(0, 10):
local_file_path = os.path.join(local_test_path, 'foo' + str(i))
remote_file_path = os.path.join(remote_test_path, 'foo' + str(i))
remote_file_paths.append(remote_file_path)
test_file = open(local_file_path, 'w')
test_file.write(test_file_data)
test_file.close()
client = SSHClient(self.host, port=self.listen_port,
pkey=self.user_key)
self.assertRaises(ValueError, client.copy_file, local_test_path, remote_test_path)
shutil.rmtree(local_test_path)

def test_ssh_agent_authentication(self):
"""Test authentication via SSH agent.
Do not provide public key to use when creating SSHClient,
Expand Down