annotate hgrunner.cpp @ 175:6def8bf3be44

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