X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=blobdiff_plain;f=filex%2Fftp.py;h=3ccd8c62c44c7100c35dcffc7e4e87962c8feb51;hb=5e547a8cc20ef2bd450b3b839564e56e99099cee;hp=4997176f0175da8bdb2b66302faffa3f60947134;hpb=b2ed8e40172bf1230a8e5d8555016b1315da8a1a;p=qcg-portal.git diff --git a/filex/ftp.py b/filex/ftp.py index 4997176..3ccd8c6 100644 --- a/filex/ftp.py +++ b/filex/ftp.py @@ -1,20 +1,26 @@ from datetime import datetime from Queue import Queue, Empty +from itertools import chain import os import re from threading import Event -from urlparse import urlparse +from urlparse import urlparse, urlunparse +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: def __init__(self, proxy=None, buffer_size=4096): + from gridftp import FTPClient, Buffer, HandleAttr, OperationAttr + self._end = Event() self._error = None self._buffer = Buffer(buffer_size) @@ -70,7 +76,7 @@ class FTPOperation: 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) @@ -95,13 +101,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())), } @@ -132,10 +139,22 @@ 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 + def exists(self, url): + self.cli.exists(url, self._done, None, self.op_attr) + + try: + self.wait() + except FTPError as e: + if 'No such file or directory' in e.message: + return False + raise + else: + return True + def delete(self, url): self.cli.delete(url, self._done, None, self.op_attr) @@ -145,3 +164,63 @@ class FTPOperation: self.cli.rmdir(url, self._done, None, self.op_attr) self.wait() + + def mkdir(self, url, parents=False): + if parents: + if self.exists(url): + return + + u = urlparse(url) + parent_url = urlunparse((u.scheme, u.netloc, os.path.dirname(os.path.normpath(u.path)), '', '', '')) + + self.mkdir(parent_url, parents=True) + + 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): + self._check_disk_stack_args(*([path, archive] + files)) + + 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): + 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'): + 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) + + @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)))