local scm = {}
local err = require("err")
+local e2lib = require("e2lib")
-- scm modules
local scms = {}
return true, nil
end
+--- apply default values where possible and a source configuration is
+-- incomplete
+-- @param info the info table
+-- @param sourcename the source name
+-- @return bool
+-- @return an error object on failure
+local function source_apply_default_licences(info, sourcename)
+ local e = err.new("applying default licences failed.")
+ local src = info.sources[ sourcename ]
+ if src.licences_default_applied then
+ return true
+ end
+ src.licences_default_applied = true
+ if not src.licences and src.licence then
+ e2lib.warnf("WDEPRECATED", "in source %s:", src.name)
+ e2lib.warnf("WDEPRECATED",
+ " licence attribute is deprecated. Replace by licences.")
+ src.licences = src.licence
+ end
+ if not src.licences then
+ e2lib.warnf("WDEFAULT", "in source %s:", src.name)
+ e2lib.warnf("WDEFAULT",
+ " licences attribute missing. Defaulting to empty list.")
+ src.licences = {}
+ elseif type(src.licences) == "string" then
+ e2lib.warnf("WDEPRECATED", "in source %s:", src.name)
+ e2lib.warnf("WDEPRECATED",
+ " licences attribute is not in table format. Converting.")
+ src.licences = { src.licences }
+ end
+ for i, s in pairs(src.licences) do
+ if type(i) ~= "number" or type(s) ~= "string" then
+ e:append("licences attribute is not a list of strings")
+ return false, e
+ end
+ end
+ for _,l in ipairs(src.licences) do
+ if not info.licences[l] then
+ e:append("unknown licence: %s", l)
+ return false, e
+ end
+ end
+ return true
+end
+
+--- validate generic source configuration, usable by SCM plugins
+-- @param info the info table
+-- @param sourcename the source name
+-- @return bool
+-- @return an error object on failure
+function scm.generic_source_validate(info, sourcename)
+ local src = info.sources[sourcename]
+ local rc, re
+ local e
+ if not src then
+ return false, err.new("invalid source: %s", sourcename)
+ end
+ e = err.new("in source %s:", sourcename)
+ rc, re = source_apply_default_licences(info, sourcename)
+ if not rc then
+ return false, e:cat(re)
+ end
+ if not src.type then
+ e:append("source has no `type' attribute")
+ end
+ if src.env and type(src.env) ~= "table" then
+ e:append("source has invalid `env' attribute")
+ else
+ if not src.env then
+ e2lib.warnf("WDEFAULT",
+ "source has no `env' attribute. Defaulting to empty dictionary")
+ src.env = {}
+ end
+ src._env = environment.new()
+ for k,v in pairs(src.env) do
+ if type(k) ~= "string" then
+ e:append("in `env' dictionary: key is not a string: %s", tostring(k))
+ elseif type(v) ~= "string" then
+ e:append("in `env' dictionary: value is not a string: %s", tostring(v))
+ else
+ src._env:set(k, v)
+ end
+ end
+ end
+ if e:getcount() > 1 then
+ return false, e
+ end
+ return true, nil
+end
+
+--- apply default values where possible
+-- @param info the info table
+-- @param sourcename the source name
+-- @return bool
+-- @return an error object on failure
+function scm.generic_source_default_working(info, sourcename)
+ local src = info.sources[ sourcename ]
+ if src.working_default_applied then
+ return true
+ end
+ src.working_default_applied = true
+ local src_working_default = string.format("in/%s", sourcename)
+ if src.working and src.working ~= src_working_default then
+ e2lib.warnf("WPOLICY", "in source %s:", src.name)
+ e2lib.warnf("WPOLICY", " configuring non standard working direcory")
+ elseif src.working then
+ e2lib.warnf("WHINT", "in source %s:", src.name)
+ e2lib.warnf("WHINT", " no need to configure working directory")
+ else
+ src.working = string.format("in/%s", sourcename)
+ e2lib.warnf("WDEFAULT", "in source %s:", src.name)
+ e2lib.warnf("WDEFAULT",
+ " `working' attribute missing. Defaulting to '%s'.", src.working)
+ end
+ return true
+end
+
+--- do some consistency checks required before using sources
+-- @param info
+-- @param sourcename string: source name
+-- @param require_workingcopy bool: return error if the workingcopy is missing
+-- @return bool
+-- @return an error object on failure
+function scm.generic_source_check(info, sourcename, require_workingcopy)
+ local rc, re
+ rc, re = scm.validate_source(info, sourcename)
+ if not rc then
+ return false, re
+ end
+ rc, re = scm.working_copy_available(info, sourcename)
+ if (not rc) and require_workingcopy then
+ return false, err.new("working copy is not available")
+ end
+ rc, re = scm.check_workingcopy(info, sourcename)
+ if not rc then
+ return false, re
+ end
+ return true, nil
+end
+
scm.register_interface("sourceid")
scm.register_interface("validate_source")
scm.register_interface("toresult")