upload facility now works in single CIS mode; some simple command-line video moderati...
[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 cis_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 cis_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             
47             if os.path.exists(local_fn):
48                 raise cis_exceptions.FileAlreadyExistsException( \
49                         'file "%s" already exists' % local_fn)
50             
51             try:
52                 file_local = open(local_fn, 'wb')
53             except IOError as e:
54                 raise cis_exceptions.FileTransferException( \
55                         "Could not open local file '%s' for writing: %s" \
56                         % (local_fn, repr(e)))
57
58             try:
59                 self.ftp.retrbinary('RETR %s' % crt_fn, file_local.write)
60                 file_local.close()
61             except ftplib.error_perm as e:
62                 raise cis_exceptions.FileTransferException( \
63                         "Could not get file '%s' from Web Server: %s" \
64                         % (remote_fn, repr(e)))
65
66     def put(self, files):
67         try:
68             self.ftp.cwd(self.remote_path)
69         except ftplib.error_perm as e:
70             raise cis_exceptions.FileTransferException( \
71                     "Could not change remote directory '%s': %s" \
72                     % (self.remote_path, repr(e)))
73
74         for crt_fn in files:
75             local_fn = os.path.join(self.local_path, crt_fn)
76
77             try:
78                 file_local = open(local_fn, 'rb')
79             except IOError as e:
80                 raise cis_exceptions.FileTransferException( \
81                         "Could not open local file '%s' for reading: %s" \
82                         % (local_fn, repr(e)))
83                 
84             try:
85                 self.ftp.storbinary('STOR %s' % crt_fn, file_local)
86                 file_local.close()
87             except ftplib.error_perm as e:
88                 raise cis_exceptions.FileTransferException( \
89                         "Could not put file '%s' to Web Server: %s" \
90                         % (local_fn, repr(e)))
91
92     def close(self):
93         if self.ftp is not None:
94             try:
95                 self.ftp.quit()
96             except:
97                 pass