def connectionLost(self,reason=ConnectionDone): self.fileInst.close() if reason.type is not ConnectionDone: print "download:"+self.fname+" error",reason os.remove(self.tmpName) else: cacheFile = os.path.join(cacheDir,self.fname) changed = False if os.path.exists(cacheFile): if not crcCheck(self.tmpName,cacheFile): os.rename(cacheFile,os.path.join(cacheDir,"%s_%s"%(datetime.now().strftime('%Y%m%d-%H%M%S'),self.fname))) changed = True else: os.remove(self.tmpName) else: changed = True if changed: self.changeFlagList.append(changed) os.rename(self.tmpName, cacheFile) localDir = self.localInfo['dir'] checkDir(localDir) if self.localInfo.get('restartRename',False): shutil.copy(cacheFile,os.paht.join(localDir,'%s.new'%(cacheFile))) else: self._processFiles(cacheFile,localDir,self.localInfo) if self.psGroup: updateLog(self.psGroup,self.psName,self.fname) else: clientUpdateLog(self.fname)
def init(): global db dataDir = os.path.join(getDatarootDir(),DATA_DIR) checkDir(dataDir) db = adbapi.ConnectionPool("sqlite3", database=os.path.join(dataDir,"stdpm.db"),check_same_thread=False) def check(txn): res = txn.execute("SELECT * FROM sqlite_master WHERE type='table' AND name=?",['Process']).fetchone() if res is None: txn.execute('CREATE TABLE Process(clientIp VARCHAR(64),procGroup VARCHAR(255),procName VARCHAR(255),procInfo TEXT,PRIMARY KEY(clientIp,procGroup,procName))') else: def initDb(result): for res in result: ip = res[0] _checkIpDict(ip) _checkResourceDictName(uniqueProcName(ip,res[1],res[2]),) db.runQuery('SELECT clientIp,procGroup,procName FROM Process').addCallback(initDb) db.runInteraction(check).addCallback(lambda x:x)
def initFtpFactory(): ftpRoot = os.path.join(getDatarootDir(),'data','ftp','') ftpData = os.path.join(ftpRoot,'data') checkDir(ftpData) passFile = os.path.join(ftpRoot,"userpass.dat") if not os.path.exists(passFile) : with open(passFile,"w+") as f: f.write("user:trunksoft\n") with open(passFile) as f: for line in f: splits = line.split(":") if len(splits)>1: checkDir(os.path.join(ftpData,splits[0])) FTPFactory.allowAnonymous = False p = Portal(FTPRealm(ftpData,ftpData),[FilePasswordDB(passFile)]) f = FTPFactory(p) return f
from twisted.protocols.ftp import FTPClient,FTPFileListProtocol import os,shutil,fnmatch import zlib,json import tarfile,zipfile from datetime import datetime try: from cStringIO import StringIO except ImportError: from StringIO import StringIO from dp.common import getDatarootDir,checkDir,TIME_FORMAT from dp.client.process import restartProc,getLPConfig,updateLog,clientUpdateLog cacheDir = os.path.join(getDatarootDir(),'data','filecache','') checkDir(cacheDir) class BufferFileTransferProtocol(Protocol): def __init__(self,fname,localInfo,psGroup,psName,changeFlagList): self.fname=fname self.psGroup = psGroup self.psName = psName self.localInfo = localInfo self.tmpName=os.path.join(cacheDir,fname+'.tmp') self.fileInst=open(self.tmpName,'wb+') self.changeFlagList = changeFlagList def dataReceived(self,data): self.fileInst.write(data) def _processFiles(self,cacheFile,localDir,localInfo): extname = os.path.splitext(self.fname)[-1].lower() def extract(func):