Merge lp:~jelmer/lptools/import-lp-shell into lp:lptools

Proposed by Jelmer Vernooij
Status: Merged
Merged at revision: 22
Proposed branch: lp:~jelmer/lptools/import-lp-shell
Merge into: lp:lptools
Diff against target: 174 lines (+164/-0)
2 files modified
bin/lp-shell (+112/-0)
doc/lp-shell.1 (+52/-0)
To merge this branch: bzr merge lp:~jelmer/lptools/import-lp-shell
Reviewer Review Type Date Requested Status
lptools Hackers Pending
Review via email: mp+72565@code.launchpad.net

Description of the change

Import lp-shell from ubuntu-dev-tools.

This does not yet lptools.config.get_launchpad as it has some other requirements (supports anonymous access, specifying lp instance to use).

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'bin/lp-shell'
2--- bin/lp-shell 1970-01-01 00:00:00 +0000
3+++ bin/lp-shell 2011-08-23 13:01:51 +0000
4@@ -0,0 +1,112 @@
5+#!/usr/bin/python
6+
7+# Open an interactive launchpadlib Python shell.
8+# It supports all known LP service instances and API versions. The login
9+# can optionally happen anonymously.
10+
11+# Author: Martin Pitt <martin.pitt@ubuntu.com>
12+# Copyright: (C) 2010 Canonical Ltd.
13+#
14+# This package is free software; you can redistribute it and/or modify
15+# it under the terms of the GNU General Public License as published by
16+# the Free Software Foundation, at version 2.
17+#
18+# This package is distributed in the hope that it will be useful,
19+# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+# GNU General Public License for more details.
22+
23+import sys
24+import code
25+from optparse import OptionParser
26+
27+from launchpadlib.launchpad import Launchpad
28+from launchpadlib.uris import lookup_service_root
29+
30+def main():
31+ instance = 'production'
32+ valid_api_versions = ('beta', '1.0', 'devel')
33+ api_version = '1.0'
34+
35+ usage = 'Usage: %prog [-a] [instance] [LP API version]'
36+ opt_parser = OptionParser(usage)
37+ opt_parser.add_option('-a', action='store_true',
38+ dest='anonymous', default=False,
39+ help='Login anonymously into LP.')
40+ opt_parser.add_option('--ipython',action='store_const',
41+ dest='shell',const='ipython', default="ipython",
42+ help='Use ipython shell (default).')
43+ opt_parser.add_option('--python',action='store_const',
44+ dest='shell',const='python',
45+ help='Use python shell.')
46+
47+
48+ (options, args) = opt_parser.parse_args()
49+
50+ if len(args) >= 1:
51+ try:
52+ instance = lookup_service_root(args[0])
53+ except ValueError, err:
54+ print 'E: %s' % (err)
55+ print 'I: Falling back to "production".'
56+
57+ if len(args) >= 2:
58+ if args[1] in valid_api_versions:
59+ api_version = args[1]
60+ else:
61+ print 'E: "%s" is not a valid LP API version.' % (args[1])
62+ print 'I: Falling back to "1.0".'
63+
64+ if options.anonymous:
65+ launchpad = Launchpad.login_anonymously('lp-shell', instance,
66+ version=api_version)
67+ banner = ('Connected anonymously to LP service "%s" with API version '
68+ '"%s":' % (instance, api_version))
69+ else:
70+ launchpad = Launchpad.login_with('lp-shell', instance,
71+ version=api_version)
72+ banner = 'Connected to LP service "%s" with API version "%s":' % \
73+ (instance, api_version)
74+
75+ banner += '\nNote: LP can be accessed through the "lp" object.'
76+
77+ sh = None
78+ if options.shell == "ipython":
79+ try:
80+ try: # ipython >= 0.11
81+ from IPython.frontend.terminal.embed import InteractiveShellEmbed
82+ sh = InteractiveShellEmbed(banner2=banner, user_ns={'lp': launchpad})
83+ except ImportError: # ipython < 0.11
84+ # pylint does not handle nested try-except, disable import error
85+ # pylint: disable-msg=E0611
86+ from IPython.Shell import IPShellEmbed
87+ sh = IPShellEmbed(argv=[], user_ns={'lp': launchpad})
88+ sh.set_banner(sh.IP.BANNER + '\n' + banner)
89+ sh.excepthook = sys.__excepthook__
90+ except ImportError:
91+ print "E: ipython not available. Using normal python shell."
92+
93+ if sh:
94+ sh()
95+ else:
96+ class CompleterConsole(code.InteractiveConsole):
97+ def __init__(self):
98+ local = {'lp': launchpad}
99+ code.InteractiveConsole.__init__(self, locals=local)
100+ try:
101+ import readline
102+ except ImportError:
103+ print 'I: readline module not available.'
104+ else:
105+ import rlcompleter
106+ readline.parse_and_bind("tab: complete")
107+
108+ # Disable default apport hook, as lp-shell is intended for interactive use
109+ # and thus exceptions often bubble up to the top level.
110+ sys.excepthook = sys.__excepthook__
111+
112+ console = CompleterConsole()
113+ console.interact(banner)
114+
115+if __name__ == '__main__':
116+ main()
117
118=== added directory 'doc'
119=== added file 'doc/lp-shell.1'
120--- doc/lp-shell.1 1970-01-01 00:00:00 +0000
121+++ doc/lp-shell.1 2011-08-23 13:01:51 +0000
122@@ -0,0 +1,52 @@
123+.TH lp-shell "1" "27 March 2010" "lptools"
124+.SH NAME
125+lp\-shell \- Open an interactive launchpadlib shell.
126+
127+.SH SYNOPSIS
128+.B lp\-shell
129+.RB [ \-a ]
130+.RB [ \-\-python ]
131+.RB [ \-\-ipython ]
132+.RI [ service ]
133+.RI [ "LP API version" ]
134+
135+.SH DESCRIPTION
136+.B lp\-shell
137+opens an interactive Python shell with a launchpadlib.Launchpad object "lp"
138+which is ready for use.
139+
140+It authenticates against Launchpad with the consumer name "udt-lp-shell". When
141+using \fBlp\-shell\fR with the \fB\-a\fR option it will use the anonymous login
142+from launchpadlib.Launchpad.
143+
144+By default \fBlp\-shell\fR connects to the "\fIproduction\fR" Launchpad service
145+using the "\fI1.0\fR" LP API version.
146+
147+If you want to connect to another Launchpad service, call \fBlp\-shell\fR with
148+the service name as the second argument. \fBlp\-shell\fR supports all services
149+known by launchpadlib Python module.
150+Currently known are (list can be incomplete or outdated): "production",
151+"staging", "dogfood".
152+
153+A different LP API version can be selected by passing the API version to use as
154+the third argument. Current supported are: "beta", "1.0" and "devel".
155+
156+.SH OPTIONS
157+.TP
158+.B \-a
159+Login anonymously into Launchpad.
160+
161+.TP
162+.B \-\-ipython
163+Use an ipython shell if available (default).
164+
165+.TP
166+.B \-\-python
167+Use a regular python shell.
168+
169+.SH AUTHORS
170+.B lp\-shell
171+was written by Martin Pitt <martin.pitt@ubuntu.com>.
172+.PP
173+It is released under the terms of the GNU General Public License, version 2
174+or (at your option) any later version.

Subscribers

People subscribed via source and target branches