| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# Open an interactive launchpadlib Python shell. |
| 4 |
# It supports all known LP service instances and API versions. The login |
| 5 |
# can optionally happen anonymously. |
| 6 |
|
| 7 |
# Author: Martin Pitt <[email protected]> |
| 8 |
# Copyright: (C) 2010 Canonical Ltd. |
| 9 |
# |
| 10 |
# This package is free software; you can redistribute it and/or modify |
| 11 |
# it under the terms of the GNU General Public License as published by |
| 12 |
# the Free Software Foundation, at version 2. |
| 13 |
# |
| 14 |
# This package is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
|
| 19 |
import sys |
| 20 |
import code |
| 21 |
from optparse import OptionParser |
| 22 |
|
| 23 |
from launchpadlib.launchpad import Launchpad |
| 24 |
from launchpadlib.uris import lookup_service_root |
| 25 |
|
| 26 |
def run_command(command, lp): |
| 27 |
exec(command) |
| 28 |
|
| 29 |
def main(): |
| 30 |
instance = 'production' |
| 31 |
valid_api_versions = ('beta', '1.0', 'devel') |
| 32 |
api_version = '1.0' |
| 33 |
|
| 34 |
usage = 'Usage: %prog [-a] [instance] [LP API version]' |
| 35 |
opt_parser = OptionParser(usage) |
| 36 |
opt_parser.add_option('-a', action='store_true', |
| 37 |
dest='anonymous', default=False, |
| 38 |
help='Login anonymously into LP.') |
| 39 |
opt_parser.add_option('-c', type=str, |
| 40 |
dest='command', default=None, |
| 41 |
help='Run code passed as string.') |
| 42 |
opt_parser.add_option('--ipython', action='store_const', |
| 43 |
dest='shell', const='ipython', default="ipython", |
| 44 |
help='Use ipython shell (default).') |
| 45 |
opt_parser.add_option('--python', action='store_const', |
| 46 |
dest='shell', const='python', |
| 47 |
help='Use python shell.') |
| 48 |
|
| 49 |
|
| 50 |
(options, args) = opt_parser.parse_args() |
| 51 |
|
| 52 |
if len(args) >= 1: |
| 53 |
try: |
| 54 |
instance = lookup_service_root(args[0]) |
| 55 |
except ValueError as err: |
| 56 |
print('E: %s' % (err)) |
| 57 |
print('I: Falling back to "production".') |
| 58 |
|
| 59 |
if len(args) >= 2: |
| 60 |
if args[1] in valid_api_versions: |
| 61 |
api_version = args[1] |
| 62 |
else: |
| 63 |
print('E: "%s" is not a valid LP API version.' % (args[1])) |
| 64 |
print('I: Falling back to "1.0".') |
| 65 |
|
| 66 |
if options.anonymous: |
| 67 |
launchpad = Launchpad.login_anonymously('lp-shell', instance, |
| 68 |
version=api_version) |
| 69 |
banner = ('Connected anonymously to LP service "%s" with API version ' |
| 70 |
'"%s":' % (instance, api_version)) |
| 71 |
else: |
| 72 |
launchpad = Launchpad.login_with('lp-shell', instance, |
| 73 |
version=api_version) |
| 74 |
banner = 'Connected to LP service "%s" with API version "%s":' % \ |
| 75 |
(instance, api_version) |
| 76 |
|
| 77 |
if options.command is not None: |
| 78 |
run_command(options.command, launchpad) |
| 79 |
return |
| 80 |
|
| 81 |
banner += '\nNote: LP can be accessed through the "lp" object.' |
| 82 |
|
| 83 |
sh = None |
| 84 |
if options.shell == "ipython": |
| 85 |
try: |
| 86 |
try: # ipython >= 0.11 |
| 87 |
from IPython.terminal.embed import InteractiveShellEmbed |
| 88 |
sh = InteractiveShellEmbed(banner2=banner, user_ns={'lp': launchpad}) |
| 89 |
except ImportError: # ipython < 0.11 |
| 90 |
# pylint does not handle nested try-except, disable import error |
| 91 |
# pylint: disable-msg=E0611 |
| 92 |
from IPython.Shell import IPShellEmbed |
| 93 |
sh = IPShellEmbed(argv=[], user_ns={'lp': launchpad}) |
| 94 |
sh.set_banner(sh.IP.BANNER + '\n' + banner) |
| 95 |
sh.excepthook = sys.__excepthook__ |
| 96 |
except ImportError: |
| 97 |
print("E: ipython not available. Using normal python shell.") |
| 98 |
|
| 99 |
if sh: |
| 100 |
sh() |
| 101 |
else: |
| 102 |
class CompleterConsole(code.InteractiveConsole): |
| 103 |
def __init__(self): |
| 104 |
local = {'lp': launchpad} |
| 105 |
code.InteractiveConsole.__init__(self, locals=local) |
| 106 |
try: |
| 107 |
import readline |
| 108 |
except ImportError: |
| 109 |
print('I: readline module not available.') |
| 110 |
else: |
| 111 |
import rlcompleter |
| 112 |
readline.parse_and_bind("tab: complete") |
| 113 |
|
| 114 |
# Disable default apport hook, as lp-shell is intended for interactive use |
| 115 |
# and thus exceptions often bubble up to the top level. |
| 116 |
sys.excepthook = sys.__excepthook__ |
| 117 |
|
| 118 |
console = CompleterConsole() |
| 119 |
console.interact(banner) |
| 120 |
|
| 121 |
if __name__ == '__main__': |
| 122 |
main() |