Skip to content
Snippets Groups Projects
sftpUtility.py 2.92 KiB
Newer Older
sveseli's avatar
sveseli committed
#!/usr/bin/env python

import copy
import stat
import pysftp
sveseli's avatar
sveseli committed
from dm.common.utility.timeUtility import TimeUtility
sveseli's avatar
sveseli committed
from dm.common.utility.loggingManager import LoggingManager
import urlparse

class SftpUtility:

    DEFAULT_PORT = 22

    def __init__(self, host, port=DEFAULT_PORT, username=None, password=None):
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.sftpClient = None

    @classmethod
    def parseFtpUrl(cls, url, defaultHost=None, defaultPort=None):
        host = defaultHost
        port = defaultPort
        scheme = None
        dirPath = url
        if url.startswith('ftp://'):
            parseResult = urlparse.urlparse(url)
            scheme = parseResult.scheme
            netlocTokens = parseResult.netloc.split(':')
            host = netlocTokens[0]
            if len(netlocTokens) > 1:
                port = int(netlocTokens[1])
            dirPath = parseResult.path
        return (scheme, host, port, dirPath)

    @classmethod
    def getSftpClient(cls, host, port=DEFAULT_PORT, username=None, password=None):
        sftp = pysftp.Connection(host, username=username, password=password, port=port)
        return sftp

    @classmethod
    def getLogger(cls):
        logger = LoggingManager.getInstance().getLogger(cls.__name__)
        return logger


    def __parseKeyValue(cls, keyValue, outputDict={}):
        key,value = keyValue.split('=')
        value = value.strip()
        outputDict[key] = value
        return outputDict
        
    def getFiles(self, dirPath, fileDict={}, replacementDirPath=None):
sveseli's avatar
sveseli committed
        if not self.sftpClient:
            self.sftpClient = self.getSftpClient(self.host, self.port, self.username, self.password)
        if not replacementDirPath:
            replacementDirPath = dirPath
sveseli's avatar
sveseli committed
        attrs = self.sftpClient.listdir_attr(dirPath)
        for attr in attrs:
            fileName = attr.filename
            mode = attr.st_mode
            if stat.S_ISDIR(mode):
                dirPath2 = '%s/%s' % (dirPath, fileName)
                replacementDirPath2 = '%s/%s' % (replacementDirPath, fileName)
                self.getFiles(dirPath2, fileDict, replacementDirPath2)
sveseli's avatar
sveseli committed
            elif stat.S_ISREG(mode):
                filePath = '%s/%s' % (replacementDirPath, fileName)
                fileInfo = {'fileSize' : attr.st_size, 
sveseli's avatar
sveseli committed
                    'fileModificationTime' : attr.st_mtime }
                fileInfo['fileModificationTimeStamp'] = TimeUtility.formatLocalTimeStamp(attr.st_mtime)
sveseli's avatar
sveseli committed
        return fileDict

#######################################################################
# Testing.

if __name__ == '__main__':
sveseli's avatar
sveseli committed
    sftpUtility = SftpUtility('xstor-devel', username='dmadmin')
    files = sftpUtility.getFiles('/data/testing/test1')
sveseli's avatar
sveseli committed
    print files
    files = sftpUtility.getFiles('/data/testing/test1', replacementDirPath='/xyz/ccc')
    print files