From 5471103c054fa6262b143dbe9b9c35512bffddc5 Mon Sep 17 00:00:00 2001 From: Sinisa Veseli <sveseli@aps.anl.gov> Date: Tue, 26 Jan 2016 18:28:25 +0000 Subject: [PATCH] add checksum and stat functionality --- src/python/dm/common/utility/ftpUtility.py | 43 +++++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/python/dm/common/utility/ftpUtility.py b/src/python/dm/common/utility/ftpUtility.py index 8cd2be4b..03a94703 100755 --- a/src/python/dm/common/utility/ftpUtility.py +++ b/src/python/dm/common/utility/ftpUtility.py @@ -1,7 +1,10 @@ #!/usr/bin/env python import copy +import os +import time from ftplib import FTP +from dm.common.utility.timeUtility import TimeUtility from dm.common.utility.loggingManager import LoggingManager import urlparse @@ -13,6 +16,7 @@ class FtpUtility: self.username = username self.password = password self.ftpClient = None + self.mlsdFileStatDict = {} @classmethod def parseFtpUrl(cls, url, defaultHost=None, defaultPort=None): @@ -64,6 +68,16 @@ class FtpUtility: elif type == 'file': self.mlsdFileDict[name] = parseDict + def __parseMlsdFileStat(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) + self.__parseKeyValue(parts[2], parseDict) + name = parts[-1].strip() + self.mlsdFileStatDict[name] = parseDict + def getFiles(self, dir, fileDict={}): if not self.ftpClient: self.ftpClient = self.getFtpClient(self.host, self.port, self.username, self.password) @@ -79,10 +93,35 @@ class FtpUtility: self.getFiles(dirPath,fileDict) return fileDict + def getMd5Sum(self, filePath, fileInfo={}): + if not self.ftpClient: + self.ftpClient = self.getFtpClient(self.host, self.port, self.username, self.password) + md5Sum = self.ftpClient.sendcmd('CKSM MD5 0 -1 %s' % filePath).split()[-1] + fileInfo['md5Sum'] = md5Sum + return md5Sum + + def statFile(self, filePath, fileInfo={}): + fileName = os.path.basename(filePath) + if not self.ftpClient: + self.ftpClient = self.getFtpClient(self.host, self.port, self.username, self.password) + # Need this to be class members for the callback function + self.ftpClient.retrlines('MLSD %s' % filePath, self.__parseMlsdFileStat) + fileStatDict = self.mlsdFileStatDict.get(fileName) + if fileStatDict: + fileInfo['fileSize'] = fileStatDict.get('Size') + modifyTime = fileStatDict.get('Modify') + modifyTime = time.mktime(time.strptime(modifyTime, '%Y%m%d%H%M%S')) + fileInfo['fileModificationTime'] = modifyTime + fileInfo['fileModificationTimeStamp'] = TimeUtility.formatLocalTimeStamp(modifyTime) + del self.mlsdFileStatDict[fileName] + return fileInfo + ####################################################################### # Testing. if __name__ == '__main__': - ftpUtility = FtpUtility('dmstorage', 2811) - files=ftpUtility.getFiles('/opt/DM/data/ESAF/e1') + ftpUtility = FtpUtility('s8dserv', 2811) + files = ftpUtility.getFiles('/export/8-id-i/test') print files + print ftpUtility.getMd5Sum('/export/8-id-i/test/testfile01') + print ftpUtility.statFile('/export/8-id-i/test/testfile01') -- GitLab