ubuntu-dev-tools/manage-credentials
Markus Korn 148d722a60 * added manage-credentials, a tool to create (and manage) credentials which
are used to access launchpad via the API. Currently this tool can be used
  to create a token with or without using the web UI. In the future, once
  related methods are available through the API, this tool can also be used
  to manage tokens in launchpad and on the users local machine.
2009-01-13 13:25:05 +01:00

124 lines
4.7 KiB
Python
Executable File

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009 Markus Korn <thekorn@gmx.de>
#
# ##################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# See file /usr/share/common-licenses/GPL for more details.
#
# ##################################################################
import sys
from optparse import OptionParser, make_option
from common import Credentials, Launchpad, translate_service
from common import LEVEL, translate_api_web, approve_application
class CmdOptions(OptionParser):
USAGE = (
"\t%prog create -c <consumer> [--email <email> --password <password>] [--service <staging|edge>]\n"
)
OPTIONS = (
make_option("-c", "--consumer", action="store", type="string",
dest="consumer", default=None),
make_option("-e", "--email", action="store", type="string",
dest="email", default=None),
make_option("-p", "--password", action="store", type="string",
dest="password", default=None),
make_option("-s", "--service", action="store", type="string",
dest="service", default="staging"),
make_option("--cache", action="store", type="string",
dest="cache", default=None),
make_option("-o", action="store", type="string",
dest="output", default=None),
make_option("-l", "--level", action="store", type="int",
dest="level", default=0,
help="integer representing the access-level (default: 0), mappping: %s" %LEVEL),
)
TOOLS = {
"create": ( ("consumer",),
("email", "password", "service", "cache", "output",
"level")),
"list": (tuple(), ("service", )),
}
def __init__(self):
OptionParser.__init__(self, option_list=self.OPTIONS)
self.set_usage(self.USAGE)
def parse_args(self, args=None, values=None):
options, args = OptionParser.parse_args(self, args, values)
given_options = set(i for i, k in self.defaults.iteritems() if not getattr(options, i) == k)
if not args:
self.error("Please define a sub-tool you would like to use")
if not len(args) == 1:
self.error("Only one sub-tool allowed")
else:
tool = args.pop()
if not tool in self.TOOLS:
self.error("Unknown tool '%s'" %tool)
needed_options = set(self.TOOLS[tool][0]) - given_options
if needed_options:
self.error("please define the following options: %s" %", ".join(needed_options))
optional_options = given_options - set(sum(self.TOOLS[tool], ()))
if optional_options:
self.error("the following options are not allowed for this tool: %s" %", ".join(optional_options))
options.service = translate_service(options.service)
if options.level in LEVEL:
options.level = LEVEL[options.level]
elif options.level.upper() in LEVEL.values():
options.level = options.level.upper()
else:
self.error("unknown access-level '%s', level must be in %s" %(options.level, self.LEVEL))
return tool, options
def create_credentials(options):
if options.password and options.email:
# use hack
credentials = Credentials(options.consumer)
credentials = approve_application(credentials, options.email,
options.password, options.level,
translate_api_web(options.service), None)
else:
launchpad = Launchpad.get_token_and_login(options.consumer,
options.service, options.cache)
credentials = launchpad.credentials
if options.output:
f = file(options.output, "w")
else:
f = sys.stdout
credentials.save(f)
return
def list_tokens(options):
print "Not implemented yet"
print "To get a list of your tokens, please visit %speople/+me/+oauth-tokens" %translate_api_web(options.service)
return 1
def main():
cmdoptions = CmdOptions()
tool, options = cmdoptions.parse_args()
if tool == "create":
return create_credentials(options)
elif tool == "list":
return list_tokens(options)
if __name__ == "__main__":
sys.exit(main())