]> git.e2factory.org Git - e2factory.git/commitdiff
Move generic source functions from git to scm
authorTobias Ulmer <tu@emlix.com>
Wed, 22 Aug 2012 14:25:46 +0000 (16:25 +0200)
committerTobias Ulmer <tu@emlix.com>
Tue, 26 Feb 2013 18:07:07 +0000 (19:07 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
local/scm.lua

index 74418fb364cc868354f7b8a9d80b40b7ed3c07aa..1167ee3f23c3c097c24af0ed599f7a3e837cafc2 100644 (file)
@@ -27,6 +27,7 @@
 
 local scm = {}
 local err = require("err")
+local e2lib = require("e2lib")
 
 -- scm modules
 local scms = {}
@@ -110,6 +111,146 @@ function scm.register_function(type, name, func)
     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")