This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pacmanrunner.cpp
188 lines (165 loc) · 5.79 KB
/
pacmanrunner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "pacmanrunner.h"
#include <QDebug>
#include <QSettings>
#include <QStandardPaths>
#include <QCoreApplication>
#include <QRegularExpression>
#include <unistd.h>
#include <cerrno>
PacmanRunner::PacmanRunner(QObject *parent) :
QObject(parent)
{}
std::tuple<QStringList, bool> PacmanRunner::frontend() const
{
QSettings settings;
if(settings.contains(QStringLiteral("frontend"))) {
return std::make_tuple(settings.value(QStringLiteral("frontend")).toStringList(),
settings.value(QStringLiteral("frontend/waved"), false).toBool());
} else {
static const QList<std::tuple<QStringList, bool>> defaultFn = {
std::make_tuple(QStringList{QStringLiteral("yay"), QStringLiteral("--rebuild"), QStringLiteral("-S")}, false),
std::make_tuple(QStringList{QStringLiteral("trizen"), QStringLiteral("-S")}, false),
std::make_tuple(QStringList{QStringLiteral("pacaur"), QStringLiteral("--rebuild"), QStringLiteral("-S")}, false),
std::make_tuple(QStringList{QStringLiteral("yaourt"), QStringLiteral("-S")}, true)
};
for(const auto &fn : defaultFn) {
if(!QStandardPaths::findExecutable(std::get<0>(fn).first()).isNull())
return fn;
}
return std::make_tuple(QStringList{QStringLiteral("pacman")}, true);
}
}
QString PacmanRunner::frontendDescription() const
{
auto fn = frontend();
return QStringLiteral("%1 %2")
.arg(std::get<0>(fn).join(QLatin1Char(' ')),
std::get<1>(fn) ? QStringLiteral("<package wave> ...") : QStringLiteral("<all packages...>"));
}
void PacmanRunner::setFrontend(const QStringList &cli, bool waved)
{
QSettings settings;
settings.setValue(QStringLiteral("frontend"), cli);
settings.setValue(QStringLiteral("frontend/waved"), waved);
qDebug() << "Updated pacman frontend to" << cli.first()
<< (waved ? "(waved)" : "(sorted)");
}
void PacmanRunner::resetFrontend()
{
QSettings settings;
settings.remove(QStringLiteral("frontend"));
}
int PacmanRunner::run(const QList<QStringList> &waves)
{
if(waves.isEmpty()) {
qWarning() << "No packages need to be rebuilt";
return EXIT_SUCCESS;
}
//check if all packages are installed
QProcess proc;
initPacman(proc);
proc.setStandardOutputFile(QProcess::nullDevice());
QStringList pacArgs {QStringLiteral("-Qi")};
for(const auto& pkgs : waves)
pacArgs.append(pkgs);
proc.setArguments(pacArgs);
qDebug() << "Checking if all packages are still installed...";
proc.start();
proc.waitForFinished(-1);
if(proc.exitCode() != EXIT_SUCCESS)
throw QStringLiteral("Please remove repkg files of uninstalled packages and mark the unchanged via `repkg clear <pkg>`");
// run the frontend to reinstall packages
QStringList cliArgs;
bool waved;
std::tie(cliArgs, waved) = frontend();
auto bin = QStandardPaths::findExecutable(cliArgs.takeFirst());
if(bin.isNull())
throw QStringLiteral("Unable to find binary \"%1\" in PATH").arg(bin);
if(waved) {
for(const auto& pkgs : waves) {
auto args = cliArgs;
args.append(pkgs);
auto res = QProcess::execute(bin, args);
if(res != EXIT_SUCCESS)
return res;
}
return EXIT_SUCCESS;
} else {
for(const auto& pkgs : waves)
cliArgs.append(pkgs);
// prepare arguments
auto argSize = cliArgs.size() + 1;
QByteArrayList rawArgs;
rawArgs.reserve(argSize);
QVector<char*> execArgs{argSize + 1, nullptr};
// bin
rawArgs.append(bin.toUtf8());
execArgs[0] = rawArgs[0].data();
// args
for(auto i = 1; i < argSize; i++) {
rawArgs.append(cliArgs[i - 1].toUtf8());
execArgs[i] = rawArgs[i].data();
}
::execv(execArgs[0], execArgs.data());
//unexcepted error
throw QStringLiteral("execv failed with error: %1").arg(qt_error_string(errno));
}
}
QString PacmanRunner::readPackageVersion(const QString &pkg)
{
//read the package version from pacman
QProcess proc;
initPacman(proc);
proc.setArguments({QStringLiteral("-Q"), pkg});
qDebug() << "Querying package version of" << pkg << "...";
proc.start();
proc.waitForFinished(-1);
if(proc.exitCode() != EXIT_SUCCESS)
throw QStringLiteral("Failed to get current version of package %1 from pacman").arg(pkg);
auto match = QRegularExpression {
QStringLiteral(R"__(^(?:%1)\s*(.*)$)__")
.arg(QRegularExpression::escape(pkg))
}.match(QString::fromUtf8(proc.readAllStandardOutput()).simplified());
if(!match.hasMatch())
throw QStringLiteral("Failed to get current version of package %1 from pacman").arg(pkg);
return match.captured(1);
}
QStringList PacmanRunner::readForeignPackages()
{
QProcess proc;
initPacman(proc);
proc.setArguments({QStringLiteral("-Qqm")});
qDebug() << "Querying all foreign packages...";
proc.start();
proc.waitForFinished(-1);
if(proc.exitCode() != EXIT_SUCCESS)
throw QStringLiteral("Failed to get foreign packages from pacman");
return QString::fromUtf8(proc.readAllStandardOutput()).simplified().split(QLatin1Char(' '));
}
QStringList PacmanRunner::listDependencies(const QString &pkg)
{
QProcess proc;
initPacman(proc, true);
proc.setArguments({
QStringLiteral("-u"),
QStringLiteral("-d1"),
pkg
});
qDebug() << "Querying all dependencies of the" << pkg << "package...";
proc.start();
proc.waitForFinished(-1);
if(proc.exitCode() != EXIT_SUCCESS)
throw QStringLiteral("Failed to get dependencies of %1 from pactree").arg(pkg);
// skip first element, is always the package itself
return QString::fromUtf8(proc.readAllStandardOutput()).simplified().split(QLatin1Char(' ')).mid(1);
}
void PacmanRunner::initPacman(QProcess &proc, bool asPactree) const
{
auto pacman = asPactree ?
QStandardPaths::findExecutable(QStringLiteral("pactree")) :
QStandardPaths::findExecutable(QStringLiteral("pacman"));
if(pacman.isNull())
throw QStringLiteral("Unable to find %1 binary in PATH").arg(asPactree ? QStringLiteral("vercmp") :QStringLiteral("pacman"));
proc.setProgram(pacman);
proc.setProcessChannelMode(QProcess::ForwardedErrorChannel);
}