From: Gordon Hecker Date: Tue, 19 Jan 2010 16:59:14 +0000 (+0100) Subject: tools: new module, move tool related code from transport to new tools module X-Git-Tag: e2factory-2.3.4pre1~41 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=d8584684419d4b89ae4ec651f54ad87b3b42f83d;p=e2factory.git tools: new module, move tool related code from transport to new tools module Signed-off-by: Gordon Hecker --- diff --git a/Changelog b/Changelog index 5b5dccd..f31b6ee 100644 --- a/Changelog +++ b/Changelog @@ -18,6 +18,7 @@ NEXT: * the local tools check for their own support for the global interface version in use. * a few minor bugs were fixed + * code related to external tools was moved into the new tools module e2factory-2.3.3 * use sha1 module instead of calling the sha1sum tool diff --git a/generic/Makefile b/generic/Makefile index ce4072e..0a5cad1 100644 --- a/generic/Makefile +++ b/generic/Makefile @@ -78,14 +78,14 @@ clean: e2generic_global.lc: strict.lua collection.lua e2lib_global_prefix.lua \ plugin.lua \ - e2lib.lua e2option.lua hash.lua \ + e2lib.lua e2option.lua hash.lua tools.lua \ transport.lua cache.lua url.lua scm.git.lua \ luafile.lua lua-version-map.lua \ error.lua lock.lua $(BUILD_LUAC) -o $@ $^ e2generic_local.lc: strict.lua collection.lua e2lib_local_prefix.lua \ - e2lib.lua e2option.lua hash.lua \ + e2lib.lua e2option.lua hash.lua tools.lua \ transport.lua cache.lua url.lua scm.git.lua \ luafile.lua lua-version-map.lua $(LUAC) -o $@ $^ diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 2ad01af..1608301 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -190,7 +190,7 @@ function e2lib.init2() -- honour tool customizations from the config file if config.tools then for k,v in pairs(config.tools) do - transport.set_tool(k, v.name, v.flags) + tools.set_tool(k, v.name, v.flags) end end @@ -200,11 +200,11 @@ function e2lib.init2() if ssh then e2lib.log(3, string.format( "using ssh command from the E2_SSH environment variable: %s", ssh)) - transport.set_tool("ssh", ssh) + tools.set_tool("ssh", ssh) end - -- initialize the transport library after resetting tools - local rc, re = transport.init() + -- initialize the tools library after resetting tools + local rc, re = tools.init() if not rc then e2lib.abort(e:cat(re)) end @@ -1547,11 +1547,11 @@ end -- @return bool -- @return string: the last line ouf captured output function e2lib.call_tool(tool, args) - local cmd = transport.get_tool(tool) + local cmd = tools.get_tool(tool) if not tool then e2lib.bomb("trying to call invalid tool: " .. tostring(tool)) end - local flags = transport.get_tool_flags(tool) + local flags = tools.get_tool_flags(tool) if not flags then e2lib.bomb("invalid tool flags for tool: " .. tostring(tool)) end @@ -1578,7 +1578,7 @@ function e2lib.git(gitdir, subtool, args) if not args then args = "" end - local git, re = transport.get_tool("git") + local git, re = tools.get_tool("git") if not git then return false, e:cat(re) end @@ -1721,11 +1721,11 @@ end -- @return bool function e2lib.sha1sum(path) local args = string.format("'%s'", path) - local sha1sum, re = transport.get_tool("sha1sum") + local sha1sum, re = tools.get_tool("sha1sum") if not sha1sum then return nil, re end - local sha1sum_flags, re = transport.get_tool_flags("sha1sum") + local sha1sum_flags, re = tools.get_tool_flags("sha1sum") if not sha1sum_flags then return nil, re end @@ -1768,7 +1768,7 @@ end function e2lib.get_sys_arch() local rc, re local e = new_error("getting host system architecture failed") - local uname = transport.get_tool("uname") + local uname = tools.get_tool("uname") local cmd = string.format("%s -m", uname) local p, msg = io.popen(cmd, "r") if not p then diff --git a/generic/scm.git.lua b/generic/scm.git.lua index 61a3a5f..fadd887 100644 --- a/generic/scm.git.lua +++ b/generic/scm.git.lua @@ -161,7 +161,7 @@ function generic_git.git_init_db1(rurl) "mkdir -p \"%s\" && GIT_DIR=\"%s\" git init-db --shared", gitdir, gitdir) if u.transport == "ssh" or u.transport == "scp" or u.transport == "rsync+ssh" then - local ssh = transport.get_tool("ssh") + local ssh = tools.get_tool("ssh") cmd = string.format("%s '%s' '%s'", ssh, u.server, gitcmd) elseif u.transport == "file" then cmd = gitcmd diff --git a/generic/tools.lua b/generic/tools.lua new file mode 100644 index 0000000..b639fc6 --- /dev/null +++ b/generic/tools.lua @@ -0,0 +1,149 @@ +--[[ + e2factory, the emlix embedded build system + + Copyright (C) 2007-2009 Gordon Hecker , emlix GmbH + Copyright (C) 2007-2009 Oskar Schirmer , emlix GmbH + Copyright (C) 2007-2008 Felix Winkelmann, emlix GmbH + + For more information have a look at http://www.e2factory.org + + e2factory is a registered trademark by emlix GmbH. + + This file is part of e2factory, the emlix embedded build system. + + e2factory is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +]] + +module("tools", package.seeall) +require("buildconfig") + +local tools = { + which = { name = "which", flags = "", optional = false }, + curl = { name = "curl", flags = "", optional = false }, + ssh = { name = "ssh", flags = "", optional = false }, + scp = { name = "scp", flags = "", optional = false }, + rsync = { name = "rsync", flags = "", optional = false }, + git = { name = "git", flags = "", optional = false }, + cvs = { name = "cvs", flags = "", optional = true }, + svn = { name = "svn", flags = "", optional = true }, + mktemp = { name = "mktemp", flags = "", optional = false }, + rm = { name = "rm", flags = "", optional = false }, + mkdir = { name = "mkdir", flags = "", optional = false }, + rmdir = { name = "rmdir", flags = "", optional = false }, + cp = { name = "cp", flags = "", optional = false }, + ln = { name = "ln", flags = "", optional = false }, + mv = { name = "mv", flags = "", optional = false }, + tar = { name = "tar", flags = "", optional = false }, + sha1sum = { name = "sha1sum", flags = "", optional = false }, + md5sum = { name = "md5sum", flags = "", optional = false }, + chmod = { name = "chmod", flags = "", optional = false }, + test = { name = "test", flags = "", optional = false }, + cat = { name = "cat", flags = "", optional = false }, + touch = { name = "touch", flags = "", optional = false }, + uname = { name = "uname", flags = "", optional = false }, + patch = { name = "patch", flags = "", optional = false }, + ["e2-su"] = { name = buildconfig.PREFIX .. "/bin/e2-su", flags = "", + optional = false }, + ["e2-su-2.2"] = { name = buildconfig.PREFIX .. "/bin/e2-su-2.2", + flags = "", optional = false }, +} + + +--- get a tool command +-- @param name string: the tool name +-- @return string: the tool command, nil on error +function get_tool(name) + if not tools[name] then + e2lib.bomb("looking up invalid tool: " .. tostring(name)) + end + return tools[name].path +end + +--- get tool flags +-- @param name string: the tool name +-- @return string: the tool flags +function get_tool_flags(name) + if not tools[name] then + e2lib.bomb("looking up flags for invalid tool: " .. + tostring(name)) + end + return tools[name].flags or "" +end + +--- set a tool command and flags +-- @param name string: the tool name +-- @param value string: the new tool command +-- @param flags string: the new tool flags. Optional. +-- @return bool +-- @return nil, an error string on error +function set_tool(name, value, flags) + if not tools[name] then + return false, "invalid tool setting" + end + if type(value) == "string" then + tools[name].name = value + end + if type(flags) == "string" then + tools[name].flags = flags + end + e2lib.log(3, string.format("setting tool: %s=%s flags=%s", + name, tools[name].name, tools[name].flags)) + return true, nil +end + +--- check if a tool is available +-- @param name string a valid tool name +-- @return bool +-- @return nil, an error string on error +function check_tool(name) + local tool = tools[name] + if not tool.path then + local which = string.format("which \"%s\"", tool.name) + local p = io.popen(which, "r") + tool.path = p:read() + p:close() + if not tool.path then + e2lib.log(3, string.format( + "tool not available: %s", tool.name)) + return false, "tool not available" + end + end + e2lib.log(4, string.format( + "tool available: %s (%s)", + tool.name, tool.path)) + return true +end + +--- initialize the library +-- @return bool +function init() + local error = false + for tool,t in pairs(tools) do + local rc = check_tool(tool) + if not rc then + local warn = "Warning" + if not t.optional then + error = true + warn = "Error" + end + e2lib.log(1, string.format( + "%s: tool is not available: %s", + warn, tool)) + end + end + if error then + return false, "missing mandatory tools" + end + return true, nil +end diff --git a/generic/transport.lua b/generic/transport.lua index fa7e827..d3229ae 100644 --- a/generic/transport.lua +++ b/generic/transport.lua @@ -26,81 +26,6 @@ ]] module("transport", package.seeall) -require("buildconfig") - -local tools = { - which = { name = "which", flags = "", optional = false }, - curl = { name = "curl", flags = "", optional = false }, - ssh = { name = "ssh", flags = "", optional = false }, - scp = { name = "scp", flags = "", optional = false }, - rsync = { name = "rsync", flags = "", optional = false }, - git = { name = "git", flags = "", optional = false }, - cvs = { name = "cvs", flags = "", optional = true }, - svn = { name = "svn", flags = "", optional = true }, - mktemp = { name = "mktemp", flags = "", optional = false }, - rm = { name = "rm", flags = "", optional = false }, - mkdir = { name = "mkdir", flags = "", optional = false }, - rmdir = { name = "rmdir", flags = "", optional = false }, - cp = { name = "cp", flags = "", optional = false }, - ln = { name = "ln", flags = "", optional = false }, - mv = { name = "mv", flags = "", optional = false }, - tar = { name = "tar", flags = "", optional = false }, - sha1sum = { name = "sha1sum", flags = "", optional = false }, - md5sum = { name = "md5sum", flags = "", optional = false }, - chmod = { name = "chmod", flags = "", optional = false }, - test = { name = "test", flags = "", optional = false }, - cat = { name = "cat", flags = "", optional = false }, - touch = { name = "touch", flags = "", optional = false }, - uname = { name = "uname", flags = "", optional = false }, - patch = { name = "patch", flags = "", optional = false }, - ["e2-su"] = { name = buildconfig.PREFIX .. "/bin/e2-su", flags = "", - optional = false }, - ["e2-su-2.2"] = { name = buildconfig.PREFIX .. "/bin/e2-su-2.2", - flags = "", optional = false }, -} - - ---- get a tool command --- @param name string: the tool name --- @return string: the tool command, nil on error -function get_tool(name) - if not tools[name] then - e2lib.bomb("looking up invalid tool: " .. tostring(name)) - end - return tools[name].path -end - ---- get tool flags --- @param name string: the tool name --- @return string: the tool flags -function get_tool_flags(name) - if not tools[name] then - e2lib.bomb("looking up flags for invalid tool: " .. - tostring(name)) - end - return tools[name].flags or "" -end - ---- set a tool command and flags --- @param name string: the tool name --- @param value string: the new tool command --- @param flags string: the new tool flags. Optional. --- @return bool --- @return nil, an error string on error -function set_tool(name, value, flags) - if not tools[name] then - return false, "invalid tool setting" - end - if type(value) == "string" then - tools[name].name = value - end - if type(flags) == "string" then - tools[name].flags = flags - end - e2lib.log(3, string.format("setting tool: %s=%s flags=%s", - name, tools[name].name, tools[name].flags)) - return true, nil -end --- fetch a file from a server -- @param surl url to the server @@ -363,49 +288,3 @@ function file_path(surl, location) local path = string.format("/%s/%s", u.path, location) return path end - ---- check if a tool is available --- @param name string a valid tool name --- @return bool --- @return nil, an error string on error -function check_tool(name) - local tool = tools[name] - if not tool.path then - local which = string.format("which \"%s\"", tool.name) - local p = io.popen(which, "r") - tool.path = p:read() - p:close() - if not tool.path then - e2lib.log(3, string.format( - "tool not available: %s", tool.name)) - return false, "tool not available" - end - end - e2lib.log(4, string.format( - "tool available: %s (%s)", - tool.name, tool.path)) - return true -end - ---- initialize the library --- @return bool -function init() - local error = false - for tool,t in pairs(tools) do - local rc = check_tool(tool) - if not rc then - local warn = "Warning" - if not t.optional then - error = true - warn = "Error" - end - e2lib.log(1, string.format( - "%s: tool is not available: %s", - warn, tool)) - end - end - if error then - return false, "missing mandatory tools" - end - return true, nil -end diff --git a/local/Makefile b/local/Makefile index 2a65f3e..243b185 100644 --- a/local/Makefile +++ b/local/Makefile @@ -113,6 +113,7 @@ e2local.lc: $(TOPLEVEL)/generic/strict.lua \ environment.lua \ $(TOPLEVEL)/generic/plugin.lua \ $(TOPLEVEL)/generic/scm.git.lua \ + $(TOPLEVEL)/generic/tools.lua \ $(TOPLEVEL)/generic/transport.lua \ $(TOPLEVEL)/generic/cache.lua \ $(TOPLEVEL)/generic/error.lua \ diff --git a/local/cvs.lua b/local/cvs.lua index 3b852c6..629be7e 100644 --- a/local/cvs.lua +++ b/local/cvs.lua @@ -95,7 +95,7 @@ function cvs.validate_source(info, sourcename) if not src.working then e:append("source has no `working' attribute") end - local rc, re = transport.check_tool("cvs") + local rc, re = tools.check_tool("cvs") if not rc then e:cat(re) end @@ -168,9 +168,9 @@ function cvs.fetch_source(info, sourcename) end -- always fetch the configured branch, as we don't know the build mode here. local rev = src.branch - local rsh = transport.get_tool("ssh") - local cvstool = transport.get_tool("cvs") - local cvsflags = transport.get_tool_flags("cvs") + local rsh = tools.get_tool("ssh") + local cvstool = tools.get_tool("cvs") + local cvsflags = tools.get_tool_flags("cvs") -- split the working directory into dirname and basename as some cvs clients -- don't like slashes (e.g. in/foo) in their checkout -d argument local dir = e2lib.dirname(src.working) @@ -220,9 +220,9 @@ function cvs.prepare_source(info, sourcename, source_set, buildpath) local cmd = nil if source_set == "tag" or source_set == "branch" then local rev = mkrev(src, source_set) - local rsh = transport.get_tool("ssh") - local cvstool = transport.get_tool("cvs") - local cvsflags = transport.get_tool_flags("cvs") + local rsh = tools.get_tool("ssh") + local cvstool = tools.get_tool("cvs") + local cvsflags = tools.get_tool_flags("cvs") -- cd buildpath && cvs -d cvsroot export -R -r rev module cmd = string.format( "cd \"%s\" && " .. @@ -253,9 +253,9 @@ function cvs.update(info, sourcename) local e = new_error("updating cvs source failed") local src = info.sources[ sourcename ] local working = string.format("%s/%s", info.root, src.working) - local rsh = transport.get_tool("ssh") - local cvstool = transport.get_tool("cvs") - local cvsflags = transport.get_tool_flags("cvs") + local rsh = tools.get_tool("ssh") + local cvstool = tools.get_tool("cvs") + local cvsflags = tools.get_tool_flags("cvs") local cmd = string.format( "cd \"%s\" && " .. "CVS_RSH=\"%s\" " .. diff --git a/local/e2build.lua b/local/e2build.lua index a30b9e6..418784f 100644 --- a/local/e2build.lua +++ b/local/e2build.lua @@ -299,7 +299,7 @@ function e2build.enter_playground(info, r, chroot_command) local e = new_error("entering playground") e2lib.log(4, "entering playground for " .. r .. " ...") local term = e2lib.terminal - local e2_su = transport.get_tool("e2-su-2.2") + local e2_su = tools.get_tool("e2-su-2.2") local cmd = string.format("%s %s chroot_2_3 '%s' %s", res.build_config.chroot_call_prefix, e2_su, res.build_config.base, chroot_command) @@ -353,7 +353,7 @@ function e2build.runbuild(info, r, return_flags) local runbuild = string.format("/bin/bash -e -x '%s/%s/%s'", res.build_config.Tc, res.build_config.scriptdir, res.build_config.build_driver_file) - local e2_su = transport.get_tool("e2-su-2.2") + local e2_su = tools.get_tool("e2-su-2.2") local cmd = string.format("%s %s chroot_2_3 '%s' %s", res.build_config.chroot_call_prefix, e2_su, res.build_config.base, runbuild) diff --git a/local/e2tool.lua b/local/e2tool.lua index aeb4e71..24e0ae9 100644 --- a/local/e2tool.lua +++ b/local/e2tool.lua @@ -1311,7 +1311,7 @@ function e2tool.verify_remote_fileid(info, file, fileid) local retcmd if u.transport == "ssh" or u.transport == "scp" or u.transport == "rsync+ssh" then - local ssh = transport.get_tool("ssh") + local ssh = tools.get_tool("ssh") retcmd = string.format("%s '%s' %s /%s", ssh, u.server, cmd, u.path) elseif u.transport == "file" then diff --git a/local/git.lua b/local/git.lua index 2af4086..2470f6f 100644 --- a/local/git.lua +++ b/local/git.lua @@ -445,8 +445,8 @@ function git.prepare_source(info, sourcename, sourceset, buildpath) if not rc then return false, re end - local tar = transport.get_tool("tar") - local tarflags = transport.get_tool_flags("tar") + local tar = tools.get_tool("tar") + local tarflags = tools.get_tool_flags("tar") local cmd1 = string.format("%s %s -c -C '%s/%s' --exclude '.git' .", tar, tarflags, info.root, src.working) local cmd2 = string.format("%s %s -x -C '%s/%s'", tar, tarflags, buildpath, diff --git a/local/svn.lua b/local/svn.lua index 993c205..b9bcf56 100644 --- a/local/svn.lua +++ b/local/svn.lua @@ -367,7 +367,7 @@ function svn.validate_source(info, sourcename) --OK if not src.working then e:append("source has no `working' attribute") end - local rc, re = transport.check_tool("svn") + local rc, re = tools.check_tool("svn") if not rc then e:cat(re) end