2023-07-10 15:57:59 +02:00
|
|
|
#include "command.h"
|
|
|
|
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QDebug>
|
2023-08-16 10:39:46 +02:00
|
|
|
#include <QDir>
|
2023-07-10 15:57:59 +02:00
|
|
|
#include <QRegularExpression>
|
|
|
|
|
|
|
|
Command::Command(QString const &command, int start_timeout, int finish_timeout)
|
|
|
|
: m_command(command.trimmed())
|
|
|
|
, m_commandResult("")
|
|
|
|
, m_waitForStartTimeout(start_timeout)
|
2023-08-14 14:28:23 +02:00
|
|
|
, m_waitForFinishTimeout(finish_timeout)
|
|
|
|
, m_exitCode(-1) {
|
2023-07-10 15:57:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Command::getCommandResult() const {
|
|
|
|
return m_commandResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::readyReadStandardOutput() {
|
|
|
|
QProcess *p = (QProcess *)sender();
|
|
|
|
m_commandResult += p->readAllStandardOutput();
|
|
|
|
// qCritical() << m_commandResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::readyReadStandardError() {
|
|
|
|
QProcess *p = (QProcess *)sender();
|
|
|
|
QByteArray buf = p->readAllStandardError();
|
|
|
|
qCritical() << buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::finished(int /*exitCode*/, QProcess::ExitStatus /*exitStatus*/) {
|
|
|
|
QProcess *p = (QProcess *)sender();
|
|
|
|
// read all remaining data sent to the process, just in case
|
|
|
|
QString d = p->readAllStandardOutput();
|
|
|
|
if (!d.isEmpty()) {
|
|
|
|
m_commandResult += d;
|
|
|
|
}
|
|
|
|
disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardOutput()));
|
|
|
|
disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardError()));
|
|
|
|
}
|
|
|
|
|
2023-07-14 12:56:33 +02:00
|
|
|
bool Command::execute(QString workingDirectory, QStringList args) {
|
2023-08-16 10:39:46 +02:00
|
|
|
|
|
|
|
if (!QDir::setCurrent(workingDirectory)) {
|
|
|
|
qCritical() << "SET WORKING_DIRECTORY" << workingDirectory
|
|
|
|
<< "FAILED FOR" << m_command;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-10 15:57:59 +02:00
|
|
|
QScopedPointer<QProcess> p(new QProcess(this));
|
2023-08-16 10:39:46 +02:00
|
|
|
p->setWorkingDirectory(workingDirectory);
|
2023-07-10 15:57:59 +02:00
|
|
|
p->setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
|
|
|
|
connect(&(*p), SIGNAL(readyReadStandardOutput()), this, SLOT(readyReadStandardOutput()));
|
|
|
|
connect(&(*p), SIGNAL(readyReadStandardError()), this, SLOT(readyReadStandardError()));
|
|
|
|
|
2023-07-14 12:56:33 +02:00
|
|
|
if (!args.isEmpty()) {
|
2023-08-16 10:39:46 +02:00
|
|
|
qDebug() << "START COMMAND" << m_command << "WITH ARGS" << args
|
|
|
|
<< "IN" << p->workingDirectory();
|
2023-07-14 12:56:33 +02:00
|
|
|
p->start(m_command, args);
|
|
|
|
} else {
|
2023-08-16 10:39:46 +02:00
|
|
|
qDebug() << "START COMMAND" << m_command
|
|
|
|
<< "IN" << p->workingDirectory();
|
2023-07-14 12:56:33 +02:00
|
|
|
p->start(m_command);
|
|
|
|
}
|
2023-07-10 15:57:59 +02:00
|
|
|
|
|
|
|
if (p->waitForStarted(m_waitForStartTimeout)) {
|
2023-08-16 10:39:46 +02:00
|
|
|
qDebug() << "PROCESS" << m_command << "STARTED IN" << p->workingDirectory();
|
2023-07-10 15:57:59 +02:00
|
|
|
if (p->state() == QProcess::ProcessState::Running) {
|
2023-08-16 10:39:46 +02:00
|
|
|
qDebug() << "PROCESS" << m_command << "RUNNING IN" << p->workingDirectory();
|
2023-07-10 15:57:59 +02:00
|
|
|
if (p->waitForFinished(m_waitForFinishTimeout)) {
|
2023-08-16 10:39:46 +02:00
|
|
|
qDebug() << "PROCESS" << m_command << "FINISHED IN" << p->workingDirectory();
|
2023-07-10 15:57:59 +02:00
|
|
|
if (p->exitStatus() == QProcess::NormalExit) {
|
2023-08-14 14:28:23 +02:00
|
|
|
if ((m_exitCode = p->exitCode()) == 0) {
|
2023-08-16 10:39:46 +02:00
|
|
|
qDebug() << "EXECUTED" << m_command
|
|
|
|
<< "with code" << m_exitCode
|
|
|
|
<< "IN" << p->workingDirectory();
|
2023-08-04 13:31:12 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
2023-08-16 10:39:46 +02:00
|
|
|
qCritical() << "EXECUTED" << m_command
|
|
|
|
<< "with code" << m_exitCode
|
|
|
|
<< "IN" << p->workingDirectory();
|
2023-08-04 13:31:12 +02:00
|
|
|
}
|
2023-07-10 15:57:59 +02:00
|
|
|
} else {
|
|
|
|
qCritical() << "PROCESS" << m_command << "CRASHED with code"
|
2023-08-16 10:39:46 +02:00
|
|
|
<< p->exitCode()
|
|
|
|
<< "IN" << p->workingDirectory();
|
2023-07-10 15:57:59 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-16 10:39:46 +02:00
|
|
|
qCritical() << "PROCESS" << m_command
|
|
|
|
<< "DID NOT FINISH"
|
|
|
|
<< "IN" << p->workingDirectory();
|
2023-07-10 15:57:59 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-16 10:39:46 +02:00
|
|
|
qCritical() << "WRONG PROCESS STATE" << p->state()
|
|
|
|
<< "IN" << p->workingDirectory();
|
2023-07-10 15:57:59 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-16 10:39:46 +02:00
|
|
|
qCritical() << "PROCESS" << m_command << "TIMEOUT AT START"
|
|
|
|
<< "IN" << p->workingDirectory();
|
2023-07-10 15:57:59 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|