2
# -*- coding: utf-8 -*-
4
# Copyright (C) 2009 Markus Korn <thekorn@gmx.de>
6
# ##################################################################
8
# This program is free software; you can redistribute it and/or
9
# modify it under the terms of the GNU General Public License
10
# as published by the Free Software Foundation; either version 3
11
# of the License, or (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# See file /usr/share/common-licenses/GPL for more details.
20
# ##################################################################
24
from optparse import OptionParser, make_option
25
from ubuntutools.lp.libsupport import *
27
class CmdOptions(OptionParser):
30
"\t%prog create -c <consumer> [--email <email> --password <password>] [--service <staging|edge>]"
34
make_option("-c", "--consumer", action="store", type="string",
35
dest="consumer", default=None),
36
make_option("-e", "--email", action="store", type="string",
37
dest="email", default=None),
38
make_option("-p", "--password", action="store", type="string",
39
dest="password", default=None),
40
make_option("-s", "--service", action="store", type="string",
41
dest="service", default="edge"),
42
make_option("--cache", action="store", type="string",
43
dest="cache", default=None),
44
make_option("-o", action="store", type="string",
45
dest="output", default=None),
46
make_option("-l", "--level", action="store", type="int",
47
dest="level", default=0,
48
help="integer representing the access-level (default: 0), mapping: %s" %LEVEL),
52
"create": ( ("consumer",),
53
("email", "password", "service", "cache", "output",
55
"list": (tuple(), ("service", )),
59
OptionParser.__init__(self, option_list=self.OPTIONS)
60
self.set_usage(self.USAGE)
62
def parse_args(self, args=None, values=None):
63
options, args = OptionParser.parse_args(self, args, values)
64
given_options = set(i for i, k in self.defaults.iteritems() if not getattr(options, i) == k)
67
self.error("Please define a sub-tool you would like to use")
68
if not len(args) == 1:
69
self.error("Only one sub-tool allowed")
72
if not tool in self.TOOLS:
73
self.error("Unknown tool '%s'" %tool)
74
needed_options = set(self.TOOLS[tool][0]) - given_options
76
self.error("Please define the following options: %s" %", ".join(needed_options))
77
optional_options = given_options - set(sum(self.TOOLS[tool], ()))
79
self.error("The following options are not allowed for this tool: %s" %", ".join(optional_options))
80
options.service = translate_service(options.service)
81
if options.level in LEVEL:
82
options.level = LEVEL[options.level]
83
elif options.level.upper() in LEVEL.values():
84
options.level = options.level.upper()
86
self.error("Unknown access-level '%s', level must be in %s" %(options.level, self.LEVEL))
89
def create_credentials(options):
90
if options.password and options.email:
92
credentials = Credentials(options.consumer)
93
credentials = approve_application(credentials, options.email,
94
options.password, options.level,
95
translate_api_web(options.service), None)
97
launchpad = Launchpad.get_token_and_login(options.consumer,
98
options.service, options.cache)
99
credentials = launchpad.credentials
102
filepath = options.output
104
credentialsDir = os.path.expanduser("~/.cache/lp_credentials")
105
if not os.path.isdir(credentialsDir):
106
os.makedirs(credentialsDir)
107
os.chmod(credentialsDir, 0700)
108
filepath = os.path.expanduser("%s/%s-%s.txt" % \
109
(credentialsDir, options.consumer, str(options.level).lower()))
111
f = open(filepath, "w")
112
# Make credentials file non-world readable.
113
os.chmod(filepath, 0600)
117
print "Credentials successfully written to %s." % filepath
121
def list_tokens(options):
122
print "Not implemented yet."
123
print "To get a list of your tokens, please visit %speople/+me/+oauth-tokens" %translate_api_web(options.service)
127
cmdoptions = CmdOptions()
128
tool, options = cmdoptions.parse_args()
130
return create_credentials(options)
132
return list_tokens(options)
134
if __name__ == "__main__":