annotate hgrunner.cpp @ 176:a6ec8d0bdd34

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