1 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
2 # vim: set filetype=python:
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 def keyfile(desc, default=None, help=None, callback=lambda x: x):
11 "Use the secret key contained in the given keyfile " "for %s requests" % desc
13 name = desc.lower().replace(" ", "-")
14 no_key = callback("no-%s-key" % name)
16 option("--with-%s-keyfile" % name, nargs=1, default=default, help=help)
18 @depends("--with-%s-keyfile" % name)
19 @checking("for the %s key" % desc, lambda x: x and x is not no_key)
20 @imports(_from="__builtin__", _import="open")
21 @imports(_from="__builtin__", _import="IOError")
25 with open(value[0]) as fh:
26 result = fh.read().strip()
28 return callback(result)
29 raise FatalCheckError("'%s' is empty." % value[0])
31 raise FatalCheckError("'%s': %s." % (value[0], e.strerror))
38 def simple_keyfile(desc, default=None):
39 value = keyfile(desc, default=default)
40 set_config("MOZ_%s_KEY" % desc.upper().replace(" ", "_"), value)
44 def id_and_secret_keyfile(desc, default=None):
45 def id_and_secret(value):
46 if value.startswith("no-") and value.endswith("-key"):
47 id = value[:-3] + "clientid"
50 id, secret = value.split(" ", 1)
52 raise FatalCheckError("%s key file has an invalid format." % desc)
60 help="Use the client id and secret key contained "
61 "in the given keyfile for %s requests" % desc,
63 callback=id_and_secret,
66 name = desc.upper().replace(" ", "_")
67 set_config("MOZ_%s_CLIENTID" % name, content.id)
68 set_config("MOZ_%s_KEY" % name, content.secret)