utils.py 2.7 KB

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