utils.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. from docker import version as docker_py_version
  9. from six.moves import input
  10. from .. import __version__
  11. def yesno(prompt, default=None):
  12. """
  13. Prompt the user for a yes or no.
  14. Can optionally specify a default value, which will only be
  15. used if they enter a blank line.
  16. Unrecognised input (anything other than "y", "n", "yes",
  17. "no" or "") will return None.
  18. """
  19. answer = input(prompt).strip().lower()
  20. if answer == "y" or answer == "yes":
  21. return True
  22. elif answer == "n" or answer == "no":
  23. return False
  24. elif answer == "":
  25. return default
  26. else:
  27. return None
  28. def call_silently(*args, **kwargs):
  29. """
  30. Like subprocess.call(), but redirects stdout and stderr to /dev/null.
  31. """
  32. with open(os.devnull, 'w') as shutup:
  33. try:
  34. return subprocess.call(*args, stdout=shutup, stderr=shutup, **kwargs)
  35. except WindowsError:
  36. # On Windows, subprocess.call() can still raise exceptions. Normalize
  37. # to POSIXy behaviour by returning a nonzero exit code.
  38. return 1
  39. def is_mac():
  40. return platform.system() == 'Darwin'
  41. def is_ubuntu():
  42. return platform.system() == 'Linux' and platform.linux_distribution()[0] == 'Ubuntu'
  43. def get_version_info(scope):
  44. versioninfo = 'docker-compose version: %s' % __version__
  45. if scope == 'compose':
  46. return versioninfo
  47. elif scope == 'full':
  48. return versioninfo + '\n' \
  49. + "docker-py version: %s\n" % docker_py_version \
  50. + "%s version: %s\n" % (platform.python_implementation(), platform.python_version()) \
  51. + "OpenSSL version: %s" % ssl.OPENSSL_VERSION
  52. else:
  53. raise RuntimeError('passed unallowed value to `cli.utils.get_version_info`')