annotate hgrunner.cpp @ 157:e411bb42d934

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