1. TortoiseHg
  2. TortoiseHg
  3. thg

Source

thg / tortoisehg / hgqt / cmdcore.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# cmdcore.py - run Mercurial commands in a separate thread or process
#
# 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, re, signal, sys, time, weakref

from PyQt4.QtCore import QIODevice, QObject, QProcess, QTimer
from PyQt4.QtCore import pyqtSignal, pyqtSlot

from tortoisehg.util import hglib, paths, pipeui
from tortoisehg.hgqt.i18n import _
from tortoisehg.hgqt import thread

_localprocexts = [
    'tortoisehg.util.pipeui',
    ]

if os.name == 'nt':
    # to translate WM_CLOSE posted by QProcess.terminate()
    _localprocexts.append('tortoisehg.util.win32ill')

    def _interruptproc(proc):
        proc.terminate()

else:
    def _interruptproc(proc):
        os.kill(int(proc.pid()), signal.SIGINT)

def _fixprocenv(proc):
    env = os.environ.copy()
    if not getattr(sys, 'frozen', False):
        # make sure hg process can look up our modules
        env['PYTHONPATH'] = (paths.get_prog_root() + os.pathsep
                             + env.get('PYTHONPATH', ''))
    # not using setProcessEnvironment() for compatibility with PyQt 4.6
    proc.setEnvironment([hglib.tounicode('%s=%s' % p) for p in env.iteritems()])

def _proccmdline(exts):
    cmdline = map(hglib.tounicode, paths.get_hg_command())
    for e in exts:
        cmdline.extend(('--config', 'extensions.%s=' % e))
    return cmdline


class CmdProc(QObject):
    'Run mercurial command in separate process'

    commandFinished = pyqtSignal(int)
    outputReceived = pyqtSignal(unicode, unicode)
    progressReceived = pyqtSignal(unicode, object, unicode, unicode, object)

    def __init__(self, parent=None, cwd=None):
        super(CmdProc, self).__init__(parent)
        self._proc = proc = QProcess(self)
        _fixprocenv(proc)
        if cwd:
            proc.setWorkingDirectory(cwd)
        proc.finished.connect(self.commandFinished)
        proc.readyReadStandardOutput.connect(self._stdout)
        proc.readyReadStandardError.connect(self._stderr)
        proc.error.connect(self._handleerror)

    def startCommand(self, cmdline):
        fullcmdline = _proccmdline(_localprocexts)
        fullcmdline.extend(cmdline)
        self._proc.start(fullcmdline[0], fullcmdline[1:], QIODevice.ReadOnly)

    def abortCommand(self):
        if not self.isCommandRunning():
            return
        _interruptproc(self._proc)

    def isCommandRunning(self):
        return self._proc.state() != QProcess.NotRunning

    def setUiParent(self, parent):
        # not interactive
        pass

    def _handleerror(self, error):
        if error == QProcess.FailedToStart:
            self.outputReceived.emit(_('failed to start command\n'),
                                     'ui.error')
            self.commandFinished.emit(-1)
        elif error != QProcess.Crashed:
            self.outputReceived.emit(_('error while running command\n'),
                                     'ui.error')

    def _stdout(self):
        data = self._proc.readAllStandardOutput().data()
        self._processRead(data, '')

    def _stderr(self):
        data = self._proc.readAllStandardError().data()
        self._processRead(data, 'ui.error')

    def _processRead(self, fulldata, defaultlabel):
        for data in pipeui.splitmsgs(fulldata):
            msg, label = pipeui.unpackmsg(data)
            msg = hglib.tounicode(msg)
            if 'ui.progress' in label.split():
                self.progressReceived.emit(*pipeui.unpackprogress(msg))
            else:
                self.outputReceived.emit(msg, label or defaultlabel)


_workertypes = {
    'thread': thread.CmdThread,
    'proc': CmdProc,
    }


_urlpassre = re.compile(r'^([a-zA-Z0-9+.\-]+://[^:@/]*):[^@/]+@')

def _reprcmdarg(arg):
    arg = _urlpassre.sub(r'\1:***@', arg)
    arg = arg.replace('\n', '^M')

    # only for display; no use to construct command string for os.system()
    if not arg or ' ' in arg or '\\' in arg or '"' in arg:
        return '"%s"' % arg.replace('"', '\\"')
    else:
        return arg

def _prettifycmdline(cmdline):
    r"""Build pretty command-line string for display

    >>> _prettifycmdline(['log', 'foo\\bar', '', 'foo bar', 'foo"bar'])
    'log "foo\\bar" "" "foo bar" "foo\\"bar"'
    >>> _prettifycmdline(['log', '--template', '{node}\n'])
    'log --template {node}^M'

    mask password in url-like string:

    >>> _prettifycmdline(['push', 'http://foo123:bar456@example.org/'])
    'push http://foo123:***@example.org/'
    >>> _prettifycmdline(['clone', 'svn+http://:bar@example.org:8080/trunk/'])
    'clone svn+http://:***@example.org:8080/trunk/'
    """
    return ' '.join(_reprcmdarg(e) for e in cmdline)

class CmdSession(QObject):
    """Run Mercurial commands in a background thread or process"""

    commandFinished = pyqtSignal(int)
    # in order to receive only notification messages of session state, use
    # "controlMessage"; otherwise use "outputReceived"
    controlMessage = pyqtSignal(unicode)
    outputReceived = pyqtSignal(unicode, unicode)
    progressReceived = pyqtSignal(unicode, object, unicode, unicode, object)

    def __init__(self, cmdlines, parent=None):
        super(CmdSession, self).__init__(parent)

        self._worker = None
        self._queue = list(cmdlines)
        self._qnextp = 0
        self._abortbyuser = False
        self._erroroutputs = []
        self._warningoutputs = []
        self._exitcode = 0
        if not cmdlines:
            # assumes null session is failure for convenience
            self._exitcode = -1

    ### Public Methods ###

    def run(self, worker):
        '''Execute Mercurial command'''
        if self.isRunning() or self._qnextp >= len(self._queue):
            return
        if self._abortbyuser:
            # -1 instead of 255 for compatibility with CmdThread
            self._finish(-1)
            return
        self._connectWorker(worker)
        self._runNext()

    def abort(self):
        '''Cancel running Mercurial command'''
        if self.isRunning():
            self._worker.abortCommand()
            self._qnextp = len(self._queue)
            self._abortbyuser = True
        elif not self.isFinished():
            # don't abort immediately because this hasn't started yet
            self._abortbyuser = True

    def isAborted(self):
        """True if commands have finished by user abort"""
        return self.isFinished() and self._abortbyuser

    def isFinished(self):
        """True if all pending commands have finished or been aborted"""
        return self._qnextp >= len(self._queue) and not self.isRunning()

    def isRunning(self):
        """True if a command is running; False if finished or not started yet"""
        # keep "running" until just before emitting commandFinished. if worker
        # is QThread, isRunning() is cleared earlier than _onCommandFinished,
        # because inter-thread signal is queued.
        return bool(self._worker)

    def errorString(self):
        """Error message received in the last command"""
        if self._abortbyuser:
            return _('Terminated by user')
        else:
            return ''.join(self._erroroutputs).rstrip()

    def warningString(self):
        """Warning message received in the last command"""
        return ''.join(self._warningoutputs).rstrip()

    def exitCode(self):
        """Integer return code of the last command"""
        return self._exitcode

    ### Private Method ###

    def _connectWorker(self, worker):
        self._worker = worker
        worker.commandFinished.connect(self._onCommandFinished)
        worker.outputReceived.connect(self.outputReceived)
        worker.outputReceived.connect(self._captureOutput)
        worker.progressReceived.connect(self.progressReceived)

    def _disconnectWorker(self):
        worker = self._worker
        if not worker:
            return
        worker.commandFinished.disconnect(self._onCommandFinished)
        worker.outputReceived.disconnect(self.outputReceived)
        worker.outputReceived.disconnect(self._captureOutput)
        worker.progressReceived.disconnect(self.progressReceived)
        self._worker = None

    def _emitControlMessage(self, msg):
        self.controlMessage.emit(msg)
        self.outputReceived.emit(msg + '\n', 'control')

    def _runNext(self):
        cmdline = self._queue[self._qnextp]
        self._qnextp += 1

        self._abortbyuser = False
        del self._erroroutputs[:]
        del self._warningoutputs[:]
        self._worker.startCommand(cmdline)
        self._emitControlMessage('% hg ' + _prettifycmdline(cmdline))

    def _finish(self, ret):
        self._qnextp = len(self._queue)
        self._disconnectWorker()
        self._exitcode = ret
        self.commandFinished.emit(ret)

    ### Signal Handlers ###

    @pyqtSlot(int)
    def _onCommandFinished(self, ret):
        if ret == -1:
            if self._abortbyuser:
                msg = _('[command terminated by user %s]')
            else:
                msg = _('[command interrupted %s]')
        elif ret:
            msg = _('[command returned code %d %%s]') % ret
        else:
            msg = _('[command completed successfully %s]')
        self._emitControlMessage(msg % time.asctime())

        if ret != 0 or self._qnextp >= len(self._queue):
            self._finish(ret)
        else:
            self._runNext()

    @pyqtSlot(unicode, unicode)
    def _captureOutput(self, msg, label):
        if not label:
            return  # fast path
        labelset = unicode(label).split()
        # typically ui.error is sent only once at end
        if 'ui.error' in labelset:
            self._erroroutputs.append(unicode(msg))
        elif 'ui.warning' in labelset:
            self._warningoutputs.append(unicode(msg))


def nullCmdSession():
    """Finished CmdSession object which can be used as the initial value

    exitCode() is -1 so that the command dialog can finish with error status
    if nothing executed.

    >>> sess = nullCmdSession()
    >>> sess.isFinished(), sess.isRunning(), sess.isAborted(), sess.exitCode()
    (True, False, False, -1)
    >>> sess.abort()  # should not change flags
    >>> sess.isFinished(), sess.isRunning(), sess.isAborted(), sess.exitCode()
    (True, False, False, -1)
    """
    return CmdSession([])


class CmdAgent(QObject):
    """Manage requests of Mercurial commands"""

    busyChanged = pyqtSignal(bool)

    # Signal forwarding:
    #   worker ---- agent   commandFinished:  session (= last one of worker)
    #      \         /      outputReceived:   worker + session
    #        session        progressReceived: worker (= session)
    commandFinished = pyqtSignal(CmdSession)
    outputReceived = pyqtSignal(unicode, unicode)
    progressReceived = pyqtSignal(unicode, object, unicode, unicode, object)

    # isBusy() is False when the last commandFinished is emitted, but you
    # shouldn't rely on the emission order of busyChanged and commandFinished.

    def __init__(self, ui, parent=None):
        super(CmdAgent, self).__init__(parent)
        self._ui = ui
        self._cwd = None
        self._workers = {}
        self._sessqueue = []  # [active, waiting...]
        self._runlater = QTimer(self, interval=0, singleShot=True)
        self._runlater.timeout.connect(self._runNextSession)

    def workingDirectory(self):
        return self._cwd

    def setWorkingDirectory(self, cwd):
        if self._workers:
            raise RuntimeError('cannot change working directory after run')
        self._cwd = unicode(cwd)

    def isBusy(self):
        return bool(self._sessqueue)

    def _enqueueSession(self, sess, workername, parentref):
        self._sessqueue.append((sess, workername, parentref))
        if len(self._sessqueue) == 1:
            self.busyChanged.emit(self.isBusy())
            # make sure no command signals emitted in the current context
            self._runlater.start()

    def _dequeueSession(self):
        del self._sessqueue[0]
        if self._sessqueue:
            # make sure client can receive commandFinished before next session
            self._runlater.start()
        else:
            self._runlater.stop()
            self.busyChanged.emit(self.isBusy())

    def runCommand(self, cmdline, parent=None, worker=None):
        """Executes a single Mercurial command asynchronously and returns
        new CmdSession object"""
        return self.runCommandSequence([cmdline], parent=parent, worker=worker)

    def runCommandSequence(self, cmdlines, parent=None, worker=None):
        """Executes a series of Mercurial commands asynchronously and returns
        new CmdSession object which will provide notification signals.

        CmdSession object will be disowned on command finished.  The specified
        parent is unrelated to the lifetime of CmdSession object.  It is used
        as the parent widget of the interactive GUI prompt.

        If one of the preceding command exits with non-zero status, the
        following commands won't be executed.
        """
        if worker and worker not in _workertypes:
            raise ValueError('invalid worker type: %r' % worker)
        if not worker:
            worker = self._defaultWorkerName()
        sess = CmdSession(cmdlines, self)
        sess.commandFinished.connect(self._onCommandFinished)
        sess.controlMessage.connect(self._forwardControlMessage)
        self._enqueueSession(sess, worker, parent and weakref.ref(parent))
        return sess

    def abortCommands(self):
        """Abort running and queued commands; all command sessions will emit
        commandFinished in order"""
        for sess, _workername, _parentref in self._sessqueue[:]:
            sess.abort()

    def _defaultWorkerName(self):
        # hidden config for debugging or testing experimental worker
        name = self._ui.config('tortoisehg', 'cmdworker', 'thread')
        if name not in _workertypes:
            self.outputReceived.emit(_("ignoring invalid cmdworker '%s'\n")
                                     % hglib.tounicode(name), 'ui.warning')
            return 'thread'
        return name

    def _prepareWorker(self, name):
        worker = self._workers.get(name)
        if worker:
            assert not worker.isCommandRunning() or thread.HAVE_QTBUG_30251
            return worker
        self._workers[name] = worker = _workertypes[name](self, self._cwd)
        worker.outputReceived.connect(self.outputReceived)
        worker.progressReceived.connect(self.progressReceived)
        return worker

    @pyqtSlot()
    def _runNextSession(self):
        sess, workername, parentref = self._sessqueue[0]
        worker = self._prepareWorker(workername)
        worker.setUiParent(parentref and parentref())
        sess.run(worker)

    @pyqtSlot()
    def _onCommandFinished(self):
        sess, _workername, _parentref = self._sessqueue[0]
        if isinstance(self.sender(), CmdSession):  # avoid bug of PyQt 4.7.3
            assert sess is self.sender()
        self._dequeueSession()
        self.commandFinished.emit(sess)
        sess.setParent(None)

    @pyqtSlot(unicode)
    def _forwardControlMessage(self, msg):
        self.outputReceived.emit(msg + '\n', 'control')