1
0

utils.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 find_candidates_in_parent_dirs(filenames, path):
  30. """
  31. Given a directory path to start, looks for filenames in the
  32. directory, and then each parent directory successively,
  33. until found.
  34. Returns tuple (candidates, path).
  35. """
  36. candidates = [filename for filename in filenames
  37. if os.path.exists(os.path.join(path, filename))]
  38. if len(candidates) == 0:
  39. parent_dir = os.path.join(path, '..')
  40. if os.path.abspath(parent_dir) != os.path.abspath(path):
  41. return find_candidates_in_parent_dirs(filenames, parent_dir)
  42. return (candidates, path)
  43. def split_buffer(reader, separator):
  44. """
  45. Given a generator which yields strings and a separator string,
  46. joins all input, splits on the separator and yields each chunk.
  47. Unlike string.split(), each chunk includes the trailing
  48. separator, except for the last one if none was found on the end
  49. of the input.
  50. """
  51. buffered = six.text_type('')
  52. separator = six.text_type(separator)
  53. for data in reader:
  54. buffered += data.decode('utf-8')
  55. while True:
  56. index = buffered.find(separator)
  57. if index == -1:
  58. break
  59. yield buffered[:index + 1]
  60. buffered = buffered[index + 1:]
  61. if len(buffered) > 0:
  62. yield buffered
  63. def call_silently(*args, **kwargs):
  64. """
  65. Like subprocess.call(), but redirects stdout and stderr to /dev/null.
  66. """
  67. with open(os.devnull, 'w') as shutup:
  68. return subprocess.call(*args, stdout=shutup, stderr=shutup, **kwargs)
  69. def is_mac():
  70. return platform.system() == 'Darwin'
  71. def is_ubuntu():
  72. return platform.system() == 'Linux' and platform.linux_distribution()[0] == 'Ubuntu'
  73. def get_version_info(scope):
  74. versioninfo = 'docker-compose version: %s' % __version__
  75. if scope == 'compose':
  76. return versioninfo
  77. elif scope == 'full':
  78. return versioninfo + '\n' \
  79. + "docker-py version: %s\n" % docker_py_version \
  80. + "%s version: %s\n" % (platform.python_implementation(), platform.python_version()) \
  81. + "OpenSSL version: %s" % ssl.OPENSSL_VERSION
  82. else:
  83. raise RuntimeError('passed unallowed value to `cli.utils.get_version_info`')