summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/maintenance/ebuildMetaProcessor.cpp
blob: 2b2541b7f6beb50d3ad8ef2c812bd47731ab2259 (plain)
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
#include "ebuildMetaProcessor.h"
#include <modifycommand.h>
#include <sqlWriter.h>
#include <selectcommandUtil.impl.h>
#include <sqlWriter.h>
#include <boost/filesystem/operations.hpp>
#include <glibmm/regex.h>
#include <tablepatch.h>
#include "fileUtils.h"
#include "xmlUtils.h"
#include "dbUtils.h"
#include <sql/maintenance/categoryInsert.sql.h>
#include <sql/maintenance/packageInsert.sql.h>
#include <sql/maintenance/packagePrune.sql.h>
#include <sql/maintenance/categoryPrune.sql.h>
#include <sql/maintenance/ebuildInsert.sql.h>
#include <sql/maintenance/ebuildUpdate.sql.h>
#include <sql/maintenance/ebuildDelete.sql.h>

namespace U = Gentoo::Utils;
using namespace Gentoo::Utils::File;

static Glib::RefPtr<Glib::Regex> packageVersion = Glib::Regex::create("^(.+)-([0-9].*)$");

namespace Gentoo {
	namespace Service {
		class EbuildCacheParser : public U::MemMap {
			public:
				typedef std::pair<const char *, const char *> Range;
				typedef std::map<std::string, const Range> KVs;

				EbuildCacheParser(const boost::filesystem::path & p) :
					U::MemMap(p)
				{
					const char * chardata = (const char *)this->data;
					while (const char * eq = strchr(chardata, '=')) {
						if (const char * nl = strchr(eq + 1, '\n')) {
							kvs.insert({ std::string(chardata, eq), { eq + 1, nl } });
							chardata = nl + 1;
						}
						else {
							kvs.insert({ std::string(chardata, eq), { eq + 1, (const char *)this->data + st.st_size } });
							return;
						}
					}
				}

				boost::optional<Glib::ustring> get(const std::string & key) const
				{
					auto kvi = kvs.find(key);
					if (kvi == kvs.end()) {
						return boost::optional<Glib::ustring>();
					}
					return Glib::ustring(kvi->second.first, kvi->second.second);
				}

			private:
				KVs kvs;
		};

		class SplitEbuildProps : public DB::SqlWriter {
			public:
				SplitEbuildProps(int e, const std::string & c, const boost::optional<Glib::ustring> & p) :
					ebuildId(e),
					colName(c),
					props(p)
				{
				}

				void writeSql(AdHoc::Buffer & sql) override
				{
					sql.appendbf("(SELECT DISTINCT ?::int ebuildId, trim(regexp_split_to_table(?, '\\s+'), '+') %s)", colName);
				}

				void bindParams(DB::Command * c, unsigned int & offset) override
				{
					c->bindParamI(offset++, ebuildId);
					Utils::Database::bindOptionalsS(c, offset++, { props });
				}

				const int ebuildId;
				const std::string colName;
				const boost::optional<Glib::ustring> props;
		};

		const int EbuildMetaProcessor::FILETYPEID = 1;

		void
		EbuildMetaProcessor::created(DB::Connection * dbc, const boost::filesystem::path & fn, const boost::filesystem::path & path) const
		{
			Glib::MatchInfo matches;
			const Glib::ustring pv = (fn / 4).string();
			if (packageVersion->match(pv, matches)) {
				EbuildCacheParser ecp(path);
				const std::string repoName = (fn / 0).string();
				const std::string categoryName = (fn / 3).string();
				const std::string packageName = matches.fetch(1);
				const std::string ebuildVersion = matches.fetch(2);

				// Maybe create a category
				auto ci = dbc->modify(sql::maintenance::categoryInsert::sql);
				ci->bindParamS(0, categoryName);
				ci->bindParamS(1, categoryName);
				ci->execute();
				// Maybe create a package
				auto pi = dbc->modify(sql::maintenance::packageInsert::sql);
				pi->bindParamS(0, packageName);
				Utils::Database::bindOptionalsS(pi.get(), 1, { ecp.get("DESCRIPTION") });
				pi->bindParamS(2, packageName);
				pi->bindParamS(3, categoryName);
				pi->execute();
				// Create an ebuild
				auto m = dbc->select(sql::maintenance::ebuildInsert::sql);
				m->bindParamS(0, ebuildVersion);
				m->bindParamS(1, ebuildVersion);
				Utils::Database::bindOptionalsS(m.get(), 2, { ecp.get("SLOT") });
				Utils::Database::bindOptionalsS(m.get(), 3, { ecp.get("LICENSE") });
				m->bindParamT(4, boost::posix_time::from_time_t(ecp.getStat().st_mtim.tv_sec));
				m->bindParamS(5, repoName);
				m->bindParamS(6, categoryName);
				m->bindParamS(7, packageName);
				m->forEachRow<int64_t>([this,dbc,&ecp,&fn] (auto ebuildId) {
						fprintf(stderr, "Created ebuild %ld for %s\n", ebuildId, fn.c_str());
						this->perEbuildUpdates(dbc, ecp, ebuildId);
					});
			}
		}

