diff options
Diffstat (limited to 'compose/utils.py')
-rw-r--r-- | compose/utils.py | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/compose/utils.py b/compose/utils.py index 925a8e79..197ae6eb 100644 --- a/compose/utils.py +++ b/compose/utils.py @@ -5,11 +5,20 @@ import codecs import hashlib import json import json.decoder +import logging +import ntpath import six +from docker.errors import DockerException +from docker.utils import parse_bytes as sdk_parse_bytes + +from .errors import StreamParseError +from .timeparse import MULTIPLIERS +from .timeparse import timeparse json_decoder = json.JSONDecoder() +log = logging.getLogger(__name__) def get_output_stream(stream): @@ -60,13 +69,21 @@ def split_buffer(stream, splitter=None, decoder=lambda a: a): yield item if buffered: - yield decoder(buffered) + try: + yield decoder(buffered) + except Exception as e: + log.error( + 'Compose tried decoding the following data chunk, but failed:' + '\n%s' % repr(buffered) + ) + raise StreamParseError(e) def json_splitter(buffer): """Attempt to parse a json object from a buffer. If there is at least one object, return it and the rest of the buffer, otherwise return None. """ + buffer = buffer.strip() try: obj, index = json_decoder.raw_decode(buffer) rest = buffer[json.decoder.WHITESPACE.match(buffer, index).end():] @@ -94,5 +111,35 @@ def microseconds_from_time_nano(time_nano): return int(time_nano % 1000000000 / 1000) +def nanoseconds_from_time_seconds(time_seconds): + return int(time_seconds / MULTIPLIERS['nano']) + + +def parse_seconds_float(value): + return timeparse(value or '') + + +def parse_nanoseconds_int(value): + parsed = timeparse(value or '') + if parsed is None: + return None + return nanoseconds_from_time_seconds(parsed) + + def build_string_dict(source_dict): return dict((k, str(v if v is not None else '')) for k, v in source_dict.items()) + + +def splitdrive(path): + if len(path) == 0: + return ('', '') + if path[0] in ['.', '\\', '/', '~']: + return ('', path) + return ntpath.splitdrive(path) + + +def parse_bytes(n): + try: + return sdk_parse_bytes(n) + except DockerException: + return None |