@@ -45,29 +45,37 @@ class FtpHandler(object):
4545 for authentication with the server.
4646 :type netrc_file: string
4747 """
48- def __init__ (self , server , username = '' , passwd = '' , netrc_file = '' , sftp = False ):
49- if sftp :
50- port = 22
51- else :
52- port = 21
48+ def __init__ (self , server , username = '' , passwd = '' , netrc_file = '' , port = 21 , sftp = False ):
49+ self .port = port
50+ self .sftp = sftp
5351 server = urlparse (server )
5452 if server .netloc :
5553 server = server .netloc
5654 elif server .path :
5755 server = server .path
58- self ._ftp = paramiko .Transport ((server , port ))
56+ if self .sftp :
57+ self ._ftp = paramiko .Transport ((server , self .port ))
58+ else :
59+ self ._ftp = FTP (server )
5960 self ._username = username
6061 self ._passwd = passwd
6162 if netrc_file :
6263 logininfo = netrc (netrc_file ).authenticators (server )
6364 self ._username , _ , self ._passwd = logininfo
6465 self .connect ()
65- self ._home = self ._sftp_client .getcwd ()
66+ if self .sftp :
67+ self ._home = self ._sftp_client .getcwd ()
68+ else :
69+ self ._home = self ._ftp .pwd ()
6670
6771 def connect (self ):
6872 """ Connects and logins to the server. """
69- self ._ftp .connect (username = self ._username , password = self ._passwd )
70- self ._sftp_client = paramiko .SFTPClient .from_transport (self ._ftp )
73+ if self .sftp :
74+ self ._ftp .connect (username = self ._username , password = self ._passwd )
75+ self ._sftp_client = paramiko .SFTPClient .from_transport (self ._ftp )
76+ else :
77+ self ._ftp .connect (port = self .port )
78+ self ._ftp .login (user = self ._username , passwd = self ._passwd )
7179
7280 def close (self ):
7381 """ Closes the connection to the server. """
@@ -104,14 +112,20 @@ def download(self, source_file, target_folder=''):
104112 working directory.
105113 :type target_folder: string
106114 """
107- current_folder = self ._sftp_client .getcwd ()
115+ if self .sftp :
116+ current_folder = self ._sftp_client .getcwd ()
117+ else :
118+ current_folder = self ._ftp .pwd ()
108119
109120 if not target_folder .startswith ('/' ): # relative path
110121 target_folder = join (getcwd (), target_folder )
111122
112123 folder = os .path .dirname (source_file )
113124 if folder :
114- self ._sftp_client .chdir (folder )
125+ if self .sftp :
126+ self ._sftp_client .chdir (folder )
127+ else :
128+ self .cd (folder )
115129
116130 if folder .startswith ("/" ):
117131 folder = folder [1 :]
@@ -124,15 +138,20 @@ def download(self, source_file, target_folder=''):
124138 source_file = os .path .basename (source_file )
125139 destination = join (destination_folder , source_file )
126140 try :
127- # with open(destination, 'wb') as result:
128- # self._ftp.retrbinary('RETR %s' % (source_file,),
129- # result.write)
130- self ._sftp_client .get (source_file , destination )
141+ if self .sftp :
142+ self ._sftp_client .get (source_file , destination )
143+ else :
144+ with open (destination , 'wb' ) as result :
145+ self ._ftp .retrbinary ('RETR %s' % (source_file ,),
146+ result .write )
131147 except error_perm as e : # source_file is a folder
132148 print (e )
133149 remove (join (target_folder , source_file ))
134150 raise
135- self ._sftp_client .chdir (current_folder )
151+ if self .sftp :
152+ self ._sftp_client .chdir (current_folder )
153+ else :
154+ self ._ftp .cwd (current_folder )
136155
137156 def cd (self , folder ):
138157 """ Changes the working directory on the server.
@@ -141,13 +160,19 @@ def cd(self, folder):
141160 :type folder: string
142161 """
143162 if folder .startswith ('/' ):
144- self ._sftp_client .chdir (folder )
163+ if self .sftp :
164+ self ._sftp_client .chdir (folder )
165+ else :
166+ self ._ftp .cwd (folder )
145167 else :
146168 for subfolder in folder .split ('/' ):
147169 if subfolder :
148- self ._sftp_client .chdir (subfolder )
170+ if self .sftp :
171+ self ._sftp_client .chdir (subfolder )
172+ else :
173+ self ._ftp .cwd (subfolder )
149174
150- def ls (self , folder = '. ' ):
175+ def ls (self , folder = '' ):
151176 """ Lists the files and folders of a specific directory
152177 default is the current working directory.
153178
@@ -157,16 +182,27 @@ def ls(self, folder='.'):
157182 :returns: a tuple with the list of files in the folder
158183 and the list of subfolders in the folder.
159184 """
160- current_folder = self ._sftp_client .getcwd ()
161- self ._sftp_client .chdir (folder )
185+ if self .sftp and folder == '' :
186+ folder = '.'
187+
162188 files = []
163189 folders = []
164190 contents = []
165- contents = self ._sftp_client .listdir ()
166191
167- files = filter (lambda a : str (self ._sftp_client .lstat (a )).split ()[0 ].startswith ('-' ), contents )
168- folders = filter (lambda a : str (self ._sftp_client .lstat (a )).split ()[0 ].startswith ('d' ), contents )
169- self ._sftp_client .chdir (current_folder )
192+ if self .sftp :
193+ current_folder = self ._sftp_client .getcwd ()
194+ self ._sftp_client .chdir (folder )
195+ contents = self ._sftp_client .listdir ()
196+ files = filter (lambda a : str (self ._sftp_client .lstat (a )).split ()[0 ].startswith ('-' ), contents )
197+ folders = filter (lambda a : str (self ._sftp_client .lstat (a )).split ()[0 ].startswith ('d' ), contents )
198+ self ._sftp_client .chdir (current_folder )
199+ else :
200+ current_folder = self ._ftp .pwd ()
201+ self .cd (folder )
202+ self ._ftp .retrlines ('LIST' , lambda a : contents .append (a ))
203+ files = filter (lambda a : a .split ()[0 ].startswith ('-' ), contents )
204+ folders = filter (lambda a : a .split ()[0 ].startswith ('d' ), contents )
205+ self ._ftp .cwd (current_folder )
170206 return files , folders
171207
172208 def dir (self , folder = '' , prefix = '' ):
@@ -200,15 +236,22 @@ def mkdir(self, folder):
200236 :param folder: the folder to be created.
201237 :type folder: string
202238 """
203- current_folder = self ._sftp_client .getcwd ()
239+
240+ if self .sftp :
241+ current_folder = self ._sftp_client .getcwd ()
242+ else :
243+ current_folder = self ._ftp .pwd ()
204244 #creates the necessary folders on
205245 #the server if they don't exist
206246 folders = folder .split ('/' )
207247 for fld in folders :
208248 try :
209249 self .cd (fld )
210250 except error_perm : # folder does not exist
211- self ._sftp_client .mkdir (fld )
251+ if self .sftp :
252+ self ._sftp_client .mkdir (fld )
253+ else :
254+ self ._ftp .mkd (fld )
212255 self .cd (fld )
213256 self .cd (current_folder )
214257
@@ -219,11 +262,17 @@ def rm(self, filename):
219262 :type filename: string
220263 """
221264 try :
222- self ._sftp_client .remove (filename )
265+ if self .sftp :
266+ self ._sftp_client .remove (filename )
267+ else :
268+ self ._ftp .delete (filename )
223269 except error_perm : # target is either a directory
224270 # either it does not exist
225271 try :
226- current_folder = self ._sftp_client .getcwd ()
272+ if self .sftp :
273+ current_folder = self ._sftp_client .getcwd ()
274+ else :
275+ current_folder = self ._ftp .pwd ()
227276 self .cd (filename )
228277 except error_perm :
229278 print ('550 Delete operation failed %s '
@@ -240,7 +289,11 @@ def rmdir(self, foldername):
240289 :param foldername: the folder to be deleted.
241290 :type foldername: string
242291 """
243- current_folder = self ._sftp_client .getcwd ()
292+ if self .sftp :
293+ current_folder = self ._sftp_client .getcwd ()
294+ else :
295+ current_folder = self ._ftp .pwd ()
296+
244297 try :
245298 self .cd (foldername )
246299 except error_perm :
@@ -249,16 +302,25 @@ def rmdir(self, foldername):
249302 else :
250303 self .cd (current_folder )
251304 try :
252- self ._sftp_client .rmdir (foldername )
305+ if self .sftp :
306+ self ._sftp_client .rmdir (foldername )
307+ else :
308+ self ._ftp .rmd (foldername )
253309 except error_perm : # folder not empty
254310 self .cd (foldername )
255311 contents = self .ls ()
256312 #delete the files
257- map (self ._sftp_client .remove , contents [0 ])
313+ if self .sftp :
314+ map (self ._sftp_client .remove , contents [0 ])
315+ else :
316+ map (self ._ftp .delete , contents [0 ])
258317 #delete the subfolders
259318 map (self .rmdir , contents [1 ])
260319 self .cd (current_folder )
261- self ._sftp_client .rmdir (foldername )
320+ if self .sftp :
321+ self ._sftp_client .rmdir (foldername )
322+ else :
323+ self ._ftp .rmd (foldername )
262324
263325 def get_filesize (self , filename ):
264326 """ Returns the filesize of a file
@@ -268,14 +330,23 @@ def get_filesize(self, filename):
268330
269331 :returns: string representation of the filesize.
270332 """
271- return self ._sftp_client .lstat (filename ).st_size
333+ if self .sftp :
334+ return self ._sftp_client .lstat (filename ).st_size
335+ else :
336+ result = []
337+ def dir_callback (val ):
338+ result .append (val .split ()[4 ])
339+ self ._ftp .dir (filename , dir_callback )
340+ return result [0 ]
272341
273342 def get_datestamp (self , filename ):
274- # datestamp = self._ftp.sendcmd('MDTM ' + filename)
275- # datestamp = datetime.strptime(datestamp[4:],
276- # "%Y%m%d%H%M%S").strftime("%Y-%M-%d")
277- datestamp = datetime .fromtimestamp (self ._sftp_client .lstat (filename ).st_mtime )
278- return datestamp .strftime ("%Y-%m-%d" )
343+ if self .sftp :
344+ datestamp = datetime .fromtimestamp (self ._sftp_client .lstat (filename ).st_mtime )
345+ return datestamp .strftime ("%Y-%m-%d" )
346+ else :
347+ datestamp = self ._ftp .sendcmd ('MDTM ' + filename )
348+ return datetime .strptime (datestamp [4 :],
349+ "%Y%m%d%H%M%S" ).strftime ("%Y-%M-%d" )
279350
280351 def check_pkgs_integrity (self , filelist , logger ,
281352 timeout = 120 , sleep_time = 10 ):
@@ -327,8 +398,17 @@ def upload(self, filename, location=''):
327398 be stored.
328399 :type location: string
329400 """
330- current_folder = self ._sftp_client .getcwd ()
401+ if self .sftp :
402+ current_folder = self ._sftp_client .getcwd ()
403+ else :
404+ current_folder = self ._ftp .pwd ()
331405 self .mkdir (location )
332406 self .cd (location )
333- self ._sftp_client .put (filename , location )
407+ if self .sftp :
408+ self ._sftp_client .put (filename , location )
409+ else :
410+ fl = open (filename , 'rb' )
411+ filename = filename .split ('/' )[- 1 ]
412+ self ._ftp .storbinary ('STOR %s' % filename , fl )
413+ fl .close ()
334414 self .cd (current_folder )
0 commit comments