cis: bugs fixed; start / stop / remove torrents commands implemented
[living-lab-site.git] / cis / api / ftp.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 import logger
18
19
20 class FTPFileTransferer(base.BaseFileTransferer):
21     """
22     FTPS implementation for file transfering between Web Server and CIS.
23     """
24
25     ftp = None
26
27     def __init__(self, local_path='', remote_path=''):
28         base.BaseFileTransferer.__init__(self, local_path, remote_path)
29
30         self.ftp = ftplib.FTP_TLS(ftp_config.FTP_HOST, ftp_config.FTP_USER,
31                 ftp_config.FTP_PASSWD, ftp_config.FTP_ACCT)
32         self.ftp.set_pasv(True)
33
34     def get(self, files):
35         try:
36             self.ftp.cwd(self.remote_path)
37         except ftplib.error_perm as e:
38             raise api_exceptions.FileTransferException( \
39                     "Could not change remote directory '%s': %s" \
40                     % (self.remote_path, repr(e)))
41
42
43         for crt_fn in files:
44             local_fn = os.path.join(self.local_path, crt_fn)
45             remote_fn = os.path.join(self.remote_path, crt_fn)
46             try:
47                 file_local = open(local_fn, 'wb')
48             except IOError as e:
49                 raise api_exceptions.FileTransferException( \
50                         "Could not open local file '%s' for writing: %s" \
51                         % (local_fn, repr(e)))
52
53             try:
54                 self.ftp.retrbinary('RETR %s' % crt_fn, file_local.write)
55                 file_local.close()
56             except ftplib.error_perm as e:
57                 raise api_exceptions.FileTransferException( \
58                         "Could not get file '%s' from Web Server: %s" \
59                         % (remote_fn, repr(e)))
60
61     def put(self, files):
62         try:
63             self.ftp.cwd(self.remote_path)
64         except ftplib.error_perm as e:
65             raise api_exceptions.FileTransferException( \
66                     "Could not change remote directory '%s': %s" \
67                     % (self.remote_path, repr(e)))
68
69         for crt_fn in files:
70             local_fn = os.path.join(self.local_path, crt_fn)
71
72             try:
73                 file_local = open(local_fn, 'rb')
74             except IOError as e:
75                 raise api_exceptions.FileTransferException( \
76                         "Could not open local file '%s' for reading: %s" \
77                         % (local_fn, repr(e)))
78                 
79             try:
80                 self.ftp.storbinary('STOR %s' % crt_fn, file_local)
81                 file_local.close()
82             except ftplib.error_perm as e:
83                 raise api_exceptions.FileTransferException( \
84                         "Could not put file '%s' to Web Server: %s" \
85                         % (local_fn, repr(e)))
86
87     def close(self):
88         if self.ftp is not None:
89             try:
90                 self.ftp.quit()
91             except:
92                 pass