From 706fc940e8e16d9bc4d242421e5d47351082b3dd Mon Sep 17 00:00:00 2001 From: Stavros Sachtouris Date: Fri, 11 Jan 2013 14:05:53 +0200 Subject: [PATCH] upload does not overwrite remote objs by default Now you can do this to upload all you .jpg imgs to myImgDir directory object: for img in *.jpg; do kamaki store upload $img mycontainer:myImgDir done --- kamaki/cli/commands/pithos_cli.py | 40 ++++++++++++++++++++++++++++++++----- kamaki/clients/__init__.py | 2 +- kamaki/clients/pithos.py | 2 -- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/kamaki/cli/commands/pithos_cli.py b/kamaki/cli/commands/pithos_cli.py index a7489a2..2b445b4 100644 --- a/kamaki/cli/commands/pithos_cli.py +++ b/kamaki/cli/commands/pithos_cli.py @@ -873,9 +873,27 @@ class store_upload(_store_container_command): progress_bar=ProgressBarArgument( 'do not show progress bar', '--no-progress-bar', - default=False) + default=False), + overwrite=FlagArgument('Force overwrite, if object exists', '-f') ) + def _remote_path(self, remote_path, local_path=''): + if self['overwrite']: + return remote_path + try: + r = self.client.get_object_info(remote_path) + except ClientError as ce: + if ce.status == 404: + return remote_path + ctype = r.get('content-type', '') + if 'application/directory' == ctype.lower(): + ret = '%s/%s' % (remote_path, local_path) + return self._remote_path(ret) if local_path else ret + raiseCLIError( + 'Object %s already exists' % remote_path, + importance=1, + details=['use -f to overwrite']) + def main(self, local_path, container____path__): super(self.__class__, self).main(container____path__) remote_path = self.path if self.path else local_path @@ -889,6 +907,7 @@ class store_upload(_store_container_command): sharing=self['sharing'], public=self['public']) try: + remote_path = self._remote_path(remote_path, local_path) progress_bar = self.arguments['progress_bar'] hash_bar = progress_bar.clone() with open(path.abspath(local_path), 'rb') as f: @@ -912,8 +931,11 @@ class store_upload(_store_container_command): progress_bar.finish() hash_bar.finish() except ClientError as err: - progress_bar.finish() - hash_bar.finish() + try: + progress_bar.finish() + hash_bar.finish() + except Exception: + pass if err.status == 404: if 'container' in ('%s' % err).lower(): raiseCLIError( @@ -924,10 +946,18 @@ class store_upload(_store_container_command): raise_connection_errors(err) raiseCLIError(err, '"%s" not accessible' % container____path__) except IOError as err: - progress_bar.finish() - hash_bar.finish() + try: + progress_bar.finish() + hash_bar.finish() + except Exception: + pass raiseCLIError(err, 'Failed to read form file %s' % local_path, 2) except Exception as e: + try: + progress_bar.finish() + hash_bar.finish() + except Exception: + pass raiseCLIError(e) print 'Upload completed' diff --git a/kamaki/clients/__init__.py b/kamaki/clients/__init__.py index 9c2417a..7b9e2f4 100644 --- a/kamaki/clients/__init__.py +++ b/kamaki/clients/__init__.py @@ -94,7 +94,7 @@ class SilentEvent(Thread): try: self._value = self.method(*(self.args), **(self.kwargs)) except Exception as e: - print('______\n%s\n_______' % e) + print('______\n%s, \n_______' % (e, type(e))) self._exception = e diff --git a/kamaki/clients/pithos.py b/kamaki/clients/pithos.py index 40678ad..b7c9534 100644 --- a/kamaki/clients/pithos.py +++ b/kamaki/clients/pithos.py @@ -246,8 +246,6 @@ class PithosClient(PithosRestAPI): if hash_cb: hash_gen.next() if offset != size: - print("Size is %i" % size) - print("Offset is %i" % offset) assert offset == size, \ "Failed to calculate uploaded blocks: " \ "Offset and object size do not match" -- 1.7.10.4