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
|
#include "ebuildMetaProcessor.h"
#include "depend.h"
#include "maintenance/abstractFileProcessor.h"
#include "portage-models.h"
#include "utils/dbUtils.h"
#include "utils/ebuildCacheParser.h"
#include "utils/splitEbuildProps.h"
#include "wrap/regex.h"
#include "wrap/ustring.h"
#include <Ice/Optional.h>
#include <boost/algorithm/string/join.hpp>
#include <boost/date_time/posix_time/conversion.hpp>
#include <command_fwd.h>
#include <connection.h>
#include <ext/alloc_traits.h>
#include <factory.h>
#include <fileUtils.h>
#include <memory>
#include <modifycommand.h>
#include <optional>
#include <plugins.h>
#include <selectcommand.h>
#include <selectcommandUtil.impl.h>
#include <sql/maintenance/categoryInsert.sql.h>
#include <sql/maintenance/categoryPrune.sql.h>
#include <sql/maintenance/ebuildArchsInsert.sql.h>
#include <sql/maintenance/ebuildDelete.sql.h>
#include <sql/maintenance/ebuildDeps.sql.h>
#include <sql/maintenance/ebuildInsert.sql.h>
#include <sql/maintenance/ebuildRDeps.sql.h>
#include <sql/maintenance/ebuildUpdate.sql.h>
#include <sql/maintenance/ebuildUsesInsert.sql.h>
#include <sql/maintenance/packageDescUpdate.sql.h>
#include <sql/maintenance/packageInsert.sql.h>
#include <sql/maintenance/packagePrune.sql.h>
#include <sqlWriter.h>
#include <staticSqlSource.h>
#include <tablepatch.h>
namespace Glib {
class Regex;
}
namespace U = Gentoo::Utils;
using namespace AdHoc;
using namespace AdHoc::FileUtils;
namespace Gentoo::Service {
static Glib::RefPtr<Glib::Regex> packageVersion = Glib::Regex::create("^(.+)-([0-9].*)$");
EbuildMetaProcessor::EbuildMetaProcessor() : ebuildIds("ebuildId") { }
unsigned char
EbuildMetaProcessor::phase() const
{
return 2;
}
unsigned char
EbuildMetaProcessor::order() const
{
return 1;
}
bool
EbuildMetaProcessor::match(const PathParts & pp) const
{
return (pp.size() == 4 && pp[0] == "metadata" && pp[1] == "md5-cache");
}
void
EbuildMetaProcessor::created(
DB::Connection * dbc, int64_t repoId, const StringList & fn, const std::filesystem::path & path)
{
Glib::MatchInfo matches;
const Glib::ustring pv = fn[3];
if (packageVersion->match(pv, matches)) {
U::EbuildCacheParser ecp(path);
const std::string categoryName = fn[2];
const std::string packageName = matches.fetch(1);
const std::string ebuildVersion = matches.fetch(2);
// Maybe create a category
auto ci = sql::maintenance::categoryInsert.modify(dbc);
ci->bindParamS(0, categoryName);
ci->bindParamS(1, categoryName);
ci->execute();
// Maybe create a package
auto pi = sql::maintenance::packageInsert.modify(dbc);
pi->bindParamS(0, packageName);
pi->bindParamS(1, ecp.getRange("DESCRIPTION").value_or(""));
pi->bindParamS(2, packageName);
pi->bindParamS(3, categoryName);
pi->execute();
// Create an ebuild
auto m = sql::maintenance::ebuildInsert.select(dbc);
m->bindParamS(0, ebuildVersion);
m->bindParamS(1, ebuildVersion);
m->bindParamS(2, ecp.get("SLOT"));
m->bindParamS(3, ecp.get("LICENSE"));
m->bindParamT(4, boost::posix_time::from_time_t(ecp.getStat().st_mtim.tv_sec));
m->bindParamI(5, repoId);
m->bindParamS(6, categoryName);
m->bindParamS(7, packageName);
m->forEachRow<int64_t, bool, int64_t>([this, dbc, &ecp](auto ebuildId, auto newest, auto packageId) {
this->perEbuildUpdates(dbc, ecp, ebuildId, newest, packageId);
});
}
}
void
EbuildMetaProcessor::modified(
DB::Connection * dbc, int64_t repoId, const StringList & fn, const std::filesystem::path & path)
{
Glib::MatchInfo matches;
const Glib::ustring pv = fn[3];
if (packageVersion->match(pv, matches)) {
auto m = sql::maintenance::ebuildUpdate.select(dbc);
U::EbuildCacheParser ecp(path);
m->bindParamS(0, ecp.get("SLOT"));
m->bindParamS(1, ecp.get("LICENSE"));
m->bindParamT(2, boost::posix_time::from_time_t(ecp.getStat().st_mtim.tv_sec));
m->bindParamI(3, repoId);
m->bindParamS(4, fn[2]); // category
m->bindParamS(5, matches.fetch(1)); // package
m->bindParamS(6, matches.fetch(2)); // version
m->forEachRow<int64_t, bool, int64_t>([this, dbc, &ecp](auto ebuildId, auto newest, auto packageId) {
this->perEbuildUpdates(dbc, ecp, ebuildId, newest, packageId);
});
}
}
void
EbuildMetaProcessor::prepare(DB::Connection * dbc)
{
depInsert = Utils::Database::namedTemp(dbc, "tmpEbuildDeps",
{
{"ebuildId", "int"}, // 1
{"runtime", "boolean"}, // 5
{"category", "text"}, // 0
{"package", "text"}, // 4
{"version", "text"}, // 7
{"slot", "text"}, // 6
{"flags", "text"}, // 2
{"op", "text"} // 3
})
.second;
Utils::Database::namedTemp(dbc, "tmpEbuildUses",
{
{"ebuildId", "int"},
{"use", "text"},
{"defaultflag", "boolean"},
});
useInsert = sql::maintenance::ebuildUsesInsert.modify(dbc);
Utils::Database::namedTemp(dbc, "tmpEbuildArchs",
{
{"ebuildId", "int"},
{"arch", "text"},
});
archInsert = sql::maintenance::ebuildArchsInsert.modify(dbc);
}
void
EbuildMetaProcessor::apply(DB::Connection * dbc, ChangeSet &)
{
if (!ebuildIds.entityIds.empty()) {
{
dbc->execute("CREATE INDEX idxTmpEbuildDeps ON tmpEbuildDeps(ebuildId, runtime)");
DB::TablePatch t;
t.pk = {"ebuildid", "packageid", "versionspec", "flags", "slot", "op"};
t.cols = {"ebuildid", "packageid", "versionspec", "flags", "slot", "op"};
t.where = &ebuildIds;
DB::StaticSqlWriter sb(sql::maintenance::ebuildDeps.getSql());
t.srcExpr = &sb;
t.dest = "gentoobrowse.ebuild_deps";
dbc->patchTable(&t);
DB::StaticSqlWriter sr(sql::maintenance::ebuildRDeps.getSql());
t.srcExpr = &sr;
t.dest = "gentoobrowse.ebuild_rdeps";
dbc->patchTable(&t);
}
{
dbc->execute("CREATE UNIQUE INDEX idxTmpEbuildUses ON tmpEbuildUses(ebuildId, use)");
DB::TablePatch t;
t.src = "tmpEbuildUses";
t.dest = "gentoobrowse.ebuild_uses";
t.pk = {"ebuildid", "use"};
t.cols = {"ebuildid", "use", "defaultflag"};
t.where = &ebuildIds;
dbc->patchTable(&t);
}
{
dbc->execute("CREATE UNIQUE INDEX idxTmpEbuildArchs ON tmpEbuildArchs(ebuildId, arch)");
DB::TablePatch t;
t.src = "tmpEbuildArchs";
t.dest = "gentoobrowse.ebuild_archs";
t.pk = {"ebuildid", "arch"};
t.cols = {"ebuildid", "arch"};
t.where = &ebuildIds;
dbc->patchTable(&t);
}
}
if (!packagesToPrune.empty()) {
bool any = false;
for (const auto & catPkg : packagesToPrune) {
// Prune packages
auto pp = sql::maintenance::packagePrune.modify(dbc);
pp->bindParamS(0, catPkg.first);
pp->bindParamS(1, catPkg.second);
any |= pp->execute();
}
if (any) {
// Prune categories
sql::maintenance::categoryPrune.modify(dbc)->execute();
}
}
}
void
EbuildMetaProcessor::tidy(DB::Connection * dbc)
{
Utils::Database::drop(dbc, "tmpEbuildDeps");
Utils::Database::drop(dbc, "tmpEbuildUses");
Utils::Database::drop(dbc, "tmpEbuildArchs");
}
void
EbuildMetaProcessor::perEbuildUpdates(
DB::Connection * dbc, const U::EbuildCacheParser & ecp, int64_t ebuildId, bool newest, int64_t packageId)
{
ebuildIds.entityIds.insert(ebuildId);
// IUSE
if (auto uses = ecp.get("IUSE")) {
useInsert->bindParamI(0, ebuildId);
useInsert->bindParamS(1, *uses);
useInsert->execute();
}
// KEYWORDS
if (auto keywords = ecp.get("KEYWORDS")) {
archInsert->bindParamI(0, ebuildId);
archInsert->bindParamS(1, *keywords);
archInsert->execute();
}
// Dependencies
depInsert->bindParamI(1, ebuildId);
if (auto depend = ecp.getRange("DEPEND")) {
depInsert->bindParamB(5, false);
insertDeps(Portage::Utils::Depend::parse(*depend));
}
if (auto rdepend = ecp.getRange("RDEPEND")) {
depInsert->bindParamB(5, true);
insertDeps(Portage::Utils::Depend::parse(*rdepend));
}
if (newest) {
DB::TablePatch t;
// HOMEPAGE
U::EntityWhereFilter<int64_t> pwf("packageId", packageId);
U::SplitEbuildProps sep_homepage("packageId", packageId, "url", ecp.get("HOMEPAGE"));
t.dest = "gentoobrowse.package_urls";
t.srcExpr = &sep_homepage;
t.pk = {"packageId", "url"};
t.cols = {"packageId", "url"};
t.where = &pwf;
dbc->patchTable(&t);
// Description
auto u = sql::maintenance::packageDescUpdate.modify(dbc);
u->bindParamS(0, ecp.getRange("DESCRIPTION").value_or(""));
u->bindParamI(1, packageId);
u->execute();
}
}
template<typename T>
const T &
operator|=(const IceUtil::Optional<T> & a, const T & b)
{
return a ? *a : b;
}
void
EbuildMetaProcessor::insertDeps(const std::vector<Gentoo::DependencyPtr> & deps)
{
for (const auto & d : deps) {
depInsert->bindParamS(0, d->category);
depInsert->bindParamS(2, boost::algorithm::join(d->use, ","));
depInsert->bindParamS(3, d->op |= std::string());
depInsert->bindParamS(4, d->package);
depInsert->bindParamS(6, d->slot |= std::string());
depInsert->bindParamS(7, d->version |= std::string());
depInsert->execute();
}
}
void
EbuildMetaProcessor::deleted(DB::Connection * dbc, int64_t repoId, const StringList & fn)
{
Glib::MatchInfo matches;
const Glib::ustring pv = fn[3];
if (packageVersion->match(pv, matches)) {
const std::string categoryName = fn[2];
const std::string packageName = matches.fetch(1);
const std::string ebuildVersion = matches.fetch(2);
// Delete ebuild
auto m = sql::maintenance::ebuildDelete.modify(dbc);
m->bindParamI(0, repoId);
m->bindParamS(1, categoryName);
m->bindParamS(2, packageName);
m->bindParamS(3, ebuildVersion);
m->execute(false);
packagesToPrune.insert({categoryName, packageName});
}
}
}
FACTORY(Gentoo::Service::EbuildMetaProcessor, Gentoo::Service::FileProcessorFactory);
|