annotate src/hgrunner.cpp @ 564:39cac58b4f92

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