Kiln » TortoiseHg » TortoiseHg
Clone URL:  
thread.py
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# thread.py - A seprated thread to run Mercurial command # # Copyright 2009 Steve Borho <steve@borho.org> # Copyright 2010 Yuki KODAMA <endflow.net@gmail.com> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2, incorporated herein by reference. import os import Queue import time import urllib2 import errno from PyQt4.QtCore import * from PyQt4.QtGui import * from mercurial import util, error from mercurial import ui as uimod from tortoisehg.util import thread2, hglib from tortoisehg.hgqt.i18n import _, localgettext from tortoisehg.hgqt import qtlib local = localgettext() class DataWrapper(object): def __init__(self, data): self.data = data class UiSignal(QObject): writeSignal = pyqtSignal(QString, QString) progressSignal = pyqtSignal(QString, object, QString, QString, object) interactSignal = pyqtSignal(DataWrapper) def __init__(self, responseq): QObject.__init__(self) self.responseq = responseq def write(self, *args, **opts): msg = hglib.tounicode(''.join(args)) label = hglib.tounicode(opts.get('label', '')) self.writeSignal.emit(msg, label) def write_err(self, *args, **opts): msg = hglib.tounicode(''.join(args)) label = hglib.tounicode(opts.get('label', 'ui.error')) self.writeSignal.emit(msg, label) def prompt(self, msg, choices, default): try: r = self._waitresponse(msg, False, choices, None) if r is None: raise EOFError if not r: return default if choices: # return char for Mercurial 1.3 choice = choices[r] return choice[choice.index('&')+1].lower() return r except EOFError: raise util.Abort(local._('response expected')) def promptchoice(self, msg, choices, default): try: r = self._waitresponse(msg, False, choices, default) if r is None: raise EOFError return r except EOFError: raise util.Abort(local._('response expected')) def getpass(self, prompt, default): r = self._waitresponse(prompt, True, None, default) if r is None: raise util.Abort(local._('response expected')) return r def _waitresponse(self, msg, password, choices, default): """Request interaction with GUI and wait response from it""" data = DataWrapper((msg, password, choices, default)) self.interactSignal.emit(data) # await response return self.responseq.get(True) def progress(self, topic, pos, item, unit, total): topic = hglib.tounicode(topic or '') item = hglib.tounicode(item or '') unit = hglib.tounicode(unit or '') self.progressSignal.emit(topic, pos, item, unit, total) class QtUi(uimod.ui): def __init__(self, src=None, responseq=None): super(QtUi, self).__init__(src) if src: self.sig = src.sig else: self.sig = UiSignal(responseq) self.setconfig('ui', 'interactive', 'on') self.setconfig('progress', 'disable', 'True') def write(self, *args, **opts): if self._buffers: self._buffers[-1].extend([str(a) for a in args]) else: self.sig.write(*args, **opts) def write_err(self, *args, **opts): self.sig.write_err(*args, **opts) def label(self, msg, label): return msg def flush(self): pass def prompt(self, msg, choices=None, default='y'): if not self.interactive(): return default return self.sig.prompt(msg, choices, default) def promptchoice(self, msg, choices, default=0): if not self.interactive(): return default return self.sig.promptchoice(msg, choices, default) def getpass(self, prompt=_('password: '), default=None): return self.sig.getpass(prompt, default) def progress(self, topic, pos, item='', unit='', total=None): return self.sig.progress(topic, pos, item, unit, total) class CmdThread(QThread): """Run an Mercurial command in a background thread, implies output is being sent to a rendered text buffer interactively and requests for feedback from Mercurial can be handled by the user via dialog windows. """ # (msg=str, label=str) outputReceived = pyqtSignal(QString, QString) # (topic=str, pos=int, item=str, unit=str, total=int) # pos and total are emitted as object, since they may be None progressReceived = pyqtSignal(QString, object, QString, QString, object) # result: -1 - command is incomplete, possibly exited with exception # 0 - command is finished successfully # others - return code of command commandFinished = pyqtSignal(int) def __init__(self, cmdline, display, parent=None): super(CmdThread, self).__init__(parent) self.cmdline = cmdline self.display = display self.ret = -1 self.abortbyuser = False self.responseq = Queue.Queue() self.rawoutput = QStringList() self.topics = {} self.curstrs = QStringList() self.curlabel = None self.timer = QTimer(self) self.timer.timeout.connect(self.flush) self.timer.start(100) self.finished.connect(self.thread_finished) def abort(self): if self.isRunning() and hasattr(self, 'thread_id'): self.abortbyuser = True try: thread2._async_raise(self.thread_id, KeyboardInterrupt) except ValueError: pass def thread_finished(self): self.timer.stop() self.flush() self.commandFinished.emit(self.ret) def flush(self): if self.curlabel is not None: self.outputReceived.emit(self.curstrs.join(''), self.curlabel) self.curlabel = None if self.timer.isActive(): keys = self.topics.keys() for topic in keys: pos, item, unit, total = self.topics[topic] self.progressReceived.emit(topic, pos, item, unit, total) if pos is None: del self.topics[topic] else: # Close all progress bars for topic in self.topics: self.progressReceived.emit(topic, None, '', '', None) self.topics = {} @pyqtSlot(QString, QString) def output_handler(self, msg, label): if label != 'control': self.rawoutput.append(msg) if label == self.curlabel: self.curstrs.append(msg) else: if self.curlabel is not None: self.outputReceived.emit(self.curstrs.join(''), self.curlabel) self.curstrs = QStringList(msg) self.curlabel = label @pyqtSlot(QString, object, QString, QString, object) def progress_handler(self, topic, pos, item, unit, total): self.topics[topic] = (pos, item, unit, total) @pyqtSlot(DataWrapper) def interact_handler(self, wrapper): prompt, password, choices, default = wrapper.data prompt = hglib.tounicode(prompt) if choices: dlg = QMessageBox(QMessageBox.Question, _('TortoiseHg Prompt'), prompt, parent=self.parent()) dlg.setWindowFlags(Qt.Sheet) dlg.setWindowModality(Qt.WindowModal) for index, choice in enumerate(choices): button = dlg.addButton(hglib.tounicode(choice), QMessageBox.ActionRole) button.response = index if index == default: dlg.setDefaultButton(button) dlg.exec_() button = dlg.clickedButton() if button is 0: self.responseq.put(None) else: self.responseq.put(button.response) else: mode = password and QLineEdit.Password \ or QLineEdit.Normal dlg = QInputDialog(self.parent(), Qt.Sheet) dlg.setWindowModality(Qt.WindowModal) dlg.setWindowTitle(_('TortoiseHg Prompt')) dlg.setLabelText(prompt.title()) dlg.setTextEchoMode(mode) if dlg.exec_(): text = hglib.fromunicode(dlg.textValue()) else: text = None self.responseq.put(text) def run(self): ui = QtUi(responseq=self.responseq) ui.sig.writeSignal.connect(self.output_handler, Qt.QueuedConnection) ui.sig.progressSignal.connect(self.progress_handler, Qt.QueuedConnection) ui.sig.interactSignal.connect(self.interact_handler, Qt.QueuedConnection) if self.display: cmd = '%% hg %s\n' % self.display else: cmd = '%% hg %s\n' % ' '.join(self.cmdline) ui.write(cmd, label='control') try: # save thread id in order to terminate by KeyboardInterrupt self.thread_id = int(QThread.currentThreadId()) for k, v in ui.configitems('defaults'): ui.setconfig('defaults', k, '') self.ret = 255 self.ret = hglib.dispatch(ui, self.cmdline) or 0 except util.Abort, e: ui.write_err(local._('abort: ') + str(e) + '\n') if e.hint: ui.write_err(local._('hint: ') + str(e.hint) + '\n') except error.RepoError, e: ui.write_err(str(e) + '\n') except urllib2.HTTPError, e: err = local._('HTTP Error: %d (%s)') % (e.code, e.msg) ui.write_err(err + '\n') except urllib2.URLError, e: err = local._('URLError: %s') % str(e.reason) try: import ssl # Python 2.6 or backport for 2.5 if isinstance(e.args[0], ssl.SSLError): parts = e.args[0].strerror.split(':') if len(parts) == 7: file, line, level, _errno, lib, func, reason = parts if func == 'SSL3_GET_SERVER_CERTIFICATE': err = local._('SSL: Server certificate verify failed') elif _errno == '00000000': err = local._('SSL: unknown error %s:%s') % (file, line) else: err = local._('SSL error: %s') % reason except ImportError: pass ui.write_err(err + '\n') except error.AmbiguousCommand, inst: ui.warn(local._("hg: command '%s' is ambiguous:\n %s\n") % (inst.args[0], " ".join(inst.args[1]))) except error.ParseError, inst: if len(inst.args) > 1: ui.warn(local._("hg: parse error at %s: %s\n") % (inst.args[1], inst.args[0])) else: ui.warn(local._("hg: parse error: %s\n") % inst.args[0]) except error.LockHeld, inst: if inst.errno == errno.ETIMEDOUT: reason = local._('timed out waiting for lock held by %s') % inst.locker else: reason = local._('lock held by %s') % inst.locker ui.warn(local._("abort: %s: %s\n") % (inst.desc or inst.filename, reason)) except error.LockUnavailable, inst: ui.warn(local._("abort: could not lock %s: %s\n") % (inst.desc or inst.filename, inst.strerror)) except error.RepoError, inst: ui.warn(local._("abort: %s!\n") % inst) except error.ResponseError, inst: ui.warn(local._("abort: %s") % inst.args[0]) if not isinstance(inst.args[1], basestring): ui.warn(" %r\n" % (inst.args[1],)) elif not inst.args[1]: ui.warn(local._(" empty string\n")) else: ui.warn("\n%r\n" % util.ellipsis(inst.args[1])) except error.RevlogError, inst: ui.warn(local._("abort: %s!\n") % inst) except IOError, inst: if hasattr(inst, "code"): ui.warn(local._("abort: %s\n") % inst) elif hasattr(inst, "reason"): try: # usually it is in the form (errno, strerror) reason = inst.reason.args[1] except: # it might be anything, for example a string reason = inst.reason ui.warn(local._("abort: error: %s\n") % reason) elif hasattr(inst, "args") and inst.args[0] == errno.EPIPE: if ui.debugflag: ui.warn(local._("broken pipe\n")) elif getattr(inst, "strerror", None): if getattr(inst, "filename", None): ui.warn(local._("abort: %s: %s\n") % (inst.strerror, inst.filename)) else: ui.warn(local._("abort: %s\n") % inst.strerror) else: raise except OSError, inst: if getattr(inst, "filename", None): ui.warn(local._("abort: %s: %s\n") % (inst.strerror, inst.filename)) else: ui.warn(local._("abort: %s\n") % inst.strerror) except Exception, inst: if 'THGDEBUG' in os.environ: import traceback traceback.print_exc() ui.write_err(str(inst) + '\n') except KeyboardInterrupt: self.ret = -1 if self.ret == -1: if self.abortbyuser: msg = _('[command terminated by user %s]') else: msg = _('[command interrupted %s]') elif self.ret: msg = _('[command returned code %d %%s]') % int(self.ret) else: msg = _('[command completed successfully %s]') ui.write(msg % time.asctime() + '\n', label='control')