From 8ee4ef81cf015adca29ac51a7d649a5ee04b3edd Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 11 Apr 2022 17:00:43 -0300 Subject: simplify check if tool is available --- src/luarocks/fetch/svn.lua | 2 +- src/luarocks/fs/freebsd.lua | 11 ----------- src/luarocks/fs/lua.lua | 9 +++------ src/luarocks/fs/netbsd.lua | 14 -------------- src/luarocks/fs/tools.lua | 6 +++--- src/luarocks/fs/unix.lua | 11 +++++++++++ src/luarocks/fs/unix/tools.lua | 2 +- src/luarocks/fs/win32.lua | 11 +++++++++++ 8 files changed, 30 insertions(+), 36 deletions(-) delete mode 100644 src/luarocks/fs/freebsd.lua delete mode 100644 src/luarocks/fs/netbsd.lua diff --git a/src/luarocks/fetch/svn.lua b/src/luarocks/fetch/svn.lua index b2ae59c9..b6618afc 100644 --- a/src/luarocks/fetch/svn.lua +++ b/src/luarocks/fetch/svn.lua @@ -20,7 +20,7 @@ function svn.get_sources(rockspec, extract, dest_dir) assert(type(dest_dir) == "string" or not dest_dir) local svn_cmd = rockspec.variables.SVN - local ok, err_msg = fs.is_tool_available(svn_cmd, "Subversion", "--version") + local ok, err_msg = fs.is_tool_available(svn_cmd, "Subversion") if not ok then return nil, err_msg end diff --git a/src/luarocks/fs/freebsd.lua b/src/luarocks/fs/freebsd.lua deleted file mode 100644 index f72faa29..00000000 --- a/src/luarocks/fs/freebsd.lua +++ /dev/null @@ -1,11 +0,0 @@ ---- FreeBSD implementation of filesystem and platform abstractions. -local freebsd = {} - -local fs = require("luarocks.fs") - -function freebsd.init() - fs.set_tool_available("zip", true) - fs.set_tool_available("unzip", true) -end - -return freebsd diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 6bd80485..2619d136 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -114,19 +114,16 @@ end -- The tool is executed using a flag, usually just to ask its version. -- @param tool_cmd string: The command to be used to check the tool's presence (e.g. hg in case of Mercurial) -- @param tool_name string: The actual name of the tool (e.g. Mercurial) --- @param arg string: The flag to pass to the tool. '--version' by default. -function fs_lua.is_tool_available(tool_cmd, tool_name, arg) +function fs_lua.is_tool_available(tool_cmd, tool_name) assert(type(tool_cmd) == "string") assert(type(tool_name) == "string") - arg = arg or "--version" - assert(type(arg) == "string") - local ok if tool_available_cache[tool_name] ~= nil then ok = tool_available_cache[tool_name] else - ok = fs.execute_quiet(tool_cmd, arg) + local tool_cmd_no_args = tool_cmd:gsub(" .*", "") + ok = fs.search_in_path(tool_cmd_no_args) tool_available_cache[tool_name] = (ok == true) end diff --git a/src/luarocks/fs/netbsd.lua b/src/luarocks/fs/netbsd.lua deleted file mode 100644 index b9610339..00000000 --- a/src/luarocks/fs/netbsd.lua +++ /dev/null @@ -1,14 +0,0 @@ ---- NetBSD implementation of filesystem and platform abstractions. -local netbsd = {} - -local fs = require("luarocks.fs") - -function netbsd.init() - local uz=io.open("/usr/bin/unzip", "r") - if uz ~= nil then - io.close(uz) - fs.set_tool_available("unzip", true) - end -end - -return netbsd diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 0a154794..5c317f65 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -22,8 +22,8 @@ do md5checker = { desc = "MD5 checker", { var = "MD5SUM", name = "md5sum" }, - { var = "OPENSSL", name = "openssl", cmdarg = "md5", checkarg = "version" }, - { var = "MD5", name = "md5", checkarg = "-shello" }, + { var = "OPENSSL", name = "openssl", cmdarg = "md5" }, + { var = "MD5", name = "md5" }, }, } @@ -33,7 +33,7 @@ do if not tool then for _, opt in ipairs(tool_options[tooltype]) do table.insert(names, opt.name) - if fs.is_tool_available(vars[opt.var], opt.name, opt.checkarg) then + if fs.is_tool_available(vars[opt.var], opt.name) then tool = opt tool_cache[tooltype] = opt break diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index 0065753e..28189089 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua @@ -234,4 +234,15 @@ function unix.system_cache_dir() return dir.path(fs.system_temp_dir(), "cache") end +function unix.search_in_path(program) + for d in (os.getenv("PATH") or ""):gmatch("([^:]+)") do + local fd = io.open(dir.path(d, program), "r") + if fd then + fd:close() + return true, d + end + end + return false +end + return unix diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index d36d407d..f0c0a05c 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -141,7 +141,7 @@ end -- @return boolean: true on success, nil and error message on failure. function tools.unzip(zipfile) assert(zipfile) - local ok, err = fs.is_tool_available(vars.UNZIP, "unzip", "--help") + local ok, err = fs.is_tool_available(vars.UNZIP, "unzip") if not ok then return nil, err end diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index a3f42df7..6c49f447 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -363,4 +363,15 @@ function win32.system_cache_dir() return dir.path(fs.system_temp_dir(), "cache") end +function win32.search_in_path(program) + for d in (os.getenv("PATH") or ""):gmatch("([^;]+)") do + local fd = io.open(dir.path(d, program .. ".exe"), "r") + if fd then + fd:close() + return true, d + end + end + return false +end + return win32 -- cgit v1.2.3-55-g6feb