utils.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 docker
  9. from six.moves import input
  10. import compose
  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 {}, build {}'.format(
  45. compose.__version__,
  46. get_build_version())
  47. if scope == 'compose':
  48. return versioninfo
  49. if scope == 'full':
  50. return (
  51. "{}\n"
  52. "docker-py version: {}\n"
  53. "{} version: {}\n"
  54. "OpenSSL version: {}"
  55. ).format(
  56. versioninfo,
  57. docker.version,
  58. platform.python_implementation(),
  59. platform.python_version(),
  60. ssl.OPENSSL_VERSION)
  61. raise ValueError("{} is not a valid version scope".format(scope))
  62. def get_build_version():
  63. filename = os.path.join(os.path.dirname(compose.__file__), 'GITSHA')
  64. if not os.path.exists(filename):
  65. return 'unknown'
  66. with open(filename) as fh:
  67. return fh.read().strip()