utils.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. import datetime
  4. import os
  5. import socket
  6. from .errors import UserError
  7. def cached_property(f):
  8. """
  9. returns a cached property that is calculated by function f
  10. http://code.activestate.com/recipes/576563-cached-property/
  11. """
  12. def get(self):
  13. try:
  14. return self._property_cache[f]
  15. except AttributeError:
  16. self._property_cache = {}
  17. x = self._property_cache[f] = f(self)
  18. return x
  19. except KeyError:
  20. x = self._property_cache[f] = f(self)
  21. return x
  22. return property(get)
  23. def yesno(prompt, default=None):
  24. """
  25. Prompt the user for a yes or no.
  26. Can optionally specify a default value, which will only be
  27. used if they enter a blank line.
  28. Unrecognised input (anything other than "y", "n", "yes",
  29. "no" or "") will return None.
  30. """
  31. answer = raw_input(prompt).strip().lower()
  32. if answer == "y" or answer == "yes":
  33. return True
  34. elif answer == "n" or answer == "no":
  35. return False
  36. elif answer == "":
  37. return default
  38. else:
  39. return None
  40. # http://stackoverflow.com/a/5164027
  41. def prettydate(d):
  42. diff = datetime.datetime.utcnow() - d
  43. s = diff.seconds
  44. if diff.days > 7 or diff.days < 0:
  45. return d.strftime('%d %b %y')
  46. elif diff.days == 1:
  47. return '1 day ago'
  48. elif diff.days > 1:
  49. return '{0} days ago'.format(diff.days)
  50. elif s <= 1:
  51. return 'just now'
  52. elif s < 60:
  53. return '{0} seconds ago'.format(s)
  54. elif s < 120:
  55. return '1 minute ago'
  56. elif s < 3600:
  57. return '{0} minutes ago'.format(s/60)
  58. elif s < 7200:
  59. return '1 hour ago'
  60. else:
  61. return '{0} hours ago'.format(s/3600)
  62. def mkdir(path, permissions=0o700):
  63. if not os.path.exists(path):
  64. os.mkdir(path)
  65. os.chmod(path, permissions)
  66. return path
  67. def docker_url():
  68. if os.environ.get('DOCKER_URL'):
  69. return os.environ['DOCKER_URL']
  70. socket_path = '/var/run/docker.sock'
  71. tcp_hosts = [
  72. ('localdocker', 4243),
  73. ('127.0.0.1', 4243),
  74. ]
  75. tcp_host = '127.0.0.1'
  76. tcp_port = 4243
  77. if os.path.exists(socket_path):
  78. return 'unix://%s' % socket_path
  79. for host, port in tcp_hosts:
  80. try:
  81. s = socket.create_connection((host, port), timeout=1)
  82. s.close()
  83. return 'http://%s:%s' % (host, port)
  84. except:
  85. pass
  86. raise UserError("""
  87. Couldn't find Docker daemon - tried:
  88. unix://%s
  89. %s
  90. If it's running elsewhere, specify a url with DOCKER_URL.
  91. """ % (socket_path, '\n'.join('tcp://%s:%s' % h for h in tcp_hosts)))