Package install :: Package MoSTBioDat :: Package Transfer :: Module FTPtools
[hide private]
[frames] | no frames]

Source Code for Module install.MoSTBioDat.Transfer.FTPtools

  1  #!/usr/bin/env python 
  2  ################################ 
  3  # FTPtools.py:                 # 
  4  # File Transfer Protocol tools # 
  5  ################################ 
  6   
  7  ###################################################### 
  8  # Copyright (c) 2007-2008 Andrzej Bak                # 
  9  # ARC Seibersdorf & University of Silesia            # 
 10  # Author: Andrzej Bak <Andrzej.Bak@us.edu.pl>        # 
 11  # License: GNU General Public License, version: 3    # 
 12  # URL: http://chemoinformatyka.us.edu.pl/mostbiodat/ # 
 13  # Version: 1, 06.01.2010                             # 
 14  ###################################################### 
 15   
 16  try: 
 17      import sys 
 18      import ftplib 
 19      import os 
 20      from fileFTPtools import fileFtpTools,fileFilter,fileInfo 
 21      from MoSTBioDat.Log.MoSTBioDatLog import  MoSTBioDatLog,getCurrentUser,filterKeys 
 22  except ImportError,error: 
 23      print 'Missing modules ...',error 
 24      sys.exit(1) 
 25   
 26  ################### FTPtools Class ################### 
