summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/maintenance/ebuildMetaProcessor.cpp
blob: 7477c63e6259aa957c43817dea87d942d7254fdb (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
#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 "utils/fileUtils.h"
#include "utils/dbUtils.h"
#include "utils/ebuildCacheParser.h"
#include "utils/entityWhereFilter.h"
#include "utils/splitEbuildProps.h"
#include <sql/maintenance/categoryInsert.sql.h>
#include <sql/maintenance/packageInsert.sql.h>
#include <sql/maintenance/packageDescUpdate.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 {

		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)) {
				U::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, bool, int64_t>([this,dbc,&ecp,&fn] (auto ebuildId, auto newest, auto packageId) {
						fprintf(stderr, "Created ebuild %ld for %s (%s)\n", ebuildId, fn.c_str(), newest ? "yes" : "no");
						this->perEbuildUpdates(dbc, ecp, ebuildId, newest, packageId);
					});
			}
		}

		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);
				U::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, bool, int64_t>([this,dbc,&ecp,&fn] (auto ebuildId, auto newest, auto packageId) {
						fprintf(stderr, "Updated ebuild %ld for %s (%s)\n", ebuildId, fn.c_str(), newest ? "yes" : "no");
						this->perEbuildUpdates(dbc, ecp, ebuildId, newest, packageId);
					});
			}
		}

		void
		EbuildMetaProcessor::perEbuildUpdates(DB::Connection * dbc, const U::EbuildCacheParser & ecp, int64_t ebuildId, bool newest, int64_t packageId) const
		{
			U::EntityWhereFilter ewf("ebuildId", ebuildId);
			// IUSE
			DB::TablePatch t;
			U::SplitEbuildProps sep_use("ebuildId", 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);
			// KEYWORDS
			U::SplitEbuildProps sep_keywords("ebuildId", 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);
			if (newest) {
				// HOMEPAGE
				U::EntityWhereFilter 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 = dbc->modify(sql::maintenance::packageDescUpdate::sql);
				Utils::Database::bindOptionalsS(u.get(), 0, { ecp.get("DESCRIPTION") });
				u->bindParamI(1, packageId);
				u->execute();
			}
		}

		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->modify(sql::maintenance::categoryPrune::sql)->execute();
			}
		}
	}
}