From cb08cbe837fce0b20055d55d240b18c2a6b7ebca Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 20 Mar 2014 22:45:10 +0400 Subject: Unmoduled per-platform fs implementations --- src/luarocks/fs/unix/tools.lua | 55 ++++++++++++++++++++++------------------- src/luarocks/fs/win32/tools.lua | 49 +++++++++++++++++++----------------- 2 files changed, 55 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index df2eee3f..a288dd60 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -1,6 +1,7 @@ --- fs operations implemented with third-party tools for Unix platform abstractions. -module("luarocks.fs.unix.tools", package.seeall) +--module("luarocks.fs.unix.tools", package.seeall) +local tools = {} local fs = require("luarocks.fs") local dir = require("luarocks.dir") @@ -17,7 +18,7 @@ end --- Obtain current directory. -- Uses the module's internal directory stack. -- @return string: the absolute pathname of the current directory. -function current_dir() +function tools.current_dir() local pipe = io.popen(vars.PWD) local current = pipe:read("*l") pipe:close() @@ -32,7 +33,7 @@ end -- @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) +function tools.execute_string(cmd) local code = os.execute(command_at(fs.current_dir(), cmd)) if code == 0 or code == true then return true @@ -46,7 +47,7 @@ end -- 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 change_dir(directory) +function tools.change_dir(directory) assert(type(directory) == "string") if fs.is_dir(directory) then table.insert(dir_stack, directory) @@ -58,12 +59,12 @@ end --- Change directory to root. -- Allows leaving a directory (e.g. for deleting it) in -- a crossplatform way. -function change_dir_to_root() +function tools.change_dir_to_root() table.insert(dir_stack, "/") end --- Change working directory to the previous in the directory stack. -function pop_dir() +function tools.pop_dir() local directory = table.remove(dir_stack) return directory ~= nil end @@ -73,7 +74,7 @@ end -- too, they are created as well. -- @param directory string: pathname of directory to create. -- @return boolean: true on success, false on failure. -function make_dir(directory) +function tools.make_dir(directory) assert(directory) local ok, err = fs.execute(vars.MKDIR.." -p", directory) if not ok then @@ -86,7 +87,7 @@ end -- Does not return errors (for example, if directory is not empty or -- if already does not exist) -- @param directory string: pathname of directory to remove. -function remove_dir_if_empty(directory) +function tools.remove_dir_if_empty(directory) assert(directory) fs.execute_quiet(vars.RMDIR, directory) end @@ -95,7 +96,7 @@ end -- Does not return errors (for example, if directory is not empty or -- if already does not exist) -- @param directory string: pathname of directory to remove. -function remove_dir_tree_if_empty(directory) +function tools.remove_dir_tree_if_empty(directory) assert(directory) fs.execute_quiet(vars.RMDIR, "-p", directory) end @@ -106,7 +107,7 @@ end -- @param perm string or nil: Permissions for destination file, -- @return boolean or (boolean, string): true on success, false on failure, -- plus an error message. -function copy(src, dest, perm) +function tools.copy(src, dest, perm) assert(src and dest) if fs.execute(vars.CP, src, dest) then if perm then @@ -130,7 +131,7 @@ end -- @param dest string: Pathname of destination -- @return boolean or (boolean, string): true on success, false on failure, -- plus an error message. -function copy_contents(src, dest) +function tools.copy_contents(src, dest) assert(src and dest) if fs.execute_quiet(vars.CP.." -pPR "..fs.Q(src).."/* "..fs.Q(dest)) then return true @@ -142,7 +143,7 @@ end -- For safety, this only accepts absolute paths. -- @param arg string: Pathname of source -- @return nil -function delete(arg) +function tools.delete(arg) assert(arg) assert(arg:sub(1,1) == "/") fs.execute_quiet(vars.RM, "-rf", arg) @@ -152,7 +153,7 @@ end -- Yields a filename on each iteration. -- @param at string: directory to list -- @return nil -function dir_iterator(at) +function tools.dir_iterator(at) local pipe = io.popen(command_at(at, vars.LS)) for file in pipe:lines() do if file ~= "." and file ~= ".." then @@ -167,7 +168,7 @@ end -- directory if none is given). -- @return table: an array of strings with the filenames representing -- the contents of a directory. -function find(at) +function tools.find(at) assert(type(at) == "string" or not at) if not at then at = fs.current_dir() @@ -189,14 +190,14 @@ end -- @param ... Filenames to be stored in the archive are given as -- additional arguments. -- @return boolean: true on success, false on failure. -function zip(zipfile, ...) +function tools.zip(zipfile, ...) return fs.execute(vars.ZIP.." -r", 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) +function tools.unzip(zipfile) assert(zipfile) return fs.execute_quiet(vars.UNZIP, zipfile) end @@ -204,7 +205,7 @@ end --- Test is file/directory exists -- @param file string: filename to test -- @return boolean: true if file exists, false otherwise. -function exists(file) +function tools.exists(file) assert(file) return fs.execute(vars.TEST, "-e", file) end @@ -212,7 +213,7 @@ 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) +function tools.is_dir(file) assert(file) return fs.execute(vars.TEST, "-d", file) end @@ -220,7 +221,7 @@ 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) +function tools.is_file(file) assert(file) return fs.execute(vars.TEST, "-f", file) end @@ -233,7 +234,7 @@ end -- 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 download(url, filename, cache) +function tools.download(url, filename, cache) assert(type(url) == "string") assert(type(filename) == "string" or not filename) @@ -263,7 +264,7 @@ function download(url, filename, cache) end end -function chmod(pathname, mode) +function tools.chmod(pathname, mode) if mode then return fs.execute(vars.CHMOD, mode, pathname) else @@ -273,7 +274,7 @@ end --- Apply a patch. -- @param patchname string: The filename of the patch. -function apply_patch(patchname) +function tools.apply_patch(patchname) return fs.execute(vars.PATCH.." -p1 -f -i ", patchname) end @@ -282,7 +283,7 @@ end -- filename extension. -- @param archive string: Filename of archive. -- @return boolean or (boolean, string): true on success, false and an error message on failure. -function unpack_archive(archive) +function tools.unpack_archive(archive) assert(type(archive) == "string") local ok @@ -314,7 +315,7 @@ local md5_cmd = { --- Get the MD5 checksum for a file. -- @param file string: The file to be computed. -- @return string: The MD5 checksum -function get_md5(file) +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))) @@ -327,13 +328,15 @@ function get_md5(file) return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) end -function get_permissions(filename) +function tools.get_permissions(filename) local pipe = io.popen(vars.STAT.." "..vars.STATFLAG.." "..fs.Q(filename)) local ret = pipe:read("*l") pipe:close() return ret end -function browser(url) +function tools.browser(url) return fs.execute(cfg.web_browser, url) end + +return tools diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 55781b66..abf3779f 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -2,7 +2,8 @@ --- fs operations implemented with third-party tools for Windows platform abstractions. -- Download http://unxutils.sourceforge.net/ for Windows GNU utilities -- used by this module. -module("luarocks.fs.win32.tools", package.seeall) +--module("luarocks.fs.win32.tools", package.seeall) +local tools = {} local fs = require("luarocks.fs") local dir = require("luarocks.dir") @@ -35,7 +36,7 @@ end --- Obtain current directory. -- Uses the module's internal directory stack. -- @return string: the absolute pathname of the current directory. -function current_dir() +function tools.current_dir() local current = cfg.cache_pwd if not current then local pipe = io.popen(fs.Q(vars.PWD)) @@ -54,7 +55,7 @@ end -- @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) +function tools.execute_string(cmd) cmd = command_at(fs.current_dir(), cmd) local code = os.execute(cmd) if code == 0 or code == true then @@ -70,7 +71,7 @@ end -- 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 change_dir(directory) +function tools.change_dir(directory) assert(type(directory) == "string") if fs.is_dir(directory) then table.insert(dir_stack, directory) @@ -82,12 +83,12 @@ end --- Change directory to root. -- Allows leaving a directory (e.g. for deleting it) in -- a crossplatform way. -function change_dir_to_root() +function tools.change_dir_to_root() table.insert(dir_stack, "/") end --- Change working directory to the previous in the directory stack. -function pop_dir() +function tools.pop_dir() local directory = table.remove(dir_stack) return directory ~= nil end @@ -97,7 +98,7 @@ end -- too, they are created as well. -- @param directory string: pathname of directory to create. -- @return boolean: true on success, false on failure. -function make_dir(directory) +function tools.make_dir(directory) assert(directory) directory = dir.normalize(directory) fs.execute_quiet(fs.Q(vars.MKDIR).." -p ", directory) @@ -111,7 +112,7 @@ end -- Does not return errors (for example, if directory is not empty or -- if already does not exist) -- @param directory string: pathname of directory to remove. -function remove_dir_if_empty(directory) +function tools.remove_dir_if_empty(directory) assert(directory) fs.execute_quiet(fs.Q(vars.RMDIR), directory) end @@ -120,7 +121,7 @@ end -- Does not return errors (for example, if directory is not empty or -- if already does not exist) -- @param directory string: pathname of directory to remove. -function remove_dir_tree_if_empty(directory) +function tools.remove_dir_tree_if_empty(directory) assert(directory) fs.execute_quiet(fs.Q(vars.RMDIR), directory) end @@ -130,7 +131,7 @@ end -- @param dest string: Pathname of destination -- @return boolean or (boolean, string): true on success, false on failure, -- plus an error message. -function copy(src, dest) +function tools.copy(src, dest) assert(src and dest) if dest:match("[/\\]$") then dest = dest:sub(1, -2) end local ok = fs.execute(fs.Q(vars.CP), src, dest) @@ -146,7 +147,7 @@ end -- @param dest string: Pathname of destination -- @return boolean or (boolean, string): true on success, false on failure, -- plus an error message. -function copy_contents(src, dest) +function tools.copy_contents(src, dest) assert(src and dest) if fs.execute_quiet(fs.Q(vars.CP).." -dR "..src.."\\*.* "..fs.Q(dest)) then return true @@ -159,7 +160,7 @@ end -- For safety, this only accepts absolute paths. -- @param arg string: Pathname of source -- @return nil -function delete(arg) +function tools.delete(arg) assert(arg) assert(arg:match("^[a-zA-Z]?:?[\\/]")) fs.execute_quiet("if exist "..fs.Q(arg.."\\").." ( RMDIR /S /Q "..fs.Q(arg).." ) else ( DEL /Q /F "..fs.Q(arg).." )") @@ -169,7 +170,7 @@ end -- Yields a filename on each iteration. -- @param at string: directory to list -- @return nil -function dir_iterator(at) +function tools.dir_iterator(at) local pipe = io.popen(command_at(at, fs.Q(vars.LS))) for file in pipe:lines() do if file ~= "." and file ~= ".." then @@ -184,7 +185,7 @@ end -- directory if none is given). -- @return table: an array of strings with the filenames representing -- the contents of a directory. Paths are returned with forward slashes. -function find(at) +function tools.find(at) assert(type(at) == "string" or not at) if not at then at = fs.current_dir() @@ -211,14 +212,14 @@ end -- @param ... Filenames to be stored in the archive are given as -- additional arguments. -- @return boolean: true on success, false on failure. -function zip(zipfile, ...) +function tools.zip(zipfile, ...) return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa 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) +function tools.unzip(zipfile) assert(zipfile) return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa x", zipfile) end @@ -226,7 +227,7 @@ 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) +function tools.is_dir(file) assert(file) return fs.execute_quiet("if not exist " .. fs.Q(file.."\\").." invalidcommandname") end @@ -234,7 +235,7 @@ 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) +function tools.is_file(file) assert(file) return fs.execute(fs.Q(vars.TEST).." -f", file) end @@ -247,7 +248,7 @@ end -- 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 download(url, filename, cache) +function tools.download(url, filename, cache) assert(type(url) == "string") assert(type(filename) == "string" or not filename) @@ -289,7 +290,7 @@ end -- filename extension. -- @param archive string: Filename of archive. -- @return boolean or (boolean, string): true on success, false and an error message on failure. -function unpack_archive(archive) +function tools.unpack_archive(archive) assert(type(archive) == "string") local ok @@ -333,7 +334,7 @@ local md5_cmd = { --- Get the MD5 checksum for a file. -- @param file string: The file to be computed. -- @return string: The MD5 checksum or nil + message -function get_md5(file) +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))) @@ -349,11 +350,13 @@ end --- Test for existance of a file. -- @param file string: filename to test -- @return boolean: true if file exists, false otherwise. -function exists(file) +function tools.exists(file) assert(file) return fs.execute_quiet("if not exist " .. fs.Q(file) .. " invalidcommandname") end -function browser(url) +function tools.browser(url) return fs.execute(cfg.web_browser..' "Starting docs..." '..fs.Q(url)) end + +return tools -- cgit v1.2.3-55-g6feb