Commit eedc0638 authored by Kaarle Ritvanen's avatar Kaarle Ritvanen

dependency resolver function

parent f3eeefc9
--[[
Dependency order resolver for Alpine Wall
Copyright (C) 2012-2013 Kaarle Ritvanen
Licensed under the terms of GPL2
]]--
module(..., package.seeall)
local util = require('awall.util')
function order(items)
local visited = {}
local res = {}
local function visit(key)
if util.contains(res, key) then return end
if visited[key] then return key end
visited[key] = true
local after = util.list(items[key].after)
for k, v in pairs(items) do
if util.contains(v.before, key) then table.insert(after, k) end
end
for i, k in ipairs(after) do
if items[k] then
local ek = visit(k)
if ek ~= nil then return ek end
end
end
table.insert(res, key)
end
for i, k in util.sortedkeys(items) do
local ek = visit(k)
if ek ~= nil then return ek end
end
return res
end
--[[ --[[
Policy file handling for Alpine Wall Policy file handling for Alpine Wall
Copyright (C) 2012 Kaarle Ritvanen Copyright (C) 2012-2013 Kaarle Ritvanen
Licensed under the terms of GPL2 Licensed under the terms of GPL2
]]-- ]]--
...@@ -10,6 +10,7 @@ require 'json' ...@@ -10,6 +10,7 @@ require 'json'
require 'lfs' require 'lfs'
require 'lpc' require 'lpc'
require 'awall.dependency'
require 'awall.object' require 'awall.object'
require 'awall.util' require 'awall.util'
...@@ -139,47 +140,32 @@ end ...@@ -139,47 +140,32 @@ end
function PolicySet:load() function PolicySet:load()
local input = {}
local source = {}
local polnames = {}
local policies = {} local policies = {}
local function require(name, fname) local function require(name, fname)
if policies[name] then return end if policies[name] then return end
table.insert(polnames, name)
policies[name] = self:loadJSON(name, fname)
for i, iname in util.listpairs(policies[name].import) do
require(iname)
end
end
for i, pol in ipairs(list(self.autodirs)) do require(unpack(pol)) end local policy = self:loadJSON(name, fname)
policies[name] = policy
if not policy.after then policy.after = policy.import end
for i, iname in util.listpairs(policy.import) do require(iname) end
end
local pending = {} for i, pol in ipairs(list(self.autodirs)) do require(unpack(pol)) end
local imported = {}
local function import(name)
if util.contains(imported, name) then return end local order = awall.dependency.order(policies)
if util.contains(pending, name) then if type(order) ~= 'table' then
error('Circular ordering directives: '..name) error('Circular ordering directives: '..order)
end end
table.insert(pending, name)
local data = policies[name]
local after = util.list(data.after or data.import) local input = {}
for pname, policy in pairs(policies) do local source = {}
if util.contains(util.list(policy.before), name) then
table.insert(after, pname)
end
end
for i, pname in ipairs(after) do import(pname) end
table.insert(imported, name) for i, name in ipairs(order) do
for cls, objs in pairs(policies[name]) do
for cls, objs in pairs(data) do
if not util.contains({'description', 'import', 'after', 'before'}, if not util.contains({'description', 'import', 'after', 'before'},
cls) then cls) then
if not source[cls] then source[cls] = {} end if not source[cls] then source[cls] = {} end
...@@ -203,10 +189,7 @@ function PolicySet:load() ...@@ -203,10 +189,7 @@ function PolicySet:load()
end end
end end
table.sort(polnames) return PolicyConfig.new(input, source, util.keys(policies))
for i, name in ipairs(polnames) do import(name) end
return PolicyConfig.new(input, source, polnames)
end end
......
--[[ --[[
Utility module for Alpine Wall Utility module for Alpine Wall
Copyright (C) 2012 Kaarle Ritvanen Copyright (C) 2012-2013 Kaarle Ritvanen
Licensed under the terms of GPL2 Licensed under the terms of GPL2
]]-- ]]--
...@@ -36,10 +36,22 @@ function maplist(var, func) ...@@ -36,10 +36,22 @@ function maplist(var, func)
end end
function contains(tbl, value) function contains(tbl, value)
for k, v in pairs(tbl) do if v == value then return true end end for k, v in listpairs(tbl) do if v == value then return true end end
return false return false
end end
function keys(tbl)
local res = {}
for k, v in pairs(tbl) do table.insert(res, k) end
return res
end
function sortedkeys(tbl)
local res = keys(tbl)
table.sort(res)
return ipairs(res)
end
function extend(tbl1, tbl2) function extend(tbl1, tbl2)
for i, var in listpairs(tbl2) do table.insert(tbl1, var) end for i, var in listpairs(tbl2) do table.insert(tbl1, var) end
end end
......
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