cis: logger created; start_downloads done
[living-lab-site.git] / cis / api / file_transfer.py
1 #!/usr/bin/env python
2
3 """
4 Classes that facilitate file transfer (between Web Server and CIS).
5
6 They may extend BaseFileTransferer class.
7 """
8
9 import sys
10 import ftplib
11 import base
12 import ftp_config
13 import socket
14 import api_exceptions
15 import os
16
17
18 class FTPFileTransferer(base.BaseFileTransferer):
19     """
20     FTPS implementation for file transfering between Web Server and CIS.
21     """
22
23     ftp = None
24
25     def __init__(self, local_path='', remote_path=''):
26         base.BaseFileTransferer.__init__(self, local_path, remote_path)
27
28         self.ftp = ftplib.FTP_TLS(ftp_config.FTP_HOST, ftp_config.FTP_USER,
29                 ftp_config.FTP_PASSWD, ftp_config.FTP_ACCT)
30         self.ftp.set_pasv(True)
31
32     def get(self, files):
33         try:
34             self.ftp.cwd(self.remote_path)
35         except ftplib.error_perm as e:
36             raise api_exceptions.FileTransferException( \
37                     "Could not change remote directory '%s': %s" \
38                     % (self.remote_path, repr(e)))
39
40
41         for crt_fn in files:
42             local_fn = os.path.join(self.local_path, crt_fn)
43             remote_fn = os.path.join(self.remote_path, crt_fn)
44             try:
45                 file_local = open(local_fn, 'wb')
46             except IOError as e:
47                 raise api_exceptions.FileTransferException( \
48                         "Could not open local file '%s' for writing: %s" \
49                         % (local_fn, repr(e)))
50
51             try:
52                 self.ftp.retrbinary('RETR %s' % crt_fn, file_local.write)
53                 file_local.close()
54             except ftplib.error_perm as e:
55                 raise api_exceptions.FileTransferException( \
56                         "Could not get file '%s' from Web Server: %s" \
57                         % (remote_fn, repr(e)))
58
59     def put(self, files):
60         try:
61             self.ftp.cwd(self.remote_path)
62         except ftplib.error_perm as e:
63             raise api_exceptions.FileTransferException( \
64                     "Could not change remote directory '%s': %s" \
65                     % (self.remote_path, repr(e)))
66
67         for crt_fn in files:
68             local_fn = os.path.join(self.local_path, crt_fn)
69
70             try:
71                 file_local = open(local_fn, 'rb')
72             except IOError as e:
73                 raise api_exceptions.FileTransferException( \
74                         "Could not open local file '%s' for reading: %s" \
75                         % (local_fn, repr(e)))
76                 
77             try:
78                 self.ftp.storbinary('STOR %s' % crt_fn, file_local)
79                 file_local.close()
80             except ftplib.error_perm as e:
81                 raise api_exceptions.FileTransferException( \
82                         "Could not put file '%s' to Web Server: %s" \
83                         % (local_fn, repr(e)))
84
85     def close(self):
86         if self.ftp is not None:
87             try:
88                 self.ftp.quit()
89             except:
90                 pass