| Line | Revision | Contents |
| 1 | 248.1.15 | #!/usr/bin/python |
| 2 | # -*- coding: utf-8 -*- |
|
| 3 | # |
|
| 4 | # Copyright (C) 2009 Markus Korn <thekorn@gmx.de> |
|
| 5 | # |
|
| 6 | # ################################################################## |
|
| 7 | # |
|
| 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. |
|
| 12 | # |
|
| 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. |
|
| 17 | # |
|
| 18 | # See file /usr/share/common-licenses/GPL for more details. |
|
| 19 | # |
|
| 20 | # ################################################################## |
|
| 21 | ||
| 22 | 272 | import os |
| 23 | 248.1.15 | import sys |
| 24 | from optparse import OptionParser, make_option |
|
| 25 | 302 | from ubuntutools.lp.libsupport import * |
| 26 | 248.1.15 | |
| 27 | class CmdOptions(OptionParser): |
|
| 28 | |
|
| 29 | USAGE = ( |
|
| 30 | 406 | "\t%prog create -c <consumer> [--email <email> --password <password>] [--service <staging|edge>]" |
| 31 | 248.1.15 | ) |
| 32 | |
|
| 33 | OPTIONS = ( |
|
| 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 | 272 | dest="service", default="edge"), |
| 42 | 248.1.15 | 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 | 267 | help="integer representing the access-level (default: 0), mapping: %s" %LEVEL), |
| 49 | 248.1.15 | ) |
| 50 | |
|
| 51 | TOOLS = { |
|
| 52 | "create": ( ("consumer",), |
|
| 53 | ("email", "password", "service", "cache", "output", |
|
| 54 | "level")), |
|
| 55 | "list": (tuple(), ("service", )), |
|
| 56 | } |
|
| 57 | |
|
| 58 | def __init__(self): |
|
| 59 | OptionParser.__init__(self, option_list=self.OPTIONS) |
|
| 60 | self.set_usage(self.USAGE) |
|
| 61 | |
|
| 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) |
|
| 65 | |
|
| 66 | if not args: |
|
| 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") |
|
| 70 | else: |
|
| 71 | tool = args.pop() |
|
| 72 | if not tool in self.TOOLS: |
|
| 73 | self.error("Unknown tool '%s'" %tool) |
|
| 74 | needed_options = set(self.TOOLS[tool][0]) - given_options |
|
| 75 | if needed_options: |
|
| 76 | 272 | self.error("Please define the following options: %s" %", ".join(needed_options)) |
| 77 | 248.1.15 | optional_options = given_options - set(sum(self.TOOLS[tool], ())) |
| 78 | if optional_options: |
|
| 79 | 272 | self.error("The following options are not allowed for this tool: %s" %", ".join(optional_options)) |
| 80 | 248.1.15 | 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() |
|
| 85 | else: |
|
| 86 | 272 | self.error("Unknown access-level '%s', level must be in %s" %(options.level, self.LEVEL)) |
| 87 | 248.1.15 | return tool, options |
| 88 | |
|
| 89 | def create_credentials(options): |
|
| 90 | if options.password and options.email: |
|
| 91 | # use hack |
|
| 92 | credentials = Credentials(options.consumer) |
|
| 93 | credentials = approve_application(credentials, options.email, |
|
| 94 | options.password, options.level, |
|
| 95 | translate_api_web(options.service), None) |
|
| 96 | else: |
|
| 97 | launchpad = Launchpad.get_token_and_login(options.consumer, |
|
| 98 | options.service, options.cache) |
|
| 99 | credentials = launchpad.credentials |
|
| 100 | 272 | |
| 101 | 248.1.15 | if options.output: |
| 102 | 276 | filepath = options.output |
| 103 | 248.1.15 | else: |
| 104 | 276 | credentialsDir = os.path.expanduser("~/.cache/lp_credentials") |
| 105 | if not os.path.isdir(credentialsDir): |
|
| 106 | 309 | os.makedirs(credentialsDir) |
| 107 | 276 | os.chmod(credentialsDir, 0700) |
| 108 | filepath = os.path.expanduser("%s/%s-%s.txt" % \ |
|
| 109 | (credentialsDir, options.consumer, str(options.level).lower())) |
|
| 110 | 272 | |
| 111 | 276 | f = open(filepath, "w") |
| 112 | # Make credentials file non-world readable. |
|
| 113 | os.chmod(filepath, 0600) |
|
| 114 | 248.1.15 | credentials.save(f) |
| 115 | 276 | f.close() |
| 116 | ||
| 117 | 329 | print "Credentials successfully written to %s." % filepath |
| 118 | 272 | |
| 119 | 248.1.15 | return |
| 120 | ||
| 121 | def list_tokens(options): |
|
| 122 | 272 | print "Not implemented yet." |
| 123 | 248.1.15 | print "To get a list of your tokens, please visit %speople/+me/+oauth-tokens" %translate_api_web(options.service) |
| 124 | return 1 |
|
| 125 | |
|
| 126 | def main(): |
|
| 127 | cmdoptions = CmdOptions() |
|
| 128 | tool, options = cmdoptions.parse_args() |
|
| 129 | if tool == "create": |
|
| 130 | return create_credentials(options) |
|
| 131 | elif tool == "list": |
|
| 132 | return list_tokens(options) |
|
| 133 | ||
| 134 | if __name__ == "__main__": |
|
| 135 | sys.exit(main()) |
Loggerhead 1.10 is a web-based interface for Bazaar branches