From a12de9fafa2d585621670d43216bd14626cb7c07 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 3 Sep 2012 21:07:02 -0300 Subject: Cleanup and reduce diff between unix.tools and win32.tools --- src/luarocks/fs/unix/tools.lua | 62 ++++++++-------- src/luarocks/fs/win32/tools.lua | 161 +++++++++++++++++++++------------------- 2 files changed, 114 insertions(+), 109 deletions(-) diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 37efcf66..54120e91 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -10,19 +10,15 @@ local dir_stack = {} local vars = cfg.variables ---- 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 execute_string(cmd) - local actual_cmd = "cd " .. fs.Q(fs.current_dir()) .. " && " .. cmd - local code = os.execute(actual_cmd) - if code == 0 or code == true then - return true - else - return false - end +local function command_at(directory, cmd) + return "cd " .. fs.Q(directory) .. " && " .. cmd +end + +--- Annotate command string for quiet execution. +-- @param cmd string: A command-line string. +-- @return string: The command-line, with silencing annotation. +function quiet(cmd) + return cmd.." 1> /dev/null 2> /dev/null" end --- Obtain current directory. @@ -38,6 +34,20 @@ function current_dir() 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. +-- @return boolean: true if command succeeds (status code 0), false +-- otherwise. +function execute_string(cmd) + local code = os.execute(command_at(fs.current_dir(), cmd)) + if code == 0 or code == true then + return true + else + return false + 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, @@ -77,7 +87,7 @@ end -- @param directory string: pathname of directory to remove. function remove_dir_if_empty(directory) assert(directory) - fs.execute_string(vars.RMDIR.." "..fs.Q(directory).." 1> /dev/null 2> /dev/null") + fs.execute_string(fs.quiet(vars.RMDIR.." "..fs.Q(directory))) end --- Remove a directory if it is empty. @@ -86,7 +96,7 @@ end -- @param directory string: pathname of directory to remove. function remove_dir_tree_if_empty(directory) assert(directory) - fs.execute_string(vars.RMDIR.." -p "..fs.Q(directory).." 1> /dev/null 2> /dev/null") + fs.execute_string(fs.quiet(vars.RMDIR.." -p "..fs.Q(directory))) end --- Copy a file. @@ -121,7 +131,7 @@ end -- plus an error message. function copy_contents(src, dest) assert(src and dest) - if fs.execute_string(vars.CP.." -pPR "..fs.Q(src).."/* "..fs.Q(dest).." 1> /dev/null 2>/dev/null") then + if fs.execute_string(fs.quiet(vars.CP.." -pPR "..fs.Q(src).."/* "..fs.Q(dest))) then return true else return false, "Failed copying "..src.." to "..dest @@ -134,10 +144,10 @@ end function delete(arg) assert(arg) assert(arg:sub(1,1) == "/") - return fs.execute_string(vars.RM.." -rf " .. fs.Q(arg) .. " 1> /dev/null 2>/dev/null") + return fs.execute_string(fs.quiet(vars.RM.." -rf " .. fs.Q(arg))) end ---- List the contents of a directory. +--- List the contents of a directory. -- @param at string or nil: directory to list (will be the current -- directory if none is given). -- @return table: an array of strings with the filenames representing @@ -151,7 +161,7 @@ function list_dir(at) return {} end local result = {} - local pipe = io.popen("cd "..fs.Q(at).." && "..vars.LS) + local pipe = io.popen(command_at(at, vars.LS)) for file in pipe:lines() do table.insert(result, file) end @@ -159,7 +169,7 @@ function list_dir(at) return result end ---- Recursively scan the contents of a directory. +--- Recursively scan the contents of a directory. -- @param at string or nil: directory to scan (will be the current -- directory if none is given). -- @return table: an array of strings with the filenames representing @@ -173,7 +183,7 @@ function find(at) return {} end local result = {} - local pipe = io.popen("cd "..fs.Q(at).." && "..vars.FIND.." * 2>/dev/null") + local pipe = io.popen(command_at(at, vars.FIND.." * 2>/dev/null")) for file in pipe:lines() do table.insert(result, file) end @@ -198,14 +208,6 @@ function unzip(zipfile) return fs.execute(vars.UNZIP, zipfile) end ---- Test for existance of a file. --- @param file string: filename to test --- @return boolean: true if file exists, false otherwise. -function exists(file) - assert(file) - return fs.execute(vars.TEST, "-r", file) -end - --- Test is file/directory is writable. -- @param file string: filename to test -- @return boolean: true if file exists, false otherwise. @@ -278,10 +280,8 @@ function unpack_archive(archive) local ok if archive:match("%.tar%.gz$") or archive:match("%.tgz$") then - -- ok = fs.execute("tar zxvpf ", archive) ok = fs.execute_string(vars.GUNZIP.." -c "..archive.."|"..vars.TAR.." -xf -") elseif archive:match("%.tar%.bz2$") then - -- ok = fs.execute("tar jxvpf ", archive) ok = fs.execute_string(vars.BUNZIP2.." -c "..archive.."|tar -xf -") elseif archive:match("%.zip$") then ok = fs.execute(vars.UNZIP, archive) diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index d376f56a..b250076a 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -5,8 +5,8 @@ module("luarocks.fs.win32.tools", package.seeall) local fs = require("luarocks.fs") -local cfg = require("luarocks.cfg") local dir = require("luarocks.dir") +local cfg = require("luarocks.cfg") local dir_stack = {} @@ -32,13 +32,11 @@ local function command_at(directory, cmd) return cmd end ---- Test for existance of a file. --- @param file string: filename to test --- @return boolean: true if file exists, false otherwise. -function exists(file) - assert(file) - return fs.execute("if not exist " .. fs.Q(file) .. - " invalidcommandname 2>NUL 1>NUL") +--- Annotate command string for quiet execution. +-- @param cmd string: A command-line string. +-- @return string: The command-line, with silencing annotation. +function quiet(cmd) + return cmd.." 2> NUL 1> NUL" end --- Obtain current directory. @@ -54,31 +52,18 @@ function current_dir() return current end ---- Test is pathname is a regular file. --- @param file string: pathname to test --- @return boolean: true if it is a regular file, false otherwise. -function is_file(file) - assert(file) - return fs.execute(vars.TEST.." -f", file) -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 get_md5(file) - local cmd = md5_cmd[cfg.md5checker] - if not cmd then return nil end - local pipe = io.popen(cmd.." "..fs.absolute_name(file)) - local computed = pipe:read("*a") - pipe:close() - if not computed then return nil end - return computed:match("("..("%x"):rep(32)..")") +--- 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 execute_string(cmd) + local code = os.execute(command_at(fs.current_dir(), cmd)) + if code == 0 or code == true then + return true + else + return false + end end --- Change the current directory. @@ -104,28 +89,6 @@ function 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 execute_string(cmd) - local code = os.execute(command_at(fs.current_dir(), cmd)) - if code == 0 or code == true then - return true - else - return false - end -end - ---- Test is pathname is a regular file. --- @param file string: pathname to test --- @return boolean: true if it is a regular file, false otherwise. -function is_dir(file) - assert(file) - return fs.execute(vars.TEST.." -d " .. fs.Q(file) .. " 2>NUL 1>NUL") -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. @@ -133,7 +96,7 @@ end -- @return boolean: true on success, false on failure. function make_dir(directory) assert(directory) - fs.execute(vars.MKDIR.." "..fs.Q(directory).." 1> NUL 2> NUL") + fs.execute(fs.quiet(vars.MKDIR.." "..fs.Q(directory))) return 1 end @@ -143,7 +106,7 @@ end -- @param directory string: pathname of directory to remove. function remove_dir_if_empty(directory) assert(directory) - fs.execute_string(vars.RMDIR.." "..fs.Q(directory).." 1> NUL 2> NUL") + fs.execute_string(fs.quiet(vars.RMDIR.." "..fs.Q(directory))) end --- Remove a directory if it is empty. @@ -152,7 +115,7 @@ end -- @param directory string: pathname of directory to remove. function remove_dir_tree_if_empty(directory) assert(directory) - fs.execute_string(vars.RMDIR.." "..fs.Q(directory).." 1> NUL 2> NUL") + fs.execute_string(fs.quiet(vars.RMDIR.." "..fs.Q(directory))) end --- Copy a file. @@ -177,7 +140,7 @@ end -- plus an error message. function copy_contents(src, dest) assert(src and dest) - if fs.execute_string(vars.CP.." -a "..src.."\\*.* "..fs.Q(dest).." 1> NUL 2> NUL") then + if fs.execute_string(fs.quiet(vars.CP.." -a "..src.."\\*.* "..fs.Q(dest))) then return true else return false, "Failed copying "..src.." to "..dest @@ -192,7 +155,7 @@ function delete(arg) assert(arg) assert(arg:match("^[\a-zA-Z]?:?[\\/]")) fs.execute(vars.CHMOD.." a+rw -R ", arg) - return fs.execute_string(vars.RM.." -rf " .. fs.Q(arg) .. " 1> NUL 2> NUL") + return fs.execute_string(fs.quiet(vars.RM.." -rf " .. fs.Q(arg))) end --- List the contents of a directory. @@ -245,6 +208,39 @@ function find(at) return result end +--- Compress files in a .zip archive. +-- @param zipfile string: pathname of .zip archive to be created. +-- @param ... Filenames to be stored in the archive are given as +-- additional arguments. +-- @return boolean: true on success, false on failure. +function zip(zipfile, ...) + return fs.execute(vars.SEVENZ.." a -tzip", zipfile, ...) +end + +--- Uncompress files from a .zip archive. +-- @param zipfile string: pathname of .zip archive to be extracted. +-- @return boolean: true on success, false on failure. +function unzip(zipfile) + assert(zipfile) + return fs.execute(vars.SEVENZ.." x", zipfile) +end + +--- Test is pathname is a directory. +-- @param file string: pathname to test +-- @return boolean: true if it is a directory, false otherwise. +function is_dir(file) + assert(file) + return fs.execute(fs.quiet(vars.TEST.." -d " .. fs.Q(file))) +end + +--- Test is pathname is a regular file. +-- @param file string: pathname to test +-- @return boolean: true if it is a regular file, false otherwise. +function is_file(file) + assert(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 @@ -269,24 +265,6 @@ function download(url, filename) end end - ---- Compress files in a .zip archive. --- @param zipfile string: pathname of .zip archive to be created. --- @param ... Filenames to be stored in the archive are given as --- additional arguments. --- @return boolean: true on success, false on failure. -function zip(zipfile, ...) - return fs.execute(vars.SEVENZ.." a -tzip", zipfile, ...) -end - ---- Uncompress files from a .zip archive. --- @param zipfile string: pathname of .zip archive to be extracted. --- @return boolean: true on success, false on failure. -function unzip(zipfile) - assert(zipfile) - return fs.execute(vars.SEVENZ.." x", zipfile) -end - --- Uncompress gzip file. -- @param archive string: Filename of archive. -- @return boolean : success status @@ -333,3 +311,30 @@ function unpack_archive(archive) end 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 get_md5(file) + local cmd = md5_cmd[cfg.md5checker] + if not cmd then return nil end + local pipe = io.popen(cmd.." "..fs.absolute_name(file)) + local computed = pipe:read("*a") + pipe:close() + if not computed then return nil end + return computed:match("("..("%x"):rep(32)..")") +end + +--- Test for existance of a file. +-- @param file string: filename to test +-- @return boolean: true if file exists, false otherwise. +function exists(file) + assert(file) + return fs.execute(fs.quiet("if not exist " .. fs.Q(file) .. " invalidcommandname")) +end -- cgit v1.2.3-55-g6feb