logging of gridftp errors
[qcg-portal.git] / filex / ftp.py
index 49f470f..2b09d3e 100644 (file)
@@ -4,14 +4,17 @@ from itertools import chain
 import os
 import re
 from threading import Event
-from urlparse import urlparse
 
+from django.utils.http import urlunquote
 from django.utils.timezone import localtime, UTC
 from gridftp import FTPClient, Buffer, HandleAttr, OperationAttr
 
 
-class FTPException(Exception):
-    pass
+class FTPError(Exception):
+    def __init__(self, message, verbose=None, *args, **kwargs):
+        super(FTPError, self).__init__(message, *args, **kwargs)
+
+        self.verbose = verbose
 
 
 class FTPOperation:
@@ -67,14 +70,11 @@ class FTPOperation:
         self._end.clear()
 
         if self._error is not None:
-            # TODO logging
-            print 'GridFTP ERROR:', self._error
-
             match = re.search(r'A system call failed: (.*)$', self._error.replace('\r\n', '\n'), re.MULTILINE)
 
             msg = match.groups()[0] if match else "Unknown error"
 
-            raise FTPException(msg)
+            raise FTPError(msg, self._error)
 
     def listing(self, url):
         self.cli.verbose_list(url, self._done, None, self.op_attr)
@@ -99,13 +99,14 @@ class FTPOperation:
                 for attr in attrs.split(';'):
                     try:
                         key, value = attr.split('=', 1)
-                        attrs_dict[key] = value
                     except ValueError:
-                        pass
+                        key, value = attr, ''
+
+                    attrs_dict[key] = value
 
                 yield {
                     'name': name,
-                    'type': 'directory' if attrs_dict['Type'] == 'dir' else 'file',
+                    'type': 'directory' if attrs_dict['Type'].endswith('dir') else 'file',
                     'size': int(attrs_dict['Size']),
                     'date': localtime(datetime.strptime(attrs_dict['Modify'], "%Y%m%d%H%M%S").replace(tzinfo=UTC())),
                 }
@@ -136,7 +137,7 @@ class FTPOperation:
         data = self.listing(url).next()
 
         if data['name'] == '.':
-            data['name'] = os.path.basename(urlparse(url).path.rstrip('/')) or u'/'
+            data['name'] = os.path.basename(os.path.normpath(url))
 
         return data
 
@@ -163,22 +164,24 @@ class FTPOperation:
         return False
 
     def compress(self, server, path, files, archive):
+        self._check_disk_stack_args(*([path, archive] + files))
+
         if self.match_ext(archive, '.tar.gz', '.tgz'):
-            cmd, args = 'tar', ['cvzf', os.path.join(path, archive), '-C', path] + files
+            cmd, args = 'tar', ['cvzf', archive, '-C', path] + files
         elif self.match_ext(archive, '.tar.bz2', '.tbz'):
-            cmd, args = 'tar', ['cvjf', os.path.join(path, archive), '-C', path] + files
+            cmd, args = 'tar', ['cvjf', archive, '-C', path] + files
         elif self.match_ext(archive, '.zip'):
-            cmd, args = 'jar', (['cvMf', os.path.join(path, archive)] +
-                                list(chain.from_iterable(('-C', path, f) for f in files)))
+            cmd, args = 'jar', (['cvMf', archive] + list(chain.from_iterable(('-C', path, f) for f in files)))
         else:
             raise ValueError('Unknown archive type: {}'.format(archive))
 
-        # FIXME handling filename with #
         self.op_attr.set_disk_stack('#'.join(["popen:argv=", cmd] + args))
 
         return self.get(server)
 
     def extract(self, server, archive, dst):
+        self._check_disk_stack_args(*[archive, dst])
+
         if self.match_ext(archive, '.tar.gz', '.tgz'):
             cmd, args = 'tar', ('xvzf', archive, '-C', dst)
         elif self.match_ext(archive, '.tar.bz2', '.tbz'):
@@ -188,7 +191,13 @@ class FTPOperation:
         else:
             raise ValueError('Unknown archive type: {}'.format(archive))
 
-        # FIXME handling filename with #
         self.op_attr.set_disk_stack('#'.join(("popen:argv=", cmd) + args))
 
         return self.get(server)
+
+    @staticmethod
+    def _check_disk_stack_args(*args):
+        for char in ['#', ',', ';', '%23', '%3B']:
+            for arg in args:
+                if char in arg:
+                    raise ValueError('Unsupported character `{}` in `{}`!'.format(urlunquote(char), urlunquote(arg)))