X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=blobdiff_plain;f=filex%2Fftp.py;h=85161a4ba8f956e84ff88176f684d9e1d6a97c89;hb=5ec84e98f89d5fe0cc67d6779861d2a0d9520cd7;hp=af06c277e07152a463bccf06d78dc57e1726d08c;hpb=89b60d7e7d07897a2404f7b9b23d9d4f0202cc43;p=qcg-portal.git diff --git a/filex/ftp.py b/filex/ftp.py index af06c27..85161a4 100644 --- a/filex/ftp.py +++ b/filex/ftp.py @@ -4,8 +4,9 @@ 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.http import urlquote from django.utils.timezone import localtime, UTC @@ -142,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) @@ -152,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() @@ -168,15 +190,22 @@ class FTPOperation: self._check_disk_stack_args(*([path, archive] + files)) if self.match_ext(archive, '.tar.gz', '.tgz'): - cmd, args = 'tar', ['cvzf', archive, '-C', path] + files + cmd, args = 'tar', ['czf', archive, '-C', path] + files elif self.match_ext(archive, '.tar.bz2', '.tbz'): - cmd, args = 'tar', ['cvjf', archive, '-C', path] + files + cmd, args = 'tar', ['cjf', 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))) + # zip doesn't support unicode file names + for arg in files: + try: + arg.encode('ascii') + except UnicodeEncodeError as e: + raise ValueError(u'Unsupported character `{}` in `{}`!'.format(arg[e.start:e.start + 1], arg)) + + cmd, args = 'jar', (['cMf', 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)) + self.op_attr.set_disk_stack('#'.join(["popen:argv=", cmd] + map(urlquote, args))) return self.get(server) @@ -184,21 +213,21 @@ class FTPOperation: self._check_disk_stack_args(*[archive, dst]) if self.match_ext(archive, '.tar.gz', '.tgz'): - cmd, args = 'tar', ('xvzf', archive, '-C', dst) + cmd, args = 'tar', ('xzf', archive, '-C', dst) elif self.match_ext(archive, '.tar.bz2', '.tbz'): - cmd, args = 'tar', ('xvjf', archive, '-C', dst) + cmd, args = 'tar', ('xjf', archive, '-C', dst) elif self.match_ext(archive, '.zip'): - cmd, args = 'unzip', (archive, '-d', dst) + cmd, args = 'unzip', ('-qo', archive, '-d', dst) else: raise ValueError('Unknown archive type: {}'.format(archive)) - self.op_attr.set_disk_stack('#'.join(("popen:argv=", cmd) + args)) + self.op_attr.set_disk_stack('#'.join(["popen:argv=", cmd] + map(urlquote, args))) return self.get(server) @staticmethod def _check_disk_stack_args(*args): - for char in ['#', ',', ';', '%23', '%3B']: + for char in ['#', ';']: for arg in args: if char in arg: - raise ValueError('Unsupported character `{}` in `{}`!'.format(urlunquote(char), urlunquote(arg))) + raise ValueError(u'Unsupported character `{}` in `{}`!'.format(char, arg))