annotate hgrunner.cpp @ 180:3d6d826b04ad

* Similarly, tidier lookup for Hg binary and extension
author Chris Cannam
date Fri, 17 Dec 2010 08:53:54 +0000
parents bb89bcd8986b
children bf366e0b9050
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@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@161 29 #include <QDir>
jtkorhonen@0 30
Chris@43 31 #include <iostream>
Chris@75 32 #include <errno.h>
Chris@75 33 #include <stdio.h>
Chris@111 34 #include <stdlib.h>
jtkorhonen@0 35
Chris@76 36 #ifndef Q_OS_WIN32
Chris@111 37 #include <unistd.h>
Chris@113 38 #include <termios.h>
Chris@111 39 #include <fcntl.h>
Chris@80 40 #endif
Chris@76 41
Chris@172 42 HgRunner::HgRunner(QString myDirPath, QWidget * parent) :
Chris@172 43 QProgressBar(parent),
Chris@172 44 m_myDirPath(myDirPath)
jtkorhonen@0 45 {
Chris@113 46 m_proc = 0;
Chris@84 47
jtkorhonen@0 48 setTextVisible(false);
jtkorhonen@0 49 setVisible(false);
Chris@84 50 m_isRunning = false;
Chris@161 51
Chris@180 52 (void)findExtension();
Chris@180 53 (void)findHgBinaryName();
jtkorhonen@0 54 }
jtkorhonen@0 55
jtkorhonen@0 56 HgRunner::~HgRunner()
jtkorhonen@0 57 {
Chris@111 58 closeTerminal();
Chris@120 59 if (m_proc) {
Chris@120 60 m_proc->kill();
Chris@122 61 m_proc->deleteLater();
Chris@120 62 }
jtkorhonen@0 63 }
jtkorhonen@0 64
Chris@180 65 QString HgRunner::findExtension()
Chris@172 66 {
Chris@176 67 QSettings settings;
Chris@176 68 settings.beginGroup("Locations");
Chris@180 69 QString extpath = settings.value("extensionpath", "").toString();
Chris@180 70 if (extpath != "") return extpath;
Chris@180 71 extpath = findInPath("easyhg.py", m_myDirPath, false);
Chris@180 72 if (extpath == "easyhg.py") {
Chris@180 73 extpath = unbundleExtension();
Chris@172 74 }
Chris@180 75 settings.setValue("extensionpath", extpath);
Chris@180 76 return extpath;
Chris@172 77 }
Chris@172 78
Chris@180 79 QString HgRunner::unbundleExtension()
Chris@161 80 {
Chris@161 81 QString bundled = ":easyhg.py";
Chris@166 82 QString home = QDir::homePath();
Chris@161 83 QString target = QString("%1/.easyhg").arg(home);
Chris@161 84 if (!QDir().mkpath(target)) {
Chris@161 85 DEBUG << "Failed to make unbundle path " << target << endl;
Chris@161 86 std::cerr << "Failed to make unbundle path " << target.toStdString() << std::endl;
Chris@180 87 return "";
Chris@161 88 }
Chris@161 89 QFile bf(bundled);
Chris@180 90 if (!bf.exists()) {
Chris@180 91 DEBUG << "Bundled extension is missing!" << endl;
Chris@180 92 return "";
Chris@180 93 }
Chris@180 94 QString extpath = QString("%1/easyhg.py").arg(target);
Chris@180 95 if (QFile(extpath).exists()) {
Chris@180 96 QFile(extpath).remove();
Chris@180 97 }
Chris@180 98 if (!bf.copy(extpath)) {
Chris@161 99 DEBUG << "Failed to unbundle extension to " << target << endl;
Chris@180 100 std::cerr << "Failed to unbundle extension to " << extpath.toStdString() << std::endl;
Chris@180 101 return "";
Chris@161 102 }
Chris@180 103 DEBUG << "Unbundled extension to " << extpath << endl;
Chris@180 104 return extpath;
Chris@161 105 }
Chris@161 106
Chris@109 107 void HgRunner::requestAction(HgAction action)
Chris@109 108 {
Chris@109 109 DEBUG << "requestAction " << action.action << endl;
Chris@109 110 bool pushIt = true;
Chris@109 111 if (m_queue.empty()) {
Chris@109 112 if (action == m_currentAction) {
Chris@109 113 // this request is identical to the thing we're executing
Chris@109 114 DEBUG << "requestAction: we're already handling this one, ignoring identical request" << endl;
Chris@109 115 pushIt = false;
Chris@109 116 }
Chris@109 117 } else {
Chris@109 118 HgAction last = m_queue.back();
Chris@109 119 if (action == last) {
Chris@109 120 // this request is identical to the previous thing we
Chris@109 121 // queued which we haven't executed yet
Chris@109 122 DEBUG << "requestAction: we're already queueing this one, ignoring identical request" << endl;
Chris@109 123 pushIt = false;
Chris@109 124 }
Chris@109 125 }
Chris@109 126 if (pushIt) m_queue.push_back(action);
Chris@109 127 checkQueue();
Chris@109 128 }
Chris@109 129
Chris@180 130 QString HgRunner::findHgBinaryName()
Chris@62 131 {
Chris@62 132 QSettings settings;
Chris@175 133 settings.beginGroup("Locations");
Chris@77 134 QString hg = settings.value("hgbinary", "").toString();
Chris@77 135 if (hg == "") {
Chris@172 136 hg = findInPath("hg", m_myDirPath, true);
Chris@77 137 }
Chris@77 138 if (hg != "hg") {
Chris@77 139 settings.setValue("hgbinary", hg);
Chris@77 140 }
Chris@62 141 return hg;
Chris@62 142 }
Chris@62 143
jtkorhonen@0 144 void HgRunner::started()
jtkorhonen@0 145 {
Chris@104 146 DEBUG << "started" << endl;
Chris@75 147 /*
Chris@104 148 m_proc->write("blah\n");
Chris@104 149 m_proc->write("blah\n");
Chris@104 150 m_proc -> closeWriteChannel();
Chris@75 151 */
jtkorhonen@0 152 }
jtkorhonen@0 153
Chris@84 154 void HgRunner::noteUsername(QString name)
Chris@75 155 {
Chris@84 156 m_userName = name;
Chris@75 157 }
Chris@75 158
Chris@84 159 void HgRunner::noteRealm(QString realm)
Chris@75 160 {
Chris@84 161 m_realm = realm;
Chris@84 162 }
Chris@84 163
Chris@84 164 void HgRunner::getUsername()
Chris@84 165 {
Chris@113 166 if (m_ptyFile) {
Chris@84 167 bool ok = false;
Chris@84 168 QString prompt = tr("User name:");
Chris@84 169 if (m_realm != "") {
Chris@84 170 prompt = tr("User name for \"%1\":").arg(m_realm);
Chris@84 171 }
Chris@84 172 QString pwd = QInputDialog::getText
Chris@84 173 (qobject_cast<QWidget *>(parent()),
Chris@84 174 tr("Enter user name"), prompt,
Chris@84 175 QLineEdit::Normal, QString(), &ok);
Chris@84 176 if (ok) {
Chris@113 177 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
Chris@113 178 m_ptyFile->flush();
Chris@84 179 return;
Chris@111 180 } else {
Chris@111 181 DEBUG << "HgRunner::getUsername: user cancelled" << endl;
Chris@111 182 killCurrentCommand();
Chris@111 183 return;
Chris@84 184 }
Chris@84 185 }
Chris@84 186 // user cancelled or something went wrong
Chris@111 187 DEBUG << "HgRunner::getUsername: something went wrong" << endl;
Chris@84 188 killCurrentCommand();
Chris@84 189 }
Chris@84 190
Chris@84 191 void HgRunner::getPassword()
Chris@84 192 {
Chris@113 193 if (m_ptyFile) {
Chris@84 194 bool ok = false;
Chris@84 195 QString prompt = tr("Password:");
Chris@84 196 if (m_userName != "") {
Chris@84 197 if (m_realm != "") {
Chris@84 198 prompt = tr("Password for \"%1\" at \"%2\":")
Chris@84 199 .arg(m_userName).arg(m_realm);
Chris@75 200 } else {
Chris@84 201 prompt = tr("Password for user \"%1\":")
Chris@84 202 .arg(m_userName);
Chris@75 203 }
Chris@75 204 }
Chris@84 205 QString pwd = QInputDialog::getText
Chris@84 206 (qobject_cast<QWidget *>(parent()),
Chris@84 207 tr("Enter password"), prompt,
Chris@84 208 QLineEdit::Password, QString(), &ok);
Chris@84 209 if (ok) {
Chris@113 210 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
Chris@113 211 m_ptyFile->flush();
Chris@84 212 return;
Chris@111 213 } else {
Chris@111 214 DEBUG << "HgRunner::getPassword: user cancelled" << endl;
Chris@111 215 killCurrentCommand();
Chris@111 216 return;
Chris@84 217 }
Chris@75 218 }
Chris@84 219 // user cancelled or something went wrong
Chris@111 220 DEBUG << "HgRunner::getPassword: something went wrong" << endl;
Chris@84 221 killCurrentCommand();
Chris@84 222 }
Chris@84 223
Chris@113 224 bool HgRunner::checkPrompts(QString chunk)
Chris@84 225 {
Chris@93 226 //DEBUG << "checkPrompts: " << chunk << endl;
Chris@84 227
Chris@128 228 if (!m_currentAction.mayBeInteractive()) return false;
Chris@128 229
Chris@84 230 QString text = chunk.trimmed();
Chris@84 231 QString lower = text.toLower();
Chris@84 232 if (lower.endsWith("password:")) {
Chris@84 233 getPassword();
Chris@113 234 return true;
Chris@84 235 }
Chris@128 236 if (lower.endsWith("user:") || lower.endsWith("username:")) {
Chris@84 237 getUsername();
Chris@113 238 return true;
Chris@84 239 }
Chris@128 240 QRegExp userRe("\\buser(name)?:\\s*([^\\s]+)");
Chris@84 241 if (userRe.indexIn(text) >= 0) {
Chris@128 242 noteUsername(userRe.cap(2));
Chris@84 243 }
Chris@84 244 QRegExp realmRe("\\brealmr:\\s*([^\\s]+)");
Chris@84 245 if (realmRe.indexIn(text) >= 0) {
Chris@84 246 noteRealm(realmRe.cap(1));
Chris@84 247 }
Chris@113 248 return false;
Chris@84 249 }
Chris@84 250
Chris@110 251 void HgRunner::dataReadyStdout()
Chris@84 252 {
Chris@110 253 DEBUG << "dataReadyStdout" << endl;
Chris@110 254 QString chunk = QString::fromUtf8(m_proc->readAllStandardOutput());
Chris@113 255 if (!checkPrompts(chunk)) {
Chris@113 256 m_stdout += chunk;
Chris@113 257 }
Chris@110 258 }
Chris@110 259
Chris@110 260 void HgRunner::dataReadyStderr()
Chris@110 261 {
Chris@110 262 DEBUG << "dataReadyStderr" << endl;
Chris@110 263 QString chunk = QString::fromUtf8(m_proc->readAllStandardError());
Chris@113 264 DEBUG << chunk;
Chris@113 265 if (!checkPrompts(chunk)) {
Chris@113 266 m_stderr += chunk;
Chris@113 267 }
Chris@113 268 }
Chris@113 269
Chris@113 270 void HgRunner::dataReadyPty()
Chris@113 271 {
Chris@113 272 DEBUG << "dataReadyPty" << endl;
Chris@113 273 QString chunk = QString::fromUtf8(m_ptyFile->readAll());
Chris@113 274 DEBUG << "chunk of " << chunk.length() << " chars" << endl;
Chris@113 275 if (!checkPrompts(chunk)) {
Chris@113 276 m_stdout += chunk;
Chris@113 277 }
Chris@75 278 }
Chris@75 279
jtkorhonen@0 280 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
jtkorhonen@0 281 {
Chris@109 282 // Save the current action and reset m_currentAction before we
Chris@109 283 // emit a signal to mark the completion; otherwise we may be
Chris@109 284 // resetting the action after a slot has already tried to set it
Chris@109 285 // to something else to start a new action
Chris@109 286
Chris@109 287 HgAction completedAction = m_currentAction;
Chris@109 288
Chris@84 289 m_isRunning = false;
Chris@109 290 m_currentAction = HgAction();
Chris@84 291
Chris@84 292 closeProcInput();
Chris@113 293 delete m_proc;
Chris@113 294 m_proc = 0;
jtkorhonen@0 295
Chris@109 296 if (completedAction.action == ACT_NONE) {
Chris@109 297 DEBUG << "HgRunner::finished: WARNING: completed action is ACT_NONE" << endl;
Chris@62 298 } else {
Chris@113 299 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
Chris@113 300 DEBUG << "HgRunner::finished: Command completed successfully"
Chris@113 301 << endl;
Chris@124 302 // DEBUG << "stdout is " << m_stdout << endl;
Chris@113 303 emit commandCompleted(completedAction, m_stdout);
Chris@113 304 } else {
Chris@113 305 DEBUG << "HgRunner::finished: Command failed, exit code "
Chris@113 306 << procExitCode << ", exit status " << procExitStatus
Chris@113 307 << ", stderr follows" << endl;
Chris@113 308 DEBUG << m_stderr << endl;
Chris@113 309 emit commandFailed(completedAction, m_stderr);
Chris@113 310 }
jtkorhonen@0 311 }
Chris@109 312
Chris@109 313 checkQueue();
jtkorhonen@0 314 }
jtkorhonen@0 315
Chris@62 316 void HgRunner::killCurrentCommand()
jtkorhonen@0 317 {
Chris@109 318 if (m_isRunning) {
Chris@113 319 m_currentAction.action = ACT_NONE; // so that we don't bother to notify
Chris@113 320 m_proc->kill();
jtkorhonen@0 321 }
jtkorhonen@0 322 }
jtkorhonen@0 323
Chris@109 324 void HgRunner::checkQueue()
Chris@62 325 {
Chris@109 326 if (m_isRunning) {
Chris@109 327 return;
Chris@109 328 }
Chris@109 329 if (m_queue.empty()) {
Chris@109 330 hide();
Chris@109 331 return;
Chris@109 332 }
Chris@109 333 HgAction toRun = m_queue.front();
Chris@109 334 m_queue.pop_front();
Chris@109 335 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
Chris@109 336 startCommand(toRun);
Chris@109 337 }
Chris@109 338
Chris@109 339 void HgRunner::startCommand(HgAction action)
Chris@109 340 {
Chris@109 341 QString executable = action.executable;
Chris@109 342 bool interactive = false;
Chris@109 343 QStringList params = action.params;
Chris@109 344
Chris@109 345 if (executable == "") {
Chris@109 346 // This is a Hg command
Chris@180 347 executable = findHgBinaryName();
Chris@161 348
Chris@161 349 if (action.mayBeInteractive()) {
Chris@161 350 params.push_front("ui.interactive=true");
Chris@161 351 params.push_front("--config");
Chris@176 352
Chris@176 353 QSettings settings;
Chris@176 354 settings.beginGroup("General");
Chris@176 355 if (settings.value("useextension", true).toBool()) {
Chris@180 356 QString extpath = findExtension();
Chris@180 357 params.push_front(QString("extensions.easyhg=%1").arg(extpath));
Chris@176 358 params.push_front("--config");
Chris@176 359 }
Chris@161 360 interactive = true;
Chris@161 361 }
Chris@161 362
Chris@161 363 //!!! want an option to use the mercurial_keyring extension as well
Chris@107 364 }
jtkorhonen@0 365
Chris@84 366 m_isRunning = true;
jtkorhonen@0 367 setRange(0, 0);
Chris@115 368 if (!action.shouldBeFast()) show();
Chris@110 369 m_stdout.clear();
Chris@110 370 m_stderr.clear();
Chris@84 371 m_realm = "";
Chris@84 372 m_userName = "";
jtkorhonen@0 373
Chris@113 374 m_proc = new QProcess;
Chris@113 375
Chris@177 376 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
Chris@177 377
Chris@177 378 #ifdef Q_OS_WIN32
Chris@177 379 if (m_myDirPath != "") {
Chris@177 380 env.insert("PATH", m_myDirPath + ";" + env.value("PATH"));
Chris@172 381 }
Chris@172 382 #endif
Chris@172 383
Chris@113 384 env.insert("LANG", "en_US.utf8");
Chris@113 385 env.insert("LC_ALL", "en_US.utf8");
Chris@113 386 env.insert("HGPLAIN", "1");
Chris@113 387 m_proc->setProcessEnvironment(env);
Chris@113 388
Chris@113 389 connect(m_proc, SIGNAL(started()), this, SLOT(started()));
Chris@113 390 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
Chris@113 391 this, SLOT(finished(int, QProcess::ExitStatus)));
Chris@113 392 connect(m_proc, SIGNAL(readyReadStandardOutput()),
Chris@113 393 this, SLOT(dataReadyStdout()));
Chris@113 394 connect(m_proc, SIGNAL(readyReadStandardError()),
Chris@113 395 this, SLOT(dataReadyStderr()));
Chris@113 396
Chris@109 397 if (!action.workingDir.isEmpty()) {
Chris@109 398 m_proc->setWorkingDirectory(action.workingDir);
jtkorhonen@0 399 }
jtkorhonen@0 400
Chris@107 401 if (interactive) {
Chris@111 402 openTerminal();
Chris@111 403 if (m_ptySlaveFilename != "") {
Chris@113 404 DEBUG << "HgRunner: connecting to pseudoterminal" << endl;
Chris@107 405 m_proc->setStandardInputFile(m_ptySlaveFilename);
Chris@114 406 // m_proc->setStandardOutputFile(m_ptySlaveFilename);
Chris@113 407 // m_proc->setStandardErrorFile(m_ptySlaveFilename);
Chris@107 408 }
Chris@84 409 }
Chris@84 410
Chris@109 411 QString cmdline = executable;
Chris@57 412 foreach (QString param, params) cmdline += " " + param;
Chris@64 413 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
Chris@109 414 << action.workingDir << endl;
Chris@43 415
Chris@109 416 m_currentAction = action;
Chris@109 417
Chris@113 418 // fill these out with what we actually ran
Chris@113 419 m_currentAction.executable = executable;
Chris@113 420 m_currentAction.params = params;
Chris@113 421
Chris@109 422 DEBUG << "set current action to " << m_currentAction.action << endl;
Chris@109 423
Chris@109 424 m_proc->start(executable, params);
Chris@84 425 }
Chris@84 426
Chris@84 427 void HgRunner::closeProcInput()
Chris@84 428 {
Chris@84 429 DEBUG << "closeProcInput" << endl;
Chris@84 430
Chris@84 431 m_proc->closeWriteChannel();
Chris@111 432 }
Chris@111 433
Chris@111 434 void HgRunner::openTerminal()
Chris@111 435 {
Chris@111 436 #ifndef Q_OS_WIN32
Chris@111 437 if (m_ptySlaveFilename != "") return; // already open
Chris@111 438 DEBUG << "HgRunner::openTerminal: trying to open new pty" << endl;
Chris@111 439 int master = posix_openpt(O_RDWR | O_NOCTTY);
Chris@111 440 if (master < 0) {
Chris@111 441 DEBUG << "openpt failed" << endl;
Chris@111 442 perror("openpt failed");
Chris@111 443 return;
Chris@111 444 }
Chris@113 445 struct termios t;
Chris@113 446 if (tcgetattr(master, &t)) {
Chris@113 447 DEBUG << "tcgetattr failed" << endl;
Chris@113 448 perror("tcgetattr failed");
Chris@113 449 }
Chris@113 450 cfmakeraw(&t);
Chris@113 451 if (tcsetattr(master, TCSANOW, &t)) {
Chris@113 452 DEBUG << "tcsetattr failed" << endl;
Chris@113 453 perror("tcsetattr failed");
Chris@113 454 }
Chris@111 455 if (grantpt(master)) {
Chris@111 456 perror("grantpt failed");
Chris@111 457 }
Chris@111 458 if (unlockpt(master)) {
Chris@111 459 perror("unlockpt failed");
Chris@111 460 }
Chris@111 461 char *slave = ptsname(master);
Chris@111 462 if (!slave) {
Chris@111 463 perror("ptsname failed");
Chris@111 464 ::close(master);
Chris@111 465 return;
Chris@111 466 }
Chris@111 467 m_ptyMasterFd = master;
Chris@113 468 m_ptyFile = new QFile();
Chris@113 469 connect(m_ptyFile, SIGNAL(readyRead()), this, SLOT(dataReadyPty()));
Chris@113 470 if (!m_ptyFile->open(m_ptyMasterFd, QFile::ReadWrite)) {
Chris@113 471 DEBUG << "HgRunner::openTerminal: Failed to open QFile on master fd" << endl;
Chris@113 472 }
Chris@111 473 m_ptySlaveFilename = slave;
Chris@111 474 DEBUG << "HgRunner::openTerminal: succeeded, slave is "
Chris@111 475 << m_ptySlaveFilename << endl;
Chris@111 476 #endif
Chris@111 477 }
Chris@111 478
Chris@111 479 void HgRunner::closeTerminal()
Chris@111 480 {
Chris@84 481 #ifndef Q_OS_WIN32
Chris@84 482 if (m_ptySlaveFilename != "") {
Chris@113 483 delete m_ptyFile;
Chris@113 484 m_ptyFile = 0;
Chris@84 485 ::close(m_ptyMasterFd);
Chris@84 486 m_ptySlaveFilename = "";
Chris@84 487 }
Chris@84 488 #endif
jtkorhonen@0 489 }