cis: logger created; start_downloads done
[living-lab-site.git] / cis / api / file_transfer.py
index 5a6ff01..2f33eac 100644 (file)
@@ -7,7 +7,7 @@ They may extend BaseFileTransferer class.
 """
 
 import sys
-from ftplib import FTP_TLS
+import ftplib
 import base
 import ftp_config
 import socket
@@ -25,49 +25,66 @@ class FTPFileTransferer(base.BaseFileTransferer):
     def __init__(self, local_path='', remote_path=''):
         base.BaseFileTransferer.__init__(self, local_path, remote_path)
 
-        self.ftp = FTP_TLS(ftp_config.FTP_HOST, ftp_config.FTP_USER,
+        self.ftp = ftplib.FTP_TLS(ftp_config.FTP_HOST, ftp_config.FTP_USER,
                 ftp_config.FTP_PASSWD, ftp_config.FTP_ACCT)
         self.ftp.set_pasv(True)
 
     def get(self, files):
-        for crt_file in files:
-            crt_file = os.path.join(self.local_path, crt_file)
+        try:
+            self.ftp.cwd(self.remote_path)
+        except ftplib.error_perm as e:
+            raise api_exceptions.FileTransferException( \
+                    "Could not change remote directory '%s': %s" \
+                    % (self.remote_path, repr(e)))
+
+
+        for crt_fn in files:
+            local_fn = os.path.join(self.local_path, crt_fn)
+            remote_fn = os.path.join(self.remote_path, crt_fn)
             try:
-                file_local = open(crt_file, 'wb')
+                file_local = open(local_fn, 'wb')
             except IOError as e:
                 raise api_exceptions.FileTransferException( \
                         "Could not open local file '%s' for writing: %s" \
-                        % (crt_file, repr(e)))
+                        % (local_fn, repr(e)))
 
             try:
-                self.ftp.cwd(self.remote_path)
-                self.ftp.retrbinary('RETR %s' % crt_file, file_local.write)
+                self.ftp.retrbinary('RETR %s' % crt_fn, file_local.write)
                 file_local.close()
             except ftplib.error_perm as e:
                 raise api_exceptions.FileTransferException( \
                         "Could not get file '%s' from Web Server: %s" \
-                        % (crt_file, repr(e)))
+                        % (remote_fn, repr(e)))
 
     def put(self, files):
-        for crt_file in files:
-            crt_file = os.path.join(self.local_path, crt_file)
+        try:
+            self.ftp.cwd(self.remote_path)
+        except ftplib.error_perm as e:
+            raise api_exceptions.FileTransferException( \
+                    "Could not change remote directory '%s': %s" \
+                    % (self.remote_path, repr(e)))
+
+        for crt_fn in files:
+            local_fn = os.path.join(self.local_path, crt_fn)
 
             try:
-                file_local = open(crt_file, 'rb')
+                file_local = open(local_fn, 'rb')
             except IOError as e:
                 raise api_exceptions.FileTransferException( \
                         "Could not open local file '%s' for reading: %s" \
-                        % (crt_file, repr(e)))
+                        % (local_fn, repr(e)))
                 
             try:
-                self.ftp.cwd(self.remote_path)
-                self.ftp.storbinary('STOR %s' % crt_file, file_local)
+                self.ftp.storbinary('STOR %s' % crt_fn, file_local)
                 file_local.close()
             except ftplib.error_perm as e:
                 raise api_exceptions.FileTransferException( \
-                        "Could not get file '%s' from Web Server: %s" \
-                        % (crt_file, repr(e)))
+                        "Could not put file '%s' to Web Server: %s" \
+                        % (local_fn, repr(e)))
 
     def close(self):
         if self.ftp is not None:
-            self.ftp.quit()
+            try:
+                self.ftp.quit()
+            except:
+                pass