add update logic
This commit is contained in:
@@ -1,5 +1,19 @@
|
||||
#include "updatemanager.hpp"
|
||||
#include <QStandardPaths>
|
||||
#include <QRegularExpression>
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr int kMaxLogLines = 1000;
|
||||
|
||||
QString strip_ansi(const QString &text)
|
||||
{
|
||||
static const QRegularExpression ansi(QStringLiteral("\\x1b\\[[0-9;?]*[ -/]*[@-~]"));
|
||||
QString result = text;
|
||||
result.remove(ansi);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateManager::UpdateManager(QObject* parent)
|
||||
{
|
||||
@@ -7,63 +21,117 @@ UpdateManager::UpdateManager(QObject* parent)
|
||||
connect(&m_process, &QProcess::errorOccurred, this, &UpdateManager::on_process_error);
|
||||
}
|
||||
|
||||
void UpdateManager::check_update()
|
||||
{
|
||||
// Ищет в системе исполняемый файл с именем checkupdates и возвращает его абсолютный путь. Если файл не найден, возвращается пустая строка.
|
||||
const QString program = QStandardPaths::findExecutable(QStringLiteral("checkupdates"));
|
||||
if (program.isEmpty())
|
||||
{
|
||||
emit finished(false, "checkupdates not found: install pacman-contrib");
|
||||
return;
|
||||
}
|
||||
|
||||
m_process.start(program, {});
|
||||
}
|
||||
|
||||
void UpdateManager::on_process_finished(int exit_code, QProcess::ExitStatus status)
|
||||
{
|
||||
if (status == QProcess::CrashExit || (exit_code != 0 && exit_code != 2))
|
||||
const QByteArray tail = m_process.readAllStandardOutput();
|
||||
append_update_output(tail);
|
||||
|
||||
if (!m_update_log_buffer.trimmed().isEmpty())
|
||||
add_log_line(m_package_manager + QStringLiteral(": ") + m_update_log_buffer);
|
||||
|
||||
m_update_log_buffer.clear();
|
||||
m_askpass_dir.reset();
|
||||
|
||||
if (status != QProcess::CrashExit && exit_code == 0)
|
||||
{
|
||||
on_process_error(m_process.error());
|
||||
stop_update(true, {});
|
||||
return;
|
||||
}
|
||||
|
||||
const QString output = QString::fromUtf8(m_process.readAllStandardOutput());
|
||||
const QStringList lines = output.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
|
||||
const QString reason = QStringLiteral("exit code %1%2") .arg(exit_code);
|
||||
stop_update(false, reason);
|
||||
}
|
||||
|
||||
QVariantList package_list;
|
||||
package_list.reserve(lines.size());
|
||||
void UpdateManager::append_update_output(const QByteArray& chunk)
|
||||
{
|
||||
m_update_output += chunk;
|
||||
m_update_log_buffer += QString::fromUtf8(chunk);
|
||||
m_update_log_buffer.replace(QLatin1Char('\r'), QLatin1Char('\n'));
|
||||
|
||||
for (const QString &line : lines)
|
||||
qsizetype new_line;
|
||||
while ((new_line = m_update_log_buffer.indexOf(QLatin1Char('\n'))) >= 0)
|
||||
{
|
||||
const QStringList parts = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
|
||||
if (parts.size() != 4 || parts.at(2) != QStringLiteral("->")) continue;
|
||||
|
||||
QVariantMap package;
|
||||
package.insert(QStringLiteral("name"), parts.at(0));
|
||||
package.insert(QStringLiteral("version"), parts.at(1) + QStringLiteral(" → ") + parts.at(3));
|
||||
package.insert(QStringLiteral("updated"), false);
|
||||
package.insert(QStringLiteral("updating"), false);
|
||||
package_list.append(package);
|
||||
const QString line = m_update_log_buffer.left(new_line);
|
||||
m_update_log_buffer.remove(0, new_line + 1);
|
||||
if (!line.trimmed().isEmpty()) add_log_line(m_package_manager + QStringLiteral(": ") + line);
|
||||
}
|
||||
|
||||
packages_ready(package_list);
|
||||
emit finished(true);
|
||||
}
|
||||
|
||||
void UpdateManager::on_process_error(QProcess::ProcessError error)
|
||||
{
|
||||
QString reason;
|
||||
if (error == QProcess::FailedToStart)
|
||||
}
|
||||
|
||||
void UpdateManager::yay(const QString& askpass)
|
||||
{
|
||||
QString package_manager = "yay";
|
||||
const QString program = QStandardPaths::findExecutable(package_manager);
|
||||
if (program.isEmpty())
|
||||
{
|
||||
reason = QStringLiteral("failed to start: ") + m_process.errorString();
|
||||
qWarning() << "cannot update: yay";
|
||||
return;
|
||||
}
|
||||
|
||||
m_update_log.clear();
|
||||
m_update_output.clear();
|
||||
m_update_log_buffer.clear();
|
||||
|
||||
emit update_log_changed();
|
||||
add_log_line(package_manager + QStringLiteral(": update started"));
|
||||
m_process.setProcessEnvironment(update_environment(askpass));
|
||||
emit set_updating_flag(false, true);
|
||||
|
||||
const QStringList args = {
|
||||
QStringLiteral("--sudoflags=-A"),
|
||||
QStringLiteral("--answerclean"), QStringLiteral("None"),
|
||||
QStringLiteral("--answerdiff"), QStringLiteral("None"),
|
||||
QStringLiteral("--answeredit"), QStringLiteral("None"),
|
||||
QStringLiteral("--answerupgrade"), QStringLiteral("None"),
|
||||
QStringLiteral("--noconfirm"),
|
||||
QStringLiteral("-Syu"),
|
||||
};
|
||||
m_process.start(program, args);
|
||||
m_process.closeWriteChannel();
|
||||
}
|
||||
|
||||
void UpdateManager::pacman(const QString& askpass)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QProcessEnvironment UpdateManager::update_environment(const QString& pass) const
|
||||
{
|
||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
||||
env.insert(QStringLiteral("SUDO_ASKPASS"), pass);
|
||||
env.insert(QStringLiteral("SUDO_ASKPASS_REQUIRE"), QStringLiteral("force"));
|
||||
env.insert(QStringLiteral("LC_ALL"), QStringLiteral("C"));
|
||||
env.insert(QStringLiteral("COLUMNS"), QStringLiteral("300"));
|
||||
return env;
|
||||
}
|
||||
|
||||
void UpdateManager::add_log_line(const QString& line)
|
||||
{
|
||||
const QString clean = strip_ansi(line).trimmed();
|
||||
if (clean.isEmpty()) return;
|
||||
|
||||
qInfo().noquote() << clean;
|
||||
|
||||
if (m_update_log.size() >= kMaxLogLines) m_update_log.removeFirst();
|
||||
m_update_log.append(clean);
|
||||
}
|
||||
|
||||
void UpdateManager::stop_update(bool success, const QString& reason)
|
||||
{
|
||||
if (success)
|
||||
{
|
||||
emit set_updating_flag(true, false);
|
||||
add_log_line(m_package_manager + QStringLiteral(": update finished"));
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = QStringLiteral("failed, exit code %1").arg(m_process.exitCode());
|
||||
const QString errText = QString::fromUtf8(m_process.readAllStandardError()).trimmed();
|
||||
if (!errText.isEmpty()) reason += QStringLiteral(": ") + errText;
|
||||
qWarning().noquote() << m_package_manager << "failed:" << reason;
|
||||
add_log_line(m_package_manager + QStringLiteral(": failed: ") + reason);
|
||||
emit set_updating_flag(false, false);
|
||||
}
|
||||
|
||||
emit finished(false, reason);
|
||||
emit finished(true);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <QObject>
|
||||
#include <QProcess>
|
||||
#include <QVariantList>
|
||||
#include <QTemporaryDir>
|
||||
#include <QTimer>
|
||||
|
||||
class UpdateManager : public QObject
|
||||
{
|
||||
@@ -15,14 +17,29 @@ class UpdateManager : public QObject
|
||||
QProcess m_process;
|
||||
|
||||
public:
|
||||
void check_update();
|
||||
void yay(const QString& password);
|
||||
void pacman(const QString& password);
|
||||
|
||||
private:
|
||||
void on_process_finished(int exitCode, QProcess::ExitStatus status);
|
||||
void on_process_error(QProcess::ProcessError error);
|
||||
QProcessEnvironment update_environment(const QString& askpass) const;
|
||||
void add_log_line(const QString& line);
|
||||
void append_update_output(const QByteArray& chunk);
|
||||
void stop_update(bool success, const QString& reason);
|
||||
|
||||
private:
|
||||
QString m_package_manager = "yay";
|
||||
std::unique_ptr<QTemporaryDir> m_askpass_dir;
|
||||
QByteArray m_update_output;
|
||||
QStringList m_update_log;
|
||||
bool m_update_timedout = false;
|
||||
QString m_update_log_buffer;
|
||||
|
||||
signals:
|
||||
void finished(bool ok, QString reason = "");
|
||||
void packages_ready(QVariantList packages);
|
||||
void update_log_changed();
|
||||
void set_updating_flag(bool updated, bool updating);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user