annotate hgrunner.cpp @ 182:bf366e0b9050

* Fix "Serve by HTTP"; stub out "Change Remote Location"
author Chris Cannam
date Fri, 17 Dec 2010 10:01:02 +0000
parents 3d6d826b04ad
children 07b908b4fa5f
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@182 316 void HgRunner::killCurrentActions()
Chris@182 317 {
Chris@182 318 m_queue.clear();
Chris@182 319 killCurrentCommand();
Chris@182 320 }
Chris@182 321
Chris@62 322 void HgRunner::killCurrentCommand()
jtkorhonen@0 323 {
Chris@109 324 if (m_isRunning) {
Chris@113 325 m_currentAction.action = ACT_NONE; // so that we don't bother to notify
Chris@113 326 m_proc->kill();
jtkorhonen@0 327 }
jtkorhonen@0 328 }
jtkorhonen@0 329
Chris@109 330 void HgRunner::checkQueue()
Chris@62 331 {
Chris@109 332 if (m_isRunning) {
Chris@109 333 return;
Chris@109 334 }
Chris@109 335 if (m_queue.empty()) {
Chris@109 336 hide();
Chris@109 337 return;
Chris@109 338 }
Chris@109 339 HgAction toRun = m_queue.front();
Chris@109 340 m_queue.pop_front();
Chris@109 341 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
Chris@109 342 startCommand(toRun);
Chris@109 343 }
Chris@109 344
Chris@109 345 void HgRunner::startCommand(HgAction action)
Chris@109 346 {
Chris@109 347 QString executable = action.executable;
Chris@109 348 bool interactive = false;
Chris@109 349 QStringList params = action.params;
Chris@109 350
Chris@109 351 if (executable == "") {
Chris@109 352 // This is a Hg command
Chris@180 353 executable = findHgBinaryName();
Chris@161 354
Chris@161 355 if (action.mayBeInteractive()) {
Chris@161 356 params.push_front("ui.interactive=true");
Chris@161 357 params.push_front("--config");
Chris@176 358
Chris@176 359 QSettings settings;
Chris@176 360 settings.beginGroup("General");
Chris@176 361 if (settings.value("useextension", true).toBool()) {
Chris@180 362 QString extpath = findExtension();
Chris@180 363 params.push_front(QString("extensions.easyhg=%1").arg(extpath));
Chris@176 364 params.push_front("--config");
Chris@176 365 }
Chris@161 366 interactive = true;
Chris@161 367 }
Chris@161 368
Chris@161 369 //!!! want an option to use the mercurial_keyring extension as well
Chris@107 370 }
jtkorhonen@0 371
Chris@84 372 m_isRunning = true;
jtkorhonen@0 373 setRange(0, 0);
Chris@115 374 if (!action.shouldBeFast()) show();
Chris@110 375 m_stdout.clear();
Chris@110 376 m_stderr.clear();
Chris@84 377 m_realm = "";
Chris@84 378 m_userName = "";
jtkorhonen@0 379
Chris@113 380 m_proc = new QProcess;
Chris@113 381
Chris@177 382 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
Chris@177 383
Chris@177 384 #ifdef Q_OS_WIN32
Chris@177 385 if (m_myDirPath != "") {
Chris@177 386 env.insert("PATH", m_myDirPath + ";" + env.value("PATH"));
Chris@172 387 }
Chris@172 388 #endif
Chris@172 389
Chris@113 390 env.insert("LANG", "en_US.utf8");
Chris@113 391 env.insert("LC_ALL", "en_US.utf8");
Chris@113 392 env.insert("HGPLAIN", "1");
Chris@113 393 m_proc->setProcessEnvironment(env);
Chris@113 394
Chris@113 395 connect(m_proc, SIGNAL(started()), this, SLOT(started()));
Chris@113 396 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
Chris@113 397 this, SLOT(finished(int, QProcess::ExitStatus)));
Chris@113 398 connect(m_proc, SIGNAL(readyReadStandardOutput()),
Chris@113 399 this, SLOT(dataReadyStdout()));
Chris@113 400 connect(m_proc, SIGNAL(readyReadStandardError()),
Chris@113 401 this, SLOT(dataReadyStderr()));
Chris@113 402
Chris@109 403 if (!action.workingDir.isEmpty()) {
Chris@109 404 m_proc->setWorkingDirectory(action.workingDir);
jtkorhonen@0 405 }
jtkorhonen@0 406
Chris@107 407 if (interactive) {
Chris@111 408 openTerminal();
Chris@111 409 if (m_ptySlaveFilename != "") {
Chris@113 410 DEBUG << "HgRunner: connecting to pseudoterminal" << endl;
Chris@107 411 m_proc->setStandardInputFile(m_ptySlaveFilename);
Chris@114 412 // m_proc->setStandardOutputFile(m_ptySlaveFilename);
Chris@113 413 // m_proc->setStandardErrorFile(m_ptySlaveFilename);
Chris@107 414 }
Chris@84 415 }
Chris@84 416
Chris@109 417 QString cmdline = executable;
Chris@57 418 foreach (QString param, params) cmdline += " " + param;
Chris@64 419 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
Chris@109 420 << action.workingDir << endl;
Chris@43 421
Chris@109 422 m_currentAction = action;
Chris@109 423
Chris@113 424 // fill these out with what we actually ran
Chris@113 425 m_currentAction.executable = executable;
Chris@113 426 m_currentAction.params = params;
Chris@113 427
Chris@109 428 DEBUG << "set current action to " << m_currentAction.action << endl;
Chris@109 429
Chris@109 430 m_proc->start(executable, params);
Chris@84 431 }
Chris@84 432
Chris@84 433 void HgRunner::closeProcInput()
Chris@84 434 {
Chris@84 435 DEBUG << "closeProcInput" << endl;
Chris@84 436
Chris@84 437 m_proc->closeWriteChannel();
Chris@111 438 }
Chris@111 439
Chris@111 440 void HgRunner::openTerminal()
Chris@111 441 {
Chris@111 442 #ifndef Q_OS_WIN32
Chris@111 443 if (m_ptySlaveFilename != "") return; // already open
Chris@111 444 DEBUG << "HgRunner::openTerminal: trying to open new pty" << endl;
Chris@111 445 int master = posix_openpt(O_RDWR | O_NOCTTY);
Chris@111 446 if (master < 0) {
Chris@111 447 DEBUG << "openpt failed" << endl;
Chris@111 448 perror("openpt failed");
Chris@111 449 return;
Chris@111 450 }
Chris@113 451 struct termios t;
Chris@113 452 if (tcgetattr(master, &t)) {
Chris@113 453 DEBUG << "tcgetattr failed" << endl;
Chris@113 454 perror("tcgetattr failed");
Chris@113 455 }
Chris@113 456 cfmakeraw(&t);
Chris@113 457 if (tcsetattr(master, TCSANOW, &t)) {
Chris@113 458 DEBUG << "tcsetattr failed" << endl;
Chris@113 459 perror("tcsetattr failed");
Chris@113 460 }
Chris@111 461 if (grantpt(master)) {
Chris@111 462 perror("grantpt failed");
Chris@111 463 }
Chris@111 464 if (unlockpt(master)) {
Chris@111 465 perror("unlockpt failed");
Chris@111 466 }
Chris@111 467 char *slave = ptsname(master);
Chris@111 468 if (!slave) {
Chris@111 469 perror("ptsname failed");
Chris@111 470 ::close(master);
Chris@111 471 return;
Chris@111 472 }
Chris@111 473 m_ptyMasterFd = master;
Chris@113 474 m_ptyFile = new QFile();
Chris@113 475 connect(m_ptyFile, SIGNAL(readyRead()), this, SLOT(dataReadyPty()));
Chris@113 476 if (!m_ptyFile->open(m_ptyMasterFd, QFile::ReadWrite)) {
Chris@113 477 DEBUG << "HgRunner::openTerminal: Failed to open QFile on master fd" << endl;
Chris@113 478 }
Chris@111 479 m_ptySlaveFilename = slave;
Chris@111 480 DEBUG << "HgRunner::openTerminal: succeeded, slave is "
Chris@111 481 << m_ptySlaveFilename << endl;
Chris@111 482 #endif
Chris@111 483 }
Chris@111 484
Chris@111 485 void HgRunner::closeTerminal()
Chris@111 486 {
Chris@84 487 #ifndef Q_OS_WIN32
Chris@84 488 if (m_ptySlaveFilename != "") {
Chris@113 489 delete m_ptyFile;
Chris@113 490 m_ptyFile = 0;
Chris@84 491 ::close(m_ptyMasterFd);
Chris@84 492 m_ptySlaveFilename = "";
Chris@84 493 }
Chris@84 494 #endif
jtkorhonen@0 495 }