Merge lp:~blr/launchpadlib/auth-token-script into lp:launchpadlib

Proposed by Kit Randel
Status: Needs review
Proposed branch: lp:~blr/launchpadlib/auth-token-script
Merge into: lp:launchpadlib
Diff against target: 101 lines (+56/-7)
2 files modified
setup.py (+3/-0)
src/launchpadlib/bin/launchpad-request-token (+53/-7)
To merge this branch: bzr merge lp:~blr/launchpadlib/auth-token-script
Reviewer Review Type Date Requested Status
LAZR Developers Pending
Review via email: mp+301600@code.launchpad.net

Commit message

* Teach launchpad-request-token to also generate access tokens.
* Install launchpad-request-token.

To post a comment you must log in.

Unmerged revisions

157. By Kit Randel on 2016-07-31

Whitespace

156. By Kit Randel on 2016-07-31

Correct usage.

155. By Kit Randel on 2016-07-31

Call print func.

154. By Kit Randel on 2016-07-31

Remove unused import.

153. By Kit Randel on 2016-07-31

Install launchpad-request-token script.

152. By Kit Randel on 2016-07-31

Teach launchpad-request-token to also generate access tokens.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'setup.py'
2--- setup.py 2014-07-04 14:55:48 +0000
3+++ setup.py 2016-07-31 23:28:12 +0000
4@@ -76,5 +76,8 @@
5 "License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)",
6 "Operating System :: OS Independent",
7 "Programming Language :: Python"],
8+ scripts=[
9+ 'src/launchpadlib/bin/launchpad-request-token',
10+ ],
11 test_suite='launchpadlib.tests',
12 )
13
14=== modified file 'src/launchpadlib/bin/launchpad-request-token'
15--- src/launchpadlib/bin/launchpad-request-token 2009-10-27 15:58:44 +0000
16+++ src/launchpadlib/bin/launchpad-request-token 2016-07-31 23:28:12 +0000
17@@ -1,6 +1,6 @@
18-#!/usr/bin/python
19+#!/usr/bin/env python
20
21-# Copyright 2009 Canonical Ltd.
22+# Copyright 2016 Canonical Ltd.
23
24 # This file is part of launchpadlib.
25 #
26@@ -25,10 +25,46 @@
27 __metaclass__ = type
28
29 from optparse import OptionParser
30+import time
31+
32 from launchpadlib.apps import RequestTokenApp
33+from launchpadlib.credentials import Credentials
34+from launchpadlib.uris import lookup_service_root
35+from lazr.restfulclient.errors import HTTPError
36+
37+
38+def print_local_settings(web_root, key, token, secret):
39+ """Print access token settings in YAML format."""
40+ print("\nAn access token has been generated with the following settings:"
41+ "\n\n"
42+ "LP_API_URL = '{api_url}'\n"
43+ "LP_API_CONSUMER_KEY = '{key}'\n"
44+ "LP_API_TOKEN = '{token}'\n"
45+ "LP_API_TOKEN_SECRET = '{secret}'").format(
46+ api_url=lookup_service_root(web_root),
47+ key=key,
48+ token=token,
49+ secret=secret)
50+
51+
52+def create_access_token(consumer_name, web_root, context=None):
53+ credentials = Credentials(consumer_name)
54+ authorise_url = credentials.get_request_token(
55+ web_root=web_root, context=context)
56+ print('\nPlease ensure you are logged in and visit {} to '
57+ 'authorise this request...'.format(authorise_url))
58+ while True:
59+ try:
60+ credentials.exchange_request_token_for_access_token(
61+ web_root=web_root)
62+ return credentials.access_token
63+ except HTTPError:
64+ time.sleep(0.5)
65+
66
67 parser = OptionParser()
68-parser.usage = "%prog CONSUMER_NAME [-r SITE NAME] [-c CONTEXT]"
69+parser.usage = ("%prog CONSUMER_NAME [-s SITE NAME]"
70+ "[-c CONTEXT] [--generate-access-token]")
71 parser.add_option("-s", "--site", dest="web_root",
72 help=("The name of the Launchpad site to ask for a request "
73 "token (default: %default). This can also be the "
74@@ -36,13 +72,23 @@
75 metavar="SITE", default="staging")
76 parser.add_option("-c", "--context", dest="context",
77 help="Restrict the token to a specific context "
78- "(example: firefox)", metavar="CONTEXT", default="")
79+ "(example: firefox)", metavar="CONTEXT", default=None)
80+parser.add_option("--generate-access-token", action="store_true",
81+ dest="access_token",
82+ help="Generate an access token.")
83+
84
85 if __name__ == '__main__':
86 (options, args) = parser.parse_args()
87 if len(args) < 1:
88 parser.error("No consumer name supplied")
89 consumer_name = args[0]
90- app = RequestTokenApp(
91- options.web_root, consumer_name, options.context)
92- print app.run()
93+ if options.access_token:
94+ access_token = create_access_token(
95+ consumer_name, options.web_root, options.context)
96+ print_local_settings(options.web_root, consumer_name,
97+ access_token.key, access_token.secret)
98+ else:
99+ app = RequestTokenApp(options.web_root, consumer_name, options.context)
100+ request_token = app.run()
101+ print(request_token)

Subscribers

People subscribed via source and target branches