From 26ea986b8322dc3bd1e82cf6d819082c57a69024 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 13:52:08 +0300 Subject: Add fs.quiet_stderr function --- src/luarocks/fs/unix.lua | 7 +++++++ src/luarocks/fs/unix/tools.lua | 6 +++--- src/luarocks/fs/win32.lua | 7 +++++++ src/luarocks/fs/win32/tools.lua | 6 +++--- 4 files changed, 20 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index e2cc825b..520b3e99 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua @@ -16,6 +16,13 @@ function unix.quiet(cmd) return cmd.." 1> /dev/null 2> /dev/null" end +--- Annotate command string for execution with quiet stderr. +-- @param cmd string: A command-line string. +-- @return string: The command-line, with stderr silencing annotation. +function unix.quiet_stderr(cmd) + return cmd.." 2> /dev/null" +end + --- Return an absolute pathname from a potentially relative one. -- @param pathname string: pathname to convert. -- @param relative_to string or nil: path to prepend when making diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index ab55897e..d75d9795 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -21,7 +21,7 @@ end function tools.current_dir() local current = cfg.cache_pwd if not current then - local pipe = io.popen(fs.Q(vars.PWD).." 2> /dev/null") + local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) current = pipe:read("*l") pipe:close() cfg.cache_pwd = current @@ -183,7 +183,7 @@ function tools.find(at) return {} end local result = {} - local pipe = io.popen(command_at(at, vars.FIND.." * 2>/dev/null")) + local pipe = io.popen(command_at(at, fs.quiet_stderr(vars.FIND.." *"))) for file in pipe:lines() do table.insert(result, file) end @@ -268,7 +268,7 @@ function tools.use_downloader(url, filename, cache) 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(curl_cmd..fs.Q(url).." 2> /dev/null 1> "..fs.Q(filename)) + ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename))) end if ok then return true, filename diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index c14c421b..74f3ed69 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -27,6 +27,13 @@ function win32.quiet(cmd) return cmd.." 2> NUL 1> NUL" end +--- Annotate command string for execution with quiet stderr. +-- @param cmd string: A command-line string. +-- @return string: The command-line, with stderr silencing annotation. +function win32.quiet_stderr(cmd) + return cmd.." 2> NUL" +end + local drive_letter = "[%.a-zA-Z]?:?[\\/]" local win_escape_chars = { diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index b9dce85c..39aa4ba1 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -39,7 +39,7 @@ end function tools.current_dir() local current = cfg.cache_pwd if not current then - local pipe = io.popen(fs.Q(vars.PWD).. " 2> NUL") + local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) current = pipe:read("*l") pipe:close() cfg.cache_pwd = current @@ -196,7 +196,7 @@ function tools.find(at) return {} end local result = {} - local pipe = io.popen(command_at(at, fs.Q(vars.FIND).." 2> NUL")) + local pipe = io.popen(command_at(at, fs.quiet_stderr(fs.Q(vars.FIND)))) for file in pipe:lines() do -- Windows find is a bit different local first_two = file:sub(1,2) @@ -278,7 +278,7 @@ function tools.use_downloader(url, filename, cache) 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(curl_cmd..fs.Q(url).." 2> NUL 1> "..fs.Q(filename)) + ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename))) end if ok then return true, filename -- cgit v1.2.3-55-g6feb 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 (limited to 'src') 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 From c79ce50f956d8d9f1454ec871dc7b0469f194c07 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:06:35 +0300 Subject: fs.tools: move common directory stack functions --- src/luarocks/fs/tools.lua | 47 +++++++++++++++++++++++++++++++++++++++++ src/luarocks/fs/unix/tools.lua | 46 ---------------------------------------- src/luarocks/fs/win32/tools.lua | 47 ----------------------------------------- 3 files changed, 47 insertions(+), 93 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 0283d8f7..08fa74a4 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -8,6 +8,53 @@ local cfg = require("luarocks.cfg") local vars = cfg.variables +local dir_stack = {} + +--- Obtain current directory. +-- Uses the module's internal directory stack. +-- @return string: the absolute pathname of the current directory. +function tools.current_dir() + local current = cfg.cache_pwd + if not current then + local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) + current = pipe:read("*l") + pipe:close() + cfg.cache_pwd = current + end + for _, directory in ipairs(dir_stack) do + current = fs.absolute_name(directory, current) + end + return current +end + +--- Change the current directory. +-- Uses the module's internal directory stack. This does not have exact +-- semantics of chdir, as it does not handle errors the same way, +-- but works well for our purposes for now. +-- @param directory string: The directory to switch to. +-- @return boolean or (nil, string): true if successful, (nil, error message) if failed. +function tools.change_dir(directory) + assert(type(directory) == "string") + if fs.is_dir(directory) then + table.insert(dir_stack, directory) + return true + end + return nil, "directory not found: "..directory +end + +--- Change directory to root. +-- Allows leaving a directory (e.g. for deleting it) in +-- a crossplatform way. +function tools.change_dir_to_root() + table.insert(dir_stack, "/") +end + +--- Change working directory to the previous in the directory stack. +function tools.pop_dir() + local directory = table.remove(dir_stack) + return directory ~= nil +end + --- Download a remote file. -- @param url string: URL to be fetched. -- @param filename string or nil: this function attempts to detect the diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 8eaa9361..2b5280d6 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -7,31 +7,12 @@ local fs = require("luarocks.fs") local dir = require("luarocks.dir") local cfg = require("luarocks.cfg") -local dir_stack = {} - local vars = cfg.variables local function command_at(directory, cmd) return "cd " .. fs.Q(fs.absolute_name(directory)) .. " && " .. cmd end ---- Obtain current directory. --- Uses the module's internal directory stack. --- @return string: the absolute pathname of the current directory. -function tools.current_dir() - local current = cfg.cache_pwd - if not current then - local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) - current = pipe:read("*l") - pipe:close() - cfg.cache_pwd = current - end - for _, directory in ipairs(dir_stack) do - current = fs.absolute_name(directory, current) - end - return current -end - --- Run the given command. -- The command is executed in the current directory in the directory stack. -- @param cmd string: No quoting/escaping is applied to the command. @@ -48,33 +29,6 @@ function tools.execute_string(cmd) end end ---- Change the current directory. --- Uses the module's internal directory stack. This does not have exact --- semantics of chdir, as it does not handle errors the same way, --- but works well for our purposes for now. --- @param directory string: The directory to switch to. -function tools.change_dir(directory) - assert(type(directory) == "string") - if fs.is_dir(directory) then - table.insert(dir_stack, directory) - return true - end - return nil, "directory not found: "..directory -end - ---- Change directory to root. --- Allows leaving a directory (e.g. for deleting it) in --- a crossplatform way. -function tools.change_dir_to_root() - table.insert(dir_stack, "/") -end - ---- Change working directory to the previous in the directory stack. -function tools.pop_dir() - local directory = table.remove(dir_stack) - return directory ~= nil -end - --- Create a directory if it does not already exist. -- If any of the higher levels in the path name does not exist -- too, they are created as well. diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index cc0da0e9..19038e1f 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -9,8 +9,6 @@ local fs = require("luarocks.fs") local dir = require("luarocks.dir") local cfg = require("luarocks.cfg") -local dir_stack = {} - local vars = cfg.variables --- Strip the last extension of a filename. @@ -33,23 +31,6 @@ local function command_at(directory, cmd) return cmd end ---- Obtain current directory. --- Uses the module's internal directory stack. --- @return string: the absolute pathname of the current directory. -function tools.current_dir() - local current = cfg.cache_pwd - if not current then - local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) - current = pipe:read("*l") - pipe:close() - cfg.cache_pwd = current - end - for _, directory in ipairs(dir_stack) do - current = fs.absolute_name(directory, current) - end - return current -end - --- Run the given command. -- The command is executed in the current directory in the directory stack. -- @param cmd string: No quoting/escaping is applied to the command. @@ -67,34 +48,6 @@ function tools.execute_string(cmd) end end ---- Change the current directory. --- Uses the module's internal directory stack. This does not have exact --- semantics of chdir, as it does not handle errors the same way, --- but works well for our purposes for now. --- @param directory string: The directory to switch to. --- @return boolean or (nil, string): true if successful, (nil, error message) if failed. -function tools.change_dir(directory) - assert(type(directory) == "string") - if fs.is_dir(directory) then - table.insert(dir_stack, directory) - return true - end - return nil, "directory not found: "..directory -end - ---- Change directory to root. --- Allows leaving a directory (e.g. for deleting it) in --- a crossplatform way. -function tools.change_dir_to_root() - table.insert(dir_stack, "/") -end - ---- Change working directory to the previous in the directory stack. -function tools.pop_dir() - local directory = table.remove(dir_stack) - return directory ~= nil -end - --- Create a directory if it does not already exist. -- If any of the higher levels in the path name does not exist -- too, they are created as well. -- cgit v1.2.3-55-g6feb From 1cb6bb91efe3af86335453221b0f7cdcd2cb3be5 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:12:16 +0300 Subject: fs.tools: move common 'get_md5' function --- src/luarocks/fs/tools.lua | 22 ++++++++++++++++++++++ src/luarocks/fs/unix/tools.lua | 22 ---------------------- src/luarocks/fs/win32/tools.lua | 22 ---------------------- 3 files changed, 22 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 08fa74a4..76b90383 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -100,4 +100,26 @@ function tools.use_downloader(url, filename, cache) end end +local md5_cmd = { + md5sum = fs.Q(vars.MD5SUM), + openssl = fs.Q(vars.OPENSSL).." md5", + md5 = fs.Q(vars.MD5), +} + +--- Get the MD5 checksum for a file. +-- @param file string: The file to be computed. +-- @return string: The MD5 checksum or nil + message +function tools.get_md5(file) + local cmd = md5_cmd[cfg.md5checker] + if not cmd then return nil, "no MD5 checker command configured" end + local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) + local computed = pipe:read("*a") + pipe:close() + if computed then + computed = computed:match("("..("%x"):rep(32)..")") + end + if computed then return computed end + return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) +end + return tools diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 2b5280d6..904dd0aa 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -228,28 +228,6 @@ function tools.unpack_archive(archive) return true end -local md5_cmd = { - md5sum = vars.MD5SUM, - openssl = vars.OPENSSL.." md5", - md5 = vars.MD5, -} - ---- Get the MD5 checksum for a file. --- @param file string: The file to be computed. --- @return string: The MD5 checksum -function tools.get_md5(file) - local cmd = md5_cmd[cfg.md5checker] - if not cmd then return nil, "no MD5 checker command configured" end - local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) - local computed = pipe:read("*a") - pipe:close() - if computed then - computed = computed:match("("..("%x"):rep(32)..")") - end - if computed then return computed end - return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) -end - function tools.get_permissions(filename) local pipe = io.popen(vars.STAT.." "..vars.STATFLAG.." "..fs.Q(filename)) local ret = pipe:read("*l") diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 19038e1f..f112ea3e 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -242,28 +242,6 @@ function tools.unpack_archive(archive) return true end -local md5_cmd = { - md5sum = fs.Q(vars.MD5SUM), - openssl = fs.Q(vars.OPENSSL).." md5", - md5 = fs.Q(vars.MD5), -} - ---- Get the MD5 checksum for a file. --- @param file string: The file to be computed. --- @return string: The MD5 checksum or nil + message -function tools.get_md5(file) - local cmd = md5_cmd[cfg.md5checker] - if not cmd then return nil, "no MD5 checker command configured" end - local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) - local computed = pipe:read("*a") - pipe:close() - if computed then - computed = computed:match("("..("%x"):rep(32)..")") - end - if computed then return computed end - return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) -end - --- Test for existance of a file. -- @param file string: filename to test -- @return boolean: true if file exists, false otherwise. -- cgit v1.2.3-55-g6feb From 319f8f619c9457c8ba426d8bbe0b1d2d74c02e5d Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:14:08 +0300 Subject: fs.win32.tools: move a local function closer to its usage --- src/luarocks/fs/win32/tools.lua | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index f112ea3e..aa405e14 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -11,17 +11,6 @@ local cfg = require("luarocks.cfg") local vars = cfg.variables ---- Strip the last extension of a filename. --- Example: "foo.tar.gz" becomes "foo.tar". --- If filename has no dots, returns it unchanged. --- @param filename string: The file name to strip. --- @return string: The stripped name. -local function strip_extension(filename) - assert(type(filename) == "string") - - return (filename:gsub("%.[^.]+$", "")) or filename -end - local function command_at(directory, cmd) local drive = directory:match("^([A-Za-z]:)") cmd = "cd " .. fs.Q(directory) .. " & " .. cmd @@ -195,6 +184,16 @@ function tools.is_file(file) return fs.execute(fs.Q(vars.TEST).." -f", file) end +--- Strip the last extension of a filename. +-- Example: "foo.tar.gz" becomes "foo.tar". +-- If filename has no dots, returns it unchanged. +-- @param filename string: The file name to strip. +-- @return string: The stripped name. +local function strip_extension(filename) + assert(type(filename) == "string") + return (filename:gsub("%.[^.]+$", "")) or filename +end + --- Uncompress gzip file. -- @param archive string: Filename of archive. -- @return boolean : success status -- cgit v1.2.3-55-g6feb From 470ca122e9120caac00cae44b472965a8828afb4 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:21:36 +0300 Subject: Expose tools.command_at function --- src/luarocks/fs/unix/tools.lua | 12 ++++++++---- src/luarocks/fs/win32/tools.lua | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 904dd0aa..807f8057 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -9,7 +9,11 @@ local cfg = require("luarocks.cfg") local vars = cfg.variables -local function command_at(directory, cmd) +--- Adds prefix to command to make it run from a directory. +-- @param directory string: Path to a directory. +-- @param cmd string: A command-line string. +-- @return string: The command-line with prefix. +function tools.command_at(directory, cmd) return "cd " .. fs.Q(fs.absolute_name(directory)) .. " && " .. cmd end @@ -21,7 +25,7 @@ end function tools.execute_string(cmd) local current = fs.current_dir() if not current then return false end - local code, err = os.execute(command_at(current, cmd)) + local code, err = os.execute(fs.command_at(current, cmd)) if code == 0 or code == true then return true else @@ -114,7 +118,7 @@ end -- @param at string: directory to list -- @return nil function tools.dir_iterator(at) - local pipe = io.popen(command_at(at, vars.LS)) + local pipe = io.popen(fs.command_at(at, vars.LS)) for file in pipe:lines() do if file ~= "." and file ~= ".." then coroutine.yield(file) @@ -137,7 +141,7 @@ function tools.find(at) return {} end local result = {} - local pipe = io.popen(command_at(at, fs.quiet_stderr(vars.FIND.." *"))) + local pipe = io.popen(fs.command_at(at, fs.quiet_stderr(vars.FIND.." *"))) for file in pipe:lines() do table.insert(result, file) end diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index aa405e14..f2d62f32 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -11,7 +11,11 @@ local cfg = require("luarocks.cfg") local vars = cfg.variables -local function command_at(directory, cmd) +--- Adds prefix to command to make it run from a directory. +-- @param directory string: Path to a directory. +-- @param cmd string: A command-line string. +-- @return string: The command-line with prefix. +function tools.command_at(directory, cmd) local drive = directory:match("^([A-Za-z]:)") cmd = "cd " .. fs.Q(directory) .. " & " .. cmd if drive then @@ -28,7 +32,7 @@ end function tools.execute_string(cmd) local current = fs.current_dir() if not current then return false end - cmd = command_at(current, cmd) + cmd = fs.command_at(current, cmd) local code = os.execute(cmd) if code == 0 or code == true then return true @@ -115,7 +119,7 @@ end -- @param at string: directory to list -- @return nil function tools.dir_iterator(at) - local pipe = io.popen(command_at(at, fs.Q(vars.LS))) + local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS))) for file in pipe:lines() do if file ~= "." and file ~= ".." then coroutine.yield(file) @@ -138,7 +142,7 @@ function tools.find(at) return {} end local result = {} - local pipe = io.popen(command_at(at, fs.quiet_stderr(fs.Q(vars.FIND)))) + local pipe = io.popen(fs.command_at(at, fs.quiet_stderr(fs.Q(vars.FIND)))) for file in pipe:lines() do -- Windows find is a bit different local first_two = file:sub(1,2) -- cgit v1.2.3-55-g6feb From 8630313d665904e21edcc954e4ca0d043b217665 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:22:59 +0300 Subject: Move common implementation of tools.execute_string --- src/luarocks/fs/tools.lua | 17 +++++++++++++++++ src/luarocks/fs/unix/tools.lua | 16 ---------------- src/luarocks/fs/win32/tools.lua | 17 ----------------- 3 files changed, 17 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 76b90383..6caa07f3 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -55,6 +55,23 @@ function tools.pop_dir() return directory ~= nil end +--- Run the given command. +-- The command is executed in the current directory in the directory stack. +-- @param cmd string: No quoting/escaping is applied to the command. +-- @return boolean: true if command succeeds (status code 0), false +-- otherwise. +function tools.execute_string(cmd) + local current = fs.current_dir() + if not current then return false end + cmd = fs.command_at(current, cmd) + local code = os.execute(cmd) + if code == 0 or code == true then + return true + else + return false + end +end + --- Download a remote file. -- @param url string: URL to be fetched. -- @param filename string or nil: this function attempts to detect the diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 807f8057..1bc307a4 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -17,22 +17,6 @@ function tools.command_at(directory, cmd) return "cd " .. fs.Q(fs.absolute_name(directory)) .. " && " .. cmd end ---- Run the given command. --- The command is executed in the current directory in the directory stack. --- @param cmd string: No quoting/escaping is applied to the command. --- @return boolean: true if command succeeds (status code 0), false --- otherwise. -function tools.execute_string(cmd) - local current = fs.current_dir() - if not current then return false end - local code, err = os.execute(fs.command_at(current, cmd)) - if code == 0 or code == true then - return true - else - return false - end -end - --- Create a directory if it does not already exist. -- If any of the higher levels in the path name does not exist -- too, they are created as well. diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index f2d62f32..f54ec9a5 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -24,23 +24,6 @@ function tools.command_at(directory, cmd) return cmd end ---- Run the given command. --- The command is executed in the current directory in the directory stack. --- @param cmd string: No quoting/escaping is applied to the command. --- @return boolean: true if command succeeds (status code 0), false --- otherwise. -function tools.execute_string(cmd) - local current = fs.current_dir() - if not current then return false end - cmd = fs.command_at(current, cmd) - local code = os.execute(cmd) - if code == 0 or code == true then - return true - else - return false - end -end - --- Create a directory if it does not already exist. -- If any of the higher levels in the path name does not exist -- too, they are created as well. -- cgit v1.2.3-55-g6feb From 69a6aff477fd794b3b594bc7b9f20775b8e07986 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:25:56 +0300 Subject: Move common implementation of tools.dir_iterator --- src/luarocks/fs/tools.lua | 14 ++++++++++++++ src/luarocks/fs/unix/tools.lua | 14 -------------- src/luarocks/fs/win32/tools.lua | 14 -------------- 3 files changed, 14 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 6caa07f3..ed51b545 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -72,6 +72,20 @@ function tools.execute_string(cmd) end end +--- Internal implementation function for fs.dir. +-- Yields a filename on each iteration. +-- @param at string: directory to list +-- @return nil +function tools.dir_iterator(at) + local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS))) + for file in pipe:lines() do + if file ~= "." and file ~= ".." then + coroutine.yield(file) + end + end + pipe:close() +end + --- Download a remote file. -- @param url string: URL to be fetched. -- @param filename string or nil: this function attempts to detect the diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 1bc307a4..84bd53fd 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -97,20 +97,6 @@ function tools.delete(arg) fs.execute_quiet(vars.RM, "-rf", arg) end ---- Internal implementation function for fs.dir. --- Yields a filename on each iteration. --- @param at string: directory to list --- @return nil -function tools.dir_iterator(at) - local pipe = io.popen(fs.command_at(at, vars.LS)) - for file in pipe:lines() do - if file ~= "." and file ~= ".." then - coroutine.yield(file) - end - end - pipe:close() -end - --- Recursively scan the contents of a directory. -- @param at string or nil: directory to scan (will be the current -- directory if none is given). diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index f54ec9a5..9cb6d47a 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -97,20 +97,6 @@ function tools.delete(arg) fs.execute_quiet("if exist "..fs.Q(arg.."\\").." ( RMDIR /S /Q "..fs.Q(arg).." ) else ( DEL /Q /F "..fs.Q(arg).." )") end ---- Internal implementation function for fs.dir. --- Yields a filename on each iteration. --- @param at string: directory to list --- @return nil -function tools.dir_iterator(at) - local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS))) - for file in pipe:lines() do - if file ~= "." and file ~= ".." then - coroutine.yield(file) - end - end - pipe:close() -end - --- Recursively scan the contents of a directory. -- @param at string or nil: directory to scan (will be the current -- directory if none is given). -- cgit v1.2.3-55-g6feb