X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=blobdiff_plain;f=filex%2Fftp.py;h=3ccd8c62c44c7100c35dcffc7e4e87962c8feb51;hb=d5cc9c8eb609649bb2a6993fcbf8c72b854507b1;hp=2c92f129b6d623923251abd2363169abc8187c30;hpb=6d6c248706a2b7a388ab37bc12a38d1b3806ba63;p=qcg-portal.git diff --git a/filex/ftp.py b/filex/ftp.py index 2c92f12..3ccd8c6 100644 --- a/filex/ftp.py +++ b/filex/ftp.py @@ -4,18 +4,23 @@ from itertools import chain import os import re from threading import Event -from django.utils.http import urlunquote +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 FTPError(Exception): - pass + 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) @@ -67,14 +72,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 FTPError(msg) + raise FTPError(msg, self._error) def listing(self, url): self.cli.verbose_list(url, self._done, None, self.op_attr) @@ -99,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())), } @@ -140,6 +143,18 @@ class FTPOperation: 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) @@ -150,7 +165,16 @@ class FTPOperation: self.wait() - def mkdir(self, url): + 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()