Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@same_docstring_as(Transport.makedirs)
def makedirs(self, path, mode=0777):
dirs = path.split('/')
if '..' in dirs:
raise gc3libs.exceptions.InvalidArgument(
"Path component '..' not allowed in `SshTransport.makedirs()`")
dest = ''
for dir in dirs:
if dir in ['', '.']:
continue
dest += '/' + dir
try:
# check connection first
self.connect()
self.sftp.mkdirs(dest, mode)
except IOError:
# sftp.mkdir raises IOError if the directory exists;
@same_docstring_as(Transport.chmod)
def chmod(self, path, mode):
try:
os.chmod(path, mode)
except Exception, ex:
raise gc3libs.exceptions.TransportError(
"Error changing local path '%s' mode to 0%o: %s: %s"
% (path, mode, ex.__class__.__name__, str(ex)))
@same_docstring_as(Transport.listdir)
def listdir(self, path):
try:
# check connection first
self.connect()
return self.sftp.listdir(path)
except Exception as ex:
raise gc3libs.exceptions.TransportError(
"Could not list directory '%s' on host '%s': %s: %s"
% (path, self.remote_frontend, ex.__class__.__name__, str(ex)))
@same_docstring_as(Transport.get)
def get(self, source, destination, ignore_nonexisting=False):
try:
gc3libs.log.debug("SshTranport.get(): remote source %s; remote host: %s; local destination: %s."
% (source, self.remote_frontend, destination))
if self.isdir(source):
# recursively descend it
for name in self.listdir(source):
# don't use `os.path.join` here, ``/`` is
# the right separator to use; see
# http://code.fabfile.org/issues/show/306
self.get(source + '/' + name, destination + '/' + name)
else:
# check connection first
self.connect()
self.sftp.get(source, destination)
except Exception, ex:
@same_docstring_as(Transport.open)
def open(self, source, mode, bufsize=-1):
try:
# check connection first
self.connect()
return self.sftp.open(source, mode, bufsize)
except Exception as ex:
raise gc3libs.exceptions.TransportError(
"Could not open file '%s' on host '%s': %s: %s"
% (source, self.remote_frontend,
ex.__class__.__name__, str(ex)))
@same_docstring_as(Transport.listdir)
def listdir(self, path):
try:
# check connection first
self.connect()
return self.sftp.listdir(path)
except Exception, ex:
raise gc3libs.exceptions.TransportError("Could not list directory '%s' on host '%s': %s: %s"
% (path, self.remote_frontend,
ex.__class__.__name__, str(ex)))
@same_docstring_as(Transport.connect)
def connect(self):
try:
self.transport_channel = self.ssh.get_transport()
if not self._is_open or self.transport_channel == None or not self.transport_channel.is_active():
gc3libs.log.info("Opening SshTransport... ")
self.ssh.load_system_host_keys()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
gc3libs.log.debug(
"Connecting to host '%s' as user '%s' via SSH ...",
self.remote_frontend, self.username)
self.ssh.connect(self.remote_frontend,
timeout=gc3libs.Default.SSH_CONNECT_TIMEOUT,
username=self.username,
allow_agent=True,
key_filename = self.keyfile)
self.sftp = self.ssh.open_sftp()
@same_docstring_as(Transport.isdir)
def isdir(self, path):
return os.path.isdir(path)
@same_docstring_as(Transport.remove)
def remove(self, path):
assert self._is_open is True, \
"`Transport.execute_command()` called" \
" on `Transport` instance closed / not yet open"
try:
gc3libs.log.debug("Removing %s", path)
return os.remove(path)
except Exception, ex:
raise gc3libs.exceptions.TransportError("Could not remove file '%s': %s: %s"
% (path, ex.__class__.__name__, str(ex)))
@same_docstring_as(Transport.remove)
def remove(self, path):
try:
gc3libs.log.debug(
"SshTransport.remove(): path: %s; remote host: %s",
path, self.remote_frontend)
# check connection first
self.connect()
self.sftp.remove(path)
except IOError as ex:
raise gc3libs.exceptions.TransportError(
"Could not remove '%s' on host '%s': %s: %s"
% (path, self.remote_frontend, ex.__class__.__name__, str(ex)))