]> git.e2factory.org Git - e2factory.git/commitdiff
cache: turn enable-writeback into a global commandline option
authorTobias Ulmer <tu@emlix.com>
Wed, 2 Nov 2016 15:54:12 +0000 (16:54 +0100)
committerTobias Ulmer <tu@emlix.com>
Wed, 16 Nov 2016 14:41:18 +0000 (15:41 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/cache.lua
generic/e2option.lua
global/e2-create-project.lua
global/e2-fetch-project.lua
global/e2-install-e2.lua
local/e2-build.lua
local/e2tool.lua

index b97512ee02d49621761e51b578bcdcbdad8e75c8..e3c2c57ea85233011e078095b40d19e21f87aded 100644 (file)
@@ -27,6 +27,19 @@ local strict = require("strict")
 local transport = require("transport")
 local url = require("url")
 
+--- Vector for keeping delayed flag options,
+-- set to false once options are processed.
+-- @field table containing the following fields:
+-- @field server server name, validated later
+-- @field flag operation name, currently only "writeback"
+-- @field value value for operation
+-- @see cache.setup_cache_apply_opts
+-- @see cache.set_writeback
+local _opts = {
+    -- { server=.., flag=.., value=.. },
+    -- ...
+}
+
 --- Internal representation of a cache. This table is locked.
 -- @table cache
 -- @field _name Human readable name.
@@ -136,6 +149,32 @@ function cache.setup_cache_local(c, project_root, project_location)
     return true
 end
 
+--- Apply delayed commandline options once cache is set up and disable
+-- the delayed mechanism
+-- @param c cache object
+-- @return True on success, false on error
+-- @return Error object on failure
+function cache.setup_cache_apply_opts(c)
+    local rc, re, opts
+
+    opts = _opts
+    _opts = false -- stop delayed processing
+
+    for _, opt in ipairs(opts) do
+        if opt.flag == "writeback" then
+            rc, re = cache.set_writeback(c, opt.server, opt.value)
+            if not rc then
+                return false, re
+            end
+        else
+            return false,
+                err.new("unknown delayed option: %s", opt.flag)
+        end
+    end
+
+    return true
+end
+
 local _server_names = strict.lock({
     dot = ".",
     -- the proj_storage server is equivalent to
@@ -777,12 +816,24 @@ function cache.writeback_enabled(c, server, flags)
 end
 
 --- enable/disable writeback for a server
--- @param c the cache data structure
+-- @param c the cache data structure or nil when the cache is not yet set up
 -- @param server the server where the file is located
 -- @param value boolean: the new setting
 -- @return boolean
 -- @return an error object on failure
 function cache.set_writeback(c, server, value)
+
+    if _opts then
+        e2lib.logf(3, "delaying cache.set_writeback(%s, %s, %s)",
+            tostring(c), tostring(server), tostring(value))
+        table.insert(_opts,
+            { flag = "writeback", server = server, value = value })
+
+        return true
+    end
+
+    assertIsTable(c)
+
     if type(value) ~= "boolean" then
         return false, err.new(
         "cache.set_writeback(): value is not boolean")
index fb950e640daf5422b28d02d66f7296617c93a005..013540a5ad2bfc62c19d7679c8e1edab0536a59b 100644 (file)
@@ -28,6 +28,7 @@ local plugin = require("plugin")
 local err = require("err")
 local strict = require("strict")
 local tools = require("tools")
+local cache = require("cache")
 
 local options = {}
 local aliases = {}
@@ -96,6 +97,19 @@ local function defaultoptions()
     end,
     "FILE")
 
+    local function disable_writeback(server)
+        cache.set_writeback(nil, server, false)
+    end
+
+    e2option.option("disable-writeback", "disable writeback for server", nil,
+        disable_writeback, "SERVER")
+
+    local function enable_writeback(server)
+        cache.set_writeback(nil, server, true)
+    end
+    e2option.option("enable-writeback", "enable writeback for server", nil,
+        enable_writeback, "SERVER")
+
     e2option.flag("quiet", "disable all log levels",
     function()
         e2lib.setlog(1, false)
index 4296c499af7126d66649b71f2240e1e5a0436cbf..b93ff67a43d992f1034225d20709817c9e058d5c 100644 (file)
@@ -121,6 +121,11 @@ local function e2_create_project(arg)
         error(e:cat(re))
     end
 
+    rc, re = cache.setup_cache_apply_opts(scache)
+    if not rc then
+        error(e:cat(re))
+    end
+
     if #arguments ~= 1 then
         e2option.usage(1)
     end
index 6dac88a5f0c3f82b03d6b0806a3d3bfae130caaa..d5de6938b99fa1b32021f2eaad1b9feef0cd76e0 100644 (file)
@@ -60,6 +60,11 @@ local function e2_fetch_project(arg)
         error(e:cat(re))
     end
 
+    rc, re = cache.setup_cache_apply_opts(scache)
+    if not rc then
+        error(e:cat(re))
+    end
+
     -- standard global tool setup finished
 
     if #arguments < 1 then
index 4650947eb34005afb4df9da890d54e841a6fc821..66ea97c9d42c50f5a48d45173aafcfb072842745 100644 (file)
@@ -64,6 +64,11 @@ local function e2_install_e2(arg)
         error(e:cat(re))
     end
 
+    rc, re = cache.setup_cache_apply_opts(scache)
+    if not rc then
+        error(e:cat(re))
+    end
+
     -- standard global tool setup finished
 
     if #arguments > 0 then
index 872541afd626978510e800272213bf26ca161149..eb6ccd540731d5af2dc14ad45ba25582ba7d5671 100644 (file)
@@ -49,43 +49,6 @@ local function e2_build(arg)
     e2option.flag("playground", "prepare environment but do not build")
     e2option.flag("keep", "do not remove chroot environment after build")
     e2option.flag("buildid", "display buildids and exit")
-    -- cache is not yet initialized when parsing command line options, so
-    -- remember settings in order of appearance, and perform settings as soon
-    -- as the cache is initialized.
-    local writeback = {}
-    local function disable_writeback(server)
-        table.insert(writeback, { set = "disable", server = server })
-    end
-    local function enable_writeback(server)
-        table.insert(writeback, { set = "enable", server = server })
-    end
-    local function perform_writeback_settings(writeback)
-        local rc, re
-        local enable_msg = "enabling writeback for server '%s' [--enable-writeback]"
-        local disable_msg =
-        "disabling writeback for server '%s' [--disable-writeback]"
-        for _,set in ipairs(writeback) do
-            if set.set == "disable" then
-                e2lib.logf(3, disable_msg, set.server)
-                rc, re = cache.set_writeback(info.cache, set.server, false)
-                if not rc then
-                    local e = err.new(disable_msg, set.server)
-                    error(e:cat(re))
-                end
-            elseif set.set == "enable" then
-                e2lib.logf(3, enable_msg, set.server)
-                rc, re = cache.set_writeback(info.cache, set.server, true)
-                if not rc then
-                    local e = err.new(enable_msg, set.server)
-                    error(e:cat(re))
-                end
-            end
-        end
-    end
-    e2option.option("disable-writeback", "disable writeback for server", nil,
-    disable_writeback, "SERVER")
-    e2option.option("enable-writeback", "enable writeback for server", nil,
-    enable_writeback, "SERVER")
 
     local opts, arguments = e2option.parse(arg)
     if not opts then
@@ -103,8 +66,6 @@ local function e2_build(arg)
         error(re)
     end
 
-    perform_writeback_settings(writeback)
-
     -- apply the standard build mode and settings to all results
     for _,res in pairs(result.results) do
         res:build_mode(build_mode)
index 127ab22cfc3a87e74a9109fa5699e58256b1c754..b1a98af5435c5954befd5dd14640e66f6a4a6be2 100644 (file)
@@ -387,6 +387,11 @@ function e2tool.collect_project_info(info, skip_load_config)
         return false, e:cat(re)
     end
 
+    rc, re = cache.setup_cache_apply_opts(info.cache)
+    if not rc then
+        return false, e:cat(re)
+    end
+
     local f = e2lib.join(info.root, e2lib.globals.e2version_file)
     local v, re = e2lib.parse_e2versionfile(f)
     if not v then