File: lp-shell

package info (click to toggle)
lptools 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 328 kB
  • sloc: python: 1,552; makefile: 2
file content (122 lines) | stat: -rwxr-xr-x 4,605 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python

# Open an interactive launchpadlib Python shell.
# It supports all known LP service instances and API versions. The login
# can optionally happen anonymously.

# 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.
#
# 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.

import sys
import code
from optparse import OptionParser

from launchpadlib.launchpad import Launchpad
from launchpadlib.uris import lookup_service_root

def run_command(command, lp):
    exec(command)

def main():
    instance = 'production'
    valid_api_versions = ('beta', '1.0', 'devel')
    api_version = '1.0'

    usage = 'Usage: %prog [-a] [instance] [LP API version]'
    opt_parser = OptionParser(usage)
    opt_parser.add_option('-a', action='store_true',
                          dest='anonymous', default=False,
                          help='Login anonymously into LP.')
    opt_parser.add_option('-c', type=str,
                          dest='command', default=None,
                          help='Run code passed as string.')
    opt_parser.add_option('--ipython', action='store_const',
                          dest='shell', const='ipython', default="ipython",
                          help='Use ipython shell (default).')
    opt_parser.add_option('--python', action='store_const',
                          dest='shell', const='python',
                          help='Use python shell.')


    (options, args) = opt_parser.parse_args()

    if len(args) >= 1:
        try:
            instance = lookup_service_root(args[0])
        except ValueError, err:
            print 'E: %s' % (err)
            print 'I: Falling back to "production".'

    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".'

    if options.anonymous:
        launchpad = Launchpad.login_anonymously('lp-shell', instance,
                                                version=api_version)
        banner = ('Connected anonymously to LP service "%s" with API version '
                  '"%s":' % (instance, api_version))
    else:
        launchpad = Launchpad.login_with('lp-shell', instance,
                                         version=api_version)
        banner = 'Connected to LP service "%s" with API version "%s":' % \
                 (instance, api_version)

    if options.command is not None:
        run_command(options.command, launchpad)
        return

    banner += '\nNote: LP can be accessed through the "lp" object.'

    sh = None
    if options.shell == "ipython":
        try:
            try: # ipython >= 0.11
                from IPython.frontend.terminal.embed import InteractiveShellEmbed
                sh = InteractiveShellEmbed(banner2=banner, user_ns={'lp': launchpad})
            except ImportError: # ipython < 0.11
                # pylint does not handle nested try-except, disable import error
                # pylint: disable-msg=E0611
                from IPython.Shell import IPShellEmbed
                sh = IPShellEmbed(argv=[], user_ns={'lp': launchpad})
                sh.set_banner(sh.IP.BANNER + '\n' + banner)
            sh.excepthook = sys.__excepthook__
        except ImportError:
            print "E: ipython not available. Using normal python shell."

    if sh:
        sh()
    else:
        class CompleterConsole(code.InteractiveConsole):
            def __init__(self):
                local = {'lp': launchpad}
                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")

        # 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__

        console = CompleterConsole()
        console.interact(banner)

if __name__ == '__main__':
    main()