2015-06-04 01:29:22 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
|
2016-03-02 19:00:31 +00:00
|
|
|
* Copyright (C) 2016 by Andrius Štikonas <andrius@stikonas.eu> *
|
2015-06-04 01:29:22 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 3 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "util/externalcommand.h"
|
|
|
|
|
|
|
|
#include "util/report.h"
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
#include <KLocalizedString>
|
|
|
|
|
|
|
|
/** Creates a new ExternalCommand instance without Report.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param cmd the command to run
|
|
|
|
@param args the arguments to pass to the command
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
ExternalCommand::ExternalCommand(const QString& cmd, const QStringList& args) :
|
2016-05-09 00:41:44 +01:00
|
|
|
QProcess(),
|
2015-07-22 14:48:03 +01:00
|
|
|
m_Report(nullptr),
|
2016-05-09 00:41:44 +01:00
|
|
|
m_Command(cmd),
|
|
|
|
m_Args(args),
|
2015-07-13 15:16:36 +01:00
|
|
|
m_ExitCode(-1),
|
|
|
|
m_Output()
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
setup();
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Creates a new ExternalCommand instance with Report.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param report the Report to write output to.
|
|
|
|
@param cmd the command to run
|
|
|
|
@param args the arguments to pass to the command
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
ExternalCommand::ExternalCommand(Report& report, const QString& cmd, const QStringList& args) :
|
2016-05-09 00:41:44 +01:00
|
|
|
QProcess(),
|
2015-07-13 15:16:36 +01:00
|
|
|
m_Report(report.newChild()),
|
|
|
|
m_Command(cmd),
|
|
|
|
m_Args(args),
|
|
|
|
m_ExitCode(-1),
|
|
|
|
m_Output()
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
setup();
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalCommand::setup()
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
setEnvironment(QStringList() << QStringLiteral("LC_ALL=C") << QStringLiteral("PATH=") + QString::fromUtf8(getenv("PATH")));
|
|
|
|
setProcessChannelMode(MergedChannels);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-05-09 00:41:44 +01:00
|
|
|
connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(onFinished(int)));
|
|
|
|
connect(this, SIGNAL(readyReadStandardOutput()), SLOT(onReadOutput()));
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
2016-05-09 00:41:44 +01:00
|
|
|
/** Starts the external command.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param timeout timeout to wait for the process to start
|
|
|
|
@return true on success
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool ExternalCommand::start(int timeout)
|
|
|
|
{
|
2016-05-09 00:41:44 +01:00
|
|
|
QProcess::start(command(), args());
|
2015-07-13 15:16:36 +01:00
|
|
|
|
|
|
|
if (report()) {
|
2016-05-09 00:41:44 +01:00
|
|
|
report()->setCommand(i18nc("@info/plain", "Command: %1 %2", command(), args().join(QStringLiteral(" "))));
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
|
2016-05-09 00:41:44 +01:00
|
|
|
if (!waitForStarted(timeout))
|
|
|
|
{
|
|
|
|
if (report())
|
|
|
|
report()->line() << i18nc("@info/plain", "(Command timeout while starting)");
|
|
|
|
return false;
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
2016-05-09 00:41:44 +01:00
|
|
|
/** Waits for the external command to finish.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param timeout timeout to wait until the process finishes.
|
|
|
|
@return true on success
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool ExternalCommand::waitFor(int timeout)
|
|
|
|
{
|
2016-05-09 00:41:44 +01:00
|
|
|
closeWriteChannel();
|
|
|
|
|
|
|
|
if (!waitForFinished(timeout)) {
|
|
|
|
if (report())
|
|
|
|
report()->line() << i18nc("@info/plain", "(Command timeout while running)");
|
|
|
|
return false;
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
2016-05-09 00:41:44 +01:00
|
|
|
|
|
|
|
onReadOutput();
|
2015-07-13 15:16:36 +01:00
|
|
|
return true;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Runs the command.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param timeout timeout to use for waiting when starting and when waiting for the process to finish
|
|
|
|
@return true on success
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool ExternalCommand::run(int timeout)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
return start(timeout) && waitFor(timeout) && exitStatus() == 0;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalCommand::onReadOutput()
|
|
|
|
{
|
2016-05-09 00:41:44 +01:00
|
|
|
const QString s = QString::fromUtf8(readAllStandardOutput());
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
m_Output += s;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (report())
|
|
|
|
*report() << s;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalCommand::onFinished(int exitCode)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
setExitCode(exitCode);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|