def _initHelper(self): if(self._login_type == "FTP"): self._trans = FTPHelper( host=self._Host, user=self._User, pwd=self._Pwd); else : self._trans = SSHHelper( host=self._Host, user=self._User, pwd=self._Pwd); return;
class PPHelper: def __init__(self, Host, User, Pwd, login_type, params): self._Host = Host; self._User = User; self._Pwd = Pwd; self._login_type = login_type; self._params = params; self._trans = ""; self._insertCode = InsertCode(); self._fileHelper = FileHelper(); # init the helper. def _initHelper(self): if(self._login_type == "FTP"): self._trans = FTPHelper( host=self._Host, user=self._User, pwd=self._Pwd); else : self._trans = SSHHelper( host=self._Host, user=self._User, pwd=self._Pwd); return; # return the login result. # use the ftplib and paramiko. def _getLoginResult(self): if(self._login_type == "FTP"): self._tryLogin = ftplib.FTP(self._Host); tmp = self._tryLogin.login(self._User, self._Pwd); tmp = tmp.split(); return True if tmp[0] == '230' else False; else: self._tryLogin = paramiko.SSHClient(); self._tryLogin.set_missing_host_key_policy(paramiko.AutoAddPolicy()); tmp = self._tryLogin.connect(self._Host, 22, self._User, self._Pwd); return True if tmp==None else False; # close the login connect. def _tryLoginClose(self): if(self._login_type == "FTP"): self._tryLogin.quit(); else : self._tryLogin.close(); self._tryLogin = ""; return; #check whether the param is worked. def checkEnter(self): print "We are checking the param, please wait..."; logging.info("trying to login the server..."); # mark the succeed or not. flag = True; try : succInfo = "success to login the server."; failedInfo = "failed to login the server."; flag = self._getLoginResult(); if(flag == True): logging.info(succInfo); else : logging.info(failedInfo); self._tryLoginClose(); except Exception as e: errorInfo = "An exception occured when login the server."; logging.info(errorInfo); self._tryLoginClose(); flag = False; return flag; # the main function. Totally 3 sub functions: # 1.Get files from FTP server to local. # 2.Insert scripts to local files. # 3.Upload files from local to FTP server. def InsertToFiles(self, params, path): self._initHelper(); # create a local folder to storage the files. _foldername = self._User+(str)(uuid.uuid1()); localpath = self._fileHelper.createFolder(_foldername + '/' + path); logging.info("trying to get server files..."); # download the files. try : self._trans.getFiles(params, path, localpath); logging.info("succeed to download files."); except Exception as e: # log the error info. logging.info("failed to download files"); logging.info("error info:"+(str)(e)); self._fileHelper.deleteFolder(_foldername); return -1; localfiles = []; localfiles = self._fileHelper.GetFiles(localpath); logging.info("insert scripts to local files."); counts = self._insertCode.insertScripts(self._params, localpath, localfiles); logging.info("finished."); logging.info("trying to upload files..."); # only modified files will be uploaded. try: self._trans.putFiles(path, localpath, counts['files']); logging.info("succeed to upload files."); except Exception as e: # log the error info. logging.info("failed to upload files."); logging.info("error info:"+(str)(e)); self._fileHelper.deleteFolder(_foldername); return -2; # delete the local folder. self._fileHelper.deleteFolder(_foldername); return 0; # get files of the path. def getList(self, path): self._initHelper(); resList = self._trans.getList(path); return resList; # get the init ftp server path. def getInitPath(self): self._initHelper(); res = self._trans.getInitPath(); return res; # delete the object. def __del__(self): del self._trans; del self._insertCode; del self._fileHelper;