Things to do within the active SSH session.
d = self.conn.sendRequest(self, 'subsystem', common.NS('sftp'), wantReply=1)
class SftpClient(object):
def __init__(self, transport, *args, **kwargs):
super(SftpClient, self).__init__(*args, **kwargs)
self.transport = transport
self._client = filetransfer.FileTransferClient()
@property
def client(self):
if not self._client.connected:
self._client.makeConnection(self.transport)
logging.debug("Made 'connection' with transport class")
return self._client
def getDirectoryContents(self, path):
d = self._remoteGlob(path)
def gotit(files):
print "Got %s: %s" % (type(files), files)
d.addCallback(gotit)
return d
# Accessory methods.
# import that module as it contains unix-dependent imports.
def _remoteGlob(self, fullPath):
logging.debug('looking up %s' % fullPath)
head, tail = os.path.split(fullPath)
if '*' in tail or '?' in tail:
glob = 1
else:
glob = 0
if tail and not glob: # could be file or directory
# try directory first
logging.debug("Opening dir")
d = self.client.openDirectory(fullPath)
d.addCallback(self._cbOpenList, '')
d.addErrback(self._ebNotADirectory, head, tail)
else:
d = self.client.openDirectory(head)
d.addCallback(self._cbOpenList, tail)
return d
def _cbOpenList(self, directory, glob):
logging.debug("Got dir")
files = []
d = directory.read()
d.addBoth(self._cbReadFile, files, directory, glob)
return d
def _ebNotADirectory(self, reason, path, glob):
logging.debug("Not a dir")
d = self.client.openDirectory(path)
d.addCallback(self._cbOpenList, glob)
return d
def _cbReadFile(self, files, l, directory, glob):
logging.debug("Reading file")
if not isinstance(files, failure.Failure):
if glob:
raise NotImplementedError("don't have fnmatch available to use on Windows so have commented this bit out")
# l.extend([f for f in files if fnmatch.fnmatch(f[0], glob)])
else:
l.extend(files)
d = directory.read()
d.addBoth(self._cbReadFile, l, directory, glob)
return d
else:
reason = files
reason.trap(EOFError)
directory.close()
return l