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
|
---------
-- Dump packages info into JSON.
----
local cjson = require('cjson')
local pkgmod = require('aports.pkg')
local get_maintainer = pkgmod.get_maintainer
local push = table.insert
-- Tables with this metatable are encoded as an array by our patched cjson.
local empty_array_mt = { __name = 'array' }
local empty_array = setmetatable({}, empty_array_mt)
--- Converts pkg's `arch` map into list of architectures.
-- Negated architecture is prefixed by "!".
--
-- @tparam {[string]=bool,...} arch
-- @treturn {string,...}
local function convert_arch(arch)
local t = {}
for name, value in pairs(arch) do
push(t, value and name or '!'..name)
end
return t
end
--- "Marks" the given table to be encoded as a JSON array.
local function array(tab)
return #tab == 0
and empty_array
or setmetatable(tab, empty_array_mt)
end
--- Converts the `pkg` into a simple table (map) that can be serialized.
--
-- @tparam table pkg The package table (see @{db.split_apkbuild}).
-- @treturn table A simple map of values.
local function pkg_to_table(pkg)
return {
pkgname = pkg.pkgname,
pkgver = pkg.pkgver,
pkgrel = tonumber(pkg.pkgrel),
pkgdesc = pkg.pkgdesc,
url = pkg.url,
license = pkg.license,
arch = array(convert_arch(pkg.arch)),
depends = array(pkg.depends),
makedepends = array(pkg.makedepends),
checkdepends = array(pkg.checkdepends),
subpackages = array(pkg.subpackages),
source = array(pkg.source),
maintainer = get_maintainer(pkg),
}
end
local M = {}
--- Dumps packages from the given iterator to a map indexed by package name.
function M.pkgs_to_map(iter, state)
local t = {}
for pkg in iter, state do
t[pkg.pkgname] = pkg_to_table(pkg)
end
return t
end
--- Dumps packages from the given iterator to JSON.
-- @see pkgs_to_map
function M.pkgs_to_json(iter, state)
cjson.encode_sort_keys = true
return cjson.encode(M.pkgs_to_map(iter, state))
end
return M
|