utils.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. from __future__ import division
  4. import datetime
  5. import os
  6. import subprocess
  7. import platform
  8. import ssl
  9. from .. import __version__
  10. def yesno(prompt, default=None):
  11. """
  12. Prompt the user for a yes or no.
  13. Can optionally specify a default value, which will only be
  14. used if they enter a blank line.
  15. Unrecognised input (anything other than "y", "n", "yes",
  16. "no" or "") will return None.
  17. """
  18. answer = raw_input(prompt).strip().lower()
  19. if answer == "y" or answer == "yes":
  20. return True
  21. elif answer == "n" or answer == "no":
  22. return False
  23. elif answer == "":
  24. return default
  25. else:
  26. return None
  27. # http://stackoverflow.com/a/5164027
  28. def prettydate(d):
  29. diff = datetime.datetime.utcnow() - d
  30. s = diff.seconds
  31. if diff.days > 7 or diff.days < 0:
  32. return d.strftime('%d %b %y')
  33. elif diff.days == 1:
  34. return '1 day ago'
  35. elif diff.days > 1:
  36. return '{0} days ago'.format(diff.days)
  37. elif s <= 1:
  38. return 'just now'
  39. elif s < 60:
  40. return '{0} seconds ago'.format(s)
  41. elif s < 120:
  42. return '1 minute ago'
  43. elif s < 3600:
  44. return '{0} minutes ago'.format(s / 60)
  45. elif s < 7200:
  46. return '1 hour ago'
  47. else:
  48. return '{0} hours ago'.format(s / 3600)
  49. def mkdir(path, permissions=0o700):
  50. if not os.path.exists(path):
  51. os.mkdir(path)
  52. os.chmod(path, permissions)
  53. return path
  54. def find_candidates_in_parent_dirs(filenames, path):
  55. """
  56. Given a directory path to start, looks for filenames in the
  57. directory, and then each parent directory successively,
  58. until found.
  59. Returns tuple (candidates, path).
  60. """
  61. candidates = [filename for filename in filenames
  62. if os.path.exists(os.path.join(path, filename))]
  63. if len(candidates) == 0:
  64. parent_dir = os.path.join(path, '..')
  65. if os.path.abspath(parent_dir) != os.path.abspath(path):
  66. return find_candidates_in_parent_dirs(filenames, parent_dir)
  67. return (candidates, path)
  68. def split_buffer(reader, separator):
  69. """
  70. Given a generator which yields strings and a separator string,
  71. joins all input, splits on the separator and yields each chunk.
  72. Unlike string.split(), each chunk includes the trailing
  73. separator, except for the last one if none was found on the end
  74. of the input.
  75. """
  76. buffered = str('')
  77. separator = str(separator)
  78. for data in reader:
  79. buffered += data
  80. while True:
  81. index = buffered.find(separator)
  82. if index == -1:
  83. break
  84. yield buffered[:index + 1]
  85. buffered = buffered[index + 1:]
  86. if len(buffered) > 0:
  87. yield buffered
  88. def call_silently(*args, **kwargs):
  89. """
  90. Like subprocess.call(), but redirects stdout and stderr to /dev/null.
  91. """
  92. with open(os.devnull, 'w') as shutup:
  93. return subprocess.call(*args, stdout=shutup, stderr=shutup, **kwargs)
  94. def is_mac():
  95. return platform.system() == 'Darwin'
  96. def is_ubuntu():
  97. return platform.system() == 'Linux' and platform.linux_distribution()[0] == 'Ubuntu'
  98. def get_version_info():
  99. return '\n'.join([
  100. 'docker-compose version: %s' % __version__,
  101. "%s version: %s" % (platform.python_implementation(), platform.python_version()),
  102. "OpenSSL version: %s" % ssl.OPENSSL_VERSION,
  103. ])