Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d868b60eda4a643c3bf7d6a7e9b187d76c49499e (plain) (blame)
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
# *****************************************************************************
# * Copyright (c) 2011, 2013 Wind River Systems, Inc. and others.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http://www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# *     Wind River Systems - initial API and implementation
# *****************************************************************************

"""
Simple interactive shell for TCF.  This is basically a Python interpreter with
a few TCF extensions.

Usage:
    python -m tcf.shell

Commands:
    peers              - Print discovered peers
    connect(params)    - Connect to TCF peer, params =
                         "<protocol>:<host>:<port>"
    cmd.<service>.<command<(args)
                       - Send command to remote service and return result
    disconnect         - Disconnect from peer
    events.record(<service>)
                       - Start recording events for service
    events             - Print last recorded events
    events.stop([<service>])
                       - Stop recording for service or for all services
"""

import code
import os
import sys


try:
    import tcf
except ImportError:
    # add parent dir to path
    sys.path.insert(0, os.path.dirname(os.getcwd()))
    import tcf

from tcf.util import sync, event
from tcf import protocol, channel


class print_peers:
    "Print list of discovered peers"
    def __call__(self):
        return tcf.peers()

    def __repr__(self):
        peers = tcf.peers()
        return '\n'.join(map(lambda p: "%s, %s" % (p.getID(), p.getName()),
                             peers.values()))


class Shell(code.InteractiveConsole, protocol.ChannelOpenListener,
            channel.ChannelListener):
    def __init__(self):
        locals = {  # @ReservedAssignment
            "connect" : tcf.connect,
            "peers" : print_peers()
        }
        protocol.startEventQueue()
        protocol.startDiscovery()
        protocol.invokeAndWait(protocol.addChannelOpenListener, self)
        code.InteractiveConsole.__init__(self, locals)

    def interact(self, banner=None):
        try:
            try:
                ps1 = sys.ps1  # @UndefinedVariable
            except AttributeError:
                ps1 = None
            sys.ps1 = "tcf> "
            super(Shell, self).interact(banner)
        finally:
            if ps1:
                sys.ps1 = ps1
            else:
                del sys.ps1
            protocol.invokeLater(protocol.removeChannelOpenListener, self)
            protocol.shutdownDiscovery()
            protocol.getEventQueue().shutdown()

    def onChannelOpen(self, channel):
        wrapper = sync.DispatchWrapper(channel)
        self.locals["channel"] = wrapper
        self.locals["disconnect"] = wrapper.close
        self.locals["cmd"] = sync.CommandControl(channel, interactive=True)
        self.locals["events"] = event.EventRecorder(channel)
        protocol.invokeAndWait(protocol.removeChannelOpenListener, self)
        wrapper.addChannelListener(self)

    def onChannelClosed(self, error):
        del self.locals["channel"]
        del self.locals["cmd"]
        del self.locals["disconnect"]
        del self.locals["events"]
        protocol.addChannelOpenListener(self)


def interact():
    try:
        # enable commandline editing if available
        import readline  # @UnusedImport
    except ImportError:
        pass
    shell = Shell()
    shell.interact("TCF Shell")

if __name__ == "__main__":
    interact()

Back to the top