27 -class FTPtools(object):
28 """ 29 methods to connect and transfer files 30 INPUT: 31 rserver - str, servername 32 rdir - str, remote directory 33 ruser - str, remote user, default Anonymous 34 rpasswd - str, remote password 35 kwargs: 36 format - string format for log handler 37 filter - filter object from logger object 38 datefmt - data/time format 39 path - directory path to log file 40 filename - log file name, default log 41 filemode - mode to open log file, default='a' 42 level - set root logger level to specified level 43 logfilelevel- set level to log file 44 OUTPUT: 45 class object 46 """
47 - def __init__(self,rserver=None,ruser='Anonymous',rpasswd='',rdir='.',port=21,*args,**kwargs):
48 self.nonpassive=False 49 if not rserver: 50 print 'Missing remote server!' 51 sys.exit(1) 52 self.servername=rserver 53 self.remotedir=rdir 54 self.remoteuser=ruser 55 if ruser=='': 56 self.remoteuser=getCurrentUser() 57 self.remotepasswd=rpasswd 58 self.port=21 59 self.connection=None 60 self.kwargs={} 61 self.kwargs=kwargs 62 keys=("rserver","ruser","rpasswd","rdir","port", 63 "format","filter","datefmt","path", 64 "filename","filemode","level","logfilelevel") 65 filter(lambda key: filterKeys(kwargs,keys,key),kwargs.keys()) 66 try: 67 self.logobj=MoSTBioDatLog(**kwargs)#create logging object 68 self.log=self.logobj.getLogHandler()#create logging handler 69 except IOError,e: 70 print 'Error: %s, %s' %(e[0],e[1]) 71 sys.exit(1)
72
73 - def isFtp(self):
74 """ 75 check whether FTP server exists 76 INPUT: 77 clas object 78 OUTPUT: 79 boolen 80 """ 81 try: 82 ftplib.FTP(self.servername).quit() # close FTP connection 83 self.log.info('Check if FTP server exists') 84 except ftplib.all_errors,error: 85 print 'Error: %s, %s ' %(error[0], error[1]) 86 self.log.exception('Ftplib error: %s',error) 87 return False 88 return True
89
90 - def isEmpty(self,path='.'):
91 """ 92 check whether directory accessible for dispalying 93 INPUT: 94 class object 95 path - str, path on remote machine, default current 96 OUTPUT: 97 boolean 98 """ 99 if self.connection is None: 100 print 'Error: Connect to server!' 101 self.log.info('Connect to server') 102 sys.exit(1) 103 return self.connection.nlst(path)==[]
104
105 - def connectFtp(self):
106 """ 107 connect to FTP server 108 INPUT: 109 class object 110 OUTPUT: 111 connection object 112 """ 113 try: 114 print 'Connecting to %s ...' %(self.servername) 115 self.connection=ftplib.FTP() 116 self.connection.connect(self.servername,self.port) #connect to server on given port 117 print 'Connection succeeded.' 118 self.log.info('Start FTP connection to %s',self.servername) 119 except ftplib.all_errors,error: 120 print 'Connection failed' 121 print "Error: %s, %s" %(error[0],error[1]) 122 self.log.exception('Ftplib error: %s',error) 123 sys.exit(1) 124 else: 125 print self.connection.welcome # welcome response from FTP server 126 127 try: 128 self.connection.login(self.remoteuser,self.remotepasswd) # login in FTP server 129 print 'Authorization succeeded.' 130 print 'Logging in as %s.' %self.remoteuser 131 self.log.info('Logging as %s',self.remoteuser) 132 self.connection.cwd(self.remotedir) # get current working directory 133 self.log.info('CWD: %s',self.remotedir) 134 if self.nonpassive: 135 self.connection.set_pasv(False) #set passive mode on off 136 except ftplib.all_errors,error: 137 print 'Authorization failed.' 138 self.log.exception('Ftplib error: %s',error) 139 print 'Connection closed!' 140 self.connection.close() 141 sys.exit(1)
142
143 - def lsFtp(self,path='.',flag=True):
144 """ 145 list files on FTP server 146 INPUT: 147 class object 148 path - str, path on remote machine, default current 149 flag - boolean: default True - list files 150 OUTPUT: 151 list of class object 152 """ 153 listing=[] 154 if not self.isEmpty(path): 155 try: 156 self.connection.dir(path, listing.append) # list of files in given path 157 except ftplib.all_errors,error: 158 print 'Error: %s' %error 159 self.log.exception('Ftplib error: %s',error) 160 listing=filter(lambda line: fileFilter(line),listing) #remove ., .. and hidden files 161 return map(lambda line: fileInfo(path,line,flag), listing) 162 ### list of fileInfo objects 163 else: 164 print 'Access denied for %s' %path 165 self.log.info('Access denied for %s',path) 166 return listing
167
168 - def pwdFtp(self,flag=True):
169 """ 170 print working directory on FTP server 171 INPUT: 172 class object 173 flag - boolean: default True - print current working directory 174 """ 175 if self.connection is None: 176 print 'Error: Connect to server!' 177 self.log.info('Connect to server') 178 sys.exit(1) 179 if flag: 180 print 'Current directory: %s' %self.connection.pwd() 181 return self.connection.pwd() # current working directory of ftp session
182
183 - def cdFtp(self,path='.'):
184 """ 185 change directory on FTP server 186 INPUT: 187 class object 188 path - str, path to be changed on remote machine - default '.' 189 OUTPUT 190 boolean: True - enable to change directory 191 """ 192 if self.connection is None: 193 print 'Error: Connect to server!' 194 self.log.info('Connect to server') 195 sys.exit(1) 196 try: 197 self.connection.cwd(path) # change directory of ftp session 198 self.log.info('CWD: %s',path) 199 except ftplib.all_errors,error: 200 print 'Error: %s' %error 201 self.log.exception('Ftplib error: %s',error) 202 return False 203 else: 204 return True
205
206 - def getFtp(self,filelist=[],localpath='',binary=True):
207 """ 208 get specified files from current working directory to local machine 209 INPUT: 210 class object 211 filelist - list of files to be downloaded [str] - default [] 212 localpath - str, path on the local machine, default empty 213 binary - boolean: default True - binary transfer 214 """ 215 if localpath=='': 216 localpath=os.path.abspath(os.curdir) # get full path to current directory 217 if len(filelist)==0: 218 print 'Specify filenames ...' 219 else: 220 robjlist=self.lsFtp(self.pwdFtp(False),False) # list of file object on remote machine 221 listFtpfile=filter(lambda fileFtpInfo: fileFtpTools(fileFtpInfo,filelist).files2download(),robjlist) 222 ### get only object files specified in filelist 223 if len(listFtpfile)==0: 224 print 'Sorry,no files to download ...' 225 else: 226 filter(lambda fileFtpInfo: fileFtpTools(fileFtpInfo,filelist).transferFtpFile(self.connection,localpath,self.log),listFtpfile) #transfer specified files to local machine
227 ### transfer specified object files 228
229 - def mgetFtp(self,localpath='',binary=True):
230 """ 231 get all files from current working directory to specified path on local machine 232 INPUT: 233 class object 234 localpath - str, path on the local machine, default current working directory, 235 binary - boolean: default True - binary transfer 236 """ 237 if localpath=='': 238 localpath=os.path.abspath(os.curdir) # get full path to current directory 239 listFtpfile=self.lsFtp(self.pwdFtp(),False) # list of file object on remote machine 240 if len(listFtpfile)==0: 241 print 'Sorry,no files to download ...' 242 else: 243 filter(lambda fileFtpInfo: fileFtpTools(fileFtpInfo).transferFtpFile(self.connection,localpath,self.log),listFtpfile)#transfer all files from directory to local machine
244 ### transfer specified object files 245
246 - def byeFtp(self):
247 """ 248 close ftp connection 249 """ 250 if self.connection is None: 251 print 'Error: Connect to server!' 252 self.log.info('Connect to server') 253 sys.exit(1) 254 try: 255 self.connection.close() # close connection 256 print 'Connection is closed.' 257 self.log.info('Connection closed') 258 except ftplib.all_errors,error: 259 self.log.exception('Ftplib error: %s',error) 260 print 'Error: %s' %error
261 262 ################ End of the FTPtools class ######################### 263 ################################ MAIN ############################## 264 ############ Example of usage ##################################### 265 if __name__=='__main__': 266 pass 267 #userpasswd=getpass('Password:') 268 # A=FTPtools(rserver='',ruser='',rpasswd='',path='/tmp/Log',filename='ftp') 269 # A.isFtp() 270 # A.connectFtp() 271 # A.cdFtp('.') 272 # A.lsFtp('.') 273 # A.getFtp(['XRF_copy','PP.doc'],'/home/baka/copy') 274 # A.mgetFtp('/home/baka/copy1') 275 # A.byeFtp() 276 ##################################################################### 277