summaryrefslogtreecommitdiffstats
path: root/aports/db.lua
blob: 4bbee16142a42ae51b24f2897bb15d1d1afd7f6a (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
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

local M = {}
local abuild = require('aports.abuild')
local pkg = require('aports.pkg')

local function split_subpkgs(str, linguas, pkgname)
	local t = {}
	local e
	if (str == nil) then
		return nil
	end
	for e in string.gmatch(str, "%S+") do
		t[#t + 1] = string.gsub(e, ":.*", "")
	end
	for k,v in pairs(linguas) do
		t[#t + 1] = ("%s-lang-%s"):format(pkgname, v)
	end
	return t
end

local function split_deps(str)
	local t = {}
	local e
	if (str == nil) then
		return nil
	end
	for e in string.gmatch(str, "%S+") do
		t[#t + 1] = string.gsub(e, "[=<>].*", "")
	end
	return t
end

local function split(str)
	local t = {}
	local e
	if (str == nil) then
		return nil
	end
	for e in string.gmatch(str, "%S+") do
		t[#t + 1] = e
	end
	return t
end

local function split_arch(archstr)
	local t = {}
	for _,arch in pairs(split(archstr)) do
		t[arch] = true
	end
	return t
end

local function split_apkbuild(line)
	local r = {}
	local dir,pkgname, pkgver, pkgrel, arch, depends, makedepends, subpackages, linguas, source, url = string.match(line, "([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)")
	r.dir = dir
	r.pkgname = pkgname
	r.pkgver = pkgver
	r.pkgrel = pkgrel
	r.depends = split_deps(depends)
	r.makedepends = split_deps(makedepends)
	r.linguas = split(linguas)
	r.subpackages = split_subpkgs(subpackages, r.linguas, pkgname)
	r.source = split(source)
	r.url = url
	r.arch = split_arch(arch)
	return r
end

-- parse the APKBUILDs and return an iterator
local function parse_apkbuilds(aportsdir, repos)
	local i,v, p
	local str=""
	if repos == nil then
		return nil
	end
	--expand repos
	for _,repo in pairs(repos) do
		str = ("%s %s/%s/*/APKBUILD"):format(str, aportsdir, repo)
	end

	local p = io.popen(". "..abuild.functions..";"..[[
		for i in ]]..str..[[; do
			pkgname=
			pkgver=
			pkgrel=
			arch=
			depends=
			makedepends=
			subpackages=
			linguas=
			source=
			url=
			dir="${i%/APKBUILD}";
			[ -n "$dir" ] || exit 1;
			cd "$dir";
			. ./APKBUILD;
			echo $dir\|$pkgname\|$pkgver\|$pkgrel\|$arch\|$depends\|$makedepends\|$subpackages\|$linguas\|$source\|$url ;
		done;
	]])
	return function()
		local line = p:read("*line")
		if line == nil then
			p:close()
			return nil
		end
		return split_apkbuild(line)
	end
end

local function init_apkdb(aportsdir, repos)
	local pkgdb = {}
	local revdeps = {}
	local a
	for a in parse_apkbuilds(aportsdir, repos) do
	--	io.write(a.pkgname.." "..a.pkgver.."\t"..a.dir.."\n")
		if pkgdb[a.pkgname] == nil then
			pkgdb[a.pkgname] = {}
		end
		pkg.init(a)
		table.insert(pkgdb[a.pkgname], a)
		-- add subpackages to package db
		local k,v
		for k,v in pairs(a.subpackages) do
			if pkgdb[v] == nil then
				pkgdb[v] = {}
			end
			table.insert(pkgdb[v], a)
		end
		-- add to reverse dependencies
		for dep in a:each_dependency() do
			if revdeps[dep] == nil then
				revdeps[dep] = {}
			end
			table.insert(revdeps[dep], a)
		end
	end
	return pkgdb, revdeps
end

local Aports = {}
function Aports:recursive_dependencies(pn)
	local visited={}
	local apkdb = self.apks

	return coroutine.wrap(function()
		function recurs(pn)
			if pn == nil or visited[pn] or apkdb[pn] == nil then
				return false
			end
			visited[pn] = true
			local _, p
			for _, p in pairs(apkdb[pn]) do
				for dep in p:each_dependency() do
					if recurs(dep) then
						return true
					end
				end
			end
			coroutine.yield(pn)
		end
		return recurs(pn)
	end)
end

function Aports:target_packages(pkgname)
	return coroutine.wrap(function()
		for k,v in pairs(self.apks[pkgname]) do
			coroutine.yield(pkgname.."-"..v.pkgver.."-r"..v.pkgrel..".apk")
		end
	end)
end

function Aports:each_name()
	local apks = self.apks
	return coroutine.wrap(function()
		for k,v in pairs(self.apks) do
			coroutine.yield(k,v)
		end
	end)
end

function Aports:each_reverse_dependency(pkg)
	return coroutine.wrap(function()
		for k,v in pairs(self.revdeps[pkg] or {}) do
			coroutine.yield(k,v)
		end
	end)
end

function Aports:each_known_dependency(pkg)
	return coroutine.wrap(function()
		for dep in pkg:each_dependency() do
			if self.apks[dep] then
				coroutine.yield(dep)
			end
		end
	end)
end

function Aports:each_pkg_with_name(name)
	if self.apks[name] == nil then
		io.stderr:write("WARNING: "..name..": not provided by any known APKBUILD\n")
		return function() return nil end
	end
	return coroutine.wrap(function()
		for index, pkg in pairs(self.apks[name]) do
			coroutine.yield(pkg, index)
		end
	end)
end

function Aports:each()
	return coroutine.wrap(function()
		for name, pkglist in self:each_name() do
			for _, pkg in pairs(pkglist) do
				coroutine.yield(pkg, name)
			end
		end
	end)
end

function Aports:each_aport()
	return coroutine.wrap(function()
		for pkg, name in self:each() do
			if name == pkg.pkgname then
				coroutine.yield(pkg)
			end
		end
	end)
end

function Aports:each_need_build()
	return coroutine.wrap(function()
		for aport in self:each_aport() do
			if aport:arch_enabled() and not aport:all_apks_exists() then
				coroutine.yield(aport)
			end
		end
	end)
end

function Aports:each_in_build_order(namelist)
	local pkgs = {}
	for _,name in pairs(namelist) do
		for pkg in self:each_pkg_with_name(name) do
			pkgs[pkg.dir] = true
		end
	end

	return coroutine.wrap(function()
		for _,name in pairs(namelist) do
			for dep in self:recursive_dependencies(name) do
				for pkg in self:each_pkg_with_name(dep) do
					if pkgs[pkg.dir] then
						coroutine.yield(pkg)
						pkgs[pkg.dir] = nil
					end
				end
			end
		end
	end)
end

function Aports:git_describe()
	local cmd = ("git --git-dir %s/.git describe"):format(self.aportsdir)
	local f = io.popen(cmd)
	if f == nil then
		return nil
	end
	local result = f:read("*line")
	f:read("*a")
	f:close()
	return result
end

function Aports:known_deps_exists(pkg)
	for name in self:each_known_dependency(pkg) do
		for dep in self:each_pkg_with_name(name) do
			if dep.pkgname ~= pkg.pkgname and dep:arch_enabled() and not dep:all_apks_exists() then
				return false
			end
		end
	end
	return true
end

function M.new(aportsdir, ...)
	local h = Aports
	h.aportsdir = aportsdir
	h.repos = {...}
	h.apks, h.revdeps = init_apkdb(aportsdir, h.repos)
	return h
end

return M