85 def stopServer(self): |
85 def stopServer(self): |
86 """ |
86 """ |
87 Public method to stop the command server. |
87 Public method to stop the command server. |
88 """ |
88 """ |
89 self.__server.closeWriteChannel() |
89 self.__server.closeWriteChannel() |
90 self.__server.waitForFinished() |
90 res = self.__server.waitForFinished(10000) |
|
91 if not res: |
|
92 self.__server.terminate() |
|
93 res = self.__server.waitForFinished(5000) |
|
94 if not res: |
|
95 self.__server.kill() |
91 |
96 |
92 def restartServer(self): |
97 def restartServer(self): |
93 """ |
98 """ |
94 Public method to restart the command server. |
99 Public method to restart the command server. |
95 |
100 |
148 |
153 |
149 def __readChannel(self): |
154 def __readChannel(self): |
150 """ |
155 """ |
151 Private method to read data from the command server. |
156 Private method to read data from the command server. |
152 |
157 |
153 @return tuple of channel designator and channel data (string, integer or string) |
158 @return tuple of channel designator and channel data |
|
159 (string, integer or string or bytes) |
154 """ |
160 """ |
155 if self.__server.bytesAvailable() > 0 or \ |
161 if self.__server.bytesAvailable() > 0 or \ |
156 self.__server.waitForReadyRead(10000): |
162 self.__server.waitForReadyRead(10000): |
157 data = bytes(self.__server.read(HgClient.OutputFormatSize)) |
163 data = bytes(self.__server.read(HgClient.OutputFormatSize)) |
158 if not data: |
164 if not data: |
161 channel, length = struct.unpack(HgClient.OutputFormat, data) |
167 channel, length = struct.unpack(HgClient.OutputFormat, data) |
162 channel = channel.decode(self.__encoding) |
168 channel = channel.decode(self.__encoding) |
163 if channel in "IL": |
169 if channel in "IL": |
164 return channel, length |
170 return channel, length |
165 else: |
171 else: |
166 return (channel, |
172 data = self.__server.read(length) |
167 str(self.__server.read(length), self.__encoding, "replace")) |
173 if channel == "r": |
|
174 return (channel, data) |
|
175 else: |
|
176 return (channel, str(data, self.__encoding, "replace")) |
168 else: |
177 else: |
169 return "", "" |
178 return "", "" |
170 |
179 |
171 def __writeDataBlock(self, data): |
180 def __writeDataBlock(self, data): |
172 """ |
181 """ |
189 have the keys 'I' and 'L' and each entry must be a function receiving |
198 have the keys 'I' and 'L' and each entry must be a function receiving |
190 the number of bytes to write. |
199 the number of bytes to write. |
191 @param outputChannels dictionary of output channels. The dictionary must |
200 @param outputChannels dictionary of output channels. The dictionary must |
192 have the keys 'o' and 'e' and each entry must be a function receiving |
201 have the keys 'o' and 'e' and each entry must be a function receiving |
193 the data. |
202 the data. |
194 @return result code of the command or -1, if the command was canceled (integer) |
203 @return result code of the command or -10, if the command was canceled (integer) |
195 """ |
204 """ |
196 if not self.__started: |
205 if not self.__started: |
197 return -1 |
206 return -10 |
198 |
207 |
199 self.__server.write(QByteArray(b'runcommand\n')) |
208 self.__server.write(QByteArray(b'runcommand\n')) |
200 self.__writeDataBlock('\0'.join(args)) |
209 self.__writeDataBlock('\0'.join(args)) |
201 |
210 |
202 while True: |
211 while True: |
217 elif channel in outputChannels: |
226 elif channel in outputChannels: |
218 outputChannels[channel](data) |
227 outputChannels[channel](data) |
219 |
228 |
220 # result channel, command is finished |
229 # result channel, command is finished |
221 elif channel == "r": |
230 elif channel == "r": |
222 return struct.unpack(HgClient.ReturnFormat, |
231 return struct.unpack(HgClient.ReturnFormat, data)[0] |
223 data.encode(self.__encoding))[0] |
|
224 |
232 |
225 # unexpected but required channel |
233 # unexpected but required channel |
226 elif channel.isupper(): |
234 elif channel.isupper(): |
227 raise RuntimeError( |
235 raise RuntimeError( |
228 "Unexpected but required channel '{0}'.".format(channel)) |
236 "Unexpected but required channel '{0}'.".format(channel)) |