Skip to content

Commit 7a1091b

Browse files
Danpkittenis
authored andcommitted
Cleanup and code quality changes. Changed SFTP test to use a remote path including a non-existing directory and a check that the directory is created remotely
1 parent a5c6b7f commit 7a1091b

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

pssh.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ def __init__(self, host,
139139
self.proxy_command = paramiko.ProxyCommand(host_config['proxycommand']) if 'proxycommand' in \
140140
host_config else None
141141
if self.proxy_command:
142-
logger.debug("Proxy configured for destination host %s - ProxyCommand: '%s'" % (
143-
self.host, " ".join(self.proxy_command.cmd),))
142+
logger.debug("Proxy configured for destination host %s - ProxyCommand: '%s'",
143+
self.host, " ".join(self.proxy_command.cmd),)
144144
if _agent:
145145
self.client._agent = _agent
146146
self._connect()
@@ -153,26 +153,26 @@ def _connect(self, retries=1):
153153
pkey=self.pkey,
154154
sock=self.proxy_command)
155155
except socket.gaierror, e:
156-
logger.error("Could not resolve host '%s'", self.host,)
156+
logger.error("Could not resolve host '%s' - retry %s/%s",
157+
self.host, retries, NUM_RETRIES)
157158
while retries < NUM_RETRIES:
158159
gevent.sleep(5)
159160
return self._connect(retries=retries+1)
160161
raise UnknownHostException("%s - %s" % (str(e.args[1]),
161162
self.host,))
162163
except socket.error, e:
163-
logger.error("Error connecting to host '%s:%s'" % (self.host,
164-
self.port,))
164+
logger.error("Error connecting to host '%s:%s' - retry %s/%s",
165+
self.host, self.port, retries, NUM_RETRIES)
165166
while retries < NUM_RETRIES:
166167
gevent.sleep(5)
167168
return self._connect(retries=retries+1)
168-
raise ConnectionErrorException("%s for host '%s:%s'" % (str(e.args[1]),
169-
self.host,
170-
self.port,))
169+
raise ConnectionErrorException("%s for host '%s:%s' - retry %s/%s",
170+
str(e.args[1]), self.host, self.port,
171+
retries, NUM_RETRIES,)
171172
except paramiko.AuthenticationException, e:
172173
raise AuthenticationException(e)
173174
except paramiko.ProxyCommandFailure, e:
174-
logger.error("Error executing ProxyCommand - %s" % (
175-
e.message,))
175+
logger.error("Error executing ProxyCommand - %s", e.message,)
176176
raise ProxyCommandException(e.message)
177177

178178
def exec_command(self, command, sudo=False, **kwargs):
@@ -188,7 +188,7 @@ def exec_command(self, command, sudo=False, **kwargs):
188188
:type sudo: bool
189189
:param kwargs: (Optional) Keyword arguments to be passed to remote \
190190
command
191-
:type kwargs: dict
191+
:type kwargs: dict
192192
:rtype: Tuple of `(channel, hostname, stdout, stderr)`. \
193193
Channel is the remote SSH channel, needed to ensure all of stdout has \
194194
been got, hostname is remote hostname the copy is to, stdout and \
@@ -215,7 +215,7 @@ def exec_command(self, command, sudo=False, **kwargs):
215215
def _make_sftp(self):
216216
"""Make SFTP client from open transport"""
217217
transport = self.client.get_transport()
218-
channel = transport.open_session()
218+
transport.open_session()
219219
return paramiko.SFTPClient.from_transport(transport)
220220

221221
def mkdir(self, sftp, directory):
@@ -246,7 +246,6 @@ def copy_file(self, local_file, remote_file):
246246
"""
247247
sftp = self._make_sftp()
248248
destination = remote_file.split(os.path.sep)
249-
filename = destination[0] if len(destination) == 1 else destination[-1]
250249
remote_file = os.path.sep.join(destination)
251250
destination = destination[:-1]
252251
for directory in destination:

tests/test_ssh_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def test_ssh_client_sftp(self):
4949
server, remove files and directory."""
5050
test_file_data = 'test'
5151
local_filename = 'test_file'
52-
remote_filename = 'test_file_copy'
52+
remote_test_dir, remote_filename = 'remote_test_dir', 'test_file_copy'
53+
remote_filename = os.path.sep.join([remote_test_dir, remote_filename])
5354
remote_dir = 'remote_dir'
5455
test_file = open(local_filename, 'w')
5556
test_file.writelines([test_file_data + os.linesep])
@@ -59,6 +60,8 @@ def test_ssh_client_sftp(self):
5960
client = SSHClient('127.0.0.1', port=self.listen_port,
6061
pkey=self.user_key)
6162
client.copy_file(local_filename, remote_filename)
63+
self.assertTrue(os.path.isdir(remote_test_dir),
64+
msg="SFTP create remote directory failed")
6265
self.assertTrue(os.path.isfile(remote_filename),
6366
msg="SFTP copy failed")
6467
copied_file = open(remote_filename, 'r')

0 commit comments

Comments
 (0)