From 910bef7481a0a22363a5b1cdb2348e758c73d56c Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Mon, 2 Dec 2013 20:35:42 +0100 Subject: [PATCH] Massage e2.conf searching/parsing into a single function. Move read_global_config() into e2lib.get_global_config(). Collapse reading of .e2/e2config into get_global_config(). Remove need for e2lib.globals.cmdline vector which was only used to pass --e2-config around. Remove e2lib.sete2config() which used to set the E2_CONFIG environment variable for calling another e2 tool. In e2-fetch-project, pass arg vector to e2-install-e2. Previously this relied on E2_CONFIG being set. Now all arguments, --e2-config, warning flags and verbosity options are passed along. The returned config table is locked to detect errors. Update ldoc comments while cleaning up. Signed-off-by: Tobias Ulmer --- generic/e2lib.lua | 90 +++++++++++++++--------------------- generic/e2option.lua | 2 +- global/e2-create-project.lua | 5 -- global/e2-fetch-project.lua | 31 ++++++++----- global/e2-install-e2.lua | 17 ++----- local/e2tool.lua | 10 ---- 6 files changed, 63 insertions(+), 92 deletions(-) diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 5ac1fba..220f714 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -59,8 +59,6 @@ local eio = require("eio") local le2lib = require("le2lib") local trace = require("trace") -local global_config = false - e2lib.globals = strict.lock({ logflags = { { "v1", true }, -- minimal @@ -83,13 +81,9 @@ e2lib.globals = strict.lock({ lock = false, -- lock object default_projects_server = "projects", default_project_version = "2", - --- command line arguments that influence global settings are stored here - -- @class table - -- @name cmdline - cmdline = {}, template_path = string.format("%s/templates", buildconfig.SYSCONFDIR), extension_config = ".e2/extensions", - e2config = ".e2/e2config", + e2config = false, global_interface_version_file = ".e2/global-version", logrotate = 5, -- configurable via config.log.logrotate _version = "e2factory, the emlix embedded build system, version " .. @@ -334,7 +328,8 @@ function e2lib.interrupt_hook() end --- Make sure the environment variables inside the globals table are --- initialized properly. +-- initialized properly, set up output channels etc. Must be called before +-- anything else. -- @return True on success, false on error. -- @return Error object on failure. function e2lib.init() @@ -395,7 +390,8 @@ function e2lib.init() return true end ---- init2. +--- Initialize e2factory further, past parsing options. Reads global config +-- and sets up tools module. -- @return True on success, false on error. -- @return Error object on failure. function e2lib.init2() @@ -497,14 +493,6 @@ function e2lib.abort(...) e2lib.finish(1) end ---- Set E2_CONFIG in the environment to file. Also sets commandline option. --- @param file Config file name (string). -function e2lib.sete2config(file) - e2lib.setenv("E2_CONFIG", file, 1) - e2lib.globals.osenv["E2_CONFIG"] = file - e2lib.globals.cmdline["e2-config"] = file -end - --- Enable or disable logging for level. -- @param level number: loglevel -- @param value bool @@ -788,10 +776,11 @@ function e2lib.tartype_by_suffix(filename) return tartype end ---- Use the global parameters from the global configuration. +--- Check the global configuration for existance of fields and their types. +-- @param config e2config table to check. -- @return True on success, false on error. -- @return Error object on failure. -local function use_global_config() +local function verify_global_config(config) local rc, re local function assert_type(x, d, t1) @@ -805,10 +794,6 @@ local function use_global_config() return true end - local config = global_config - if not config then - return false, err.new("global config not available") - end if config.log then rc, re = assert_type(config.log, "config.log", "table") if not rc then @@ -856,26 +841,39 @@ local function use_global_config() return true end ---- read the global config file --- local tools call this function inside collect_project_info() --- global tools must call this function after parsing command line options --- @param e2_config_file string: config file path (optional) --- @return bool --- @return error string on error -function e2lib.read_global_config(e2_config_file) - local cf, home - local rc, re - if type(e2lib.globals.cmdline["e2-config"]) == "string" then - cf = e2lib.globals.cmdline["e2-config"] +--- Cache for global config table. +local get_global_config_cache = false + +--- Selects and read the global config file on first call, returns cached table +-- on further calls. Local tools call this function inside +-- collect_project_info(). Global tools must call this function after parsing +-- command line options. +-- @return Global config table on success, false on error. +-- @return Error object on failure. +function e2lib.get_global_config() + local rc, re, cf, cf2, cf_path, home, root + + if get_global_config_cache then + return get_global_config_cache + end + + if type(e2lib.globals.e2config) == "string" then + cf = e2lib.globals.e2config elseif type(e2lib.globals.osenv["E2_CONFIG"]) == "string" then cf = e2lib.globals.osenv["E2_CONFIG"] end - local cf_path + -- e2config contains path to e2.conf. Optional, errors are ignored. + root, re = e2lib.locate_project_root() + if root then + local e2_e2config = e2lib.join(root, ".e2/e2config") + cf2, re = eio.file_read_line(e2_e2config) + end + if cf then cf_path = { cf } - elseif e2_config_file then - cf_path = { e2_config_file } + elseif cf2 then + cf_path = { cf2 } else home = e2lib.globals.osenv["HOME"] cf_path = { @@ -909,13 +907,13 @@ function e2lib.read_global_config(e2_config_file) if not c.data then return false, err.new("invalid configuration") end - global_config = c.data - rc, re = use_global_config() + rc, re = verify_global_config(c.data) if not rc then return false, re end - return true + get_global_config_cache = strict.lock(c.data) + return get_global_config_cache else e2lib.logf(4, "global config file does not exist: %s", path) end @@ -950,18 +948,6 @@ function e2lib.read_extension_config() return extension, nil end ---- Get the global configuration. --- @return The global configuration, or false on error. --- @return Error object on failure. -function e2lib.get_global_config() - local config = global_config - if not config then - return false, err.new("global config not available") - end - - return config -end - --- Successively returns the file names in the directory. -- @param path Directory path (string). -- @param dotfiles If true, also return files starting with a '.'. Optional. diff --git a/generic/e2option.lua b/generic/e2option.lua index bbd48b3..220ef9c 100644 --- a/generic/e2option.lua +++ b/generic/e2option.lua @@ -102,7 +102,7 @@ local function defaultoptions() local category = "Verbosity Control Options" e2option.option("e2-config", "specify configuration file", nil, function(arg) - e2lib.sete2config(arg) + e2lib.globals.e2config = arg end, "FILE") diff --git a/global/e2-create-project.lua b/global/e2-create-project.lua index 330c5d6..c928fae 100644 --- a/global/e2-create-project.lua +++ b/global/e2-create-project.lua @@ -111,11 +111,6 @@ local function e2_create_project(arg) return false, arguments end - local rc, e = e2lib.read_global_config() - if not rc then - return false, e - end - rc, re = e2lib.init2() if not rc then return false, re diff --git a/global/e2-fetch-project.lua b/global/e2-fetch-project.lua index cf5496b..0c7e4d2 100644 --- a/global/e2-fetch-project.lua +++ b/global/e2-fetch-project.lua @@ -43,6 +43,9 @@ local function e2_fetch_project(arg) end local e = err.new("fetching project failed") + + -- e2-install-e2 (called below) uses the same arg vector. Update + -- e2-install-e2 to ignore any options specific to e2-fetch-project. e2option.option("branch", "retrieve a specific project branch") e2option.option("tag", "retrieve a specific project tag") @@ -51,22 +54,11 @@ local function e2_fetch_project(arg) return false, arguments end - rc, re = e2lib.read_global_config() - if not rc then - return false, e:cat(re) - end - rc, re = e2lib.init2() if not rc then return false, re end - -- get the global configuration - local config, re = e2lib.get_global_config() - if not config then - return false, e:cat(re) - end - -- setup cache local scache, re = e2lib.setup_cache() if not scache then @@ -217,9 +209,24 @@ local function e2_fetch_project(arg) return false, e:cat(re) end - -- call e2-install-e2 local e2_install_e2 = { buildconfig.LUA, e2lib.join(buildconfig.TOOLDIR, "e2-install-e2") } + + -- pass flags and options to e2-install-e2, but skip the arguments. + for _,v in ipairs(arg) do + for _,a in ipairs(arguments) do + if v == a then + v = nil + break + end + end + + if v then + table.insert(e2_install_e2, v) + end + end + + -- call e2-install-e2 rc, re = e2lib.callcmd_log(e2_install_e2) if not rc or rc ~= 0 then local e = err.new("installing local e2 failed") diff --git a/global/e2-install-e2.lua b/global/e2-install-e2.lua index 51ae49c..014a5c4 100644 --- a/global/e2-install-e2.lua +++ b/global/e2-install-e2.lua @@ -41,6 +41,11 @@ local function e2_install_e2(arg) return false, re end + -- When called from e2-fetch-project, we inherit its arg vector. + -- Parse the options but do nothing with them. + e2option.option("branch", "ignored option, do not document") + e2option.option("tag", "ignored option, do not document") + local opts, arguments = e2option.parse(arg) if not opts then return false, arguments @@ -51,16 +56,6 @@ local function e2_install_e2(arg) return false, err.new("can't locate project root.") end - -- try to get project specific config file path - -- don't care if this succeeds, the parameter is optional. - local config_file_config = e2lib.join(root, e2lib.globals.e2config) - local config_file = eio.file_read_line(config_file_config) - - local rc, e = e2lib.read_global_config(config_file) - if not rc then - return false, e - end - rc, re = e2lib.init2() if not rc then return false, re @@ -89,8 +84,6 @@ local function e2_install_e2(arg) e2option.usage(1) end - local rc, re - -- change to the project root directory rc, re = e2lib.chdir(root) if not rc then diff --git a/local/e2tool.lua b/local/e2tool.lua index e4e08b3..65546c4 100644 --- a/local/e2tool.lua +++ b/local/e2tool.lua @@ -1284,16 +1284,6 @@ function e2tool.collect_project_info(info, skip_load_config) e2lib.finish(1) end - -- try to get project specific config file path - -- don't care if this succeeds, the parameter is optional. - local config_file_config = e2lib.join(info.root, e2lib.globals.e2config) - local config_file = eio.file_read_line(config_file_config) - - local rc, re = e2lib.read_global_config(config_file) - if not rc then - return false, e:cat(re) - end - info.local_template_path = e2lib.join(info.root, ".e2/lib/e2/templates") rc, re = e2lib.init2() -- configuration must be available -- 2.39.5