Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 80bb9b21580f0f9a61b43cb2ddcd72027375ba81 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# *****************************************************************************
# * Copyright (c) 2011, 2013-2014, 2016 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
# *****************************************************************************

from .. import processes
from ... import channel
from ...channel.Command import Command


class ProcessContext(processes.ProcessContext):
    def __init__(self, service, props):
        super(ProcessContext, self).__init__(props)
        self.service = service

    def attach(self, done):
        return self._command("attach", done)

    def detach(self, done):
        return self._command("detach", done)

    def terminate(self, done):
        return self._command("terminate", done)

    def _command(self, command, done):
        service = self.service
        done = service._makeCallback(done)
        contextID = self.getID()

        class _Command(Command):
            def __init__(self):
                super(_Command, self).__init__(service.channel, service,
                                               command, (contextID,))

            def done(self, error, args):
                if not error:
                    assert len(args) == 1
                    error = self.toError(args[0])
                done.doneCommand(self.token, error)
        return _Command().token


class ProcessesProxy(processes.ProcessesService):
    def __init__(self, channel):
        self.channel = channel
        self.listeners = {}

    def getChildren(self, parent_context_id, attached_only, done):
        done = self._makeCallback(done)
        service = self

        class GetChildrenCommand(Command):
            def __init__(self):
                super(GetChildrenCommand, self).__init__(service.channel,
                                                         service,
                                                         "getChildren",
                                                         (parent_context_id,
                                                          attached_only))

            def done(self, error, args):
                contexts = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    contexts = args[1]
                done.doneGetChildren(self.token, error, contexts)
        return GetChildrenCommand().token

    def getContext(self, context_id, done):
        done = self._makeCallback(done)
        service = self

        class GetContextCommand(Command):
            def __init__(self):
                super(GetContextCommand, self).__init__(service.channel,
                                                        service, "getContext",
                                                        (context_id,))

            def done(self, error, args):
                ctx = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    if args[1]:
                        ctx = ProcessContext(service, args[1])
                done.doneGetContext(self.token, error, ctx)
        return GetContextCommand().token

    def getEnvironment(self, done):
        done = self._makeCallback(done)
        service = self

        class GetEnvCommand(Command):
            def __init__(self):
                super(GetEnvCommand, self).__init__(service.channel, service,
                                                    "getEnvironment", None)

            def done(self, error, args):
                env = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    env = _toEnvMap(args[1])
                done.doneGetEnvironment(self.token, error, env)
        return GetEnvCommand().token

    def start(self, directory, filePath, command_line, environment, attach,
              done):
        done = self._makeCallback(done)
        service = self
        env = _toEnvStringArray(environment)

        class StartCommand(Command):
            def __init__(self):
                super(StartCommand, self).__init__(service.channel, service,
                                                   "start",
                                                   (directory, filePath,
                                                    command_line, env, attach))

            def done(self, error, args):
                ctx = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    if args[1]:
                        ctx = ProcessContext(service, args[1])
                done.doneStart(self.token, error, ctx)
        return StartCommand().token

    def getSignalList(self, context_id, done):
        done = self._makeCallback(done)
        service = self

        class GetSignalsCommand(Command):
            def __init__(self):
                super(GetSignalsCommand, self).__init__(service.channel,
                                                        service,
                                                        "getSignalList",
                                                        (context_id,))

            def done(self, error, args):
                lst = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    lst = args[1]
                done.doneGetSignalList(self.token, error, lst)
        return GetSignalsCommand().token

    def getSignalMask(self, context_id, done):
        done = self._makeCallback(done)
        service = self

        class GetSignalMaskCommand(Command):
            def __init__(self):
                super(GetSignalMaskCommand, self).__init__(service.channel,
                                                           service,
                                                           "getSignalMask",
                                                           (context_id,))

            def done(self, error, args):
                dont_stop = 0
                dont_pass = 0
                pending = 0
                if not error:
                    assert len(args) == 4
                    error = self.toError(args[0])
                    dont_stop, dont_pass, pending = args[1:4]
                done.doneGetSignalMask(self.token, error, dont_stop, dont_pass,
                                       pending)
        return GetSignalMaskCommand().token

    def setSignalMask(self, context_id, dont_stop, dont_pass, done):
        done = self._makeCallback(done)
        service = self

        class SetSignalMaskCommand(Command):
            def __init__(self):
                super(SetSignalMaskCommand, self).__init__(service.channel,
                                                           service,
                                                           "setSignalMask",
                                                           (context_id,
                                                            dont_stop,
                                                            dont_pass))

            def done(self, error, args):
                if not error:
                    assert len(args) == 1
                    error = self.toError(args[0])
                done.doneCommand(self.token, error)
        return SetSignalMaskCommand().token

    def signal(self, context_id, signal, done):
        done = self._makeCallback(done)
        service = self

        class SignalCommand(Command):
            def __init__(self):
                super(SignalCommand, self).__init__(service.channel,
                                                    service, "signal",
                                                    (context_id, signal))

            def done(self, error, args):
                if not error:
                    assert len(args) == 1
                    error = self.toError(args[0])
                done.doneCommand(self.token, error)
        return SignalCommand().token

    def addListener(self, listener):
        l = ChannelEventListener(self, listener)
        self.channel.addEventListener(self, l)
        self.listeners[listener] = l

    def removeListener(self, listener):
        l = self.listeners.get(listener)
        if l:
            del self.listeners[listener]
            self.channel.removeEventListener(self, l)


class ChannelEventListener(channel.EventListener):
    def __init__(self, service, listener):
        self.service = service
        self.listener = listener

    def event(self, name, data):
        try:
            args = channel.fromJSONSequence(data)
            if name == "exited":
                assert len(args) == 2
                self.listener.exited(args[0], args[1])
            else:
                raise IOError("Processes service: unknown event: " + name)
        except Exception as x:
            import sys
            x.tb = sys.exc_info()[2]
            self.service.channel.terminate(x)


def _toEnvStringArray(envVars):
    arr = []
    if not envVars:
        return arr
    for name, value in list(envVars.items()):
        arr.append("%s=%s" % (name, value))
    return arr


def _toEnvMap(arr):
    envVars = {}
    if not arr:
        return envVars
    for env in arr:
        i = env.find('=')
        if i >= 0:
            envVars[env[:i]] = env[i + 1:]
        else:
            envVars[env] = ""
    return envVars

Back to the top