-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk9portage.cpp
421 lines (370 loc) · 12.6 KB
/
k9portage.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright (c) 2021-2023, K9spud LLC.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "k9portage.h"
#include "datastorage.h"
#include <QDir>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDateTime>
K9Portage::K9Portage(QObject *parent) : QObject(parent)
{
separator.setPattern("([^A-Za-z0-9]+)");
digitVersion.setPattern("([0-9]+)");
alphaVersion.setPattern("([A-Za-z]+)");
// see https://devmanual.gentoo.org/general-concepts/dependencies/
// See Restrictions upon Names: https://projects.gentoo.org/pms/8/pms.html#x1-150003
// https://dev.gentoo.org/~ulm/pms/head/pms.html#chapter-3
dependKeywordsBasicRE.setPattern("([^~=><][^/]*)/([^:\\n\\s]+)\\s+(.+)$");
dependKeywordsVersionRE.setPattern("(~|=|>=|<=|>|<)([^/]+)/([^:]+)-([0-9\\*][0-9\\-\\.A-z\\*]*)\\s+(.+)$");
dependKeywordsSlotRE.setPattern("([^~=><][^/]*)/([^:\\n]+):([^:\\n\\s]+)\\s+(.+)$");
dependKeywordsRepositoryRE.setPattern("(~|=|>=|<=|>|<)([^/]+)/(.+)-([0-9\\*][0-9\\-\\.A-z\\*]*):([^:]*):([^:\\n\\s]+)\\s+(.+)$");
/*
To specify "version 2.x (not 1.x or 3.x)" of a package, it is necessary to use the asterisk postfix like this: "=x11-libs/gtk+-2*"
how come there is no dot before the asterisk? wouldn't this allow version 21.x as well as 2.x?
<ionen> K9spud: * matches on a full version component in this context, e.g. it could patch 2_alpha, 2.1.3 but not 21
ionen> 2.* would've been inconvenient for components that start with _
<sam_> K9spud: read PMS 8.3.1 for the definition of what =* does
https://dev.gentoo.org/~ulm/pms/head/pms.html#section-8.3.1
8.3.1 Operators
The following operators are available:
< Strictly less than the specified version.
<= Less than or equal to the specified version.
= Exactly equal to the specified version.
Special exception: if the version specified has an asterisk immediately following it,
then only the given number of version components is used for comparison,
i. e. the asterisk acts as a wildcard for any further components.
When an asterisk is used, the specification must remain valid if the asterisk were removed.
(An asterisk used with any other operator is illegal.)
~ Equal to the specified version when revision parts are ignored.
>= Greater than or equal to the specified version.
> Strictly greater than the specified version.
*/
#if defined(__x86_64__)
arch = "amd64";
#elif defined(__i386__)
arch = "x86";
#elif defined(__aarch64__)
arch = "arm64";
#elif defined(__arm__)
arch = "arm";
#elif defined(__mips__)
arch = "mips";
#elif defined(__ppc64__)
arch = "ppc64";
#elif defined(__powerpc__)
arch = "ppc";
#elif defined(__sparc__)
arch = "sparc";
#elif defined(__m68k__)
arch = "m68k";
#elif defined(__alpha__)
arch = "alpha";
#elif defined(__hppa__)
arch = "hppa";
#elif defined(__ia64__)
arch = "ia64";
#elif defined(__riscv__)
arch = "riscv";
#elif defined(__s390__)
arch = "s390";
#endif
}
void K9Portage::autoKeyword(QString app, QString op)
{
QString keywords;
QFile file("/etc/portage/package.accept_keywords/appswipe.tmp");
if(file.exists() == false || file.open(QIODevice::Text | QIODevice::Append | QIODevice::WriteOnly) == false)
{
return;
}
QSqlDatabase db;
if(QSqlDatabase::contains("GuiThread") == false)
{
db = QSqlDatabase::addDatabase("QSQLITE", "GuiThread");
}
else
{
db = QSqlDatabase::database("GuiThread");
if(db.isValid())
{
db.close();
}
}
db.setDatabaseName(ds->storageFolder + ds->databaseFileName);
db.open();
QSqlQuery query(db);
QString s = R"EOF(
select p.KEYWORDS, p.MASKED, r.REPO, c.CATEGORY, p.PACKAGE, p.VERSION, p.SLOT, p.SUBSLOT
from PACKAGE p
inner join CATEGORY c on c.CATEGORYID = p.CATEGORYID
inner join REPO r on r.REPOID = p.REPOID
where c.CATEGORY || '/' || p.PACKAGE || '-' || p.VERSION = ?
order by c.CATEGORY, p.PACKAGE, p.MASKED, p.V1 desc, p.V2 desc, p.V3 desc, p.V4 desc, p.V5 desc, p.V6 desc, p.V7 desc, p.V8 desc, p.V9 desc, p.V10 desc
)EOF";
query.prepare(s);
query.bindValue(0, app);
if(query.exec() && query.first())
{
keywords = query.value(0).toString();
int masked = query.value(1).toInt();
QString repo = query.value(2).toString();
QString category = query.value(3).toString();
QString package = query.value(4).toString();
QString version = query.value(5).toString();
QString slot = query.value(6).toString();
QString subslot = query.value(7).toString();
if(masked == 0 && keywords.contains(arch) && keywords.contains(QString("~%1").arg(arch)) == false)
{
// this package doesn't need to be auto keyworded
return;
}
QDir dir;
dir.setPath("/etc/portage/package.accept_keywords");
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
foreach(QString keywordFile, dir.entryList())
{
if(foundKeyworded(QString("/etc/portage/package.accept_keywords/%1").arg(keywordFile), category, package, version, repo, slot, subslot, keywords))
{
// this package has already been keyworded before
return;
}
}
}
QTextStream out(&file);
if(op.isEmpty())
{
op = "=";
}
if(keywords.contains(QString("~%1").arg(arch)))
{
out << QString("%1%2 ~%3").arg(op, app, arch) << Qt::endl;
}
else
{
out << QString("%1%2 **").arg(op, app) << Qt::endl;
}
file.close();
}
bool K9Portage::globMatch(QString glob, QString s)
{
if(glob == "*")
{
return true;
}
if(glob.contains('*') == false)
{
return (glob == s);
}
QString globregex = QRegularExpression::wildcardToRegularExpression(glob);
QRegularExpression regex;
regex.setPattern(globregex);
return regex.match(s).hasMatch();
}
bool K9Portage::keywordMatch(QString acceptKeywords, QString appKeywords)
{
QString s;
/*
See https://wiki.gentoo.org/wiki/ACCEPT_KEYWORDS
~amd64 - package is visisble if ~amd64 testing keyword is present, even if you're running on arm64
~amd64 ~arm64 - package is only visible if both ~amd64 and ~arm64 testing keywords are present in the ebuild
See https://wiki.gentoo.org/wiki/Knowledge_Base:Accepting_a_keyword_for_a_single_package
sounds like the keyword itself can be omitted. this makes portage install the testing version.
*/
if(acceptKeywords.isEmpty())
{
if(appKeywords.contains(QString("~%1").arg(arch)))
{
return true;
}
else
{
return false;
}
}
QStringList keywordsList = acceptKeywords.remove("\n").split(' ');
QStringList appKeywordsList = appKeywords.remove("\n").split(' ');
QStringList keys;
foreach(s, keywordsList)
{
if(s == "**") // Package is always visible (KEYWORDS are ignored completely). even 9999 live ebuilds now get included
{
return true;
}
else if(s == "*") // Package is visible if it is stable on any architecture.
{
foreach(s, appKeywordsList)
{
if(s.count() > 0 && s.startsWith('~') == false)
{
return true;
}
}
}
else if(s == "~*") // Package is visible if it is in testing on any architecture.
{
foreach(s, appKeywordsList)
{
if(s.startsWith('~') == true)
{
return true;
}
}
}
else if(s.count())
{
keys.append(s);
}
}
foreach(s, keys)
{
if(appKeywordsList.contains(s) == false)
{
return false;
}
}
return false;
}
bool K9Portage::foundKeyworded(QString fileName, QString appCategory, QString appPackage, QString appVersion, QString appRepo, QString appSlot, QString appSubslot, QString appKeywords)
{
QFile input;
input.setFileName(fileName);
if(!input.exists() || !input.open(QIODevice::ReadOnly | QIODevice::Text))
{
return false;
}
QString wholeFile = input.readAll();
input.close();
QStringList lines = wholeFile.split('\n');
QRegularExpressionMatch match;
VersionString vs;
QString s;
QString filter;
QString category;
QString package;
QString version;
VersionString appVerString;
appVerString.parse(appVersion);
QString repo;
QString slot;
QString subslot;
QString keywords;
foreach(s, lines)
{
s = s.trimmed();
if(s.isEmpty() || s.startsWith('#'))
{
continue;
}
match = dependKeywordsVersionRE.match(s);
if(match.hasMatch())
{
filter = match.captured(1);
category = match.captured(2);
package = match.captured(3);
version = match.captured(4);
keywords = match.captured(5).trimmed();
if(category == appCategory && package == appPackage && appVerString.match(filter, version))
{
if(keywordMatch(keywords, appKeywords))
{
return true;
}
}
continue;
}
match = dependKeywordsBasicRE.match(s);
if(match.hasMatch())
{
category = match.captured(1);
package = match.captured(2);
keywords = match.captured(3).trimmed();
if(globMatch(category, appCategory) && globMatch(package, appPackage))
{
if(keywordMatch(keywords, appKeywords))
{
return true;
}
}
continue;
}
match = dependKeywordsSlotRE.match(s);
if(match.hasMatch())
{
category = match.captured(1);
package = match.captured(2);
slot = match.captured(3);
keywords = match.captured(4).trimmed();
if(slot.contains('/'))
{
int ix = slot.indexOf('/');
subslot = slot.mid(ix + 1);
slot = slot.left(ix);
}
else
{
subslot = "*";
}
if(globMatch(category, appCategory) && globMatch(package, appPackage) &&
globMatch(slot, appSlot) && globMatch(subslot, appSubslot))
{
if(keywordMatch(keywords, appKeywords))
{
return true;
}
}
continue;
}
match = dependKeywordsRepositoryRE.match(s);
if(match.hasMatch())
{
filter = match.captured(1);
category = match.captured(2);
package = match.captured(3);
version = match.captured(4);
slot = match.captured(5);
keywords = match.captured(6).trimmed();
if(slot.contains('/'))
{
int ix = slot.indexOf('/');
subslot = slot.mid(ix + 1);
slot = slot.left(ix);
}
else
{
subslot = "*";
}
repo = match.captured(6);
if(globMatch(category, appCategory) && globMatch(package, appPackage) &&
appVerString.match(filter, version) && (repo.isEmpty() || globMatch(repo, appRepo)) &&
globMatch(slot, appSlot) && globMatch(subslot, appSubslot))
{
if(keywordMatch(keywords, appKeywords))
{
return true;
}
}
continue;
}
qDebug() << "Could not parse depend string:";
qDebug() << " " << s;
continue;
}
return false;
}