utils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import codecs
  4. import hashlib
  5. import json
  6. import json.decoder
  7. import logging
  8. import ntpath
  9. import random
  10. import six
  11. from docker.errors import DockerException
  12. from docker.utils import parse_bytes as sdk_parse_bytes
  13. from .errors import StreamParseError
  14. from .timeparse import MULTIPLIERS
  15. from .timeparse import timeparse
  16. json_decoder = json.JSONDecoder()
  17. log = logging.getLogger(__name__)
  18. def get_output_stream(stream):
  19. if six.PY3:
  20. return stream
  21. return codecs.getwriter('utf-8')(stream)
  22. def stream_as_text(stream):
  23. """Given a stream of bytes or text, if any of the items in the stream
  24. are bytes convert them to text.
  25. This function can be removed once docker-py returns text streams instead
  26. of byte streams.
  27. """
  28. for data in stream:
  29. if not isinstance(data, six.text_type):
  30. data = data.decode('utf-8', 'replace')
  31. yield data
  32. def line_splitter(buffer, separator=u'\n'):
  33. index = buffer.find(six.text_type(separator))
  34. if index == -1:
  35. return None
  36. return buffer[:index + 1], buffer[index + 1:]
  37. def split_buffer(stream, splitter=None, decoder=lambda a: a):
  38. """Given a generator which yields strings and a splitter function,
  39. joins all input, splits on the separator and yields each chunk.
  40. Unlike string.split(), each chunk includes the trailing
  41. separator, except for the last one if none was found on the end
  42. of the input.
  43. """
  44. splitter = splitter or line_splitter
  45. buffered = six.text_type('')
  46. for data in stream_as_text(stream):
  47. buffered += data
  48. while True:
  49. buffer_split = splitter(buffered)
  50. if buffer_split is None:
  51. break
  52. item, buffered = buffer_split
  53. yield item
  54. if buffered:
  55. try:
  56. yield decoder(buffered)
  57. except Exception as e:
  58. log.error(
  59. 'Compose tried decoding the following data chunk, but failed:'
  60. '\n%s' % repr(buffered)
  61. )
  62. raise StreamParseError(e)
  63. def json_splitter(buffer):
  64. """Attempt to parse a json object from a buffer. If there is at least one
  65. object, return it and the rest of the buffer, otherwise return None.
  66. """
  67. buffer = buffer.strip()
  68. try:
  69. obj, index = json_decoder.raw_decode(buffer)
  70. rest = buffer[json.decoder.WHITESPACE.match(buffer, index).end():]
  71. return obj, rest
  72. except ValueError:
  73. return None
  74. def json_stream(stream):
  75. """Given a stream of text, return a stream of json objects.
  76. This handles streams which are inconsistently buffered (some entries may
  77. be newline delimited, and others are not).
  78. """
  79. return split_buffer(stream, json_splitter, json_decoder.decode)
  80. def json_hash(obj):
  81. dump = json.dumps(obj, sort_keys=True, separators=(',', ':'), default=lambda x: x.repr())
  82. h = hashlib.sha256()
  83. h.update(dump.encode('utf8'))
  84. return h.hexdigest()
  85. def microseconds_from_time_nano(time_nano):
  86. return int(time_nano % 1000000000 / 1000)
  87. def nanoseconds_from_time_seconds(time_seconds):
  88. return int(time_seconds / MULTIPLIERS['nano'])
  89. def parse_seconds_float(value):
  90. return timeparse(value or '')
  91. def parse_nanoseconds_int(value):
  92. parsed = timeparse(value or '')
  93. if parsed is None:
  94. return None
  95. return nanoseconds_from_time_seconds(parsed)
  96. def build_string_dict(source_dict):
  97. return dict((k, str(v if v is not None else '')) for k, v in source_dict.items())
  98. def splitdrive(path):
  99. if len(path) == 0:
  100. return ('', '')
  101. if path[0] in ['.', '\\', '/', '~']:
  102. return ('', path)
  103. return ntpath.splitdrive(path)
  104. def parse_bytes(n):
  105. try:
  106. return sdk_parse_bytes(n)
  107. except DockerException:
  108. return None
  109. def unquote_path(s):
  110. if not s:
  111. return s
  112. if s[0] == '"' and s[-1] == '"':
  113. return s[1:-1]
  114. return s
  115. def generate_random_id():
  116. while True:
  117. val = hex(random.getrandbits(32 * 8))[2:-1]
  118. try:
  119. int(truncate_id(val))
  120. continue
  121. except ValueError:
  122. return val
  123. def truncate_id(value):
  124. if ':' in value:
  125. value = value[value.index(':') + 1:]
  126. if len(value) > 12:
  127. return value[:12]
  128. return value