diff options
| author | Jenkins <jenkins@review.openstack.org> | 2015-02-03 18:55:47 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2015-02-03 18:55:47 +0000 |
| commit | eb28d8aff6d6b6e985e76643bee7dbf729055c82 (patch) | |
| tree | e8d2a07545e676cb5d916dfe0b217084fe57139b /swiftclient | |
| parent | 21473f1bc475fa69aa9d1cdd6b60cc827c4f7f1b (diff) | |
| parent | bd42c2b00d0e4a18d15fd494bd9b9101742c4a37 (diff) | |
| download | python-swiftclient-eb28d8aff6d6b6e985e76643bee7dbf729055c82.tar.gz | |
Merge "This patch fixes downloading files to stdout."
Diffstat (limited to 'swiftclient')
| -rw-r--r-- | swiftclient/multithreading.py | 11 | ||||
| -rw-r--r-- | swiftclient/service.py | 115 | ||||
| -rwxr-xr-x | swiftclient/shell.py | 7 |
3 files changed, 73 insertions, 60 deletions
diff --git a/swiftclient/multithreading.py b/swiftclient/multithreading.py index 6e7f143..7ae82fa 100644 --- a/swiftclient/multithreading.py +++ b/swiftclient/multithreading.py @@ -67,6 +67,17 @@ class OutputManager(object): self.error_print_pool.__exit__(exc_type, exc_value, traceback) self.print_pool.__exit__(exc_type, exc_value, traceback) + def print_raw(self, data): + self.print_pool.submit(self._write, data, self.print_stream) + + def _write(self, data, stream): + if six.PY3: + stream.buffer.write(data) + stream.flush() + if six.PY2: + stream.write(data) + stream.flush() + def print_msg(self, msg, *fmt_args): if fmt_args: msg = msg % fmt_args diff --git a/swiftclient/service.py b/swiftclient/service.py index 32d3bd0..d7a5795 100644 --- a/swiftclient/service.py +++ b/swiftclient/service.py @@ -319,10 +319,15 @@ class _SwiftReader(object): except ValueError: raise SwiftError('content-length header must be an integer') - def __enter__(self): - return self + def __iter__(self): + for chunk in self._body: + if self._actual_md5: + self._actual_md5.update(chunk) + self._actual_read += len(chunk) + yield chunk + self._check_contents() - def __exit__(self, exc_type, exc_val, exc_tb): + def _check_contents(self): if self._actual_md5 and self._expected_etag: etag = self._actual_md5.hexdigest() if etag != self._expected_etag: @@ -337,13 +342,6 @@ class _SwiftReader(object): self._path, self._actual_read, self._content_length)) - def buffer(self): - for chunk in self._body: - if self._actual_md5: - self._actual_md5.update(chunk) - self._actual_read += len(chunk) - yield chunk - def bytes_read(self): return self._actual_read @@ -1003,64 +1001,67 @@ class SwiftService(object): try: start_time = time() - headers, body = \ conn.get_object(container, obj, resp_chunk_size=65536, headers=req_headers, response_dict=results_dict) headers_receipt = time() - reader = _SwiftReader(path, body, headers) - with reader as obj_body: - fp = None - try: - no_file = options['no_download'] - content_type = headers.get('content-type') - if (content_type and - content_type.split(';', 1)[0] == 'text/directory'): - make_dir = not no_file and out_file != "-" - if make_dir and not isdir(path): - mkdirs(path) + obj_body = _SwiftReader(path, body, headers) - else: - make_dir = not (no_file or out_file) - if make_dir: - dirpath = dirname(path) - if dirpath and not isdir(dirpath): - mkdirs(dirpath) - - if not no_file: - if out_file == "-": - res = { - 'path': path, - 'contents': obj_body - } - return res - if out_file: - fp = open(out_file, 'wb') - else: - if basename(path): - fp = open(path, 'wb') - else: - pseudodir = True + no_file = options['no_download'] + if out_file == "-" and not no_file: + res = { + 'action': 'download_object', + 'container': container, + 'object': obj, + 'path': path, + 'pseudodir': pseudodir, + 'contents': obj_body + } + return res - for chunk in obj_body.buffer(): - if fp is not None: - fp.write(chunk) + fp = None + try: + content_type = headers.get('content-type') + if (content_type and + content_type.split(';', 1)[0] == 'text/directory'): + make_dir = not no_file and out_file != "-" + if make_dir and not isdir(path): + mkdirs(path) - finish_time = time() + else: + make_dir = not (no_file or out_file) + if make_dir: + dirpath = dirname(path) + if dirpath and not isdir(dirpath): + mkdirs(dirpath) + + if not no_file: + if out_file: + fp = open(out_file, 'wb') + else: + if basename(path): + fp = open(path, 'wb') + else: + pseudodir = True - finally: - bytes_read = obj_body.bytes_read() + for chunk in obj_body: if fp is not None: - fp.close() - if 'x-object-meta-mtime' in headers and not no_file: - mtime = float(headers['x-object-meta-mtime']) - if options['out_file'] \ - and not options['out_file'] == "-": - utime(options['out_file'], (mtime, mtime)) - else: - utime(path, (mtime, mtime)) + fp.write(chunk) + + finish_time = time() + + finally: + bytes_read = obj_body.bytes_read() + if fp is not None: + fp.close() + if 'x-object-meta-mtime' in headers and not no_file: + mtime = float(headers['x-object-meta-mtime']) + if options['out_file']: + utime(options['out_file'], (mtime, mtime)) + else: + utime(path, (mtime, mtime)) res = { 'action': 'download_object', diff --git a/swiftclient/shell.py b/swiftclient/shell.py index 6b3ee3f..ce779b3 100755 --- a/swiftclient/shell.py +++ b/swiftclient/shell.py @@ -16,9 +16,9 @@ from __future__ import print_function +import logging import signal import socket -import logging from optparse import OptionParser, OptionGroup, SUPPRESS_HELP from os import environ, walk, _exit as os_exit @@ -261,8 +261,9 @@ def st_download(parser, args, output_manager): for down in down_iter: if options.out_file == '-' and 'contents' in down: - for chunk in down['contents']: - output_manager.print_msg(chunk) + contents = down['contents'] + for chunk in contents: + output_manager.print_raw(chunk) else: if down['success']: if options.verbose: |
