From 472e74d582055992cf1f5c75821e083131a895ac Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:01:31 +0300 Subject: Move common implementation of fs.use_downloader into fs.tools module --- Makefile.setup.inc | 2 +- src/luarocks/fs.lua | 5 +++- src/luarocks/fs/tools.lua | 56 +++++++++++++++++++++++++++++++++++++++++ src/luarocks/fs/unix/tools.lua | 45 --------------------------------- src/luarocks/fs/win32/tools.lua | 45 --------------------------------- 5 files changed, 61 insertions(+), 92 deletions(-) create mode 100644 src/luarocks/fs/tools.lua diff --git a/Makefile.setup.inc b/Makefile.setup.inc index 0a049bc4..092d0423 100644 --- a/Makefile.setup.inc +++ b/Makefile.setup.inc @@ -5,7 +5,7 @@ BINDIR ?= $(PREFIX)/bin LUADIR ?= $(PREFIX)/share/lua/$(LUA_VERSION)/ BIN_FILES = luarocks luarocks-admin -LUAROCKS_FILES = fs/unix/tools.lua fs/unix.lua fs/win32/tools.lua fs/win32.lua \ +LUAROCKS_FILES = fs/tools.lua fs/unix/tools.lua fs/unix.lua fs/win32/tools.lua fs/win32.lua \ fs/lua.lua persist.lua list.lua require.lua repos.lua dir.lua make_manifest.lua \ command_line.lua config_cmd.lua install.lua build/command.lua build/cmake.lua \ build/make.lua build/builtin.lua fetch/cvs.lua fetch/git.lua fetch/sscm.lua \ diff --git a/src/luarocks/fs.lua b/src/luarocks/fs.lua index 57302c7f..f3d86a13 100644 --- a/src/luarocks/fs.lua +++ b/src/luarocks/fs.lua @@ -68,7 +68,10 @@ load_fns(fs_lua) -- Load platform-specific fallbacks for missing Lua modules local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") -if ok and fs_plat_tools then load_fns(fs_plat_tools) end +if ok and fs_plat_tools then + load_fns(fs_plat_tools) + load_fns(require("luarocks.fs.tools")) +end return fs diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua new file mode 100644 index 00000000..0283d8f7 --- /dev/null +++ b/src/luarocks/fs/tools.lua @@ -0,0 +1,56 @@ + +--- Common fs operations implemented with third-party tools. +local tools = {} + +local fs = require("luarocks.fs") +local dir = require("luarocks.dir") +local cfg = require("luarocks.cfg") + +local vars = cfg.variables + +--- Download a remote file. +-- @param url string: URL to be fetched. +-- @param filename string or nil: this function attempts to detect the +-- resulting local filename of the remote file as the basename of the URL; +-- if that is not correct (due to a redirection, for example), the local +-- filename can be given explicitly as this second argument. +-- @return (boolean, string): true and the filename on success, +-- false and the error message on failure. +function tools.use_downloader(url, filename, cache) + assert(type(url) == "string") + assert(type(filename) == "string" or not filename) + + filename = fs.absolute_name(filename or dir.base_name(url)) + + local ok + if cfg.downloader == "wget" then + local wget_cmd = fs.Q(vars.WGET).." "..vars.WGETNOCERTFLAG.." --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet " + if cfg.connection_timeout and cfg.connection_timeout > 0 then + wget_cmd = wget_cmd .. "--timeout="..tonumber(cfg.connection_timeout).." --tries=1 " + end + if cache then + -- --timestamping is incompatible with --output-document, + -- but that's not a problem for our use cases. + fs.change_dir(dir.dir_name(filename)) + ok = fs.execute_quiet(wget_cmd.." --timestamping ", url) + fs.pop_dir() + elseif filename then + ok = fs.execute_quiet(wget_cmd.." --output-document ", filename, url) + else + ok = fs.execute_quiet(wget_cmd, url) + end + elseif cfg.downloader == "curl" then + local curl_cmd = fs.Q(vars.CURL).." "..vars.CURLNOCERTFLAG.." -f -L --user-agent \""..cfg.user_agent.." via curl\" " + if cfg.connection_timeout and cfg.connection_timeout > 0 then + curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." " + end + ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename))) + end + if ok then + return true, filename + else + return false + end +end + +return tools diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index d75d9795..8eaa9361 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -232,51 +232,6 @@ function tools.is_file(file) return fs.execute(vars.TEST, "-f", file) end ---- Download a remote file. --- @param url string: URL to be fetched. --- @param filename string or nil: this function attempts to detect the --- resulting local filename of the remote file as the basename of the URL; --- if that is not correct (due to a redirection, for example), the local --- filename can be given explicitly as this second argument. --- @return (boolean, string): true and the filename on success, --- false and the error message on failure. -function tools.use_downloader(url, filename, cache) - assert(type(url) == "string") - assert(type(filename) == "string" or not filename) - - filename = fs.absolute_name(filename or dir.base_name(url)) - - local ok - if cfg.downloader == "wget" then - local wget_cmd = fs.Q(vars.WGET).." "..vars.WGETNOCERTFLAG.." --no-cache --user-agent='"..cfg.user_agent.." via wget' --quiet " - if cfg.connection_timeout and cfg.connection_timeout > 0 then - wget_cmd = wget_cmd .. "--timeout="..tonumber(cfg.connection_timeout).." --tries=1 " - end - if cache then - -- --timestamping is incompatible with --output-document, - -- but that's not a problem for our use cases. - fs.change_dir(dir.dir_name(filename)) - ok = fs.execute_quiet(wget_cmd.." --timestamping ", url) - fs.pop_dir() - elseif filename then - ok = fs.execute_quiet(wget_cmd.." --output-document ", filename, url) - else - ok = fs.execute_quiet(wget_cmd, url) - end - elseif cfg.downloader == "curl" then - local curl_cmd = fs.Q(vars.CURL).." "..vars.CURLNOCERTFLAG.." -f -L --user-agent '"..cfg.user_agent.." via curl' " - if cfg.connection_timeout and cfg.connection_timeout > 0 then - curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." " - end - ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename))) - end - if ok then - return true, filename - else - return false - end -end - function tools.chmod(pathname, mode) if mode then return fs.execute(vars.CHMOD, mode, pathname) diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 39aa4ba1..cc0da0e9 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -242,51 +242,6 @@ function tools.is_file(file) return fs.execute(fs.Q(vars.TEST).." -f", file) end ---- Download a remote file. --- @param url string: URL to be fetched. --- @param filename string or nil: this function attempts to detect the --- resulting local filename of the remote file as the basename of the URL; --- if that is not correct (due to a redirection, for example), the local --- filename can be given explicitly as this second argument. --- @return (boolean, string): true and the filename on success, --- false and the error message on failure. -function tools.use_downloader(url, filename, cache) - assert(type(url) == "string") - assert(type(filename) == "string" or not filename) - - filename = fs.absolute_name(filename or dir.base_name(url)) - - local ok - if cfg.downloader == "wget" then - local wget_cmd = fs.Q(vars.WGET).." "..vars.WGETNOCERTFLAG.." --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet " - if cfg.connection_timeout and cfg.connection_timeout > 0 then - wget_cmd = wget_cmd .. "--timeout="..tonumber(cfg.connection_timeout).." --tries=1 " - end - if cache then - -- --timestamping is incompatible with --output-document, - -- but that's not a problem for our use cases. - fs.change_dir(dir.dir_name(filename)) - ok = fs.execute_quiet(wget_cmd.." --timestamping ", url) - fs.pop_dir() - elseif filename then - ok = fs.execute_quiet(wget_cmd.." --output-document ", filename, url) - else - ok = fs.execute_quiet(wget_cmd, url) - end - elseif cfg.downloader == "curl" then - local curl_cmd = fs.Q(vars.CURL).." "..vars.CURLNOCERTFLAG.." -f -L --user-agent \""..cfg.user_agent.." via curl\" " - if cfg.connection_timeout and cfg.connection_timeout > 0 then - curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." " - end - ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename))) - end - if ok then - return true, filename - else - return false - end -end - --- Uncompress gzip file. -- @param archive string: Filename of archive. -- @return boolean : success status -- cgit v1.2.3-55-g6feb