X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=blobdiff_plain;f=filex%2Fftp.py;h=3ccd8c62c44c7100c35dcffc7e4e87962c8feb51;hb=a106596addd10217af2c01f895b931ff04762b8b;hp=2b09d3e65f841a1a831cdba8c9945c227bc82e21;hpb=e77c85499adde5b280a5178271774e279dc227f7;p=qcg-portal.git diff --git a/filex/ftp.py b/filex/ftp.py index 2b09d3e..3ccd8c6 100644 --- a/filex/ftp.py +++ b/filex/ftp.py @@ -4,10 +4,10 @@ from itertools import chain import os import re from threading import Event +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): @@ -19,6 +19,8 @@ class FTPError(Exception): 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) @@ -141,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) @@ -151,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()