parser: Post-process a package table

Parse some of the fields there.
parent 7701e24a
......@@ -18,6 +18,7 @@ along with Updater. If not, see <http://www.gnu.org/licenses/>.
]]--
local error = error
local type = type
module "backend"
......@@ -87,4 +88,53 @@ function split_blocks(string)
return filter_empty
end
--[[
Postprocess the table representing a status of a package. The original table
is modified and returned.
It does:
Splitting these fielts into subtables of items:
Conffiles
Depends
Status
]]
function package_postprocess(status)
--[[
If the field is present, it replaces it with a new table.
The table is created from the field by splitting it by
separator (list of „forbidden“ characters and extracting
two fields from cleanup pattern. If only one is provided,
the second is replaced by true. If the cleanup doesn't match,
the part is thrown away.
]]
local function replace(name, separator, cleanup)
if type(cleanup) == "string" then
local c = cleanup
cleanup = function (s) return s:match(c) end
end
local value = status[name]
if value then
local result = {}
for item in value:gmatch("[^" .. separator .. "]+") do
local n, v = cleanup(item)
if n then
if not v then v = true end
result[n] = v
end
end
status[name] = result
end
end
-- Conffiles are lines with two „words“
replace("Conffiles", "\n", "%s*(%S+)%s+(%S+)")
-- Depends are separated by commas and may contain a version in parentheses
local idx = 0
replace("Depends", ",", function (s)
idx = idx + 1
return idx, s:gsub("%s", ""):gsub("%(", " (")
end)
replace("Status", " ", "(%S+)")
return status
end
return _M
......@@ -90,3 +90,43 @@ block 2
]], {'block 1', 'block 2'})
-- Few empty lines at the beginning - should not produce an empty block
end
--[[
Test post-processing packages. Examples taken and combined from real status file
(however, this exact package doesn't exist).
]]
function test_package_postprocces()
local package = {
Package = "dnsmasq-dhcpv6",
Version = "2.73-1",
Depends = "libc, kernel (= 3.18.21-1-70ea6b9a4b789c558ac9d579b5c1022f-10), kmod-nls-base",
Status = "install user installed",
Architecture = "mpc85xx",
Conffiles = [[
/etc/config/dhcp f81fe9bd228dede2165be71e5c9dcf76cc
/etc/dnsmasq.conf 1e6ab19c1ae5e70d609ac7b6246541d520]]
}
local output = B.package_postprocess(package)
-- Make sure it modifies the table in-place
assert_equal(package, output)
assert_table_equal({install = true, user = true, installed = true}, output.Status)
assert_table_equal({["/etc/config/dhcp"] = "f81fe9bd228dede2165be71e5c9dcf76cc", ["/etc/dnsmasq.conf"] = "1e6ab19c1ae5e70d609ac7b6246541d520"}, output.Conffiles)
assert_table_equal({"libc", "kernel (=3.18.21-1-70ea6b9a4b789c558ac9d579b5c1022f-10)", "kmod-nls-base"}, output.Depends)
--[[
Now check it doesn't get confused when some of the modified fields aren't there
(or none, in this case).
]]
local pack_nomod = {
Package = "wget",
Version = "1.17.1-1",
Architecture = "mpc85xx"
}
local pack_nomod_cp = {}
for n, v in pairs(pack_nomod) do
pack_nomod_cp[n] = v
end
local output = B.package_postprocess(pack_nomod)
assert_not_equal(pack_nomod_cp, output)
assert_equal(pack_nomod, output)
assert_table_equal(pack_nomod_cp, output)
end
......@@ -6,9 +6,13 @@ function launch(test)
end
function assert_table_equal(t1, t2)
lunit.assert_table(t1)
lunit.assert_table(t2)
local function cmp(t1, t2, name)
for k, v in pairs(t1) do
lunit.assert_equal(v, t2[k], "Values for key '" .. k .. "' differ: '" .. v .. "' vs '" .. t2[k] .. "'" .. name)
local v2 = t2[k]
if v2 == nil then v2 = "nil" end
lunit.assert_equal(v, t2[k], "Values for key '" .. k .. "' differ: '" .. tostring(v) .. "' vs '" .. tostring(v2) .. "'" .. name)
end
end
cmp(t1, t2, " pass 1")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment