Chris@57
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@57
|
2
|
Chris@57
|
3 /*
|
Chris@57
|
4 EasyMercurial
|
Chris@57
|
5
|
Chris@57
|
6 Based on HgExplorer by Jari Korhonen
|
Chris@57
|
7 Copyright (c) 2010 Jari Korhonen
|
Chris@57
|
8 Copyright (c) 2010 Chris Cannam
|
Chris@57
|
9 Copyright (c) 2010 Queen Mary, University of London
|
Chris@57
|
10
|
Chris@57
|
11 This program is free software; you can redistribute it and/or
|
Chris@57
|
12 modify it under the terms of the GNU General Public License as
|
Chris@57
|
13 published by the Free Software Foundation; either version 2 of the
|
Chris@57
|
14 License, or (at your option) any later version. See the file
|
Chris@57
|
15 COPYING included with this distribution for more information.
|
Chris@57
|
16 */
|
jtkorhonen@0
|
17
|
jtkorhonen@0
|
18 #include "hgrunner.h"
|
Chris@62
|
19 #include "common.h"
|
Chris@57
|
20 #include "debug.h"
|
Chris@50
|
21
|
Chris@50
|
22 #include <QPushButton>
|
Chris@50
|
23 #include <QListWidget>
|
Chris@50
|
24 #include <QDialog>
|
Chris@50
|
25 #include <QLabel>
|
Chris@50
|
26 #include <QVBoxLayout>
|
Chris@62
|
27 #include <QSettings>
|
Chris@75
|
28 #include <QInputDialog>
|
Chris@188
|
29 #include <QTemporaryFile>
|
Chris@161
|
30 #include <QDir>
|
jtkorhonen@0
|
31
|
Chris@43
|
32 #include <iostream>
|
Chris@75
|
33 #include <errno.h>
|
Chris@75
|
34 #include <stdio.h>
|
Chris@111
|
35 #include <stdlib.h>
|
jtkorhonen@0
|
36
|
Chris@76
|
37 #ifndef Q_OS_WIN32
|
Chris@111
|
38 #include <unistd.h>
|
Chris@113
|
39 #include <termios.h>
|
Chris@111
|
40 #include <fcntl.h>
|
Chris@80
|
41 #endif
|
Chris@76
|
42
|
Chris@172
|
43 HgRunner::HgRunner(QString myDirPath, QWidget * parent) :
|
Chris@172
|
44 QProgressBar(parent),
|
Chris@172
|
45 m_myDirPath(myDirPath)
|
jtkorhonen@0
|
46 {
|
Chris@113
|
47 m_proc = 0;
|
Chris@84
|
48
|
jtkorhonen@0
|
49 setTextVisible(false);
|
jtkorhonen@0
|
50 setVisible(false);
|
Chris@84
|
51 m_isRunning = false;
|
Chris@161
|
52
|
Chris@180
|
53 (void)findExtension();
|
Chris@180
|
54 (void)findHgBinaryName();
|
jtkorhonen@0
|
55 }
|
jtkorhonen@0
|
56
|
jtkorhonen@0
|
57 HgRunner::~HgRunner()
|
jtkorhonen@0
|
58 {
|
Chris@111
|
59 closeTerminal();
|
Chris@120
|
60 if (m_proc) {
|
Chris@120
|
61 m_proc->kill();
|
Chris@122
|
62 m_proc->deleteLater();
|
Chris@120
|
63 }
|
jtkorhonen@0
|
64 }
|
jtkorhonen@0
|
65
|
Chris@180
|
66 QString HgRunner::findExtension()
|
Chris@172
|
67 {
|
Chris@188
|
68 // If we haven't unbundled an extension, always do that (so that
|
Chris@188
|
69 // it's available in case someone wants to use it later, e.g. to
|
Chris@188
|
70 // fix a malfunctioning setup). But the path we actually prefer
|
Chris@189
|
71 // is the one in the settings first, if it exists; then the
|
Chris@189
|
72 // unbundled one; then anything in the path if for some reason
|
Chris@189
|
73 // unbundling failed
|
Chris@188
|
74
|
Chris@176
|
75 QSettings settings;
|
Chris@176
|
76 settings.beginGroup("Locations");
|
Chris@188
|
77
|
Chris@188
|
78 QString unbundled = getUnbundledFileName();
|
Chris@188
|
79 if (!QFile(unbundled).exists()) {
|
Chris@188
|
80 unbundled = unbundleExtension();
|
Chris@188
|
81 }
|
Chris@188
|
82
|
Chris@180
|
83 QString extpath = settings.value("extensionpath", "").toString();
|
Chris@189
|
84 if (extpath != "" && QFile(extpath).exists()) return extpath;
|
Chris@188
|
85
|
Chris@188
|
86 extpath = unbundled;
|
Chris@188
|
87 if (extpath == "") {
|
Chris@188
|
88 extpath = findInPath("easyhg.py", m_myDirPath, false);
|
Chris@188
|
89 if (extpath == "easyhg.py") {
|
Chris@188
|
90 extpath = "";
|
Chris@188
|
91 }
|
Chris@172
|
92 }
|
Chris@188
|
93
|
Chris@180
|
94 settings.setValue("extensionpath", extpath);
|
Chris@180
|
95 return extpath;
|
Chris@172
|
96 }
|
Chris@172
|
97
|
Chris@188
|
98 QString HgRunner::getUnbundledFileName()
|
Chris@188
|
99 {
|
Chris@188
|
100 QString home = QDir::homePath();
|
Chris@188
|
101 QString target = QString("%1/.easyhg").arg(home);
|
Chris@188
|
102 QString extpath = QString("%1/easyhg.py").arg(target);
|
Chris@188
|
103 return extpath;
|
Chris@188
|
104 }
|
Chris@188
|
105
|
Chris@180
|
106 QString HgRunner::unbundleExtension()
|
Chris@161
|
107 {
|
Chris@188
|
108 // Pull out the bundled Python file into a temporary file, and
|
Chris@188
|
109 // copy it to our known extension location, replacing the magic
|
Chris@188
|
110 // text NO_EASYHG_IMPORT_PATH with our installation location
|
Chris@188
|
111
|
Chris@161
|
112 QString bundled = ":easyhg.py";
|
Chris@188
|
113 QString unbundled = getUnbundledFileName();
|
Chris@188
|
114
|
Chris@188
|
115 QString target = QFileInfo(unbundled).path();
|
Chris@161
|
116 if (!QDir().mkpath(target)) {
|
Chris@161
|
117 DEBUG << "Failed to make unbundle path " << target << endl;
|
Chris@188
|
118 std::cerr << "Failed to make unbundle path " << target << std::endl;
|
Chris@180
|
119 return "";
|
Chris@161
|
120 }
|
Chris@188
|
121
|
Chris@161
|
122 QFile bf(bundled);
|
Chris@188
|
123 DEBUG << "unbundle: bundled file will be " << bundled << endl;
|
Chris@188
|
124 if (!bf.exists() || !bf.open(QIODevice::ReadOnly)) {
|
Chris@180
|
125 DEBUG << "Bundled extension is missing!" << endl;
|
Chris@180
|
126 return "";
|
Chris@180
|
127 }
|
Chris@188
|
128
|
Chris@188
|
129 QTemporaryFile tmpfile(QString("%1/easyhg.py.XXXXXX").arg(target));
|
Chris@188
|
130 tmpfile.setAutoRemove(false);
|
Chris@188
|
131 DEBUG << "unbundle: temp file will be " << tmpfile.fileName() << endl;
|
Chris@188
|
132 if (!tmpfile.open()) {
|
Chris@188
|
133 DEBUG << "Failed to open temporary file " << tmpfile.fileName() << endl;
|
Chris@188
|
134 std::cerr << "Failed to open temporary file " << tmpfile.fileName() << std::endl;
|
Chris@180
|
135 return "";
|
Chris@161
|
136 }
|
Chris@188
|
137
|
Chris@188
|
138 QString all = QString::fromUtf8(bf.readAll());
|
Chris@188
|
139 all.replace("NO_EASYHG_IMPORT_PATH", m_myDirPath);
|
Chris@188
|
140 tmpfile.write(all.toUtf8());
|
Chris@188
|
141 DEBUG << "unbundle: wrote " << all.length() << " characters" << endl;
|
Chris@188
|
142
|
Chris@188
|
143 tmpfile.close();
|
Chris@188
|
144
|
Chris@188
|
145 QFile ef(unbundled);
|
Chris@188
|
146 if (ef.exists()) {
|
Chris@188
|
147 DEBUG << "unbundle: removing old file " << unbundled << endl;
|
Chris@188
|
148 ef.remove();
|
Chris@188
|
149 }
|
Chris@188
|
150 DEBUG << "unbundle: renaming " << tmpfile.fileName() << " to " << unbundled << endl;
|
Chris@188
|
151 if (!tmpfile.rename(unbundled)) {
|
Chris@188
|
152 DEBUG << "Failed to move temporary file to target file " << unbundled << endl;
|
Chris@188
|
153 std::cerr << "Failed to move temporary file to target file " << unbundled << std::endl;
|
Chris@188
|
154 return "";
|
Chris@188
|
155 }
|
Chris@188
|
156
|
Chris@188
|
157 DEBUG << "Unbundled extension to " << unbundled << endl;
|
Chris@188
|
158 return unbundled;
|
Chris@161
|
159 }
|
Chris@161
|
160
|
Chris@109
|
161 void HgRunner::requestAction(HgAction action)
|
Chris@109
|
162 {
|
Chris@109
|
163 DEBUG << "requestAction " << action.action << endl;
|
Chris@109
|
164 bool pushIt = true;
|
Chris@109
|
165 if (m_queue.empty()) {
|
Chris@109
|
166 if (action == m_currentAction) {
|
Chris@109
|
167 // this request is identical to the thing we're executing
|
Chris@109
|
168 DEBUG << "requestAction: we're already handling this one, ignoring identical request" << endl;
|
Chris@109
|
169 pushIt = false;
|
Chris@109
|
170 }
|
Chris@109
|
171 } else {
|
Chris@109
|
172 HgAction last = m_queue.back();
|
Chris@109
|
173 if (action == last) {
|
Chris@109
|
174 // this request is identical to the previous thing we
|
Chris@109
|
175 // queued which we haven't executed yet
|
Chris@109
|
176 DEBUG << "requestAction: we're already queueing this one, ignoring identical request" << endl;
|
Chris@109
|
177 pushIt = false;
|
Chris@109
|
178 }
|
Chris@109
|
179 }
|
Chris@109
|
180 if (pushIt) m_queue.push_back(action);
|
Chris@109
|
181 checkQueue();
|
Chris@109
|
182 }
|
Chris@109
|
183
|
Chris@180
|
184 QString HgRunner::findHgBinaryName()
|
Chris@62
|
185 {
|
Chris@62
|
186 QSettings settings;
|
Chris@175
|
187 settings.beginGroup("Locations");
|
Chris@77
|
188 QString hg = settings.value("hgbinary", "").toString();
|
Chris@77
|
189 if (hg == "") {
|
Chris@172
|
190 hg = findInPath("hg", m_myDirPath, true);
|
Chris@77
|
191 }
|
Chris@77
|
192 if (hg != "hg") {
|
Chris@77
|
193 settings.setValue("hgbinary", hg);
|
Chris@77
|
194 }
|
Chris@62
|
195 return hg;
|
Chris@62
|
196 }
|
Chris@62
|
197
|
jtkorhonen@0
|
198 void HgRunner::started()
|
jtkorhonen@0
|
199 {
|
Chris@104
|
200 DEBUG << "started" << endl;
|
Chris@75
|
201 /*
|
Chris@104
|
202 m_proc->write("blah\n");
|
Chris@104
|
203 m_proc->write("blah\n");
|
Chris@104
|
204 m_proc -> closeWriteChannel();
|
Chris@75
|
205 */
|
jtkorhonen@0
|
206 }
|
jtkorhonen@0
|
207
|
Chris@84
|
208 void HgRunner::noteUsername(QString name)
|
Chris@75
|
209 {
|
Chris@84
|
210 m_userName = name;
|
Chris@75
|
211 }
|
Chris@75
|
212
|
Chris@84
|
213 void HgRunner::noteRealm(QString realm)
|
Chris@75
|
214 {
|
Chris@84
|
215 m_realm = realm;
|
Chris@84
|
216 }
|
Chris@84
|
217
|
Chris@84
|
218 void HgRunner::getUsername()
|
Chris@84
|
219 {
|
Chris@113
|
220 if (m_ptyFile) {
|
Chris@84
|
221 bool ok = false;
|
Chris@84
|
222 QString prompt = tr("User name:");
|
Chris@84
|
223 if (m_realm != "") {
|
Chris@84
|
224 prompt = tr("User name for \"%1\":").arg(m_realm);
|
Chris@84
|
225 }
|
Chris@84
|
226 QString pwd = QInputDialog::getText
|
Chris@84
|
227 (qobject_cast<QWidget *>(parent()),
|
Chris@84
|
228 tr("Enter user name"), prompt,
|
Chris@84
|
229 QLineEdit::Normal, QString(), &ok);
|
Chris@84
|
230 if (ok) {
|
Chris@113
|
231 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@113
|
232 m_ptyFile->flush();
|
Chris@84
|
233 return;
|
Chris@111
|
234 } else {
|
Chris@111
|
235 DEBUG << "HgRunner::getUsername: user cancelled" << endl;
|
Chris@111
|
236 killCurrentCommand();
|
Chris@111
|
237 return;
|
Chris@84
|
238 }
|
Chris@84
|
239 }
|
Chris@84
|
240 // user cancelled or something went wrong
|
Chris@111
|
241 DEBUG << "HgRunner::getUsername: something went wrong" << endl;
|
Chris@84
|
242 killCurrentCommand();
|
Chris@84
|
243 }
|
Chris@84
|
244
|
Chris@84
|
245 void HgRunner::getPassword()
|
Chris@84
|
246 {
|
Chris@113
|
247 if (m_ptyFile) {
|
Chris@84
|
248 bool ok = false;
|
Chris@84
|
249 QString prompt = tr("Password:");
|
Chris@84
|
250 if (m_userName != "") {
|
Chris@84
|
251 if (m_realm != "") {
|
Chris@84
|
252 prompt = tr("Password for \"%1\" at \"%2\":")
|
Chris@84
|
253 .arg(m_userName).arg(m_realm);
|
Chris@75
|
254 } else {
|
Chris@84
|
255 prompt = tr("Password for user \"%1\":")
|
Chris@84
|
256 .arg(m_userName);
|
Chris@75
|
257 }
|
Chris@75
|
258 }
|
Chris@84
|
259 QString pwd = QInputDialog::getText
|
Chris@84
|
260 (qobject_cast<QWidget *>(parent()),
|
Chris@84
|
261 tr("Enter password"), prompt,
|
Chris@84
|
262 QLineEdit::Password, QString(), &ok);
|
Chris@84
|
263 if (ok) {
|
Chris@113
|
264 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@113
|
265 m_ptyFile->flush();
|
Chris@84
|
266 return;
|
Chris@111
|
267 } else {
|
Chris@111
|
268 DEBUG << "HgRunner::getPassword: user cancelled" << endl;
|
Chris@111
|
269 killCurrentCommand();
|
Chris@111
|
270 return;
|
Chris@84
|
271 }
|
Chris@75
|
272 }
|
Chris@84
|
273 // user cancelled or something went wrong
|
Chris@111
|
274 DEBUG << "HgRunner::getPassword: something went wrong" << endl;
|
Chris@84
|
275 killCurrentCommand();
|
Chris@84
|
276 }
|
Chris@84
|
277
|
Chris@113
|
278 bool HgRunner::checkPrompts(QString chunk)
|
Chris@84
|
279 {
|
Chris@93
|
280 //DEBUG << "checkPrompts: " << chunk << endl;
|
Chris@84
|
281
|
Chris@128
|
282 if (!m_currentAction.mayBeInteractive()) return false;
|
Chris@128
|
283
|
Chris@84
|
284 QString text = chunk.trimmed();
|
Chris@84
|
285 QString lower = text.toLower();
|
Chris@84
|
286 if (lower.endsWith("password:")) {
|
Chris@84
|
287 getPassword();
|
Chris@113
|
288 return true;
|
Chris@84
|
289 }
|
Chris@128
|
290 if (lower.endsWith("user:") || lower.endsWith("username:")) {
|
Chris@84
|
291 getUsername();
|
Chris@113
|
292 return true;
|
Chris@84
|
293 }
|
Chris@128
|
294 QRegExp userRe("\\buser(name)?:\\s*([^\\s]+)");
|
Chris@84
|
295 if (userRe.indexIn(text) >= 0) {
|
Chris@128
|
296 noteUsername(userRe.cap(2));
|
Chris@84
|
297 }
|
Chris@84
|
298 QRegExp realmRe("\\brealmr:\\s*([^\\s]+)");
|
Chris@84
|
299 if (realmRe.indexIn(text) >= 0) {
|
Chris@84
|
300 noteRealm(realmRe.cap(1));
|
Chris@84
|
301 }
|
Chris@113
|
302 return false;
|
Chris@84
|
303 }
|
Chris@84
|
304
|
Chris@110
|
305 void HgRunner::dataReadyStdout()
|
Chris@84
|
306 {
|
Chris@110
|
307 DEBUG << "dataReadyStdout" << endl;
|
Chris@110
|
308 QString chunk = QString::fromUtf8(m_proc->readAllStandardOutput());
|
Chris@113
|
309 if (!checkPrompts(chunk)) {
|
Chris@113
|
310 m_stdout += chunk;
|
Chris@113
|
311 }
|
Chris@110
|
312 }
|
Chris@110
|
313
|
Chris@110
|
314 void HgRunner::dataReadyStderr()
|
Chris@110
|
315 {
|
Chris@110
|
316 DEBUG << "dataReadyStderr" << endl;
|
Chris@110
|
317 QString chunk = QString::fromUtf8(m_proc->readAllStandardError());
|
Chris@113
|
318 DEBUG << chunk;
|
Chris@113
|
319 if (!checkPrompts(chunk)) {
|
Chris@113
|
320 m_stderr += chunk;
|
Chris@113
|
321 }
|
Chris@113
|
322 }
|
Chris@113
|
323
|
Chris@113
|
324 void HgRunner::dataReadyPty()
|
Chris@113
|
325 {
|
Chris@113
|
326 DEBUG << "dataReadyPty" << endl;
|
Chris@113
|
327 QString chunk = QString::fromUtf8(m_ptyFile->readAll());
|
Chris@113
|
328 DEBUG << "chunk of " << chunk.length() << " chars" << endl;
|
Chris@113
|
329 if (!checkPrompts(chunk)) {
|
Chris@113
|
330 m_stdout += chunk;
|
Chris@113
|
331 }
|
Chris@75
|
332 }
|
Chris@75
|
333
|
Chris@189
|
334 void HgRunner::error(QProcess::ProcessError)
|
Chris@189
|
335 {
|
Chris@189
|
336 finished(-1, QProcess::CrashExit);
|
Chris@189
|
337 }
|
Chris@189
|
338
|
jtkorhonen@0
|
339 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
340 {
|
Chris@109
|
341 // Save the current action and reset m_currentAction before we
|
Chris@109
|
342 // emit a signal to mark the completion; otherwise we may be
|
Chris@109
|
343 // resetting the action after a slot has already tried to set it
|
Chris@109
|
344 // to something else to start a new action
|
Chris@109
|
345
|
Chris@109
|
346 HgAction completedAction = m_currentAction;
|
Chris@109
|
347
|
Chris@84
|
348 m_isRunning = false;
|
Chris@109
|
349 m_currentAction = HgAction();
|
Chris@84
|
350
|
Chris@195
|
351 //closeProcInput();
|
Chris@189
|
352 m_proc->deleteLater();
|
Chris@113
|
353 m_proc = 0;
|
jtkorhonen@0
|
354
|
Chris@109
|
355 if (completedAction.action == ACT_NONE) {
|
Chris@109
|
356 DEBUG << "HgRunner::finished: WARNING: completed action is ACT_NONE" << endl;
|
Chris@62
|
357 } else {
|
Chris@113
|
358 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
|
Chris@113
|
359 DEBUG << "HgRunner::finished: Command completed successfully"
|
Chris@113
|
360 << endl;
|
Chris@124
|
361 // DEBUG << "stdout is " << m_stdout << endl;
|
Chris@113
|
362 emit commandCompleted(completedAction, m_stdout);
|
Chris@113
|
363 } else {
|
Chris@113
|
364 DEBUG << "HgRunner::finished: Command failed, exit code "
|
Chris@113
|
365 << procExitCode << ", exit status " << procExitStatus
|
Chris@113
|
366 << ", stderr follows" << endl;
|
Chris@113
|
367 DEBUG << m_stderr << endl;
|
Chris@113
|
368 emit commandFailed(completedAction, m_stderr);
|
Chris@113
|
369 }
|
jtkorhonen@0
|
370 }
|
Chris@109
|
371
|
Chris@109
|
372 checkQueue();
|
jtkorhonen@0
|
373 }
|
jtkorhonen@0
|
374
|
Chris@182
|
375 void HgRunner::killCurrentActions()
|
Chris@182
|
376 {
|
Chris@182
|
377 m_queue.clear();
|
Chris@182
|
378 killCurrentCommand();
|
Chris@182
|
379 }
|
Chris@182
|
380
|
Chris@62
|
381 void HgRunner::killCurrentCommand()
|
jtkorhonen@0
|
382 {
|
Chris@109
|
383 if (m_isRunning) {
|
Chris@113
|
384 m_currentAction.action = ACT_NONE; // so that we don't bother to notify
|
Chris@113
|
385 m_proc->kill();
|
jtkorhonen@0
|
386 }
|
jtkorhonen@0
|
387 }
|
jtkorhonen@0
|
388
|
Chris@109
|
389 void HgRunner::checkQueue()
|
Chris@62
|
390 {
|
Chris@109
|
391 if (m_isRunning) {
|
Chris@109
|
392 return;
|
Chris@109
|
393 }
|
Chris@109
|
394 if (m_queue.empty()) {
|
Chris@109
|
395 hide();
|
Chris@109
|
396 return;
|
Chris@109
|
397 }
|
Chris@109
|
398 HgAction toRun = m_queue.front();
|
Chris@109
|
399 m_queue.pop_front();
|
Chris@109
|
400 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
|
Chris@109
|
401 startCommand(toRun);
|
Chris@109
|
402 }
|
Chris@109
|
403
|
Chris@109
|
404 void HgRunner::startCommand(HgAction action)
|
Chris@109
|
405 {
|
Chris@109
|
406 QString executable = action.executable;
|
Chris@109
|
407 bool interactive = false;
|
Chris@109
|
408 QStringList params = action.params;
|
Chris@109
|
409
|
Chris@109
|
410 if (executable == "") {
|
Chris@109
|
411 // This is a Hg command
|
Chris@180
|
412 executable = findHgBinaryName();
|
Chris@161
|
413
|
Chris@161
|
414 if (action.mayBeInteractive()) {
|
Chris@161
|
415 params.push_front("ui.interactive=true");
|
Chris@161
|
416 params.push_front("--config");
|
Chris@176
|
417
|
Chris@176
|
418 QSettings settings;
|
Chris@176
|
419 settings.beginGroup("General");
|
Chris@176
|
420 if (settings.value("useextension", true).toBool()) {
|
Chris@180
|
421 QString extpath = findExtension();
|
Chris@180
|
422 params.push_front(QString("extensions.easyhg=%1").arg(extpath));
|
Chris@176
|
423 params.push_front("--config");
|
Chris@176
|
424 }
|
Chris@161
|
425 interactive = true;
|
Chris@161
|
426 }
|
Chris@161
|
427
|
Chris@161
|
428 //!!! want an option to use the mercurial_keyring extension as well
|
Chris@107
|
429 }
|
jtkorhonen@0
|
430
|
Chris@84
|
431 m_isRunning = true;
|
jtkorhonen@0
|
432 setRange(0, 0);
|
Chris@115
|
433 if (!action.shouldBeFast()) show();
|
Chris@110
|
434 m_stdout.clear();
|
Chris@110
|
435 m_stderr.clear();
|
Chris@84
|
436 m_realm = "";
|
Chris@84
|
437 m_userName = "";
|
jtkorhonen@0
|
438
|
Chris@113
|
439 m_proc = new QProcess;
|
Chris@113
|
440
|
Chris@177
|
441 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
Chris@177
|
442
|
Chris@177
|
443 #ifdef Q_OS_WIN32
|
Chris@177
|
444 if (m_myDirPath != "") {
|
Chris@177
|
445 env.insert("PATH", m_myDirPath + ";" + env.value("PATH"));
|
Chris@172
|
446 }
|
Chris@172
|
447 #endif
|
Chris@172
|
448
|
Chris@113
|
449 env.insert("LANG", "en_US.utf8");
|
Chris@113
|
450 env.insert("LC_ALL", "en_US.utf8");
|
Chris@113
|
451 env.insert("HGPLAIN", "1");
|
Chris@113
|
452 m_proc->setProcessEnvironment(env);
|
Chris@113
|
453
|
Chris@113
|
454 connect(m_proc, SIGNAL(started()), this, SLOT(started()));
|
Chris@189
|
455 connect(m_proc, SIGNAL(error(QProcess::ProcessError)),
|
Chris@189
|
456 this, SLOT(error(QProcess::ProcessError)));
|
Chris@113
|
457 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
|
Chris@113
|
458 this, SLOT(finished(int, QProcess::ExitStatus)));
|
Chris@113
|
459 connect(m_proc, SIGNAL(readyReadStandardOutput()),
|
Chris@113
|
460 this, SLOT(dataReadyStdout()));
|
Chris@113
|
461 connect(m_proc, SIGNAL(readyReadStandardError()),
|
Chris@113
|
462 this, SLOT(dataReadyStderr()));
|
Chris@113
|
463
|
Chris@109
|
464 if (!action.workingDir.isEmpty()) {
|
Chris@109
|
465 m_proc->setWorkingDirectory(action.workingDir);
|
jtkorhonen@0
|
466 }
|
jtkorhonen@0
|
467
|
Chris@107
|
468 if (interactive) {
|
Chris@111
|
469 openTerminal();
|
Chris@111
|
470 if (m_ptySlaveFilename != "") {
|
Chris@113
|
471 DEBUG << "HgRunner: connecting to pseudoterminal" << endl;
|
Chris@107
|
472 m_proc->setStandardInputFile(m_ptySlaveFilename);
|
Chris@114
|
473 // m_proc->setStandardOutputFile(m_ptySlaveFilename);
|
Chris@113
|
474 // m_proc->setStandardErrorFile(m_ptySlaveFilename);
|
Chris@107
|
475 }
|
Chris@84
|
476 }
|
Chris@84
|
477
|
Chris@109
|
478 QString cmdline = executable;
|
Chris@57
|
479 foreach (QString param, params) cmdline += " " + param;
|
Chris@64
|
480 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
|
Chris@109
|
481 << action.workingDir << endl;
|
Chris@43
|
482
|
Chris@109
|
483 m_currentAction = action;
|
Chris@109
|
484
|
Chris@113
|
485 // fill these out with what we actually ran
|
Chris@113
|
486 m_currentAction.executable = executable;
|
Chris@113
|
487 m_currentAction.params = params;
|
Chris@113
|
488
|
Chris@109
|
489 DEBUG << "set current action to " << m_currentAction.action << endl;
|
Chris@109
|
490
|
Chris@109
|
491 m_proc->start(executable, params);
|
Chris@84
|
492 }
|
Chris@84
|
493
|
Chris@84
|
494 void HgRunner::closeProcInput()
|
Chris@84
|
495 {
|
Chris@84
|
496 DEBUG << "closeProcInput" << endl;
|
Chris@84
|
497
|
Chris@84
|
498 m_proc->closeWriteChannel();
|
Chris@111
|
499 }
|
Chris@111
|
500
|
Chris@111
|
501 void HgRunner::openTerminal()
|
Chris@111
|
502 {
|
Chris@111
|
503 #ifndef Q_OS_WIN32
|
Chris@111
|
504 if (m_ptySlaveFilename != "") return; // already open
|
Chris@111
|
505 DEBUG << "HgRunner::openTerminal: trying to open new pty" << endl;
|
Chris@111
|
506 int master = posix_openpt(O_RDWR | O_NOCTTY);
|
Chris@111
|
507 if (master < 0) {
|
Chris@111
|
508 DEBUG << "openpt failed" << endl;
|
Chris@111
|
509 perror("openpt failed");
|
Chris@111
|
510 return;
|
Chris@111
|
511 }
|
Chris@113
|
512 struct termios t;
|
Chris@113
|
513 if (tcgetattr(master, &t)) {
|
Chris@113
|
514 DEBUG << "tcgetattr failed" << endl;
|
Chris@113
|
515 perror("tcgetattr failed");
|
Chris@113
|
516 }
|
Chris@113
|
517 cfmakeraw(&t);
|
Chris@113
|
518 if (tcsetattr(master, TCSANOW, &t)) {
|
Chris@113
|
519 DEBUG << "tcsetattr failed" << endl;
|
Chris@113
|
520 perror("tcsetattr failed");
|
Chris@113
|
521 }
|
Chris@111
|
522 if (grantpt(master)) {
|
Chris@111
|
523 perror("grantpt failed");
|
Chris@111
|
524 }
|
Chris@111
|
525 if (unlockpt(master)) {
|
Chris@111
|
526 perror("unlockpt failed");
|
Chris@111
|
527 }
|
Chris@111
|
528 char *slave = ptsname(master);
|
Chris@111
|
529 if (!slave) {
|
Chris@111
|
530 perror("ptsname failed");
|
Chris@111
|
531 ::close(master);
|
Chris@111
|
532 return;
|
Chris@111
|
533 }
|
Chris@111
|
534 m_ptyMasterFd = master;
|
Chris@113
|
535 m_ptyFile = new QFile();
|
Chris@113
|
536 connect(m_ptyFile, SIGNAL(readyRead()), this, SLOT(dataReadyPty()));
|
Chris@113
|
537 if (!m_ptyFile->open(m_ptyMasterFd, QFile::ReadWrite)) {
|
Chris@113
|
538 DEBUG << "HgRunner::openTerminal: Failed to open QFile on master fd" << endl;
|
Chris@113
|
539 }
|
Chris@111
|
540 m_ptySlaveFilename = slave;
|
Chris@111
|
541 DEBUG << "HgRunner::openTerminal: succeeded, slave is "
|
Chris@111
|
542 << m_ptySlaveFilename << endl;
|
Chris@111
|
543 #endif
|
Chris@111
|
544 }
|
Chris@111
|
545
|
Chris@111
|
546 void HgRunner::closeTerminal()
|
Chris@111
|
547 {
|
Chris@84
|
548 #ifndef Q_OS_WIN32
|
Chris@84
|
549 if (m_ptySlaveFilename != "") {
|
Chris@113
|
550 delete m_ptyFile;
|
Chris@113
|
551 m_ptyFile = 0;
|
Chris@84
|
552 ::close(m_ptyMasterFd);
|
Chris@84
|
553 m_ptySlaveFilename = "";
|
Chris@84
|
554 }
|
Chris@84
|
555 #endif
|
jtkorhonen@0
|
556 }
|