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>
|
jtkorhonen@0
|
31 #include <unistd.h>
|
Chris@75
|
32 #include <pty.h>
|
Chris@75
|
33 #include <errno.h>
|
Chris@75
|
34 #include <stdio.h>
|
jtkorhonen@0
|
35
|
jtkorhonen@0
|
36 HgRunner::HgRunner(QWidget * parent): QProgressBar(parent)
|
jtkorhonen@0
|
37 {
|
jtkorhonen@0
|
38 proc = new QProcess(this);
|
jtkorhonen@0
|
39
|
cannam@45
|
40 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
cannam@45
|
41 env.insert("LANG", "en_US.utf8");
|
cannam@45
|
42 env.insert("LC_ALL", "en_US.utf8");
|
cannam@45
|
43 proc->setProcessEnvironment(env);
|
cannam@45
|
44
|
jtkorhonen@0
|
45 setTextVisible(false);
|
jtkorhonen@0
|
46 setVisible(false);
|
jtkorhonen@0
|
47 isRunning = false;
|
jtkorhonen@0
|
48
|
jtkorhonen@0
|
49 stdOut.clear();
|
jtkorhonen@0
|
50 stdErr.clear();
|
jtkorhonen@0
|
51
|
Chris@75
|
52 procInput = 0;
|
Chris@75
|
53 char name[1024];
|
Chris@75
|
54 if (openpty(&ptyMasterFd, &ptySlaveFd, name, NULL, NULL)) {
|
Chris@75
|
55 perror("openpty failed");
|
Chris@75
|
56 } else {
|
Chris@75
|
57 DEBUG << "openpty succeeded: master " << ptyMasterFd
|
Chris@75
|
58 << " slave " << ptySlaveFd << " filename " << name << endl;
|
Chris@75
|
59 procInput = new QFile;
|
Chris@75
|
60 procInput->open(ptyMasterFd, QFile::WriteOnly);
|
Chris@75
|
61 ptySlaveFilename = name;
|
Chris@75
|
62 proc->setStandardInputFile(ptySlaveFilename);
|
Chris@75
|
63 ::close(ptySlaveFd);
|
Chris@75
|
64 }
|
Chris@75
|
65
|
jtkorhonen@0
|
66 connect(proc, SIGNAL(started()), this, SLOT(started()));
|
Chris@62
|
67 connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)),
|
Chris@62
|
68 this, SLOT(finished(int, QProcess::ExitStatus)));
|
Chris@75
|
69 connect(proc, SIGNAL(readyReadStandardOutput()),
|
Chris@75
|
70 this, SLOT(stdOutReady()));
|
Chris@75
|
71 connect(proc, SIGNAL(readyReadStandardError()),
|
Chris@75
|
72 this, SLOT(stdErrReady()));
|
Chris@62
|
73
|
Chris@62
|
74 reportErrors = false;
|
jtkorhonen@0
|
75 }
|
jtkorhonen@0
|
76
|
jtkorhonen@0
|
77 HgRunner::~HgRunner()
|
jtkorhonen@0
|
78 {
|
Chris@75
|
79 if (ptySlaveFilename != "") {
|
Chris@75
|
80 ::close(ptyMasterFd);
|
Chris@75
|
81 // ::close(ptySlaveFd);
|
Chris@75
|
82 }
|
jtkorhonen@0
|
83 delete proc;
|
jtkorhonen@0
|
84 }
|
jtkorhonen@0
|
85
|
Chris@62
|
86 QString HgRunner::getHgBinaryName()
|
Chris@62
|
87 {
|
Chris@62
|
88 QSettings settings;
|
Chris@62
|
89 QString hg = settings.value("hgbinary", "hg").toString();
|
Chris@62
|
90 if (hg == "") hg = "hg";
|
Chris@62
|
91 hg = findExecutable(hg);
|
Chris@62
|
92 settings.setValue("hgbinary", hg);
|
Chris@62
|
93 return hg;
|
Chris@62
|
94 }
|
Chris@62
|
95
|
jtkorhonen@0
|
96 void HgRunner::started()
|
jtkorhonen@0
|
97 {
|
Chris@75
|
98 /*
|
Chris@75
|
99 if (procInput) procInput->write("blah\n");
|
Chris@75
|
100 if (procInput) procInput->write("blah\n");
|
Chris@75
|
101 if (procInput) {
|
Chris@75
|
102 procInput->close();
|
Chris@75
|
103 // ::close(ptyMasterFd);
|
Chris@75
|
104 }
|
jtkorhonen@0
|
105 proc -> closeWriteChannel();
|
Chris@75
|
106 */
|
jtkorhonen@0
|
107 }
|
jtkorhonen@0
|
108
|
Chris@62
|
109 void HgRunner::saveOutput()
|
Chris@62
|
110 {
|
Chris@75
|
111 stdOut += QString::fromUtf8(proc -> readAllStandardOutput());
|
Chris@75
|
112 stdErr += QString::fromUtf8(proc -> readAllStandardError());
|
Chris@64
|
113
|
Chris@64
|
114 DEBUG << "saveOutput: " << stdOut.split("\n").size() << " line(s) of stdout, " << stdErr.split("\n").size() << " line(s) of stderr" << endl;
|
Chris@64
|
115
|
Chris@64
|
116 // std::cerr << "stdout was " << stdOut.toStdString() << std::endl;
|
Chris@62
|
117 }
|
Chris@62
|
118
|
jtkorhonen@0
|
119 void HgRunner::setProcExitInfo(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
120 {
|
jtkorhonen@0
|
121 exitCode = procExitCode;
|
jtkorhonen@0
|
122 exitStatus = procExitStatus;
|
jtkorhonen@0
|
123 }
|
jtkorhonen@0
|
124
|
jtkorhonen@0
|
125 QString HgRunner::getLastCommandLine()
|
jtkorhonen@0
|
126 {
|
jtkorhonen@0
|
127 return QString("Command line: " + lastHgCommand + " " + lastParams);
|
jtkorhonen@0
|
128 }
|
jtkorhonen@0
|
129
|
Chris@75
|
130 void HgRunner::stdOutReady()
|
Chris@75
|
131 {
|
Chris@75
|
132 DEBUG << "stdOutReady" << endl;
|
Chris@75
|
133 QString chunk = QString::fromUtf8(proc->readAllStandardOutput());
|
Chris@75
|
134 DEBUG << "stdout was " << chunk << endl;
|
Chris@75
|
135 stdOut += chunk;
|
Chris@75
|
136 }
|
Chris@75
|
137
|
Chris@75
|
138 void HgRunner::stdErrReady()
|
Chris@75
|
139 {
|
Chris@75
|
140 DEBUG << "stdErrReady" << endl;
|
Chris@75
|
141 QString chunk = QString::fromUtf8(proc->readAllStandardError());
|
Chris@75
|
142 DEBUG << "stderr was " << chunk << endl;
|
Chris@75
|
143 stdErr += chunk;
|
Chris@75
|
144 if (procInput) {
|
Chris@75
|
145 if (chunk.toLower().trimmed() == "password:") {
|
Chris@75
|
146 bool ok = false;
|
Chris@75
|
147 QString pwd = QInputDialog::getText
|
Chris@75
|
148 (qobject_cast<QWidget *>(parent()),
|
Chris@75
|
149 tr("Enter password"), tr("Password (but for what user name and repository??"),
|
Chris@75
|
150 QLineEdit::Password, QString(), &ok);
|
Chris@75
|
151 if (ok) {
|
Chris@75
|
152 procInput->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@75
|
153 procInput->flush();
|
Chris@75
|
154 } else {
|
Chris@75
|
155 //!!! do what? close the terminal?
|
Chris@75
|
156 }
|
Chris@75
|
157 }
|
Chris@75
|
158 }
|
Chris@75
|
159 }
|
Chris@75
|
160
|
jtkorhonen@0
|
161 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
162 {
|
jtkorhonen@0
|
163 setProcExitInfo(procExitCode, procExitStatus);
|
Chris@64
|
164 saveOutput();
|
Chris@62
|
165 isRunning = false;
|
jtkorhonen@0
|
166
|
Chris@74
|
167 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
|
Chris@74
|
168 DEBUG << "HgRunner::finished: Command completed successfully: stderr says: " << stdErr << endl;
|
Chris@62
|
169 emit commandCompleted();
|
Chris@62
|
170 } else {
|
Chris@74
|
171 DEBUG << "HgRunner::finished: Command failed: stderr says: " << stdErr << endl;
|
Chris@62
|
172 emit commandFailed();
|
jtkorhonen@0
|
173 }
|
jtkorhonen@0
|
174 }
|
jtkorhonen@0
|
175
|
Chris@62
|
176 bool HgRunner::isCommandRunning()
|
jtkorhonen@0
|
177 {
|
jtkorhonen@0
|
178 return isRunning;
|
jtkorhonen@0
|
179 }
|
jtkorhonen@0
|
180
|
Chris@62
|
181 void HgRunner::killCurrentCommand()
|
jtkorhonen@0
|
182 {
|
Chris@62
|
183 if (isCommandRunning()) {
|
jtkorhonen@0
|
184 proc -> kill();
|
jtkorhonen@0
|
185 }
|
jtkorhonen@0
|
186 }
|
jtkorhonen@0
|
187
|
Chris@62
|
188 void HgRunner::startHgCommand(QString workingDir, QStringList params)
|
Chris@62
|
189 {
|
Chris@62
|
190 startCommand(getHgBinaryName(), workingDir, params);
|
Chris@62
|
191 }
|
jtkorhonen@0
|
192
|
Chris@62
|
193 void HgRunner::startCommand(QString command, QString workingDir, QStringList params)
|
jtkorhonen@0
|
194 {
|
jtkorhonen@0
|
195 isRunning = true;
|
jtkorhonen@0
|
196 setRange(0, 0);
|
jtkorhonen@0
|
197 setVisible(true);
|
jtkorhonen@0
|
198 stdOut.clear();
|
jtkorhonen@0
|
199 stdErr.clear();
|
jtkorhonen@0
|
200 exitCode = 0;
|
jtkorhonen@0
|
201 exitStatus = QProcess::NormalExit;
|
jtkorhonen@0
|
202
|
jtkorhonen@0
|
203 if (!workingDir.isEmpty())
|
jtkorhonen@0
|
204 {
|
jtkorhonen@0
|
205 proc -> setWorkingDirectory(workingDir);
|
jtkorhonen@0
|
206 }
|
jtkorhonen@0
|
207
|
Chris@62
|
208 lastHgCommand = command;
|
jtkorhonen@0
|
209 lastParams = params.join(" ");
|
jtkorhonen@0
|
210
|
Chris@62
|
211 QString cmdline = command;
|
Chris@57
|
212 foreach (QString param, params) cmdline += " " + param;
|
Chris@64
|
213 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
|
Chris@64
|
214 << workingDir << endl;
|
Chris@43
|
215
|
Chris@62
|
216 proc -> start(command, params);
|
jtkorhonen@0
|
217 }
|
jtkorhonen@0
|
218
|
jtkorhonen@0
|
219 int HgRunner::getExitCode()
|
jtkorhonen@0
|
220 {
|
jtkorhonen@0
|
221 return exitCode;
|
jtkorhonen@0
|
222 }
|
jtkorhonen@0
|
223
|
jtkorhonen@0
|
224 QString HgRunner::getStdOut()
|
jtkorhonen@0
|
225 {
|
jtkorhonen@0
|
226 return stdOut;
|
jtkorhonen@0
|
227 }
|
jtkorhonen@0
|
228
|
jtkorhonen@0
|
229 void HgRunner::hideProgBar()
|
jtkorhonen@0
|
230 {
|
jtkorhonen@0
|
231 setVisible(false);
|
jtkorhonen@0
|
232 }
|
jtkorhonen@0
|
233
|
jtkorhonen@0
|
234
|