aboutsummaryrefslogtreecommitdiffstats
path: root/aport.lua
blob: 71113475afc457217ecef2cf981d75bf2512af81 (plain) (blame)
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
#!/usr/bin/lua


-- those should be read from some config file
aportsdir = "~/aports"
repos = { "main", "testing" }


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

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

-- parse the APKBUILDs and return a list
function parse_apkbuilds(dir, repos)
	local i,v, p
	local str=""
	if repos == nil then
		return
	end
	--expand repos
	for i,v in ipairs(repos) do
		str = str..dir.."/"..v.."/*/APKBUILD "
	end

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

function target_packages(pkgdb, pkgname)
	local i,v
	local t = {}
	for i,v in ipairs(pkgdb[pkgname]) do
		table.insert(t, pkgname.."-"..v.pkgver.."-r"..v.pkgrel..".apk")
	end
	return t
end

function list_depends( pkg, pkgdb )
	local dl = {}
	local s
	if pkg and not pkg.added then
		pkg.added = true

		if pkg.depends then
			for i,v in ipairs(pkg.depends) do
				--print("v = <"..v..">")
				s = list_depends( pkgdb[v], pkgdb )
				if #s > 0 then
					dl[#dl + 1] = s
				end
			end
		end
		if pkg.makedepends then
			for i,v in ipairs(pkg.makedepends) do
				--print("v = {"..v.."}")
				s = list_depends( pkgdb[v], pkgdb )
				if #s > 0 then
					dl[#dl + 1] = s
				end
			end
		end
		dl[#dl + 1] = pkg.pkgname
	end

	s = table.concat(dl," ")
	--print("s = ["..s.."]")
	return s
end

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
		--table.insert(pkgdb[a.pkgname], a)
		pkgdb[a.pkgname] = a
		--print("pk "..a.pkgname.." is a "..type(a).." ("..pkgdb[a.pkgname].pkgname..")")
		-- add subpackages to package db
		local k,v
		for k,v in ipairs(a.subpackages) do
			if pkgdb[v] == nil then
				pkgdb[v] = {}
			end
			--table.insert(pkgdb[v], a)
			pkgdb[v] = a
		end
		-- add to reverse dependencies
		for k,v in ipairs(a.makedepends) do
			if revdeps[v] == nil then
				revdeps[v] = {}
			end
			table.insert(revdeps[v], a)
		end
	end
	return pkgdb, revdeps
end

-- PKGBUILD import
function split_pkgbuild(line)
	local r = {}
	local pkgname, pkgver = string.match(line, "([^|]*)|([^|]*)")
	r.pkgname = pkgname
	r.pkgver = pkgver
	return r
end

function parse_pkgbuilds(dir, repos)
	local i,v, p
	local str=""
	if repos == nil then
		return
	end
	--expand repos
	for i,v in ipairs(repos) do
		str = str..dir.."/"..v.."/*/PKGBUILD "
	end

	local p = io.popen([[/bin/bash -c '
		for i in ]]..str..[[; do
			pkgname=
			pkgver=
			source $i
			echo $pkgname\|$pkgver
		done
		' 2>/dev/null
	]])
	return function()
		local line = p:read("*line")
		if line == nil then
			p:close()
			return nil
		end
		return split_pkgbuild(line)
	end
end

function init_absdb(dir, repos)
	local p
	local db = {}
	for p in parse_pkgbuilds(dir, repos) do
		if db[p.pkgname] == nil then
			db[p.pkgname] = {}
		end
		table.insert(db[p.pkgname], p.pkgver)
	end
	return db
end


-- Applets -----------------------
applet = {}
function applet.revdep(arg)
	local pkg = arg[2]
	if pkg == nil then
		-- usage?
		return nil
	end
	local apkdb, rev = init_apkdb(aportsdir, repos)
	local _,p
	for _,p in ipairs(rev[pkg] or {}) do
		print(p.pkgname)
	end
end
--absdb = init_absdb("/var/abs", { "core", "extra", "community" })

function applet.deplist(arg)
	local apkdb, rev = init_apkdb(arg[2],{ arg[3] })

	local deplist = {}
	local nm,pk
	for nm,pk in pairs(apkdb) do
		local dl
		--print("pk "..nm.." is a "..type(pk).." ("..apkdb[nm].pkgname..")")
		--deplist[#deplist + 1] = "***"
		dl = list_depends(pk,apkdb)
		-- print("deplist for "..nm..": "..deplist)
		if #dl > 0 then
			deplist[#deplist + 1] = dl
		end
	end
	print(table.concat(deplist," "))
end

cmd = arg[1]

if cmd == nil then
	-- usage
	io.stderr:write( "no command given\n" );
	return
end

if type(applet[cmd]) == "function" then
	applet[cmd](arg)
else
	io.stderr:write(cmd..": invalid applet\n")
end