gridftp: handle paths beginning with `~`
[qcg-portal.git] / filex / ftp.py
index c0b3e25..29c4598 100644 (file)
@@ -1,5 +1,7 @@
 from datetime import datetime
 from Queue import Queue, Empty
+from itertools import chain
+import os
 import re
 from threading import Event
 
@@ -64,6 +66,9 @@ 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"
@@ -84,25 +89,25 @@ class FTPOperation:
 
     @staticmethod
     def _parse_mlst(listing):
-        data = []
-
         for item in listing.strip().splitlines():
             # we may receive empty string when there are multiple consecutive newlines in listing
             if item:
                 attrs, name = item.split(' ', 1)
 
-                attrs = dict((attr.split('=') for attr in attrs.split(';') if attr))
+                attrs_dict = {}
+                for attr in attrs.split(';'):
+                    try:
+                        key, value = attr.split('=', 1)
+                        attrs_dict[key] = value
+                    except ValueError:
+                        pass
 
-                date = localtime(datetime.strptime(attrs['Modify'], "%Y%m%d%H%M%S").replace(tzinfo=UTC()))
-
-                data.append({
+                yield {
                     'name': name,
-                    'type': 'file' if attrs['Type'] == 'file' else 'directory',
-                    'size': int(attrs['Size']),
-                    'date': date,
-                })
-
-        return data
+                    'type': 'directory' if attrs_dict['Type'] == 'dir' else 'file',
+                    'size': int(attrs_dict['Size']),
+                    'date': localtime(datetime.strptime(attrs_dict['Modify'], "%Y%m%d%H%M%S").replace(tzinfo=UTC())),
+                }
 
     def get(self, url):
         self.cli.get(url, self._done, None, self.op_attr)
@@ -125,3 +130,61 @@ class FTPOperation:
         self.cli.move(src, dst, self._done, None, self.op_attr)
 
         self.wait()
+
+    def info(self, url):
+        data = self.listing(url).next()
+
+        if data['name'] == '.':
+            data['name'] = os.path.basename(os.path.normpath(url))
+
+        return data
+
+    def delete(self, url):
+        self.cli.delete(url, self._done, None, self.op_attr)
+
+        self.wait()
+
+    def rmdir(self, url):
+        self.cli.rmdir(url, self._done, None, self.op_attr)
+
+        self.wait()
+
+    def mkdir(self, url):
+        self.cli.mkdir(url, self._done, None, self.op_attr)
+
+        self.wait()
+
+    @staticmethod
+    def match_ext(archive, *extensions):
+        for ext in extensions:
+            if archive.endswith(ext):
+                return True
+        return False
+
+    def compress(self, server, path, files, archive):
+        if self.match_ext(archive, '.tar.gz', '.tgz'):
+            cmd, args = 'tar', ['cvzf', archive, '-C', path] + files
+        elif self.match_ext(archive, '.tar.bz2', '.tbz'):
+            cmd, args = 'tar', ['cvjf', archive, '-C', path] + files
+        elif self.match_ext(archive, '.zip'):
+            cmd, args = 'jar', (['cvMf', archive] + list(chain.from_iterable(('-C', path, f) for f in files)))
+        else:
+            raise ValueError('Unknown archive type: {}'.format(archive))
+
+        self.op_attr.set_disk_stack('#'.join(["popen:argv=", cmd] + args))
+
+        return self.get(server)
+
+    def extract(self, server, 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'):
+            cmd, args = 'tar', ('xvjf', archive, '-C', dst)
+        elif self.match_ext(archive, '.zip'):
+            cmd, args = 'unzip', (archive, '-d', dst)
+        else:
+            raise ValueError('Unknown archive type: {}'.format(archive))
+
+        self.op_attr.set_disk_stack('#'.join(("popen:argv=", cmd) + args))
+
+        return self.get(server)