utils.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 sys
  9. import docker
  10. import compose
  11. # WindowsError is not defined on non-win32 platforms. Avoid runtime errors by
  12. # defining it as OSError (its parent class) if missing.
  13. try:
  14. WindowsError
  15. except NameError:
  16. WindowsError = OSError
  17. def yesno(prompt, default=None):
  18. """
  19. Prompt the user for a yes or no.
  20. Can optionally specify a default value, which will only be
  21. used if they enter a blank line.
  22. Unrecognised input (anything other than "y", "n", "yes",
  23. "no" or "") will return None.
  24. """
  25. answer = input(prompt).strip().lower()
  26. if answer == "y" or answer == "yes":
  27. return True
  28. elif answer == "n" or answer == "no":
  29. return False
  30. elif answer == "":
  31. return default
  32. else:
  33. return None
  34. def input(prompt):
  35. """
  36. Version of input (raw_input in Python 2) which forces a flush of sys.stdout
  37. to avoid problems where the prompt fails to appear due to line buffering
  38. """
  39. sys.stdout.write(prompt)
  40. sys.stdout.flush()
  41. return sys.stdin.readline().rstrip(b'\n')
  42. def call_silently(*args, **kwargs):
  43. """
  44. Like subprocess.call(), but redirects stdout and stderr to /dev/null.
  45. """
  46. with open(os.devnull, 'w') as shutup:
  47. try:
  48. return subprocess.call(*args, stdout=shutup, stderr=shutup, **kwargs)
  49. except WindowsError:
  50. # On Windows, subprocess.call() can still raise exceptions. Normalize
  51. # to POSIXy behaviour by returning a nonzero exit code.
  52. return 1
  53. def is_mac():
  54. return platform.system() == 'Darwin'
  55. def is_ubuntu():
  56. return platform.system() == 'Linux' and platform.linux_distribution()[0] == 'Ubuntu'
  57. def get_version_info(scope):
  58. versioninfo = 'docker-compose version {}, build {}'.format(
  59. compose.__version__,
  60. get_build_version())
  61. if scope == 'compose':
  62. return versioninfo
  63. if scope == 'full':
  64. return (
  65. "{}\n"
  66. "docker-py version: {}\n"
  67. "{} version: {}\n"
  68. "OpenSSL version: {}"
  69. ).format(
  70. versioninfo,
  71. docker.version,
  72. platform.python_implementation(),
  73. platform.python_version(),
  74. ssl.OPENSSL_VERSION)
  75. raise ValueError("{} is not a valid version scope".format(scope))
  76. def get_build_version():
  77. filename = os.path.join(os.path.dirname(compose.__file__), 'GITSHA')
  78. if not os.path.exists(filename):
  79. return 'unknown'
  80. with open(filename) as fh:
  81. return fh.read().strip()