13 pass |
13 pass |
14 |
14 |
15 import struct |
15 import struct |
16 import io |
16 import io |
17 |
17 |
18 from PyQt4.QtCore import QProcess, QObject, QByteArray, QCoreApplication, QThread |
18 from PyQt4.QtCore import QProcess, QObject, QByteArray, QCoreApplication, \ |
|
19 QThread |
19 from PyQt4.QtGui import QDialog |
20 from PyQt4.QtGui import QDialog |
20 |
21 |
21 from .HgUtilities import prepareProcess |
22 from .HgUtilities import prepareProcess |
22 |
23 |
23 import Preferences |
24 import Preferences |
137 return False, self.trUtf8("Received data on unexpected channel.") |
137 return False, self.trUtf8("Received data on unexpected channel.") |
138 |
138 |
139 msg = msg.split("\n") |
139 msg = msg.split("\n") |
140 |
140 |
141 if not msg[0].startswith("capabilities: "): |
141 if not msg[0].startswith("capabilities: "): |
142 return False, self.trUtf8("Bad 'hello' message, expected 'capabilities: '" |
142 return False, self.trUtf8( |
143 " but got '{0}'.").format(msg[0]) |
143 "Bad 'hello' message, expected 'capabilities: '" |
|
144 " but got '{0}'.").format(msg[0]) |
144 self.__capabilities = msg[0][len('capabilities: '):] |
145 self.__capabilities = msg[0][len('capabilities: '):] |
145 if not self.__capabilities: |
146 if not self.__capabilities: |
146 return False, self.trUtf8("'capabilities' message did not contain" |
147 return False, self.trUtf8("'capabilities' message did not contain" |
147 " any capability.") |
148 " any capability.") |
148 |
149 |
149 self.__capabilities = set(self.__capabilities.split()) |
150 self.__capabilities = set(self.__capabilities.split()) |
150 if "runcommand" not in self.__capabilities: |
151 if "runcommand" not in self.__capabilities: |
151 return False, "'capabilities' did not contain 'runcommand'." |
152 return False, "'capabilities' did not contain 'runcommand'." |
152 |
153 |
153 if not msg[1].startswith("encoding: "): |
154 if not msg[1].startswith("encoding: "): |
154 return False, self.trUtf8("Bad 'hello' message, expected 'encoding: '" |
155 return False, self.trUtf8( |
155 " but got '{0}'.").format(msg[1]) |
156 "Bad 'hello' message, expected 'encoding: '" |
|
157 " but got '{0}'.").format(msg[1]) |
156 encoding = msg[1][len('encoding: '):] |
158 encoding = msg[1][len('encoding: '):] |
157 if not encoding: |
159 if not encoding: |
158 return False, self.trUtf8("'encoding' message did not contain" |
160 return False, self.trUtf8("'encoding' message did not contain" |
159 " any encoding.") |
161 " any encoding.") |
160 self.__encoding = encoding |
162 self.__encoding = encoding |
209 |
211 |
210 @param data data to be sent (string) |
212 @param data data to be sent (string) |
211 """ |
213 """ |
212 if not isinstance(data, bytes): |
214 if not isinstance(data, bytes): |
213 data = data.encode(self.__encoding) |
215 data = data.encode(self.__encoding) |
214 self.__server.write(QByteArray(struct.pack(HgClient.InputFormat, len(data)))) |
216 self.__server.write( |
|
217 QByteArray(struct.pack(HgClient.InputFormat, len(data)))) |
215 self.__server.write(QByteArray(data)) |
218 self.__server.write(QByteArray(data)) |
216 self.__server.waitForBytesWritten() |
219 self.__server.waitForBytesWritten() |
217 |
220 |
218 def __runcommand(self, args, inputChannels, outputChannels): |
221 def __runcommand(self, args, inputChannels, outputChannels): |
219 """ |
222 """ |
220 Private method to run a command in the server (low level). |
223 Private method to run a command in the server (low level). |
221 |
224 |
222 @param args list of arguments for the command (list of string) |
225 @param args list of arguments for the command (list of string) |
223 @param inputChannels dictionary of input channels. The dictionary must |
226 @param inputChannels dictionary of input channels. The dictionary must |
224 have the keys 'I' and 'L' and each entry must be a function receiving |
227 have the keys 'I' and 'L' and each entry must be a function |
225 the number of bytes to write. |
228 receiving the number of bytes to write. |
226 @param outputChannels dictionary of output channels. The dictionary must |
229 @param outputChannels dictionary of output channels. The dictionary |
227 have the keys 'o' and 'e' and each entry must be a function receiving |
230 must have the keys 'o' and 'e' and each entry must be a function |
228 the data. |
231 receiving the data. |
229 @return result code of the command, -1 if the command server wasn't started or |
232 @return result code of the command, -1 if the command server wasn't |
230 -10, if the command was canceled (integer) |
233 started or -10, if the command was canceled (integer) |
|
234 @exception RuntimeError raised to indicate an unexpected command |
|
235 channel |
231 """ |
236 """ |
232 if not self.__started: |
237 if not self.__started: |
233 return -1 |
238 return -1 |
234 |
239 |
235 self.__server.write(QByteArray(b'runcommand\n')) |
240 self.__server.write(QByteArray(b'runcommand\n')) |
278 """ |
283 """ |
279 Private method to prompt the user for some input. |
284 Private method to prompt the user for some input. |
280 |
285 |
281 @param size maximum length of the requested input (integer) |
286 @param size maximum length of the requested input (integer) |
282 @param message message sent by the server (string) |
287 @param message message sent by the server (string) |
|
288 @return data entered by the user (string) |
283 """ |
289 """ |
284 from .HgClientPromptDialog import HgClientPromptDialog |
290 from .HgClientPromptDialog import HgClientPromptDialog |
285 input = "" |
291 input = "" |
286 dlg = HgClientPromptDialog(size, message) |
292 dlg = HgClientPromptDialog(size, message) |
287 if dlg.exec_() == QDialog.Accepted: |
293 if dlg.exec_() == QDialog.Accepted: |
288 input = dlg.getInput() + '\n' |
294 input = dlg.getInput() + '\n' |
289 return input |
295 return input |
290 |
296 |
291 def runcommand(self, args, prompt=None, input=None, output=None, error=None): |
297 def runcommand(self, args, prompt=None, input=None, output=None, |
|
298 error=None): |
292 """ |
299 """ |
293 Public method to execute a command via the command server. |
300 Public method to execute a command via the command server. |
294 |
301 |
295 @param args list of arguments for the command (list of string) |
302 @param args list of arguments for the command (list of string) |
296 @keyparam prompt function to reply to prompts by the server. It |
303 @keyparam prompt function to reply to prompts by the server. It |
298 of the output channel received so far. |
305 of the output channel received so far. |
299 @keyparam input function to reply to bulk data requests by the server. |
306 @keyparam input function to reply to bulk data requests by the server. |
300 It receives the max number of bytes to return. |
307 It receives the max number of bytes to return. |
301 @keyparam output function receiving the data from the server (string). |
308 @keyparam output function receiving the data from the server (string). |
302 If a prompt function is given, this parameter will be ignored. |
309 If a prompt function is given, this parameter will be ignored. |
303 @keyparam error function receiving error messages from the server (string) |
310 @keyparam error function receiving error messages from the server |
304 @return output and errors of the command server (string). In case output |
311 (string) |
305 and/or error functions were given, the respective return value will |
312 @return output and errors of the command server (string). In case |
306 be an empty string. |
313 output and/or error functions were given, the respective return |
|
314 value will be an empty string. |
307 """ |
315 """ |
308 self.__commandRunning = True |
316 self.__commandRunning = True |
309 outputChannels = {} |
317 outputChannels = {} |
310 outputBuffer = None |
318 outputBuffer = None |
311 errorBuffer = None |
319 errorBuffer = None |
362 self.restartServer() |
370 self.restartServer() |
363 |
371 |
364 def wasCanceled(self): |
372 def wasCanceled(self): |
365 """ |
373 """ |
366 Public method to check, if the last command was canceled. |
374 Public method to check, if the last command was canceled. |
|
375 |
|
376 @return flag indicating the cancel state (boolean) |
367 """ |
377 """ |
368 return self.__cancel |
378 return self.__cancel |
369 |
379 |
370 def isExecuting(self): |
380 def isExecuting(self): |
371 """ |
381 """ |
372 Public method to check, if the server is executing a command. |
382 Public method to check, if the server is executing a command. |
373 |
383 |
374 @return flag indicating the execution of a command (boolean) |
384 @return flag indicating the execution of a command (boolean) |
375 """ |
385 """ |
|
386 return self.__commandRunning |