Skip to content
Snippets Groups Projects
Forked from DM / dm-docs
261 commits behind, 807 commits ahead of the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
sftpFileSystemObserverAgent.py 1.62 KiB
#!/usr/bin/env python

from threading import Timer
from pollingFileSystemObserverAgent import PollingFileSystemObserverAgent
from dm.common.utility.sftpUtility import SftpUtility

class SftpFileSystemObserverAgent(PollingFileSystemObserverAgent):

    DEFAULT_POLLING_PERIOD_IN_SECONDS = 15
    DEFAULT_PORT = 22

    def __init__(self, host, port=DEFAULT_PORT, username=None, password=None, privateKey=None, pollingPeriod=DEFAULT_POLLING_PERIOD_IN_SECONDS):
        PollingFileSystemObserverAgent.__init__(self, pollingPeriod)
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.privateKey = privateKey

    def getFiles(self, dataDirectory):
        (scheme, host, port, dirPath) = SftpUtility.parseFtpUrl(dataDirectory, defaultHost=self.host, defaultPort=self.port)
        self.logger.debug('Retrieving files from SFTP host: %s, port: %s, directory path: %s' % (host, port, dirPath))
        sftpUtility = SftpUtility(host, port, self.username, self.password, self.privateKey)
        return sftpUtility.getFiles(dirPath, {})

####################################################################
# Testing

if __name__ == '__main__':
    import time
    dirPath='/export/beams12/S1IDUSER/mnt/orthros/park_apr16_rec_reduced'
    agent = SftpFileSystemObserverAgent('s1dserv', privateKey='/home/beams/DMADMIN/.ssh/id_dsa')
    print 'TIME1: ', time.time()
    print 'ORIGINAL FILES: ', len(agent.getFiles(dirPath))
    print 'TIME2: ', time.time()
    #agent.startObservingPath('/export/dm/test', 'e1')
    #time.sleep(100)
    #agent.stopObservingPath('/export/dm/test', 'e1')