Skip to content

Commit

Permalink
Implement #689
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Oct 19, 2018
1 parent bd3ba34 commit d063fd0
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 106 deletions.
19 changes: 11 additions & 8 deletions IM/SSH.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,11 @@ def sftp_get(self, src, dest):
def _sftp_put(sftp, src, dest):
f_flags = LIBSSH2_FXF_CREAT | LIBSSH2_FXF_WRITE
fileinfo = os.stat(src)
with open(src, 'rb') as local_fh, sftp.open(dest, f_flags, fileinfo.st_mode) as remote_fh:
remote_fh = sftp.open(dest, f_flags, fileinfo.st_mode)
with open(src, 'rb') as local_fh:
for data in local_fh:
remote_fh.write(data)
remote_fh.close()

def sftp_put(self, src, dest):
""" Puts a file to the remote server
Expand Down Expand Up @@ -396,10 +398,10 @@ def sftp_list(self, directory):
client = self.connect()
sftp = client.sftp_init()
res = []
with sftp.opendir(directory) as fh:
for _, name, attrs in fh.readdir():
print(attrs.permissions)
res.append(name)
fh = sftp.opendir(directory)
for _, name, _ in fh.readdir():
res.append(name)
fh.close()
return res

def sftp_list_attr(self, directory):
Expand All @@ -415,9 +417,10 @@ def sftp_list_attr(self, directory):
client = self.connect()
sftp = client.sftp_init()
res = []
with sftp.opendir(directory) as fh:
for _, _, attrs in fh.readdir():
res.append(attrs)
fh = sftp.opendir(directory)
for _, _, attrs in fh.readdir():
res.append(attrs)
fh.close()
return res

def getcwd(self):
Expand Down
30 changes: 15 additions & 15 deletions IM/SSHRetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from IM.retry import retry
from IM.SSH import SSH, AuthenticationException
import paramiko
from ssh2.exceptions import AuthenticationError


class SSHRetry(SSH):
Expand All @@ -26,72 +26,72 @@ class SSHRetry(SSH):
DELAY = 3
BACKOFF = 2

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def execute(self, command, timeout=None):
return SSH.execute(self, command, timeout)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_get(self, src, dest):
return SSH.sftp_get(self, src, dest)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_get_files(self, src, dest):
return SSH.sftp_get_files(self, src, dest)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_put_files(self, files):
return SSH.sftp_put_files(self, files)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_put(self, src, dest):
return SSH.sftp_put(self, src, dest)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_put_dir(self, src, dest):
return SSH.sftp_put_dir(self, src, dest)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_get_dir(self, src, dest):
return SSH.sftp_get_dir(self, src, dest)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_put_content(self, content, dest):
return SSH.sftp_put_content(self, content, dest)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_mkdir(self, directory):
return SSH.sftp_mkdir(self, directory)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_list(self, directory):
return SSH.sftp_list(self, directory)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_list_attr(self, directory):
return SSH.sftp_list_attr(self, directory)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def getcwd(self):
return SSH.getcwd(self)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_remove(self, path):
return SSH.sftp_remove(self, path)

@retry(Exception, (AuthenticationException, paramiko.AuthenticationException),
@retry(Exception, (AuthenticationException, AuthenticationError),
tries=TRIES, delay=DELAY, backoff=BACKOFF)
def sftp_chmod(self, path, mode):
return SSH.sftp_chmod(self, path, mode)
Loading

0 comments on commit d063fd0

Please sign in to comment.