utils.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from __future__ import absolute_import
  2. from __future__ import division
  3. from __future__ import unicode_literals
  4. import os
  5. import platform
  6. import ssl
  7. import subprocess
  8. import six
  9. from docker import version as docker_py_version
  10. from six.moves import input
  11. from .. import __version__
  12. def yesno(prompt, default=None):
  13. """
  14. Prompt the user for a yes or no.
  15. Can optionally specify a default value, which will only be
  16. used if they enter a blank line.
  17. Unrecognised input (anything other than "y", "n", "yes",
  18. "no" or "") will return None.
  19. """
  20. answer = input(prompt).strip().lower()
  21. if answer == "y" or answer == "yes":
  22. return True
  23. elif answer == "n" or answer == "no":
  24. return False
  25. elif answer == "":
  26. return default
  27. else:
  28. return None
  29. def split_buffer(reader, separator):
  30. """
  31. Given a generator which yields strings and a separator string,
  32. joins all input, splits on the separator and yields each chunk.
  33. Unlike string.split(), each chunk includes the trailing
  34. separator, except for the last one if none was found on the end
  35. of the input.
  36. """
  37. buffered = six.text_type('')
  38. separator = six.text_type(separator)
  39. for data in reader:
  40. buffered += data.decode('utf-8')
  41. while True:
  42. index = buffered.find(separator)
  43. if index == -1:
  44. break
  45. yield buffered[:index + 1]
  46. buffered = buffered[index + 1:]
  47. if len(buffered) > 0:
  48. yield buffered
  49. def call_silently(*args, **kwargs):
  50. """
  51. Like subprocess.call(), but redirects stdout and stderr to /dev/null.
  52. """
  53. with open(os.devnull, 'w') as shutup:
  54. return subprocess.call(*args, stdout=shutup, stderr=shutup, **kwargs)
  55. def is_mac():
  56. return platform.system() == 'Darwin'
  57. def is_ubuntu():
  58. return platform.system() == 'Linux' and platform.linux_distribution()[0] == 'Ubuntu'
  59. def get_version_info(scope):
  60. versioninfo = 'docker-compose version: %s' % __version__
  61. if scope == 'compose':
  62. return versioninfo
  63. elif scope == 'full':
  64. return versioninfo + '\n' \
  65. + "docker-py version: %s\n" % docker_py_version \
  66. + "%s version: %s\n" % (platform.python_implementation(), platform.python_version()) \
  67. + "OpenSSL version: %s" % ssl.OPENSSL_VERSION
  68. else:
  69. raise RuntimeError('passed unallowed value to `cli.utils.get_version_info`')