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@244
|
8 Copyright (c) 2011 Chris Cannam
|
Chris@244
|
9 Copyright (c) 2011 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@240
|
21 #include "settingsdialog.h"
|
Chris@50
|
22
|
Chris@50
|
23 #include <QPushButton>
|
Chris@50
|
24 #include <QListWidget>
|
Chris@50
|
25 #include <QDialog>
|
Chris@50
|
26 #include <QLabel>
|
Chris@50
|
27 #include <QVBoxLayout>
|
Chris@62
|
28 #include <QSettings>
|
Chris@75
|
29 #include <QInputDialog>
|
Chris@444
|
30 #include <QDesktopServices>
|
Chris@188
|
31 #include <QTemporaryFile>
|
Chris@161
|
32 #include <QDir>
|
jtkorhonen@0
|
33
|
Chris@43
|
34 #include <iostream>
|
Chris@75
|
35 #include <errno.h>
|
Chris@75
|
36 #include <stdio.h>
|
Chris@111
|
37 #include <stdlib.h>
|
jtkorhonen@0
|
38
|
Chris@76
|
39 #ifndef Q_OS_WIN32
|
Chris@111
|
40 #include <unistd.h>
|
Chris@113
|
41 #include <termios.h>
|
Chris@111
|
42 #include <fcntl.h>
|
Chris@80
|
43 #endif
|
Chris@76
|
44
|
Chris@172
|
45 HgRunner::HgRunner(QString myDirPath, QWidget * parent) :
|
Chris@172
|
46 QProgressBar(parent),
|
Chris@172
|
47 m_myDirPath(myDirPath)
|
jtkorhonen@0
|
48 {
|
Chris@113
|
49 m_proc = 0;
|
Chris@84
|
50
|
Chris@239
|
51 // Always unbundle the extension: even if it already exists (in
|
Chris@239
|
52 // case we're upgrading) and even if we're not going to use it (so
|
Chris@239
|
53 // that it's available in case someone wants to use it later,
|
Chris@239
|
54 // e.g. to fix a malfunctioning setup). But the path we actually
|
Chris@239
|
55 // prefer is the one in the settings first, if it exists; then the
|
Chris@239
|
56 // unbundled one; then anything in the path if for some reason
|
Chris@239
|
57 // unbundling failed
|
Chris@239
|
58 unbundleExtension();
|
Chris@239
|
59
|
jtkorhonen@0
|
60 setTextVisible(false);
|
jtkorhonen@0
|
61 setVisible(false);
|
Chris@84
|
62 m_isRunning = false;
|
jtkorhonen@0
|
63 }
|
jtkorhonen@0
|
64
|
jtkorhonen@0
|
65 HgRunner::~HgRunner()
|
jtkorhonen@0
|
66 {
|
Chris@111
|
67 closeTerminal();
|
Chris@120
|
68 if (m_proc) {
|
Chris@120
|
69 m_proc->kill();
|
Chris@122
|
70 m_proc->deleteLater();
|
Chris@120
|
71 }
|
Chris@444
|
72 if (m_authFilePath != "") {
|
Chris@444
|
73 QFile(m_authFilePath).remove();
|
Chris@444
|
74 }
|
Chris@444
|
75 //!!! and remove any other misc auth file paths...
|
jtkorhonen@0
|
76 }
|
jtkorhonen@0
|
77
|
Chris@188
|
78 QString HgRunner::getUnbundledFileName()
|
Chris@188
|
79 {
|
Chris@240
|
80 return SettingsDialog::getUnbundledExtensionFileName();
|
Chris@188
|
81 }
|
Chris@188
|
82
|
Chris@180
|
83 QString HgRunner::unbundleExtension()
|
Chris@161
|
84 {
|
Chris@188
|
85 // Pull out the bundled Python file into a temporary file, and
|
Chris@188
|
86 // copy it to our known extension location, replacing the magic
|
Chris@188
|
87 // text NO_EASYHG_IMPORT_PATH with our installation location
|
Chris@188
|
88
|
Chris@161
|
89 QString bundled = ":easyhg.py";
|
Chris@188
|
90 QString unbundled = getUnbundledFileName();
|
Chris@188
|
91
|
Chris@188
|
92 QString target = QFileInfo(unbundled).path();
|
Chris@161
|
93 if (!QDir().mkpath(target)) {
|
Chris@161
|
94 DEBUG << "Failed to make unbundle path " << target << endl;
|
Chris@188
|
95 std::cerr << "Failed to make unbundle path " << target << std::endl;
|
Chris@180
|
96 return "";
|
Chris@161
|
97 }
|
Chris@188
|
98
|
Chris@161
|
99 QFile bf(bundled);
|
Chris@188
|
100 DEBUG << "unbundle: bundled file will be " << bundled << endl;
|
Chris@188
|
101 if (!bf.exists() || !bf.open(QIODevice::ReadOnly)) {
|
Chris@180
|
102 DEBUG << "Bundled extension is missing!" << endl;
|
Chris@180
|
103 return "";
|
Chris@180
|
104 }
|
Chris@188
|
105
|
Chris@188
|
106 QTemporaryFile tmpfile(QString("%1/easyhg.py.XXXXXX").arg(target));
|
Chris@188
|
107 tmpfile.setAutoRemove(false);
|
Chris@188
|
108 DEBUG << "unbundle: temp file will be " << tmpfile.fileName() << endl;
|
Chris@188
|
109 if (!tmpfile.open()) {
|
Chris@188
|
110 DEBUG << "Failed to open temporary file " << tmpfile.fileName() << endl;
|
Chris@188
|
111 std::cerr << "Failed to open temporary file " << tmpfile.fileName() << std::endl;
|
Chris@180
|
112 return "";
|
Chris@161
|
113 }
|
Chris@188
|
114
|
Chris@188
|
115 QString all = QString::fromUtf8(bf.readAll());
|
Chris@188
|
116 all.replace("NO_EASYHG_IMPORT_PATH", m_myDirPath);
|
Chris@188
|
117 tmpfile.write(all.toUtf8());
|
Chris@188
|
118 DEBUG << "unbundle: wrote " << all.length() << " characters" << endl;
|
Chris@188
|
119
|
Chris@188
|
120 tmpfile.close();
|
Chris@188
|
121
|
Chris@188
|
122 QFile ef(unbundled);
|
Chris@188
|
123 if (ef.exists()) {
|
Chris@188
|
124 DEBUG << "unbundle: removing old file " << unbundled << endl;
|
Chris@188
|
125 ef.remove();
|
Chris@188
|
126 }
|
Chris@188
|
127 DEBUG << "unbundle: renaming " << tmpfile.fileName() << " to " << unbundled << endl;
|
Chris@188
|
128 if (!tmpfile.rename(unbundled)) {
|
Chris@188
|
129 DEBUG << "Failed to move temporary file to target file " << unbundled << endl;
|
Chris@188
|
130 std::cerr << "Failed to move temporary file to target file " << unbundled << std::endl;
|
Chris@188
|
131 return "";
|
Chris@188
|
132 }
|
Chris@188
|
133
|
Chris@188
|
134 DEBUG << "Unbundled extension to " << unbundled << endl;
|
Chris@188
|
135 return unbundled;
|
Chris@161
|
136 }
|
Chris@161
|
137
|
Chris@109
|
138 void HgRunner::requestAction(HgAction action)
|
Chris@109
|
139 {
|
Chris@109
|
140 DEBUG << "requestAction " << action.action << endl;
|
Chris@109
|
141 bool pushIt = true;
|
Chris@109
|
142 if (m_queue.empty()) {
|
Chris@109
|
143 if (action == m_currentAction) {
|
Chris@109
|
144 // this request is identical to the thing we're executing
|
Chris@109
|
145 DEBUG << "requestAction: we're already handling this one, ignoring identical request" << endl;
|
Chris@109
|
146 pushIt = false;
|
Chris@109
|
147 }
|
Chris@109
|
148 } else {
|
Chris@109
|
149 HgAction last = m_queue.back();
|
Chris@109
|
150 if (action == last) {
|
Chris@109
|
151 // this request is identical to the previous thing we
|
Chris@109
|
152 // queued which we haven't executed yet
|
Chris@109
|
153 DEBUG << "requestAction: we're already queueing this one, ignoring identical request" << endl;
|
Chris@109
|
154 pushIt = false;
|
Chris@109
|
155 }
|
Chris@109
|
156 }
|
Chris@109
|
157 if (pushIt) m_queue.push_back(action);
|
Chris@109
|
158 checkQueue();
|
Chris@109
|
159 }
|
Chris@109
|
160
|
Chris@239
|
161 QString HgRunner::getHgBinaryName()
|
Chris@62
|
162 {
|
Chris@62
|
163 QSettings settings;
|
Chris@175
|
164 settings.beginGroup("Locations");
|
Chris@239
|
165 return settings.value("hgbinary", "").toString();
|
Chris@62
|
166 }
|
Chris@62
|
167
|
chris@406
|
168 QString HgRunner::getSshBinaryName()
|
chris@406
|
169 {
|
chris@406
|
170 QSettings settings;
|
chris@406
|
171 settings.beginGroup("Locations");
|
chris@406
|
172 return settings.value("sshbinary", "").toString();
|
chris@406
|
173 }
|
chris@406
|
174
|
Chris@239
|
175 QString HgRunner::getExtensionLocation()
|
Chris@239
|
176 {
|
Chris@239
|
177 QSettings settings;
|
Chris@239
|
178 settings.beginGroup("Locations");
|
Chris@239
|
179 QString extpath = settings.value("extensionpath", "").toString();
|
Chris@239
|
180 if (extpath != "" && QFile(extpath).exists()) return extpath;
|
Chris@239
|
181 return "";
|
Chris@239
|
182 }
|
Chris@239
|
183
|
jtkorhonen@0
|
184 void HgRunner::started()
|
jtkorhonen@0
|
185 {
|
Chris@104
|
186 DEBUG << "started" << endl;
|
Chris@75
|
187 /*
|
Chris@104
|
188 m_proc->write("blah\n");
|
Chris@104
|
189 m_proc->write("blah\n");
|
Chris@104
|
190 m_proc -> closeWriteChannel();
|
Chris@75
|
191 */
|
jtkorhonen@0
|
192 }
|
jtkorhonen@0
|
193
|
Chris@84
|
194 void HgRunner::noteUsername(QString name)
|
Chris@75
|
195 {
|
Chris@84
|
196 m_userName = name;
|
Chris@75
|
197 }
|
Chris@75
|
198
|
Chris@84
|
199 void HgRunner::noteRealm(QString realm)
|
Chris@75
|
200 {
|
Chris@84
|
201 m_realm = realm;
|
Chris@84
|
202 }
|
Chris@84
|
203
|
Chris@84
|
204 void HgRunner::getUsername()
|
Chris@84
|
205 {
|
Chris@113
|
206 if (m_ptyFile) {
|
Chris@84
|
207 bool ok = false;
|
Chris@84
|
208 QString prompt = tr("User name:");
|
Chris@84
|
209 if (m_realm != "") {
|
Chris@84
|
210 prompt = tr("User name for \"%1\":").arg(m_realm);
|
Chris@84
|
211 }
|
Chris@84
|
212 QString pwd = QInputDialog::getText
|
Chris@84
|
213 (qobject_cast<QWidget *>(parent()),
|
Chris@84
|
214 tr("Enter user name"), prompt,
|
Chris@84
|
215 QLineEdit::Normal, QString(), &ok);
|
Chris@84
|
216 if (ok) {
|
Chris@113
|
217 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@113
|
218 m_ptyFile->flush();
|
Chris@84
|
219 return;
|
Chris@111
|
220 } else {
|
Chris@111
|
221 DEBUG << "HgRunner::getUsername: user cancelled" << endl;
|
Chris@111
|
222 killCurrentCommand();
|
Chris@111
|
223 return;
|
Chris@84
|
224 }
|
Chris@84
|
225 }
|
Chris@84
|
226 // user cancelled or something went wrong
|
Chris@111
|
227 DEBUG << "HgRunner::getUsername: something went wrong" << endl;
|
Chris@84
|
228 killCurrentCommand();
|
Chris@84
|
229 }
|
Chris@84
|
230
|
Chris@84
|
231 void HgRunner::getPassword()
|
Chris@84
|
232 {
|
Chris@113
|
233 if (m_ptyFile) {
|
Chris@84
|
234 bool ok = false;
|
Chris@84
|
235 QString prompt = tr("Password:");
|
Chris@84
|
236 if (m_userName != "") {
|
Chris@84
|
237 if (m_realm != "") {
|
Chris@84
|
238 prompt = tr("Password for \"%1\" at \"%2\":")
|
Chris@84
|
239 .arg(m_userName).arg(m_realm);
|
Chris@75
|
240 } else {
|
Chris@84
|
241 prompt = tr("Password for user \"%1\":")
|
Chris@84
|
242 .arg(m_userName);
|
Chris@75
|
243 }
|
Chris@75
|
244 }
|
Chris@84
|
245 QString pwd = QInputDialog::getText
|
Chris@84
|
246 (qobject_cast<QWidget *>(parent()),
|
Chris@84
|
247 tr("Enter password"), prompt,
|
Chris@84
|
248 QLineEdit::Password, QString(), &ok);
|
Chris@84
|
249 if (ok) {
|
Chris@113
|
250 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@113
|
251 m_ptyFile->flush();
|
Chris@84
|
252 return;
|
Chris@111
|
253 } else {
|
Chris@111
|
254 DEBUG << "HgRunner::getPassword: user cancelled" << endl;
|
Chris@111
|
255 killCurrentCommand();
|
Chris@111
|
256 return;
|
Chris@84
|
257 }
|
Chris@75
|
258 }
|
Chris@84
|
259 // user cancelled or something went wrong
|
Chris@111
|
260 DEBUG << "HgRunner::getPassword: something went wrong" << endl;
|
Chris@84
|
261 killCurrentCommand();
|
Chris@84
|
262 }
|
Chris@84
|
263
|
Chris@113
|
264 bool HgRunner::checkPrompts(QString chunk)
|
Chris@84
|
265 {
|
Chris@93
|
266 //DEBUG << "checkPrompts: " << chunk << endl;
|
Chris@84
|
267
|
Chris@128
|
268 if (!m_currentAction.mayBeInteractive()) return false;
|
Chris@128
|
269
|
Chris@84
|
270 QString text = chunk.trimmed();
|
Chris@84
|
271 QString lower = text.toLower();
|
Chris@84
|
272 if (lower.endsWith("password:")) {
|
Chris@84
|
273 getPassword();
|
Chris@113
|
274 return true;
|
Chris@84
|
275 }
|
Chris@128
|
276 if (lower.endsWith("user:") || lower.endsWith("username:")) {
|
Chris@84
|
277 getUsername();
|
Chris@113
|
278 return true;
|
Chris@84
|
279 }
|
Chris@128
|
280 QRegExp userRe("\\buser(name)?:\\s*([^\\s]+)");
|
Chris@84
|
281 if (userRe.indexIn(text) >= 0) {
|
Chris@128
|
282 noteUsername(userRe.cap(2));
|
Chris@84
|
283 }
|
Chris@84
|
284 QRegExp realmRe("\\brealmr:\\s*([^\\s]+)");
|
Chris@84
|
285 if (realmRe.indexIn(text) >= 0) {
|
Chris@84
|
286 noteRealm(realmRe.cap(1));
|
Chris@84
|
287 }
|
Chris@113
|
288 return false;
|
Chris@84
|
289 }
|
Chris@84
|
290
|
Chris@110
|
291 void HgRunner::dataReadyStdout()
|
Chris@84
|
292 {
|
Chris@110
|
293 DEBUG << "dataReadyStdout" << endl;
|
Chris@408
|
294 if (!m_proc) return;
|
Chris@110
|
295 QString chunk = QString::fromUtf8(m_proc->readAllStandardOutput());
|
Chris@113
|
296 if (!checkPrompts(chunk)) {
|
Chris@113
|
297 m_stdout += chunk;
|
Chris@113
|
298 }
|
Chris@110
|
299 }
|
Chris@110
|
300
|
Chris@110
|
301 void HgRunner::dataReadyStderr()
|
Chris@110
|
302 {
|
Chris@110
|
303 DEBUG << "dataReadyStderr" << endl;
|
Chris@408
|
304 if (!m_proc) return;
|
Chris@110
|
305 QString chunk = QString::fromUtf8(m_proc->readAllStandardError());
|
Chris@113
|
306 DEBUG << chunk;
|
Chris@113
|
307 if (!checkPrompts(chunk)) {
|
Chris@113
|
308 m_stderr += chunk;
|
Chris@113
|
309 }
|
Chris@113
|
310 }
|
Chris@113
|
311
|
Chris@113
|
312 void HgRunner::dataReadyPty()
|
Chris@113
|
313 {
|
Chris@113
|
314 DEBUG << "dataReadyPty" << endl;
|
Chris@113
|
315 QString chunk = QString::fromUtf8(m_ptyFile->readAll());
|
Chris@113
|
316 DEBUG << "chunk of " << chunk.length() << " chars" << endl;
|
Chris@113
|
317 if (!checkPrompts(chunk)) {
|
Chris@113
|
318 m_stdout += chunk;
|
Chris@113
|
319 }
|
Chris@75
|
320 }
|
Chris@75
|
321
|
Chris@189
|
322 void HgRunner::error(QProcess::ProcessError)
|
Chris@189
|
323 {
|
Chris@189
|
324 finished(-1, QProcess::CrashExit);
|
Chris@189
|
325 }
|
Chris@189
|
326
|
jtkorhonen@0
|
327 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
328 {
|
chris@385
|
329 if (!m_proc) return;
|
chris@385
|
330
|
chris@385
|
331 // Save the current action and reset m_currentAction before we
|
Chris@109
|
332 // emit a signal to mark the completion; otherwise we may be
|
Chris@109
|
333 // resetting the action after a slot has already tried to set it
|
Chris@109
|
334 // to something else to start a new action
|
Chris@109
|
335
|
Chris@109
|
336 HgAction completedAction = m_currentAction;
|
Chris@109
|
337
|
Chris@84
|
338 m_isRunning = false;
|
Chris@109
|
339 m_currentAction = HgAction();
|
Chris@84
|
340
|
Chris@195
|
341 //closeProcInput();
|
Chris@189
|
342 m_proc->deleteLater();
|
Chris@113
|
343 m_proc = 0;
|
jtkorhonen@0
|
344
|
Chris@109
|
345 if (completedAction.action == ACT_NONE) {
|
Chris@109
|
346 DEBUG << "HgRunner::finished: WARNING: completed action is ACT_NONE" << endl;
|
Chris@62
|
347 } else {
|
Chris@113
|
348 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
|
Chris@113
|
349 DEBUG << "HgRunner::finished: Command completed successfully"
|
Chris@113
|
350 << endl;
|
Chris@124
|
351 // DEBUG << "stdout is " << m_stdout << endl;
|
Chris@113
|
352 emit commandCompleted(completedAction, m_stdout);
|
Chris@113
|
353 } else {
|
Chris@113
|
354 DEBUG << "HgRunner::finished: Command failed, exit code "
|
Chris@113
|
355 << procExitCode << ", exit status " << procExitStatus
|
Chris@113
|
356 << ", stderr follows" << endl;
|
Chris@113
|
357 DEBUG << m_stderr << endl;
|
Chris@113
|
358 emit commandFailed(completedAction, m_stderr);
|
Chris@113
|
359 }
|
jtkorhonen@0
|
360 }
|
Chris@109
|
361
|
Chris@109
|
362 checkQueue();
|
jtkorhonen@0
|
363 }
|
jtkorhonen@0
|
364
|
Chris@182
|
365 void HgRunner::killCurrentActions()
|
Chris@182
|
366 {
|
Chris@182
|
367 m_queue.clear();
|
Chris@182
|
368 killCurrentCommand();
|
Chris@182
|
369 }
|
Chris@182
|
370
|
Chris@62
|
371 void HgRunner::killCurrentCommand()
|
jtkorhonen@0
|
372 {
|
Chris@109
|
373 if (m_isRunning) {
|
Chris@113
|
374 m_currentAction.action = ACT_NONE; // so that we don't bother to notify
|
chris@385
|
375 if (m_proc) m_proc->kill();
|
jtkorhonen@0
|
376 }
|
jtkorhonen@0
|
377 }
|
jtkorhonen@0
|
378
|
Chris@109
|
379 void HgRunner::checkQueue()
|
Chris@62
|
380 {
|
Chris@109
|
381 if (m_isRunning) {
|
Chris@109
|
382 return;
|
Chris@109
|
383 }
|
Chris@109
|
384 if (m_queue.empty()) {
|
Chris@109
|
385 hide();
|
Chris@109
|
386 return;
|
Chris@109
|
387 }
|
Chris@109
|
388 HgAction toRun = m_queue.front();
|
Chris@109
|
389 m_queue.pop_front();
|
Chris@109
|
390 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
|
Chris@109
|
391 startCommand(toRun);
|
Chris@109
|
392 }
|
Chris@109
|
393
|
Chris@455
|
394 void HgRunner::pruneOldAuthFiles()
|
Chris@455
|
395 {
|
Chris@455
|
396 QString path = QDesktopServices::storageLocation
|
Chris@455
|
397 (QDesktopServices::CacheLocation);
|
Chris@455
|
398 QDir d(path);
|
Chris@455
|
399 if (!d.exists()) return;
|
Chris@455
|
400 QStringList filters;
|
Chris@455
|
401 filters << "easyhg.*.dat";
|
Chris@455
|
402 QStringList fl = d.entryList(filters);
|
Chris@455
|
403 foreach (QString f, fl) {
|
Chris@455
|
404 QStringList parts = f.split('.');
|
Chris@455
|
405 if (parts.size() > 1) {
|
Chris@455
|
406 int pid = parts[1].toInt();
|
Chris@455
|
407 DEBUG << "Checking pid " << pid << " for cache file " << f << endl;
|
Chris@455
|
408
|
Chris@455
|
409 ProcessStatus ps = GetProcessStatus(pid);
|
Chris@455
|
410 if (ps == ProcessNotRunning) {
|
Chris@455
|
411 DEBUG << "Removing stale cache file " << f << endl;
|
Chris@455
|
412 QDir(d).remove(f);
|
Chris@455
|
413 }
|
Chris@455
|
414 }
|
Chris@455
|
415 }
|
Chris@455
|
416 }
|
Chris@455
|
417
|
Chris@455
|
418 QString HgRunner::getAuthFilePath()
|
Chris@455
|
419 {
|
Chris@455
|
420 if (m_authFilePath == "") {
|
Chris@455
|
421
|
Chris@455
|
422 pruneOldAuthFiles();
|
Chris@455
|
423
|
Chris@455
|
424 QByteArray fileExt = randomKey();
|
Chris@455
|
425 if (fileExt == QByteArray()) {
|
Chris@455
|
426 DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth file ext" << endl;
|
Chris@455
|
427 return "";
|
Chris@455
|
428 }
|
Chris@455
|
429 QString fileExt16 = QString::fromLocal8Bit(fileExt.toBase64()).left(16)
|
Chris@455
|
430 .replace('+', '-').replace('/', '_');
|
Chris@455
|
431 QString path = QDesktopServices::storageLocation
|
Chris@455
|
432 (QDesktopServices::CacheLocation);
|
Chris@455
|
433 QDir().mkpath(path);
|
Chris@455
|
434 if (path == "") {
|
Chris@455
|
435 DEBUG << "HgRunner::addExtensionOptions: Failed to get cache location" << endl;
|
Chris@455
|
436 return "";
|
Chris@455
|
437 }
|
Chris@455
|
438
|
Chris@455
|
439 m_authFilePath = QString("%1/easyhg.%2.%3.dat").arg(path)
|
Chris@455
|
440 .arg(getpid()).arg(fileExt16);
|
Chris@455
|
441 }
|
Chris@455
|
442
|
Chris@455
|
443 return m_authFilePath;
|
Chris@455
|
444 }
|
Chris@455
|
445
|
Chris@455
|
446 QString HgRunner::getAuthKey()
|
Chris@455
|
447 {
|
Chris@455
|
448 if (m_authKey == "") {
|
Chris@455
|
449 QByteArray key = randomKey();
|
Chris@455
|
450 if (key == QByteArray()) {
|
Chris@455
|
451 DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth key" << endl;
|
Chris@455
|
452 return "";
|
Chris@455
|
453 }
|
Chris@455
|
454 QString key16 = QString::fromLocal8Bit(key.toBase64()).left(16);
|
Chris@455
|
455 m_authKey = key16;
|
Chris@455
|
456 }
|
Chris@455
|
457
|
Chris@455
|
458 return m_authKey;
|
Chris@455
|
459 }
|
Chris@455
|
460
|
Chris@444
|
461 QStringList HgRunner::addExtensionOptions(QStringList params)
|
Chris@444
|
462 {
|
Chris@444
|
463 QString extpath = getExtensionLocation();
|
Chris@444
|
464 if (extpath == "") {
|
Chris@444
|
465 DEBUG << "HgRunner::addExtensionOptions: Failed to get extension location" << endl;
|
Chris@444
|
466 return params;
|
Chris@444
|
467 }
|
Chris@444
|
468
|
Chris@455
|
469 QString afp = getAuthFilePath();
|
Chris@455
|
470 QString afk = getAuthKey();
|
Chris@444
|
471
|
Chris@455
|
472 if (afp != "" && afk != "") {
|
Chris@455
|
473 params.push_front(QString("easyhg.authkey=%1").arg(m_authKey));
|
Chris@455
|
474 params.push_front("--config");
|
Chris@455
|
475 params.push_front(QString("easyhg.authfile=%1").arg(m_authFilePath));
|
Chris@455
|
476 params.push_front("--config");
|
Chris@444
|
477 }
|
Chris@444
|
478
|
Chris@444
|
479 // Looks like this one must be without quotes, even though the SSH
|
Chris@444
|
480 // one above only works on Windows if it has quotes (at least where
|
Chris@444
|
481 // there is a space in the path). Odd
|
Chris@444
|
482 params.push_front(QString("extensions.easyhg=%1").arg(extpath));
|
Chris@444
|
483 params.push_front("--config");
|
Chris@444
|
484
|
Chris@444
|
485 return params;
|
Chris@444
|
486 }
|
Chris@444
|
487
|
Chris@109
|
488 void HgRunner::startCommand(HgAction action)
|
Chris@109
|
489 {
|
Chris@109
|
490 QString executable = action.executable;
|
Chris@109
|
491 bool interactive = false;
|
Chris@109
|
492 QStringList params = action.params;
|
Chris@109
|
493
|
Chris@340
|
494 if (action.workingDir.isEmpty()) {
|
Chris@340
|
495 // We require a working directory, never just operate in pwd
|
Chris@340
|
496 emit commandFailed(action, "EasyMercurial: No working directory supplied, will not run Mercurial command without one");
|
Chris@340
|
497 return;
|
Chris@340
|
498 }
|
Chris@340
|
499
|
Chris@257
|
500 QSettings settings;
|
Chris@257
|
501 settings.beginGroup("General");
|
Chris@257
|
502
|
Chris@109
|
503 if (executable == "") {
|
Chris@109
|
504 // This is a Hg command
|
Chris@239
|
505 executable = getHgBinaryName();
|
chris@406
|
506 if (executable == "") executable = "hg";
|
chris@406
|
507
|
chris@406
|
508 QString ssh = getSshBinaryName();
|
chris@406
|
509 if (ssh != "") {
|
chris@406
|
510 params.push_front(QString("ui.ssh=\"%1\"").arg(ssh));
|
chris@406
|
511 params.push_front("--config");
|
chris@406
|
512 }
|
Chris@161
|
513
|
Chris@161
|
514 if (action.mayBeInteractive()) {
|
Chris@161
|
515 params.push_front("ui.interactive=true");
|
Chris@161
|
516 params.push_front("--config");
|
Chris@176
|
517 if (settings.value("useextension", true).toBool()) {
|
Chris@444
|
518 params = addExtensionOptions(params);
|
Chris@176
|
519 }
|
Chris@161
|
520 interactive = true;
|
Chris@161
|
521 }
|
Chris@161
|
522
|
Chris@161
|
523 //!!! want an option to use the mercurial_keyring extension as well
|
Chris@107
|
524 }
|
jtkorhonen@0
|
525
|
Chris@84
|
526 m_isRunning = true;
|
jtkorhonen@0
|
527 setRange(0, 0);
|
Chris@115
|
528 if (!action.shouldBeFast()) show();
|
Chris@110
|
529 m_stdout.clear();
|
Chris@110
|
530 m_stderr.clear();
|
Chris@84
|
531 m_realm = "";
|
Chris@84
|
532 m_userName = "";
|
jtkorhonen@0
|
533
|
Chris@113
|
534 m_proc = new QProcess;
|
Chris@113
|
535
|
Chris@177
|
536 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
Chris@177
|
537
|
Chris@177
|
538 #ifdef Q_OS_WIN32
|
Chris@257
|
539 // On Win32 we like to bundle Hg and other executables with EasyHg
|
Chris@177
|
540 if (m_myDirPath != "") {
|
Chris@177
|
541 env.insert("PATH", m_myDirPath + ";" + env.value("PATH"));
|
Chris@172
|
542 }
|
Chris@172
|
543 #endif
|
Chris@172
|
544
|
Chris@257
|
545 #ifdef Q_OS_MAC
|
Chris@321
|
546 if (settings.value("python32", false).toBool()) {
|
Chris@261
|
547 env.insert("VERSIONER_PYTHON_PREFER_32_BIT", "1");
|
Chris@257
|
548 }
|
Chris@257
|
549 #endif
|
Chris@257
|
550
|
Chris@113
|
551 env.insert("LANG", "en_US.utf8");
|
Chris@113
|
552 env.insert("LC_ALL", "en_US.utf8");
|
Chris@408
|
553 env.insert("HGENCODING", "utf8");
|
Chris@113
|
554 env.insert("HGPLAIN", "1");
|
Chris@113
|
555 m_proc->setProcessEnvironment(env);
|
Chris@113
|
556
|
Chris@113
|
557 connect(m_proc, SIGNAL(started()), this, SLOT(started()));
|
Chris@189
|
558 connect(m_proc, SIGNAL(error(QProcess::ProcessError)),
|
Chris@189
|
559 this, SLOT(error(QProcess::ProcessError)));
|
Chris@113
|
560 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
|
Chris@113
|
561 this, SLOT(finished(int, QProcess::ExitStatus)));
|
Chris@113
|
562 connect(m_proc, SIGNAL(readyReadStandardOutput()),
|
Chris@113
|
563 this, SLOT(dataReadyStdout()));
|
Chris@113
|
564 connect(m_proc, SIGNAL(readyReadStandardError()),
|
Chris@113
|
565 this, SLOT(dataReadyStderr()));
|
Chris@113
|
566
|
Chris@340
|
567 m_proc->setWorkingDirectory(action.workingDir);
|
jtkorhonen@0
|
568
|
Chris@107
|
569 if (interactive) {
|
Chris@111
|
570 openTerminal();
|
Chris@111
|
571 if (m_ptySlaveFilename != "") {
|
Chris@113
|
572 DEBUG << "HgRunner: connecting to pseudoterminal" << endl;
|
Chris@107
|
573 m_proc->setStandardInputFile(m_ptySlaveFilename);
|
Chris@114
|
574 // m_proc->setStandardOutputFile(m_ptySlaveFilename);
|
Chris@113
|
575 // m_proc->setStandardErrorFile(m_ptySlaveFilename);
|
Chris@107
|
576 }
|
Chris@84
|
577 }
|
Chris@84
|
578
|
Chris@109
|
579 QString cmdline = executable;
|
Chris@57
|
580 foreach (QString param, params) cmdline += " " + param;
|
Chris@64
|
581 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
|
Chris@109
|
582 << action.workingDir << endl;
|
Chris@43
|
583
|
Chris@109
|
584 m_currentAction = action;
|
Chris@109
|
585
|
Chris@113
|
586 // fill these out with what we actually ran
|
Chris@113
|
587 m_currentAction.executable = executable;
|
Chris@113
|
588 m_currentAction.params = params;
|
Chris@113
|
589
|
Chris@109
|
590 DEBUG << "set current action to " << m_currentAction.action << endl;
|
Chris@109
|
591
|
Chris@238
|
592 emit commandStarting(action);
|
Chris@238
|
593
|
Chris@109
|
594 m_proc->start(executable, params);
|
Chris@84
|
595 }
|
Chris@84
|
596
|
Chris@84
|
597 void HgRunner::closeProcInput()
|
Chris@84
|
598 {
|
Chris@84
|
599 DEBUG << "closeProcInput" << endl;
|
Chris@84
|
600
|
chris@385
|
601 if (m_proc) m_proc->closeWriteChannel();
|
Chris@111
|
602 }
|
Chris@111
|
603
|
Chris@111
|
604 void HgRunner::openTerminal()
|
Chris@111
|
605 {
|
Chris@111
|
606 #ifndef Q_OS_WIN32
|
Chris@111
|
607 if (m_ptySlaveFilename != "") return; // already open
|
Chris@111
|
608 DEBUG << "HgRunner::openTerminal: trying to open new pty" << endl;
|
Chris@111
|
609 int master = posix_openpt(O_RDWR | O_NOCTTY);
|
Chris@111
|
610 if (master < 0) {
|
Chris@111
|
611 DEBUG << "openpt failed" << endl;
|
Chris@111
|
612 perror("openpt failed");
|
Chris@111
|
613 return;
|
Chris@111
|
614 }
|
Chris@113
|
615 struct termios t;
|
Chris@113
|
616 if (tcgetattr(master, &t)) {
|
Chris@113
|
617 DEBUG << "tcgetattr failed" << endl;
|
Chris@113
|
618 perror("tcgetattr failed");
|
Chris@113
|
619 }
|
Chris@113
|
620 cfmakeraw(&t);
|
Chris@113
|
621 if (tcsetattr(master, TCSANOW, &t)) {
|
Chris@113
|
622 DEBUG << "tcsetattr failed" << endl;
|
Chris@113
|
623 perror("tcsetattr failed");
|
Chris@113
|
624 }
|
Chris@111
|
625 if (grantpt(master)) {
|
Chris@111
|
626 perror("grantpt failed");
|
Chris@111
|
627 }
|
Chris@111
|
628 if (unlockpt(master)) {
|
Chris@111
|
629 perror("unlockpt failed");
|
Chris@111
|
630 }
|
Chris@111
|
631 char *slave = ptsname(master);
|
Chris@111
|
632 if (!slave) {
|
Chris@111
|
633 perror("ptsname failed");
|
Chris@111
|
634 ::close(master);
|
Chris@111
|
635 return;
|
Chris@111
|
636 }
|
Chris@111
|
637 m_ptyMasterFd = master;
|
Chris@113
|
638 m_ptyFile = new QFile();
|
Chris@113
|
639 connect(m_ptyFile, SIGNAL(readyRead()), this, SLOT(dataReadyPty()));
|
Chris@113
|
640 if (!m_ptyFile->open(m_ptyMasterFd, QFile::ReadWrite)) {
|
Chris@113
|
641 DEBUG << "HgRunner::openTerminal: Failed to open QFile on master fd" << endl;
|
Chris@113
|
642 }
|
Chris@111
|
643 m_ptySlaveFilename = slave;
|
Chris@111
|
644 DEBUG << "HgRunner::openTerminal: succeeded, slave is "
|
Chris@111
|
645 << m_ptySlaveFilename << endl;
|
Chris@111
|
646 #endif
|
Chris@111
|
647 }
|
Chris@111
|
648
|
Chris@111
|
649 void HgRunner::closeTerminal()
|
Chris@111
|
650 {
|
Chris@84
|
651 #ifndef Q_OS_WIN32
|
Chris@84
|
652 if (m_ptySlaveFilename != "") {
|
Chris@113
|
653 delete m_ptyFile;
|
Chris@113
|
654 m_ptyFile = 0;
|
Chris@84
|
655 ::close(m_ptyMasterFd);
|
Chris@84
|
656 m_ptySlaveFilename = "";
|
Chris@84
|
657 }
|
Chris@84
|
658 #endif
|
jtkorhonen@0
|
659 }
|