Skip to content
Snippets Groups Projects
Commit 68ab5154 authored by sveseli's avatar sveseli
Browse files

add ftp utility for monitoring file systems

parent 68e463b5
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
import copy
from ftplib import FTP
from dm.common.utility.loggingManager import LoggingManager
class FtpUtility:
def __init__(self, host, port, username=None, password=None):
self.host = host
self.port = port
self.username = username
self.password = password
self.ftpClient = None
@classmethod
def getFtpClient(cls, host, port, username=None, password=None):
ftp = FTP()
ftp.connect(host, port)
ftp.login(username, password)
return ftp
@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 __parseMlsdOutput(self, line):
# ['Type=dir', 'Modify=20151018024430', 'Size=4096', 'Perm=el', 'UNIX.mode=0775', 'UNIX.owner=sveseli', 'UNIX.uid=500', 'UNIX.group=sveseli', 'UNIX.gid=500', 'Unique=fd00-c2e3e', ' dir2\r']
parts = line.split(';')
parseDict = {}
self.__parseKeyValue(parts[0], parseDict)
self.__parseKeyValue(parts[1], parseDict)
name = parts[-1].strip()
parseDict['Name'] = name
type = parseDict.get('Type', '')
if type == 'dir' :
self.mlsdDirList.append(name)
elif type == 'file':
self.mlsdFileDict[name] = parseDict
def getFiles(self, dir, fileDict={}):
if not self.ftpClient:
self.ftpClient = self.getFtpClient(self.host, self.port, self.username, self.password)
# Need these to be class members for the callback function
self.mlsdFileDict = {}
self.mlsdDirList = []
self.ftpClient.retrlines('MLSD %s' % dir, self.__parseMlsdOutput)
for (fileName,fileInfo) in self.mlsdFileDict.items():
filePath = '%s/%s' % (dir, fileName)
fileDict[filePath] = fileInfo
for d in copy.copy(self.mlsdDirList):
dirPath = '%s/%s' % (dir,d)
self.getFiles(dirPath,fileDict)
return fileDict
#######################################################################
# Testing.
if __name__ == '__main__':
ftpUtility = FtpUtility('zagreb', 2811)
files=ftpUtility.getFiles('/tmp/test')
print files
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment