2010-03-27 20:19:58 +01:00
|
|
|
#!/usr/bin/python
|
2010-01-13 14:32:07 +01:00
|
|
|
|
2010-03-27 20:19:58 +01:00
|
|
|
# Open an interactive launchpadlib Python shell.
|
|
|
|
# It supports all known LP service instances and API versions. The login
|
|
|
|
# can optionally happen anonymously.
|
2010-01-13 14:32:07 +01:00
|
|
|
|
|
|
|
# Author: Martin Pitt <martin.pitt@ubuntu.com>
|
|
|
|
# Copyright: (C) 2010 Canonical Ltd.
|
|
|
|
#
|
|
|
|
# This package is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, at version 2.
|
2010-12-03 00:06:43 +01:00
|
|
|
#
|
2010-01-13 14:32:07 +01:00
|
|
|
# This package is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
2010-03-27 20:19:58 +01:00
|
|
|
import sys
|
|
|
|
import code
|
|
|
|
from optparse import OptionParser
|
2010-01-13 14:32:07 +01:00
|
|
|
|
2010-03-18 11:03:55 +01:00
|
|
|
from launchpadlib.launchpad import Launchpad
|
|
|
|
from launchpadlib.uris import lookup_service_root
|
2010-01-13 14:32:07 +01:00
|
|
|
|
2010-12-03 13:54:28 +01:00
|
|
|
instance = 'production'
|
2010-03-27 20:19:58 +01:00
|
|
|
valid_api_versions = ('beta', '1.0', 'devel')
|
|
|
|
api_version = '1.0'
|
|
|
|
|
|
|
|
usage = 'Usage: %prog [-a] [instance] [LP API version]'
|
|
|
|
optParser = OptionParser(usage)
|
|
|
|
optParser.add_option('-a', action='store_true',
|
|
|
|
dest='anonymous', default=False,
|
|
|
|
help='Login anonymously into LP.')
|
|
|
|
|
|
|
|
(options, args) = optParser.parse_args()
|
|
|
|
|
|
|
|
if len(args) >= 1:
|
2010-03-18 11:03:55 +01:00
|
|
|
try:
|
2010-03-27 20:19:58 +01:00
|
|
|
instance = lookup_service_root(args[0])
|
2010-03-18 11:03:55 +01:00
|
|
|
except ValueError, err:
|
|
|
|
print 'E: %s' % (err)
|
2010-12-03 13:54:28 +01:00
|
|
|
print 'I: Falling back to "production".'
|
2010-03-27 20:19:58 +01:00
|
|
|
|
|
|
|
if len(args) >= 2:
|
|
|
|
if args[1] in valid_api_versions:
|
|
|
|
api_version = args[1]
|
|
|
|
else:
|
|
|
|
print 'E: "%s" is not a valid LP API version.' % (args[1])
|
|
|
|
print 'I: Falling back to "1.0".'
|
2010-01-13 14:32:07 +01:00
|
|
|
|
2010-03-27 20:19:58 +01:00
|
|
|
if options.anonymous:
|
2010-04-13 18:43:51 +02:00
|
|
|
lp = Launchpad.login_anonymously('udt-lp-shell', instance, version=api_version)
|
2010-03-27 20:19:58 +01:00
|
|
|
banner = 'Connected anonymously to LP service "%s" with API version "%s":' % (
|
|
|
|
instance, api_version)
|
|
|
|
else:
|
2010-04-13 18:43:51 +02:00
|
|
|
lp = Launchpad.login_with('udt-lp-shell', instance, version=api_version)
|
2010-03-27 20:19:58 +01:00
|
|
|
banner = 'Connected to LP service "%s" with API version "%s":' % (
|
|
|
|
instance, api_version)
|
|
|
|
|
|
|
|
banner += '\nNote: LP can be accessed through the "lp" object.'
|
|
|
|
|
|
|
|
class CompleterConsole(code.InteractiveConsole):
|
|
|
|
def __init__(self):
|
|
|
|
local = {'lp': lp}
|
|
|
|
code.InteractiveConsole.__init__(self,
|
|
|
|
locals=local)
|
|
|
|
try:
|
|
|
|
import readline
|
|
|
|
except ImportError:
|
|
|
|
print 'I: readline module not available.'
|
|
|
|
else:
|
|
|
|
import rlcompleter
|
|
|
|
readline.parse_and_bind("tab: complete")
|
2010-02-24 10:41:55 +00:00
|
|
|
|
|
|
|
# Disable default apport hook, as lp-shell is intended for interactive use
|
|
|
|
# and thus exceptions often bubble up to the top level.
|
|
|
|
sys.excepthook = sys.__excepthook__
|
2010-03-27 20:19:58 +01:00
|
|
|
|
|
|
|
console = CompleterConsole()
|
|
|
|
console.interact(banner)
|