		void
		EbuildMetaProcessor::modified(DB::Connection * dbc, const boost::filesystem::path & fn, const boost::filesystem::path & path) const
		{
			Glib::MatchInfo matches;
			const Glib::ustring pv = (fn / 4).string();
			if (packageVersion->match(pv, matches)) {
				auto m = dbc->select(sql::maintenance::ebuildUpdate::sql);
				EbuildCacheParser ecp(path);
				Utils::Database::bindOptionalsS(m.get(), 0, { ecp.get("SLOT") });
				Utils::Database::bindOptionalsS(m.get(), 1, { ecp.get("LICENSE") });
				m->bindParamT(2, boost::posix_time::from_time_t(ecp.getStat().st_mtim.tv_sec));
				m->bindParamS(3, (fn / 0).string()); // repo
				m->bindParamS(4, (fn / 3).string()); // category
				m->bindParamS(5, matches.fetch(1)); // package
				m->bindParamS(6, matches.fetch(2)); // version
				m->forEachRow<int64_t>([this,dbc,&ecp,&fn] (auto ebuildId) {
						fprintf(stderr, "Updated ebuild %ld for %s\n", ebuildId, fn.c_str());
						this->perEbuildUpdates(dbc, ecp, ebuildId);
					});
			}
		}

		class EbuildWhereFilter : public DB::SqlWriter {
			public:
				EbuildWhereFilter(int64_t e) : ebuildId(e) { }

				void writeSql(AdHoc::Buffer & sql) override
				{
					sql.append("b.ebuildId = ?");
				}

				void bindParams(DB::Command * c, unsigned int & offset) override
				{
					c->bindParamI(offset++, ebuildId);
				}

				const int64_t ebuildId;
		};

		void
		EbuildMetaProcessor::perEbuildUpdates(DB::Connection * dbc, const EbuildCacheParser & ecp, int64_t ebuildId) const
		{
			EbuildWhereFilter ewf(ebuildId);
			// USE flags
			DB::TablePatch t;
			SplitEbuildProps sep_use(ebuildId, "use", ecp.get("IUSE"));
			t.dest = "gentoobrowse.ebuild_uses";
			t.srcExpr = &sep_use;
			t.pk = { "ebuildid", "use" };
			t.cols = { "ebuildid", "use" };
			t.where = &ewf;
			dbc->patchTable(&t);
			// USE flags
			SplitEbuildProps sep_keywords(ebuildId, "arch", ecp.get("KEYWORDS"));
			t.dest = "gentoobrowse.ebuild_archs";
			t.srcExpr = &sep_keywords;
			t.pk = { "ebuildid", "arch" };
			t.cols = { "ebuildid", "arch" };
			t.where = &ewf;
			dbc->patchTable(&t);
		}

		void
		EbuildMetaProcessor::deleted(DB::Connection * dbc, const boost::filesystem::path & fn) const
		{
			Glib::MatchInfo matches;
			const Glib::ustring pv = (fn / 4).string();
			if (packageVersion->match(pv, matches)) {
				const std::string repoName = (fn / 0).string();
				const std::string categoryName = (fn / 3).string();
				const std::string packageName = matches.fetch(1);
				const std::string ebuildVersion = matches.fetch(2);

				// Delete ebuild
				auto m = dbc->modify(sql::maintenance::ebuildDelete::sql);
				m->bindParamS(0, repoName);
				m->bindParamS(1, categoryName);
				m->bindParamS(2, packageName);
				m->bindParamS(3, ebuildVersion);
				m->execute(false);
				// Prune packages
				auto pp = dbc->modify(sql::maintenance::packagePrune::sql);
				pp->bindParamS(0, categoryName);
				pp->execute();
				// Prune categories
				dbc->execute(sql::maintenance::categoryPrune::sql);
			}
		}
	}
}