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

Source Code for Module install.MoSTBioDat.Transfer.fileSFTPtools

  1  #!/usr/bin/env python 
  2  ############################################################## 
  3  # fileSFTPtools.py:                                          # 
  4  # Supportive classes and functions for the Secure FTP module # 
  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 os 
 19      import stat 
 20      from getpass import getpass, getuser 
 21      import time 
 22      import paramiko 
 23      import MoSTBioDat.Transfer.fileFTPtools as fileFTPtools 
 24  except ImportError,error: 
 25      print 'Missing modules ...',error 
 26      sys.exit(1) 
 27  ############ Exception class ######################## 
28 -class Error(Exception):
29 - def __init__(self,error=""):
30 self.error=error
31 - def __str__(self):
32 return self.error
33
34 -class ServerNameError(Error):
35 - def __init__(self,error="",username=""):
36 Error.__init__(self,error) 37 self.username=username
38
39 - def __str__(self):
40 return "%s: %s" %(self.error,self.username)
41
42 -class TransportError(Error):
43 - def __init__(self,error="",servername="",port=""):
44 Error.__init__(self,error) 45 self.servername=servername 46 self.port=port
47
48 - def __str__(self):
49 return "%s: %s: %s" %(self.error,self.servername,self.port)
50
51 -class AuthenticationError(Error):
52 - def __init__(self,error="",username=""):
53 Error.__init__(self,error) 54 self.username=username
55
56 - def __str__(self):
57 return "%s: %s" %(self.error,self.username)
58 59 ############ The end of the Exception class ######### 60 ############## LocalHostKey class ###################
61 -class LocalHostKeys(object):
62 - def __init__(self,lhostkeyspath1='~/.ssh/known_hosts',lhostkeyspath2='~/ssh/known_hosts'):
63 self.lhostkeyspath1=lhostkeyspath1 64 self.lhostkeyspath2=lhostkeyspath2
65
66 - def getLocalHostKeys(self):
67 """ 68 get local host keys 69 INPUT: 70 class object 71 local host keys path - str, default='~/.ssh/known_hosts' 72 local host keys path - str, default='/ssh/known_hosts' 73 OUTPUT: 74 local host keys dictionary 75 """ 76 try: 77 self.lhostkeys=paramiko.util.load_host_keys(os.path.expanduser(self.lhostkeyspath1)) 78 ### load file of known SSH host keys in given location 79 except IOError,e: 80 print 'Error: %s, %s in %s' %(e[0],e[1],self.lhostkeyspath1) 81 try: 82 self.lhostkeys=paramiko.util.load_host_keys(os.path.expanduser(self.lhostkeyspath2)) 83 ### load file of known SSH host keys in given location 84 except IOError: 85 print 'Error: %s, %s in %s' %(e[0],e[1],self.lhostkeyspath2) 86 self.lhostkeys={} 87 return self.lhostkeys
88 ############# The end of the LocalHostKey class ######## 89 ########## Supportive function for lsSFtp method ######
90 -def fileFilter(fileobj):
91 """ 92 remove None object 93 INPUT: 94 file class object 95 OUTPUT: 96 boolean 97 """ 98 if fileobj==None: 99 return False 100 else: 101 return True
102 103 ####### Supportive function for lsSFtp method ####################
104 -def fileInfo(cwd,fileobj,filename,flag):
105 """ 106 detailed file description parser 107 INPUT: 108 ftp file's description: 109 file class object 110 cwd 111 filename 112 flag 113 114 OUTPUT: 115 file class object 116 """ 117 if filename.startswith('.'): #remove hidden files 118 return 119 mode=fileobj.st_mode 120 fullmode=fileFTPtools.parseFtpPerm('').num2fullstr(mode) # change numeric mode to full string 121 links=0 122 isdir, islink=fileFTPtools.parseFtpPerm(fullmode).str2perm() 123 owner=fileobj.st_uid 124 group=fileobj.st_gid 125 size=fileobj.st_size 126 mode=fileobj.st_mode 127 date=fileobj.st_mtime 128 datestr=fileFTPtools.parseFtpDate('').displayDate(date) # change date to string format 129 if flag: 130 print fileobj 131 fileobj=fileFTPtools.fileFtpInfo(cwd,filename,fullmode,isdir,islink,mode,links,owner,group,int(size),datestr,date,line='') 132 # create fielFtpInfo file object 133 return fileobj
134 #################### fileSFtpTools class ###########################
135 -class fileSFtpTools(fileFTPtools.fileFtpTools):
136 """ 137 methods operating on file object 138 BASE CLASS: fileFTPtools.fileFtpTools 139 INPUT: 140 fileFtpInfo - file class object 141 listfile - list, list of files to download - [str] 142 """
143 - def __init__(self,fileFtpInfo,listfile=[]):
144 self.fileFtpInfo=fileFtpInfo #class object 145 self.listfile=listfile #list of files to download
146
147 - def transferFtpFile(self,sftpobj,path,log,binary=True,callback=None):
148 """ 149 transfer file from remote directory to local machine 150 INPUT: 151 sftp - class object 152 path - str, path on local machine to download files - str 153 binary - boolean, default True 154 callback - default None 155 """ 156 if self.fileFtpInfo.isdir or self.fileFtpInfo.islink: 157 print '%s is not a regular file' %self.fileFtpInfo.filefullname 158 return 159 status=self.downloadFile(path) # check file status in given path 160 if status==fileSFtpTools.notDownload: 161 print 'The exact copy of the %s exists in %s ' %(self.fileFtpInfo.filename,path) 162 log.info('The exact copy of the %s exists in %s',self.fileFtpInfo.filename,path) 163 return 164 165 if not callback: 166 filecopy=self.makeLocalFile(path,binary,status==fileSFtpTools.partialDownload) 167 ### make local copy of file in given path 168 if filecopy: 169 callback=filecopy.write 170 try: 171 print 'Downloading %s ...' %self.fileFtpInfo.filename 172 filename=os.path.join(path,self.fileFtpInfo.filename) # full path to file 173 sftpobj.get(self.fileFtpInfo.filename,filename) # copy file from remote machine to local 174 except paramiko.SSHException, e: 175 print "Error %s:" %e 176 self.log.exception('SSH error: %s',e) 177 self.byeSFtp() 178 sys.exit(1) 179 except IOError,e: 180 print "Error: %s, %s or directory" %(e[0],e[1]) 181 self.log.exception('IO error: %s',e) 182 self.byeSFtp() 183 sys.exit(1) 184 except Exception,e: 185 print 'Error: %s' %e 186 self.log.exception('Error: %s',e) 187 sys.exit(1) 188 else: 189 if filecopy: 190 filecopy.close() 191 print 'Download done.' 192 log.info('Downloaded %s to %s',self.fileFtpInfo.filename,path) 193 os.utime(filename,(self.fileFtpInfo.date,self.fileFtpInfo.date)) #set time for copied file 194 os.chmod(filename,self.fileFtpInfo.mode) # set permission for copied file
195 196 ############# the end of the fileSFTPTools class################ 197 ################## MAIN ####################################### 198 if __name__=='__main__': 199 print 200