keyfiles.configure 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. @template
  6. def keyfile(desc, help=None, callback=lambda x: x):
  7. help = help or ('Use the secret key contained in the given keyfile '
  8. 'for %s requests' % desc)
  9. name = desc.lower().replace(' ', '-')
  10. no_key = callback('no-%s-key' % name)
  11. option('--with-%s-keyfile' % name, nargs=1, help=help)
  12. @depends('--with-%s-keyfile' % name)
  13. @checking('for the %s key' % desc, lambda x: x and x is not no_key)
  14. @imports(_from='__builtin__', _import='open')
  15. @imports(_from='__builtin__', _import='IOError')
  16. def keyfile(value):
  17. if value:
  18. try:
  19. with open(value[0]) as fh:
  20. result = fh.read().strip()
  21. if result:
  22. return callback(result)
  23. raise FatalCheckError("'%s' is empty." % value[0])
  24. except IOError as e:
  25. raise FatalCheckError("'%s': %s." % (value[0], e.strerror))
  26. return no_key
  27. return keyfile
  28. @template
  29. def simple_keyfile(desc):
  30. value = keyfile(desc)
  31. set_config('MOZ_%s_KEY' % desc.upper().replace(' ', '_'), value)
  32. @template
  33. def id_and_secret_keyfile(desc):
  34. def id_and_secret(value):
  35. if value.startswith('no-') and value.endswith('-key'):
  36. id = value[:-3] + 'clientid'
  37. secret = value
  38. elif ' ' in value:
  39. id, secret = value.split(' ', 1)
  40. else:
  41. raise FatalCheckError('%s key file has an invalid format.' % desc)
  42. return namespace(
  43. id=id,
  44. secret=secret,
  45. )
  46. content = keyfile(desc, help='Use the client id and secret key contained '
  47. 'in the given keyfile for %s requests' % desc,
  48. callback=id_and_secret)
  49. name = desc.upper().replace(' ', '_')
  50. set_config('MOZ_%s_CLIENTID' % name, delayed_getattr(content, 'id'))
  51. set_config('MOZ_%s_KEY' % name, delayed_getattr(content, 'secret'))