checkin for saving current state
This commit is contained in:
		
							
								
								
									
										121
									
								
								common/src/command.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										121
									
								
								common/src/command.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,121 @@
 | 
			
		||||
#include "command.h"
 | 
			
		||||
 | 
			
		||||
#include <QProcess>
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
#include <QDir>
 | 
			
		||||
#include <QRegularExpression>
 | 
			
		||||
#include <QDateTime>
 | 
			
		||||
 | 
			
		||||
Command::Command(QString command, QStringList args, QString workingDirectory,
 | 
			
		||||
                 int start_timeout, int finish_timeout)
 | 
			
		||||
    : m_command(command.trimmed())
 | 
			
		||||
    , m_commandResult("")
 | 
			
		||||
    , m_waitForStartTimeout(start_timeout)
 | 
			
		||||
    , m_waitForFinishTimeout(finish_timeout)
 | 
			
		||||
    , m_exitCode(-1)
 | 
			
		||||
    , m_workingDirectory(workingDirectory)
 | 
			
		||||
    , m_args(args) {
 | 
			
		||||
    m_p.reset(new QProcess(this));
 | 
			
		||||
    if (m_p) {
 | 
			
		||||
        m_p->setWorkingDirectory(workingDirectory);
 | 
			
		||||
        m_p->setProcessChannelMode(QProcess::MergedChannels);
 | 
			
		||||
 | 
			
		||||
        connect(m_p.get(), SIGNAL(readyReadStandardOutput()), this, SLOT(readyReadStandardOutput()));
 | 
			
		||||
        connect(m_p.get(), SIGNAL(readyReadStandardError()), this, SLOT(readyReadStandardError()));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Command::readyReadStandardOutput() {
 | 
			
		||||
    QProcess *p = (QProcess *)sender();
 | 
			
		||||
    if (p) {
 | 
			
		||||
        qCritical().noquote() << p->readAllStandardOutput();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Command::readyReadStandardError() {
 | 
			
		||||
    QProcess *p = (QProcess *)sender();
 | 
			
		||||
    if (p) {
 | 
			
		||||
        qCritical().noquote() << p->readAllStandardError();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
QString Command::getCommandResult(bool reset) {
 | 
			
		||||
    if (reset == false) {
 | 
			
		||||
        return m_commandResult;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    QString commandResult = m_commandResult;
 | 
			
		||||
    m_commandResult.clear();
 | 
			
		||||
 | 
			
		||||
    return commandResult;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Command::exec() {
 | 
			
		||||
 | 
			
		||||
    if (!m_args.isEmpty()) {
 | 
			
		||||
        m_p->start(m_command, m_args);
 | 
			
		||||
    } else {
 | 
			
		||||
        m_p->start(m_command);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qint64 const start = QDateTime::currentDateTime().toMSecsSinceEpoch();
 | 
			
		||||
 | 
			
		||||
    bool started = false;
 | 
			
		||||
    if ((started = m_p->waitForStarted(m_waitForStartTimeout)) == true) {
 | 
			
		||||
        // qCritical() << "PROCESS" << m_command << "STARTED IN" << m_p->workingDirectory();
 | 
			
		||||
        if (m_p->state() == QProcess::ProcessState::Running) {
 | 
			
		||||
            // qDebug() << "PROCESS" << m_command << "RUNNING IN" << p->workingDirectory();
 | 
			
		||||
            // wait forever for git/opkg-commands to finish
 | 
			
		||||
            int wait = m_waitForFinishTimeout;
 | 
			
		||||
            if (m_command.trimmed().startsWith("git", Qt::CaseInsensitive) ||
 | 
			
		||||
                m_command.trimmed().startsWith("opkg", Qt::CaseInsensitive)) {
 | 
			
		||||
                wait = -1;
 | 
			
		||||
            }
 | 
			
		||||
            bool const no_timeout = m_p->waitForFinished(wait);
 | 
			
		||||
            if (no_timeout) {
 | 
			
		||||
                // qDebug() << "PROCESS" << m_command << "FINISHED IN" << p->workingDirectory();
 | 
			
		||||
                if (m_p->exitStatus() == QProcess::NormalExit) {
 | 
			
		||||
                    if ((m_exitCode = m_p->exitCode()) == 0) {
 | 
			
		||||
                        qCritical().noquote() << m_p->readAllStandardOutput();
 | 
			
		||||
                        //qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 | 
			
		||||
                        //qDebug() << "EXECUTED" << m_command
 | 
			
		||||
                        //         << QString("(runtime %1ms)").arg(end-start)
 | 
			
		||||
                        //         << "with code" << m_exitCode
 | 
			
		||||
                        //         << "IN" << m_p->workingDirectory();
 | 
			
		||||
                        return true;
 | 
			
		||||
                    } else {
 | 
			
		||||
                        qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 | 
			
		||||
                        qCritical() << "EXECUTED" << m_command
 | 
			
		||||
                                    << QString("(runtime %1ms)").arg(end-start)
 | 
			
		||||
                                    << "with code" << m_exitCode
 | 
			
		||||
                                    << "IN" << m_p->workingDirectory();
 | 
			
		||||
                    }
 | 
			
		||||
                } else {
 | 
			
		||||
                    qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 | 
			
		||||
                    qCritical() << "PROCESS" << m_command << "CRASHED with code"
 | 
			
		||||
                                << m_p->exitCode()
 | 
			
		||||
                                << QString("(after %1ms)").arg(end-start)
 | 
			
		||||
                                << "IN" << m_p->workingDirectory();
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 | 
			
		||||
                qCritical() << "PROCESS" << m_command
 | 
			
		||||
                            << "DID NOT FINISH WITH" << wait
 | 
			
		||||
                            << "MS IN" << m_p->workingDirectory()
 | 
			
		||||
                            << QString("(runtime %1ms)").arg(end-start);
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            qCritical() << "WRONG PROCESS STATE" << m_p->state()
 | 
			
		||||
                        << "IN" << m_p->workingDirectory();
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 | 
			
		||||
        qCritical() << "PROCESS" << m_command << "TIMEOUT AT START"
 | 
			
		||||
                    << QString("(runtime %1ms)").arg(end-start)
 | 
			
		||||
                    << "IN" << m_p->workingDirectory() << m_waitForStartTimeout;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										55
									
								
								common/src/utils.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								common/src/utils.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
 | 
			
		||||
#include <QFile>
 | 
			
		||||
#include <QDir>
 | 
			
		||||
#include <QTextStream>
 | 
			
		||||
 | 
			
		||||
namespace internal {
 | 
			
		||||
 | 
			
		||||
int read1stLineOfFile(QString fileName) {
 | 
			
		||||
    QFile f(fileName);
 | 
			
		||||
    if (f.exists()) {
 | 
			
		||||
        if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
 | 
			
		||||
            QTextStream in(&f);
 | 
			
		||||
            in.setCodec("UTF-8");
 | 
			
		||||
            while(!in.atEnd()) {
 | 
			
		||||
                return in.readLine().toInt();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString customerRepoRoot() {
 | 
			
		||||
    return "/opt/app/tools/atbupdate/";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString customerRepoDirName() {
 | 
			
		||||
    int const customerNr = read1stLineOfFile("/mnt/system_data/cust_nr");
 | 
			
		||||
    return (customerNr != -1) ? QString("customer_%1").arg(customerNr) : "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString customerRepoDir() {
 | 
			
		||||
    QString const &n = customerRepoDirName();
 | 
			
		||||
    QString const &r = customerRepoRoot();
 | 
			
		||||
    return !n.isEmpty() ? QDir::cleanPath(r + QDir::separator() + n) : "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool customerRepoExists() {
 | 
			
		||||
    QString const repoDir{customerRepoDir()};
 | 
			
		||||
    return !repoDir.isEmpty() ? QDir(repoDir).exists() : false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString repositoryUrl() {
 | 
			
		||||
    return "gitea@ptu-config.atb-comm.de:ATB/";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString branchName() {
 | 
			
		||||
    int const zoneNr = read1stLineOfFile("/mnt/system_data/zone_nr");
 | 
			
		||||
    if (zoneNr != -1) {
 | 
			
		||||
        return QString("zg1/zone%1").arg(zoneNr);
 | 
			
		||||
    }
 | 
			
		||||
    return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace internal
 | 
			
		||||
							
								
								
									
										55
									
								
								common/src/utils_internal.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								common/src/utils_internal.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
 | 
			
		||||
#include <QFile>
 | 
			
		||||
#include <QDir>
 | 
			
		||||
#include <QTextStream>
 | 
			
		||||
 | 
			
		||||
namespace internal {
 | 
			
		||||
 | 
			
		||||
int read1stLineOfFile(QString fileName) {
 | 
			
		||||
    QFile f(fileName);
 | 
			
		||||
    if (f.exists()) {
 | 
			
		||||
        if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
 | 
			
		||||
            QTextStream in(&f);
 | 
			
		||||
            in.setCodec("UTF-8");
 | 
			
		||||
            while(!in.atEnd()) {
 | 
			
		||||
                return in.readLine().toInt();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString customerRepoRoot() {
 | 
			
		||||
    return "/opt/app/tools/atbupdate/";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString customerRepoDirName() {
 | 
			
		||||
    int const customerNr = read1stLineOfFile("/mnt/system_data/cust_nr");
 | 
			
		||||
    return (customerNr != -1) ? QString("customer_%1").arg(customerNr) : "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString customerRepoDir() {
 | 
			
		||||
    QString const &n = customerRepoDirName();
 | 
			
		||||
    QString const &r = customerRepoRoot();
 | 
			
		||||
    return !n.isEmpty() ? QDir::cleanPath(r + QDir::separator() + n) : "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool customerRepoExists() {
 | 
			
		||||
    QString const repoDir{customerRepoDir()};
 | 
			
		||||
    return !repoDir.isEmpty() ? QDir(repoDir).exists() : false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString repositoryUrl() {
 | 
			
		||||
    return "gitea@ptu-config.atb-comm.de:ATB/";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString branchName() {
 | 
			
		||||
    int const zoneNr = read1stLineOfFile("/mnt/system_data/zone_nr");
 | 
			
		||||
    if (zoneNr != -1) {
 | 
			
		||||
        return QString("zg1/zone%1").arg(zoneNr);
 | 
			
		||||
    }
 | 
			
		||||
    return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace internal
 | 
			
		||||
		Reference in New Issue
	
	Block a user