~abentley/bzrtools/bzrtools.dev

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
# arch-tag: 0abcff18-da5d-49f0-a03e-1d866a86b5cd
# Copyright (C) 2004 Canonical Ltd.
#               Author: David Allouche <david@canonical.com>
#
#    This program 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; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program 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.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Twisted process handling

The name of this module, "knotted", was suggested by Andrew Bennetts
as a way to avoid the name clash with the"twisted" top-level package::

    <spiv> I'd say "knotted" would be good.
    <spiv> And then you can tell people to "get knotted" ;)

I disclaim all responsibility for the bad taste of Twisted developers.
"""

import Queue
from twisted.internet import protocol
from arch import errors
import commandline


def make_exec_problem(data, command, args, expected, chdir):
    status, signal, error = data
    proc = DummyProcess(command, args, expected, chdir, status, signal, error)
    return errors.ExecProblem(proc)


class TwistedSpawningStrategy(commandline.SpawningStrategy):

    def __init__(self, command, logger):
        self._command = command
        self._logger = logger

    def _spawn(self, args, chdir):
        if __debug__:
            from twisted.python import threadable
            assert not threadable.isInIOThread()
        from twisted.internet import reactor
        # The queue size must be unlimited, otherwise the blocking
        # put() in the reactor could cause a deadlock.
        queue = Queue.Queue(0) # unlimited queue
        process_protocol = ProcessProtocol(queue)
        argv = [self._command]
        argv.extend(args)
        reactor.callFromThread(
            reactor.spawnProcess,
            process_protocol, self._command, args=argv, env=None, path=chdir)
        return queue

    def status_cmd(self, args, chdir, expected):
        queue = self._spawn(args, chdir)
        while True:
            msg = queue.get(block=True)
            if msg[0] == 'start':
                self._logger.log_command(self._command, args)
            elif msg[0] == 'output':
                text = ''.join(msg[1])
                self._logger.log_output(text)
            elif msg[0] == 'error':
                text = ''.join(msg[1])
                self._logger.log_error(text)
            elif msg[0] == 'terminate':
                if msg[1] in expected and msg[2] is None:
                    return msg[1]
                else:
                    raise make_exec_problem(
                        msg[1:], self._command, args, expected, chdir)
            else:
                raise AssertionError('bad message: %r', msg)

    def sequence_cmd(self, args, chdir, expected):
        queue = self._spawn(args, chdir)
        return SequenceCmd(
            queue, self._command, args, chdir, expected, self._logger)

    def status_text_cmd(self, args, chdir, expected):
        queue = self._spawn(args, chdir)
        seq = SequenceCmd(
            queue, self._command, args, chdir, expected, self._logger)
        seq.strip = False
        text = ''.join(seq)
        return seq.status, text


class SequenceCmd(object):

    def __init__(self, queue, command, args, chdir, expected, logger):
        self._queue = queue
        self._command = command
        self._logger = logger
        self._args = args
        self._chdir = chdir
        self._expected = expected
        self._buffer = []
        self._status = None
        self.finished = False
        self.strip = True

    def __iter__(self):
        return self

    def next(self):
        while True:
            if self.finished:
                raise StopIteration
            if self._buffer:
                line = self._buffer.pop(0)
                if self.strip:
                    return line.rstrip('\n')
                else:
                    return line
            msg = self._queue.get(block=True)
            if msg[0] == 'start':
                self._logger.log_command(self._command, self._args)
            elif msg[0] == 'output':
                self._buffer = msg[1] + self._buffer
            elif msg[0] == 'error':
                text = ''.join(msg[1])
                self._logger.log_error(text)
            elif msg[0] == 'terminate':
                self.finished = True
                if msg[1] in self._expected and msg[2] is None:
                    self._status = msg[1]
                else:
                    raise make_exec_problem(
                        msg[1:], self._command, self._args,
                        self._expected, self._chdir)
            else:
                raise AssertionError('bad message: %r', msg)

    def _get_status(self):
        if self._status is None:
            raise AttributeError, "no status, process has not finished"
        return self._status
    status = property(_get_status)


class DummyProcess(object):
    """Dummy object providing the same interface as `forkexec.ChildProcess`."""

    def __init__(self, command, args, expected, chdir, status, signal, error):
        self.command = command
        self.args = args
        self.chdir = chdir
        self.expected = expected
        self.error = ''.join(error)
        self.status = status
        self.signal = signal


class ProcessProtocol(protocol.ProcessProtocol):

    def __init__(self, queue):
        self.__queue = queue
        self.__out_buffer = str()
        self.__err_buffer = str()
        self.error_lines = []
        self.status = None

    def connectionMade(self):
        """The process has started."""
        self.__queue.put(('start',), block=True)

    def __send_output(self, lines):
        self.__queue.put(('output', lines), block=True)

    def __send_error(self, lines):
        self.__queue.put(('error', lines), block=True)

    def outReceived(self, data):
        """The process has produced data on standard output."""
        data = self.__out_buffer + data
        lines = data.splitlines(True)
        if lines[-1].endswith('\n'):
            self.__out_buffer = str()
            complete_lines = lines
        else:
            self.__out_buffer = lines[-1]
            complete_lines = lines[:-1]
        self.__send_output(complete_lines)

    def errReceived(self, data):
        """The process has produced data on standard error."""
        data = self.__err_buffer + data
        lines = data.splitlines(True)
        if lines[-1].endswith('\n'):
            self.__err_buffer = str()
            complete_lines = lines
        else:
            self.__err_buffer = lines[-1]
            complete_lines = lines[:-1]
        self.__send_error(complete_lines)
        self.error_lines.extend(complete_lines)

    def outConnectionLost(self):
        """The process has closed standard output."""
        if self.__out_buffer:
            self.__send_output([self.__out_buffer])
            self.__out_buffer = str()

    def errConnectionLost(self):
        """The process has closed standard error."""
        if self.__err_buffer:
            self.__send_error([self.__err_buffer])
            self.error_lines.append(self.__err_buffer)
            self.__err_buffer = str()

    def processEnded(self, reason):
        """The process has terminated."""
        signal = reason.value.signal
        status = reason.value.exitCode
        msg = ('terminate', status, signal, self.error_lines)
        self.__queue.put(msg, block=True)