checkin for saving current state
This commit is contained in:
84
Git/Git.pro
84
Git/Git.pro
@@ -0,0 +1,84 @@
|
||||
QT += core
|
||||
|
||||
TARGET = ATBUpdateGit
|
||||
|
||||
VERSION="1.0.0"
|
||||
win32 {
|
||||
BUILD_DATE=$$system("date /t")
|
||||
BUILD_TIME=$$system("time /t")
|
||||
} else {
|
||||
BUILD_DATE=$$system("date +%d-%m-%y")
|
||||
BUILD_TIME=$$system("date +%H:%M:%S")
|
||||
}
|
||||
|
||||
GIT_COMMIT=$$system("git log -1 --format=oneline | cut -d' ' -f1")
|
||||
|
||||
EXTENDED_VERSION="$${VERSION}-$${GIT_COMMIT}"
|
||||
|
||||
|
||||
INCLUDEPATH += \
|
||||
plugins \
|
||||
$${INCLUDEINTERFACES} \
|
||||
$${_PRO_FILE_PWD_}/../common/include
|
||||
|
||||
CONFIG += c++17
|
||||
|
||||
DEFINES+=APP_VERSION=\\\"$$VERSION\\\"
|
||||
DEFINES+=APP_BUILD_DATE=\\\"$$BUILD_DATE\\\"
|
||||
DEFINES+=APP_BUILD_TIME=\\\"$$BUILD_TIME\\\"
|
||||
DEFINES+=APP_EXTENDED_VERSION=\\\"$$EXTENDED_VERSION\\\"
|
||||
|
||||
# keep comments, as /* fall through */
|
||||
QMAKE_CXXFLAGS += -C
|
||||
QMAKE_CXXFLAGS += -g
|
||||
QMAKE_CXXFLAGS += -Wno-deprecated-copy -O
|
||||
|
||||
contains( CONFIG, PTU5 ) {
|
||||
CONFIG += link_pkgconfig
|
||||
QMAKE_CXXFLAGS += -O2 -std=c++17 # for GCC >= 4.7
|
||||
# QMAKE_CXXFLAGS += -Wno-deprecated-copy
|
||||
PTU5BASEPATH = /opt/devel/ptu5
|
||||
|
||||
INCLUDEPATH += $$PTU5BASEPATH/qt/libs/devicecontroller/include
|
||||
LIBS += -L$$PTU5BASEPATH/qt/libs/devicecontroller/library
|
||||
|
||||
ARCH = PTU5
|
||||
DEFINES+=PTU5
|
||||
}
|
||||
contains( CONFIG, PTU5_YOCTO ) {
|
||||
QMAKE_CXXFLAGS += -std=c++17 # for GCC >= 4.7
|
||||
# QMAKE_CXXFLAGS += -Wno-deprecated-copy
|
||||
PTU5BASEPATH = /opt/devel/ptu5
|
||||
ARCH = PTU5
|
||||
DEFINES+=PTU5
|
||||
}
|
||||
contains( CONFIG, DesktopLinux ) {
|
||||
# QMAKE_CC = ccache $$QMAKE_CC
|
||||
# QMAKE_CXX = ccache $$QMAKE_CXX
|
||||
QMAKE_CXXFLAGS += -std=c++17
|
||||
# QMAKE_CXXFLAGS += -Wno-deprecated-copy
|
||||
linux-clang { QMAKE_CXXFLAGS += -Qunused-arguments }
|
||||
ARCH = DesktopLinux
|
||||
DEFINES+=DesktopLinux
|
||||
}
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
message_handler.cpp \
|
||||
../common/src/utils_internal.cpp \
|
||||
../common/src/command.cpp \
|
||||
git_command.cpp
|
||||
|
||||
HEADERS += \
|
||||
message_handler.h \
|
||||
../common/include/utils_internal.h \
|
||||
../common/include/command.h \
|
||||
git_command.h
|
||||
|
||||
##########################################################################################
|
||||
# for running program on target through QtCreator
|
||||
contains( CONFIG, PTU5 ) {
|
||||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
else: unix:!android: target.path = /opt/app/tools/atbupdate/
|
||||
!isEmpty(target.path): INSTALLS += target
|
||||
}
|
||||
|
96
Git/git_command.cpp
Normal file
96
Git/git_command.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "git_command.h"
|
||||
|
||||
#include "command.h"
|
||||
#include "utils_internal.h"
|
||||
using namespace internal;
|
||||
|
||||
#include <QProcess>
|
||||
#include <QByteArray>
|
||||
#include <QFileInfo>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
|
||||
bool GitCommand::initEnv = false;
|
||||
static bool initEnv() {
|
||||
QString gitSSHCommand{""};
|
||||
QByteArray const v = qgetenv("GIT_SSH_COMMAND");
|
||||
if (v.isEmpty()) {
|
||||
QString sshKeyFile("/opt/app/tools/atbupdate/.keys/id_ed25519_ptuConfig");
|
||||
if (QFileInfo(sshKeyFile).exists()) {
|
||||
if (qgetenv("GIT_SSH_COMMAND").isNull()) {
|
||||
gitSSHCommand = "ssh -i /opt/app/tools/atbupdate/.keys/id_ed25519_ptuConfig";
|
||||
if (!qputenv("GIT_SSH_COMMAND", QByteArray(gitSSHCommand.toStdString().c_str()))) {
|
||||
qCritical() << "ERROR: GIT_SSH_COMMAND not put into env. Exiting...";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
qCritical() << "ERROR ssh-key-file" << sshKeyFile << "does not exists. Exiting...";
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
gitSSHCommand = QString(v.toStdString().c_str());
|
||||
qCritical() << "WARNING GIT_SSH_COMMAND already set in enviroment:"
|
||||
<< gitSSHCommand;
|
||||
if (gitSSHCommand != "ssh -i /opt/app/tools/atbupdate/.keys/id_ed25519_ptuConfig") {
|
||||
qCritical() << "ERROR" << gitSSHCommand << "wrong. Exiting...";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// qCritical() << __func__ << ":" << __LINE__ << gitSSHCommand;
|
||||
return true;
|
||||
}
|
||||
|
||||
GitCommand::GitCommand()
|
||||
: m_workingDirectory(customerRepoDir()) {
|
||||
// qCritical() << __func__ << ":" << __LINE__ << m_workingDirectory;
|
||||
if (!GitCommand::initEnv) {
|
||||
GitCommand::initEnv = ::initEnv();
|
||||
}
|
||||
}
|
||||
|
||||
bool GitCommand::exec(QStringList const &options, int start_timeout, int finish_timeout) {
|
||||
if (GitCommand::initEnv) {
|
||||
Command cmd(QString("git"), options, m_workingDirectory,
|
||||
start_timeout, finish_timeout);
|
||||
return cmd.exec();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool GitCommand::check(int start_timeout, int finish_timeout) {
|
||||
QStringList const lst{"fsck"};
|
||||
return exec(lst, start_timeout, finish_timeout);
|
||||
}
|
||||
|
||||
bool GitCommand::checkout(int start_timeout, int finish_timeout) {
|
||||
int const zoneNr = read1stLineOfFile("/mnt/system_data/zone_nr");
|
||||
if (zoneNr != -1) {
|
||||
QStringList const lst{"checkout", QString("zg1/zone%1").arg(zoneNr)};
|
||||
// qCritical() << __func__ << __LINE__ << lst;
|
||||
return exec(lst, start_timeout, finish_timeout);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GitCommand::clone(int start_timeout, int finish_timeout) {
|
||||
m_workingDirectory = customerRepoRoot(); // /opt/app/tools/atbupdate !
|
||||
QDir const repoDir{customerRepoDir()};
|
||||
if (repoDir.exists() && repoDir.entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).count() != 0) {
|
||||
qCritical() << "clone error:" << repoDir << "exists and is not empty";
|
||||
return false;
|
||||
}
|
||||
// git clone "gitea@ptu-config.atb-comm.de:ATB/customer_999"
|
||||
QStringList const lst{"clone", repositoryUrl() + customerRepoDirName()};
|
||||
return exec(lst, start_timeout, finish_timeout);
|
||||
}
|
||||
|
||||
bool GitCommand::pull(int start_timeout, int finish_timeout) {
|
||||
QStringList const lst{"pull"};
|
||||
return exec(lst, start_timeout, finish_timeout);
|
||||
}
|
||||
|
||||
bool GitCommand::status(int start_timeout, int finish_timeout) {
|
||||
QStringList const lst{"status"};
|
||||
return exec(lst, start_timeout, finish_timeout);
|
||||
}
|
25
Git/git_command.h
Normal file
25
Git/git_command.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef GIT_COMMAND_H_INCLUDED
|
||||
#define GIT_COMMAND_H_INCLUDED
|
||||
|
||||
#include <QStringList>
|
||||
#include <QScopedPointer>
|
||||
|
||||
class QProcess;
|
||||
class GitCommand {
|
||||
static bool initEnv;
|
||||
QString m_workingDirectory;
|
||||
|
||||
bool exec(QStringList const &options, int start_timeout = 100000,
|
||||
int finish_timeout = 100000);
|
||||
public:
|
||||
GitCommand();
|
||||
|
||||
|
||||
bool status(int start_timeout = 100000, int finish_timeout = 100000);
|
||||
bool check(int start_timeout = 100000, int finish_timeout = 100000);
|
||||
bool checkout(int start_timeout = 100000, int finish_timeout = 100000);
|
||||
bool clone(int start_timeout = 100000, int finish_timeout = 100000);
|
||||
bool pull(int start_timeout = 100000, int finish_timeout = 100000);
|
||||
};
|
||||
|
||||
#endif // GIT_COMMAND_H_INCLUDED
|
105
Git/main.cpp
105
Git/main.cpp
@@ -0,0 +1,105 @@
|
||||
#include <QtGlobal>
|
||||
#include <QCoreApplication>
|
||||
#include <QByteArray>
|
||||
|
||||
#include <QProcess>
|
||||
#include <QCommandLineParser>
|
||||
#include <QStandardPaths>
|
||||
#include <QSettings>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QRegularExpression>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "message_handler.h"
|
||||
#include "utils_internal.h"
|
||||
#include "git_command.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
QByteArray const value = qgetenv("LC_ALL");
|
||||
if (value.isEmpty() || value != "C") {
|
||||
qputenv("LC_ALL", "C");
|
||||
}
|
||||
|
||||
openlog("ATB-UPDATE-GIT", LOG_PERROR | LOG_PID | LOG_CONS, LOG_USER);
|
||||
|
||||
QCoreApplication a(argc, argv);
|
||||
QCoreApplication::setApplicationName("ATBUpdateGit");
|
||||
QCoreApplication::setApplicationVersion(APP_VERSION);
|
||||
|
||||
if (!messageHandlerInstalled()) { // change internal qt-QDebug-handling
|
||||
atbInstallMessageHandler(nullptr);
|
||||
//atbInstallMessageHandler(atbDebugOutput);
|
||||
setDebugLevel(LOG_NOTICE);
|
||||
}
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription("git-commands for the update-system");
|
||||
|
||||
QCommandLineOption const checkCustomerRepositoryOption{"check"};
|
||||
QCommandLineOption const cloneCustomerRepositoryOption{"clone"};
|
||||
QCommandLineOption const pullBranchOption{"pull"};
|
||||
QCommandLineOption const checkoutBranchOption("checkout");
|
||||
|
||||
parser.addOption(checkCustomerRepositoryOption);
|
||||
parser.addOption(cloneCustomerRepositoryOption);
|
||||
parser.addOption(pullBranchOption);
|
||||
parser.addOption(checkoutBranchOption);
|
||||
|
||||
QCommandLineOption verboseOption{parser.addVersionOption()};
|
||||
parser.process(a);
|
||||
|
||||
if (parser.isSet(verboseOption)) {
|
||||
parser.showVersion();
|
||||
return 0;
|
||||
}
|
||||
|
||||
GitCommand gitCmd;
|
||||
|
||||
if (parser.isSet(checkCustomerRepositoryOption)) {
|
||||
if (!gitCmd.check()) {
|
||||
return -1;
|
||||
}
|
||||
} else
|
||||
if (parser.isSet(checkoutBranchOption)) {
|
||||
if (!gitCmd.checkout()) {
|
||||
return -2;
|
||||
}
|
||||
} else
|
||||
if (parser.isSet(cloneCustomerRepositoryOption)) {
|
||||
if (!gitCmd.clone()) {
|
||||
return -3;
|
||||
}
|
||||
} else
|
||||
if (parser.isSet(pullBranchOption)) {
|
||||
if (!gitCmd.pull()) {
|
||||
return -4;
|
||||
}
|
||||
} else {
|
||||
if (internal::customerRepoExists()) {
|
||||
if (!gitCmd.checkout()) {
|
||||
return -2;
|
||||
}
|
||||
if (!gitCmd.pull()) {
|
||||
return -4;
|
||||
}
|
||||
} else {
|
||||
if (!gitCmd.clone()) {
|
||||
return -3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//int const machineNr = read1stLineOfFile("/mnt/system_data/machine_nr");
|
||||
//int const customerNr = read1stLineOfFile("/mnt/system_data/cust_nr");
|
||||
//int const zoneNr = read1stLineOfFile("/mnt/system_data/zone_nr");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
97
Git/message_handler.cpp
Executable file
97
Git/message_handler.cpp
Executable file
@@ -0,0 +1,97 @@
|
||||
#include "message_handler.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <cstring>
|
||||
#include <QString>
|
||||
#include <QFileInfo>
|
||||
#include <QMessageLogContext>
|
||||
|
||||
|
||||
static char const *DBG_NAME[] = { "DBG ", "WARN ", "CRIT ", "FATAL", "INFO " };
|
||||
static bool installedMsgHandler = false;
|
||||
static int debugLevel = LOG_NOTICE;
|
||||
|
||||
int getDebugLevel() { return debugLevel; }
|
||||
void setDebugLevel(int newDebugLevel) {
|
||||
debugLevel = newDebugLevel;
|
||||
}
|
||||
|
||||
bool messageHandlerInstalled() {
|
||||
return installedMsgHandler;
|
||||
}
|
||||
|
||||
QtMessageHandler atbInstallMessageHandler(QtMessageHandler handler) {
|
||||
installedMsgHandler = (handler != 0);
|
||||
static QtMessageHandler prevHandler = nullptr;
|
||||
if (handler) {
|
||||
prevHandler = qInstallMessageHandler(handler);
|
||||
return prevHandler;
|
||||
} else {
|
||||
return qInstallMessageHandler(prevHandler);
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// \brief Print message according to given debug level.
|
||||
///
|
||||
/// \note Install this function using qInstallMsgHandler().
|
||||
///
|
||||
/// int main(int argc, char **argv) {
|
||||
/// installMsgHandler(atbDebugOutput);
|
||||
/// QApplication app(argc, argv);
|
||||
/// ...
|
||||
/// return app.exec();
|
||||
/// }
|
||||
///
|
||||
#if (QT_VERSION > QT_VERSION_CHECK(5, 0, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
void atbDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
|
||||
Q_UNUSED(context);
|
||||
QString const localMsg = QString(DBG_NAME[type]) + msg.toLocal8Bit();
|
||||
|
||||
switch (debugLevel) {
|
||||
case LOG_DEBUG: { // debug-level message
|
||||
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
|
||||
} break;
|
||||
case LOG_INFO: { // informational message
|
||||
if (type != QtDebugMsg) {
|
||||
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
|
||||
}
|
||||
} break;
|
||||
case LOG_NOTICE: { // normal, but significant, condition
|
||||
if (type != QtDebugMsg) {
|
||||
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
|
||||
}
|
||||
} break;
|
||||
case LOG_WARNING: { // warning conditions
|
||||
if (type != QtInfoMsg && type != QtDebugMsg) {
|
||||
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
|
||||
}
|
||||
} break;
|
||||
case LOG_ERR: { // error conditions
|
||||
if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
|
||||
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
|
||||
}
|
||||
} break;
|
||||
case LOG_CRIT: { // critical conditions
|
||||
if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
|
||||
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
|
||||
}
|
||||
} break;
|
||||
case LOG_ALERT: { // action must be taken immediately
|
||||
if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
|
||||
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
|
||||
}
|
||||
} break;
|
||||
case LOG_EMERG: { // system is unusable
|
||||
if (type != QtInfoMsg && type != QtDebugMsg && type != QtWarningMsg) {
|
||||
syslog(LOG_DEBUG, "%s", localMsg.toStdString().c_str());
|
||||
}
|
||||
} break;
|
||||
default: {
|
||||
//fprintf(stderr, "%s No ErrorLevel defined! %s\n",
|
||||
// datetime.toStdString().c_str(), msg.toStdString().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
23
Git/message_handler.h
Executable file
23
Git/message_handler.h
Executable file
@@ -0,0 +1,23 @@
|
||||
#ifndef MESSAGE_HANDLER_H_INCLUDED
|
||||
#define MESSAGE_HANDLER_H_INCLUDED
|
||||
|
||||
#include <QtGlobal>
|
||||
#ifdef __linux__
|
||||
#include <syslog.h>
|
||||
#endif
|
||||
|
||||
int getDebugLevel();
|
||||
void setDebugLevel(int newDebugLevel);
|
||||
|
||||
bool messageHandlerInstalled();
|
||||
QtMessageHandler atbInstallMessageHandler(QtMessageHandler handler);
|
||||
|
||||
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
||||
// typedef void (*QtMessageHandler)(QtMsgType, const char *);
|
||||
void atbDebugOutput(QtMsgType type, const char *msg);
|
||||
#elif QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
// typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &);
|
||||
void atbDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);
|
||||
#endif
|
||||
|
||||
#endif // MESSAGE_HANDLER_H_INCLUDED
|
Reference in New Issue
Block a user