diff options
| author | mpeterv <petjamelnik@yandex.ru> | 2014-03-20 22:45:10 +0400 |
|---|---|---|
| committer | mpeterv <petjamelnik@yandex.ru> | 2014-03-20 22:45:10 +0400 |
| commit | cb08cbe837fce0b20055d55d240b18c2a6b7ebca (patch) | |
| tree | 8438d8b2045a55e0bccd77574f367d11080b07dc | |
| parent | 232317a633c5952f882ab4a891fa245d130dcc29 (diff) | |
| download | luarocks-cb08cbe837fce0b20055d55d240b18c2a6b7ebca.tar.gz luarocks-cb08cbe837fce0b20055d55d240b18c2a6b7ebca.tar.bz2 luarocks-cb08cbe837fce0b20055d55d240b18c2a6b7ebca.zip | |
Unmoduled per-platform fs implementations
| -rw-r--r-- | src/luarocks/fs/unix/tools.lua | 55 | ||||
| -rw-r--r-- | src/luarocks/fs/win32/tools.lua | 49 |
2 files changed, 55 insertions, 49 deletions
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 @@ | |||
| 1 | 1 | ||
| 2 | --- fs operations implemented with third-party tools for Unix platform abstractions. | 2 | --- fs operations implemented with third-party tools for Unix platform abstractions. |
| 3 | module("luarocks.fs.unix.tools", package.seeall) | 3 | --module("luarocks.fs.unix.tools", package.seeall) |
| 4 | local tools = {} | ||
| 4 | 5 | ||
| 5 | local fs = require("luarocks.fs") | 6 | local fs = require("luarocks.fs") |
| 6 | local dir = require("luarocks.dir") | 7 | local dir = require("luarocks.dir") |
| @@ -17,7 +18,7 @@ end | |||
| 17 | --- Obtain current directory. | 18 | --- Obtain current directory. |
| 18 | -- Uses the module's internal directory stack. | 19 | -- Uses the module's internal directory stack. |
| 19 | -- @return string: the absolute pathname of the current directory. | 20 | -- @return string: the absolute pathname of the current directory. |
| 20 | function current_dir() | 21 | function tools.current_dir() |
| 21 | local pipe = io.popen(vars.PWD) | 22 | local pipe = io.popen(vars.PWD) |
| 22 | local current = pipe:read("*l") | 23 | local current = pipe:read("*l") |
| 23 | pipe:close() | 24 | pipe:close() |
| @@ -32,7 +33,7 @@ end | |||
| 32 | -- @param cmd string: No quoting/escaping is applied to the command. | 33 | -- @param cmd string: No quoting/escaping is applied to the command. |
| 33 | -- @return boolean: true if command succeeds (status code 0), false | 34 | -- @return boolean: true if command succeeds (status code 0), false |
| 34 | -- otherwise. | 35 | -- otherwise. |
| 35 | function execute_string(cmd) | 36 | function tools.execute_string(cmd) |
| 36 | local code = os.execute(command_at(fs.current_dir(), cmd)) | 37 | local code = os.execute(command_at(fs.current_dir(), cmd)) |
| 37 | if code == 0 or code == true then | 38 | if code == 0 or code == true then |
| 38 | return true | 39 | return true |
| @@ -46,7 +47,7 @@ end | |||
| 46 | -- semantics of chdir, as it does not handle errors the same way, | 47 | -- semantics of chdir, as it does not handle errors the same way, |
| 47 | -- but works well for our purposes for now. | 48 | -- but works well for our purposes for now. |
| 48 | -- @param directory string: The directory to switch to. | 49 | -- @param directory string: The directory to switch to. |
| 49 | function change_dir(directory) | 50 | function tools.change_dir(directory) |
| 50 | assert(type(directory) == "string") | 51 | assert(type(directory) == "string") |
| 51 | if fs.is_dir(directory) then | 52 | if fs.is_dir(directory) then |
| 52 | table.insert(dir_stack, directory) | 53 | table.insert(dir_stack, directory) |
| @@ -58,12 +59,12 @@ end | |||
| 58 | --- Change directory to root. | 59 | --- Change directory to root. |
| 59 | -- Allows leaving a directory (e.g. for deleting it) in | 60 | -- Allows leaving a directory (e.g. for deleting it) in |
| 60 | -- a crossplatform way. | 61 | -- a crossplatform way. |
| 61 | function change_dir_to_root() | 62 | function tools.change_dir_to_root() |
| 62 | table.insert(dir_stack, "/") | 63 | table.insert(dir_stack, "/") |
| 63 | end | 64 | end |
| 64 | 65 | ||
| 65 | --- Change working directory to the previous in the directory stack. | 66 | --- Change working directory to the previous in the directory stack. |
| 66 | function pop_dir() | 67 | function tools.pop_dir() |
| 67 | local directory = table.remove(dir_stack) | 68 | local directory = table.remove(dir_stack) |
| 68 | return directory ~= nil | 69 | return directory ~= nil |
| 69 | end | 70 | end |
| @@ -73,7 +74,7 @@ end | |||
| 73 | -- too, they are created as well. | 74 | -- too, they are created as well. |
| 74 | -- @param directory string: pathname of directory to create. | 75 | -- @param directory string: pathname of directory to create. |
| 75 | -- @return boolean: true on success, false on failure. | 76 | -- @return boolean: true on success, false on failure. |
| 76 | function make_dir(directory) | 77 | function tools.make_dir(directory) |
| 77 | assert(directory) | 78 | assert(directory) |
| 78 | local ok, err = fs.execute(vars.MKDIR.." -p", directory) | 79 | local ok, err = fs.execute(vars.MKDIR.." -p", directory) |
| 79 | if not ok then | 80 | if not ok then |
| @@ -86,7 +87,7 @@ end | |||
| 86 | -- Does not return errors (for example, if directory is not empty or | 87 | -- Does not return errors (for example, if directory is not empty or |
| 87 | -- if already does not exist) | 88 | -- if already does not exist) |
| 88 | -- @param directory string: pathname of directory to remove. | 89 | -- @param directory string: pathname of directory to remove. |
| 89 | function remove_dir_if_empty(directory) | 90 | function tools.remove_dir_if_empty(directory) |
| 90 | assert(directory) | 91 | assert(directory) |
| 91 | fs.execute_quiet(vars.RMDIR, directory) | 92 | fs.execute_quiet(vars.RMDIR, directory) |
| 92 | end | 93 | end |
| @@ -95,7 +96,7 @@ end | |||
| 95 | -- Does not return errors (for example, if directory is not empty or | 96 | -- Does not return errors (for example, if directory is not empty or |
| 96 | -- if already does not exist) | 97 | -- if already does not exist) |
| 97 | -- @param directory string: pathname of directory to remove. | 98 | -- @param directory string: pathname of directory to remove. |
| 98 | function remove_dir_tree_if_empty(directory) | 99 | function tools.remove_dir_tree_if_empty(directory) |
| 99 | assert(directory) | 100 | assert(directory) |
| 100 | fs.execute_quiet(vars.RMDIR, "-p", directory) | 101 | fs.execute_quiet(vars.RMDIR, "-p", directory) |
| 101 | end | 102 | end |
| @@ -106,7 +107,7 @@ end | |||
| 106 | -- @param perm string or nil: Permissions for destination file, | 107 | -- @param perm string or nil: Permissions for destination file, |
| 107 | -- @return boolean or (boolean, string): true on success, false on failure, | 108 | -- @return boolean or (boolean, string): true on success, false on failure, |
| 108 | -- plus an error message. | 109 | -- plus an error message. |
| 109 | function copy(src, dest, perm) | 110 | function tools.copy(src, dest, perm) |
| 110 | assert(src and dest) | 111 | assert(src and dest) |
| 111 | if fs.execute(vars.CP, src, dest) then | 112 | if fs.execute(vars.CP, src, dest) then |
| 112 | if perm then | 113 | if perm then |
| @@ -130,7 +131,7 @@ end | |||
| 130 | -- @param dest string: Pathname of destination | 131 | -- @param dest string: Pathname of destination |
| 131 | -- @return boolean or (boolean, string): true on success, false on failure, | 132 | -- @return boolean or (boolean, string): true on success, false on failure, |
| 132 | -- plus an error message. | 133 | -- plus an error message. |
| 133 | function copy_contents(src, dest) | 134 | function tools.copy_contents(src, dest) |
| 134 | assert(src and dest) | 135 | assert(src and dest) |
| 135 | if fs.execute_quiet(vars.CP.." -pPR "..fs.Q(src).."/* "..fs.Q(dest)) then | 136 | if fs.execute_quiet(vars.CP.." -pPR "..fs.Q(src).."/* "..fs.Q(dest)) then |
| 136 | return true | 137 | return true |
| @@ -142,7 +143,7 @@ end | |||
| 142 | -- For safety, this only accepts absolute paths. | 143 | -- For safety, this only accepts absolute paths. |
| 143 | -- @param arg string: Pathname of source | 144 | -- @param arg string: Pathname of source |
| 144 | -- @return nil | 145 | -- @return nil |
| 145 | function delete(arg) | 146 | function tools.delete(arg) |
| 146 | assert(arg) | 147 | assert(arg) |
| 147 | assert(arg:sub(1,1) == "/") | 148 | assert(arg:sub(1,1) == "/") |
| 148 | fs.execute_quiet(vars.RM, "-rf", arg) | 149 | fs.execute_quiet(vars.RM, "-rf", arg) |
| @@ -152,7 +153,7 @@ end | |||
| 152 | -- Yields a filename on each iteration. | 153 | -- Yields a filename on each iteration. |
| 153 | -- @param at string: directory to list | 154 | -- @param at string: directory to list |
| 154 | -- @return nil | 155 | -- @return nil |
| 155 | function dir_iterator(at) | 156 | function tools.dir_iterator(at) |
| 156 | local pipe = io.popen(command_at(at, vars.LS)) | 157 | local pipe = io.popen(command_at(at, vars.LS)) |
| 157 | for file in pipe:lines() do | 158 | for file in pipe:lines() do |
| 158 | if file ~= "." and file ~= ".." then | 159 | if file ~= "." and file ~= ".." then |
| @@ -167,7 +168,7 @@ end | |||
| 167 | -- directory if none is given). | 168 | -- directory if none is given). |
| 168 | -- @return table: an array of strings with the filenames representing | 169 | -- @return table: an array of strings with the filenames representing |
| 169 | -- the contents of a directory. | 170 | -- the contents of a directory. |
| 170 | function find(at) | 171 | function tools.find(at) |
| 171 | assert(type(at) == "string" or not at) | 172 | assert(type(at) == "string" or not at) |
| 172 | if not at then | 173 | if not at then |
| 173 | at = fs.current_dir() | 174 | at = fs.current_dir() |
| @@ -189,14 +190,14 @@ end | |||
| 189 | -- @param ... Filenames to be stored in the archive are given as | 190 | -- @param ... Filenames to be stored in the archive are given as |
| 190 | -- additional arguments. | 191 | -- additional arguments. |
| 191 | -- @return boolean: true on success, false on failure. | 192 | -- @return boolean: true on success, false on failure. |
| 192 | function zip(zipfile, ...) | 193 | function tools.zip(zipfile, ...) |
| 193 | return fs.execute(vars.ZIP.." -r", zipfile, ...) | 194 | return fs.execute(vars.ZIP.." -r", zipfile, ...) |
| 194 | end | 195 | end |
| 195 | 196 | ||
| 196 | --- Uncompress files from a .zip archive. | 197 | --- Uncompress files from a .zip archive. |
| 197 | -- @param zipfile string: pathname of .zip archive to be extracted. | 198 | -- @param zipfile string: pathname of .zip archive to be extracted. |
| 198 | -- @return boolean: true on success, false on failure. | 199 | -- @return boolean: true on success, false on failure. |
| 199 | function unzip(zipfile) | 200 | function tools.unzip(zipfile) |
| 200 | assert(zipfile) | 201 | assert(zipfile) |
| 201 | return fs.execute_quiet(vars.UNZIP, zipfile) | 202 | return fs.execute_quiet(vars.UNZIP, zipfile) |
| 202 | end | 203 | end |
| @@ -204,7 +205,7 @@ end | |||
| 204 | --- Test is file/directory exists | 205 | --- Test is file/directory exists |
| 205 | -- @param file string: filename to test | 206 | -- @param file string: filename to test |
| 206 | -- @return boolean: true if file exists, false otherwise. | 207 | -- @return boolean: true if file exists, false otherwise. |
| 207 | function exists(file) | 208 | function tools.exists(file) |
| 208 | assert(file) | 209 | assert(file) |
| 209 | return fs.execute(vars.TEST, "-e", file) | 210 | return fs.execute(vars.TEST, "-e", file) |
| 210 | end | 211 | end |
| @@ -212,7 +213,7 @@ end | |||
| 212 | --- Test is pathname is a directory. | 213 | --- Test is pathname is a directory. |
| 213 | -- @param file string: pathname to test | 214 | -- @param file string: pathname to test |
| 214 | -- @return boolean: true if it is a directory, false otherwise. | 215 | -- @return boolean: true if it is a directory, false otherwise. |
| 215 | function is_dir(file) | 216 | function tools.is_dir(file) |
| 216 | assert(file) | 217 | assert(file) |
| 217 | return fs.execute(vars.TEST, "-d", file) | 218 | return fs.execute(vars.TEST, "-d", file) |
| 218 | end | 219 | end |
| @@ -220,7 +221,7 @@ end | |||
| 220 | --- Test is pathname is a regular file. | 221 | --- Test is pathname is a regular file. |
| 221 | -- @param file string: pathname to test | 222 | -- @param file string: pathname to test |
| 222 | -- @return boolean: true if it is a regular file, false otherwise. | 223 | -- @return boolean: true if it is a regular file, false otherwise. |
| 223 | function is_file(file) | 224 | function tools.is_file(file) |
| 224 | assert(file) | 225 | assert(file) |
| 225 | return fs.execute(vars.TEST, "-f", file) | 226 | return fs.execute(vars.TEST, "-f", file) |
| 226 | end | 227 | end |
| @@ -233,7 +234,7 @@ end | |||
| 233 | -- filename can be given explicitly as this second argument. | 234 | -- filename can be given explicitly as this second argument. |
| 234 | -- @return (boolean, string): true and the filename on success, | 235 | -- @return (boolean, string): true and the filename on success, |
| 235 | -- false and the error message on failure. | 236 | -- false and the error message on failure. |
| 236 | function download(url, filename, cache) | 237 | function tools.download(url, filename, cache) |
| 237 | assert(type(url) == "string") | 238 | assert(type(url) == "string") |
| 238 | assert(type(filename) == "string" or not filename) | 239 | assert(type(filename) == "string" or not filename) |
| 239 | 240 | ||
| @@ -263,7 +264,7 @@ function download(url, filename, cache) | |||
| 263 | end | 264 | end |
| 264 | end | 265 | end |
| 265 | 266 | ||
| 266 | function chmod(pathname, mode) | 267 | function tools.chmod(pathname, mode) |
| 267 | if mode then | 268 | if mode then |
| 268 | return fs.execute(vars.CHMOD, mode, pathname) | 269 | return fs.execute(vars.CHMOD, mode, pathname) |
| 269 | else | 270 | else |
| @@ -273,7 +274,7 @@ end | |||
| 273 | 274 | ||
| 274 | --- Apply a patch. | 275 | --- Apply a patch. |
| 275 | -- @param patchname string: The filename of the patch. | 276 | -- @param patchname string: The filename of the patch. |
| 276 | function apply_patch(patchname) | 277 | function tools.apply_patch(patchname) |
| 277 | return fs.execute(vars.PATCH.." -p1 -f -i ", patchname) | 278 | return fs.execute(vars.PATCH.." -p1 -f -i ", patchname) |
| 278 | end | 279 | end |
| 279 | 280 | ||
| @@ -282,7 +283,7 @@ end | |||
| 282 | -- filename extension. | 283 | -- filename extension. |
| 283 | -- @param archive string: Filename of archive. | 284 | -- @param archive string: Filename of archive. |
| 284 | -- @return boolean or (boolean, string): true on success, false and an error message on failure. | 285 | -- @return boolean or (boolean, string): true on success, false and an error message on failure. |
| 285 | function unpack_archive(archive) | 286 | function tools.unpack_archive(archive) |
| 286 | assert(type(archive) == "string") | 287 | assert(type(archive) == "string") |
| 287 | 288 | ||
| 288 | local ok | 289 | local ok |
| @@ -314,7 +315,7 @@ local md5_cmd = { | |||
| 314 | --- Get the MD5 checksum for a file. | 315 | --- Get the MD5 checksum for a file. |
| 315 | -- @param file string: The file to be computed. | 316 | -- @param file string: The file to be computed. |
| 316 | -- @return string: The MD5 checksum | 317 | -- @return string: The MD5 checksum |
| 317 | function get_md5(file) | 318 | function tools.get_md5(file) |
| 318 | local cmd = md5_cmd[cfg.md5checker] | 319 | local cmd = md5_cmd[cfg.md5checker] |
| 319 | if not cmd then return nil, "no MD5 checker command configured" end | 320 | if not cmd then return nil, "no MD5 checker command configured" end |
| 320 | local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) | 321 | local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) |
| @@ -327,13 +328,15 @@ function get_md5(file) | |||
| 327 | return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) | 328 | return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) |
| 328 | end | 329 | end |
| 329 | 330 | ||
| 330 | function get_permissions(filename) | 331 | function tools.get_permissions(filename) |
| 331 | local pipe = io.popen(vars.STAT.." "..vars.STATFLAG.." "..fs.Q(filename)) | 332 | local pipe = io.popen(vars.STAT.." "..vars.STATFLAG.." "..fs.Q(filename)) |
| 332 | local ret = pipe:read("*l") | 333 | local ret = pipe:read("*l") |
| 333 | pipe:close() | 334 | pipe:close() |
| 334 | return ret | 335 | return ret |
| 335 | end | 336 | end |
| 336 | 337 | ||
| 337 | function browser(url) | 338 | function tools.browser(url) |
| 338 | return fs.execute(cfg.web_browser, url) | 339 | return fs.execute(cfg.web_browser, url) |
| 339 | end | 340 | end |
| 341 | |||
| 342 | 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 @@ | |||
| 2 | --- fs operations implemented with third-party tools for Windows platform abstractions. | 2 | --- fs operations implemented with third-party tools for Windows platform abstractions. |
| 3 | -- Download http://unxutils.sourceforge.net/ for Windows GNU utilities | 3 | -- Download http://unxutils.sourceforge.net/ for Windows GNU utilities |
| 4 | -- used by this module. | 4 | -- used by this module. |
| 5 | module("luarocks.fs.win32.tools", package.seeall) | 5 | --module("luarocks.fs.win32.tools", package.seeall) |
| 6 | local tools = {} | ||
| 6 | 7 | ||
| 7 | local fs = require("luarocks.fs") | 8 | local fs = require("luarocks.fs") |
| 8 | local dir = require("luarocks.dir") | 9 | local dir = require("luarocks.dir") |
| @@ -35,7 +36,7 @@ end | |||
| 35 | --- Obtain current directory. | 36 | --- Obtain current directory. |
| 36 | -- Uses the module's internal directory stack. | 37 | -- Uses the module's internal directory stack. |
| 37 | -- @return string: the absolute pathname of the current directory. | 38 | -- @return string: the absolute pathname of the current directory. |
| 38 | function current_dir() | 39 | function tools.current_dir() |
| 39 | local current = cfg.cache_pwd | 40 | local current = cfg.cache_pwd |
| 40 | if not current then | 41 | if not current then |
| 41 | local pipe = io.popen(fs.Q(vars.PWD)) | 42 | local pipe = io.popen(fs.Q(vars.PWD)) |
| @@ -54,7 +55,7 @@ end | |||
| 54 | -- @param cmd string: No quoting/escaping is applied to the command. | 55 | -- @param cmd string: No quoting/escaping is applied to the command. |
| 55 | -- @return boolean: true if command succeeds (status code 0), false | 56 | -- @return boolean: true if command succeeds (status code 0), false |
| 56 | -- otherwise. | 57 | -- otherwise. |
| 57 | function execute_string(cmd) | 58 | function tools.execute_string(cmd) |
| 58 | cmd = command_at(fs.current_dir(), cmd) | 59 | cmd = command_at(fs.current_dir(), cmd) |
| 59 | local code = os.execute(cmd) | 60 | local code = os.execute(cmd) |
| 60 | if code == 0 or code == true then | 61 | if code == 0 or code == true then |
| @@ -70,7 +71,7 @@ end | |||
| 70 | -- but works well for our purposes for now. | 71 | -- but works well for our purposes for now. |
| 71 | -- @param directory string: The directory to switch to. | 72 | -- @param directory string: The directory to switch to. |
| 72 | -- @return boolean or (nil, string): true if successful, (nil, error message) if failed. | 73 | -- @return boolean or (nil, string): true if successful, (nil, error message) if failed. |
| 73 | function change_dir(directory) | 74 | function tools.change_dir(directory) |
| 74 | assert(type(directory) == "string") | 75 | assert(type(directory) == "string") |
| 75 | if fs.is_dir(directory) then | 76 | if fs.is_dir(directory) then |
| 76 | table.insert(dir_stack, directory) | 77 | table.insert(dir_stack, directory) |
| @@ -82,12 +83,12 @@ end | |||
| 82 | --- Change directory to root. | 83 | --- Change directory to root. |
| 83 | -- Allows leaving a directory (e.g. for deleting it) in | 84 | -- Allows leaving a directory (e.g. for deleting it) in |
| 84 | -- a crossplatform way. | 85 | -- a crossplatform way. |
| 85 | function change_dir_to_root() | 86 | function tools.change_dir_to_root() |
| 86 | table.insert(dir_stack, "/") | 87 | table.insert(dir_stack, "/") |
| 87 | end | 88 | end |
| 88 | 89 | ||
| 89 | --- Change working directory to the previous in the directory stack. | 90 | --- Change working directory to the previous in the directory stack. |
| 90 | function pop_dir() | 91 | function tools.pop_dir() |
| 91 | local directory = table.remove(dir_stack) | 92 | local directory = table.remove(dir_stack) |
| 92 | return directory ~= nil | 93 | return directory ~= nil |
| 93 | end | 94 | end |
| @@ -97,7 +98,7 @@ end | |||
| 97 | -- too, they are created as well. | 98 | -- too, they are created as well. |
| 98 | -- @param directory string: pathname of directory to create. | 99 | -- @param directory string: pathname of directory to create. |
| 99 | -- @return boolean: true on success, false on failure. | 100 | -- @return boolean: true on success, false on failure. |
| 100 | function make_dir(directory) | 101 | function tools.make_dir(directory) |
| 101 | assert(directory) | 102 | assert(directory) |
| 102 | directory = dir.normalize(directory) | 103 | directory = dir.normalize(directory) |
| 103 | fs.execute_quiet(fs.Q(vars.MKDIR).." -p ", directory) | 104 | fs.execute_quiet(fs.Q(vars.MKDIR).." -p ", directory) |
| @@ -111,7 +112,7 @@ end | |||
| 111 | -- Does not return errors (for example, if directory is not empty or | 112 | -- Does not return errors (for example, if directory is not empty or |
| 112 | -- if already does not exist) | 113 | -- if already does not exist) |
| 113 | -- @param directory string: pathname of directory to remove. | 114 | -- @param directory string: pathname of directory to remove. |
| 114 | function remove_dir_if_empty(directory) | 115 | function tools.remove_dir_if_empty(directory) |
| 115 | assert(directory) | 116 | assert(directory) |
| 116 | fs.execute_quiet(fs.Q(vars.RMDIR), directory) | 117 | fs.execute_quiet(fs.Q(vars.RMDIR), directory) |
| 117 | end | 118 | end |
| @@ -120,7 +121,7 @@ end | |||
| 120 | -- Does not return errors (for example, if directory is not empty or | 121 | -- Does not return errors (for example, if directory is not empty or |
| 121 | -- if already does not exist) | 122 | -- if already does not exist) |
| 122 | -- @param directory string: pathname of directory to remove. | 123 | -- @param directory string: pathname of directory to remove. |
| 123 | function remove_dir_tree_if_empty(directory) | 124 | function tools.remove_dir_tree_if_empty(directory) |
| 124 | assert(directory) | 125 | assert(directory) |
| 125 | fs.execute_quiet(fs.Q(vars.RMDIR), directory) | 126 | fs.execute_quiet(fs.Q(vars.RMDIR), directory) |
| 126 | end | 127 | end |
| @@ -130,7 +131,7 @@ end | |||
| 130 | -- @param dest string: Pathname of destination | 131 | -- @param dest string: Pathname of destination |
| 131 | -- @return boolean or (boolean, string): true on success, false on failure, | 132 | -- @return boolean or (boolean, string): true on success, false on failure, |
| 132 | -- plus an error message. | 133 | -- plus an error message. |
| 133 | function copy(src, dest) | 134 | function tools.copy(src, dest) |
| 134 | assert(src and dest) | 135 | assert(src and dest) |
| 135 | if dest:match("[/\\]$") then dest = dest:sub(1, -2) end | 136 | if dest:match("[/\\]$") then dest = dest:sub(1, -2) end |
| 136 | local ok = fs.execute(fs.Q(vars.CP), src, dest) | 137 | local ok = fs.execute(fs.Q(vars.CP), src, dest) |
| @@ -146,7 +147,7 @@ end | |||
| 146 | -- @param dest string: Pathname of destination | 147 | -- @param dest string: Pathname of destination |
| 147 | -- @return boolean or (boolean, string): true on success, false on failure, | 148 | -- @return boolean or (boolean, string): true on success, false on failure, |
| 148 | -- plus an error message. | 149 | -- plus an error message. |
| 149 | function copy_contents(src, dest) | 150 | function tools.copy_contents(src, dest) |
| 150 | assert(src and dest) | 151 | assert(src and dest) |
| 151 | if fs.execute_quiet(fs.Q(vars.CP).." -dR "..src.."\\*.* "..fs.Q(dest)) then | 152 | if fs.execute_quiet(fs.Q(vars.CP).." -dR "..src.."\\*.* "..fs.Q(dest)) then |
| 152 | return true | 153 | return true |
| @@ -159,7 +160,7 @@ end | |||
| 159 | -- For safety, this only accepts absolute paths. | 160 | -- For safety, this only accepts absolute paths. |
| 160 | -- @param arg string: Pathname of source | 161 | -- @param arg string: Pathname of source |
| 161 | -- @return nil | 162 | -- @return nil |
| 162 | function delete(arg) | 163 | function tools.delete(arg) |
| 163 | assert(arg) | 164 | assert(arg) |
| 164 | assert(arg:match("^[a-zA-Z]?:?[\\/]")) | 165 | assert(arg:match("^[a-zA-Z]?:?[\\/]")) |
| 165 | fs.execute_quiet("if exist "..fs.Q(arg.."\\").." ( RMDIR /S /Q "..fs.Q(arg).." ) else ( DEL /Q /F "..fs.Q(arg).." )") | 166 | 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 | |||
| 169 | -- Yields a filename on each iteration. | 170 | -- Yields a filename on each iteration. |
| 170 | -- @param at string: directory to list | 171 | -- @param at string: directory to list |
| 171 | -- @return nil | 172 | -- @return nil |
| 172 | function dir_iterator(at) | 173 | function tools.dir_iterator(at) |
| 173 | local pipe = io.popen(command_at(at, fs.Q(vars.LS))) | 174 | local pipe = io.popen(command_at(at, fs.Q(vars.LS))) |
| 174 | for file in pipe:lines() do | 175 | for file in pipe:lines() do |
| 175 | if file ~= "." and file ~= ".." then | 176 | if file ~= "." and file ~= ".." then |
| @@ -184,7 +185,7 @@ end | |||
| 184 | -- directory if none is given). | 185 | -- directory if none is given). |
| 185 | -- @return table: an array of strings with the filenames representing | 186 | -- @return table: an array of strings with the filenames representing |
| 186 | -- the contents of a directory. Paths are returned with forward slashes. | 187 | -- the contents of a directory. Paths are returned with forward slashes. |
| 187 | function find(at) | 188 | function tools.find(at) |
| 188 | assert(type(at) == "string" or not at) | 189 | assert(type(at) == "string" or not at) |
| 189 | if not at then | 190 | if not at then |
| 190 | at = fs.current_dir() | 191 | at = fs.current_dir() |
| @@ -211,14 +212,14 @@ end | |||
| 211 | -- @param ... Filenames to be stored in the archive are given as | 212 | -- @param ... Filenames to be stored in the archive are given as |
| 212 | -- additional arguments. | 213 | -- additional arguments. |
| 213 | -- @return boolean: true on success, false on failure. | 214 | -- @return boolean: true on success, false on failure. |
| 214 | function zip(zipfile, ...) | 215 | function tools.zip(zipfile, ...) |
| 215 | return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa a -tzip", zipfile, ...) | 216 | return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa a -tzip", zipfile, ...) |
| 216 | end | 217 | end |
| 217 | 218 | ||
| 218 | --- Uncompress files from a .zip archive. | 219 | --- Uncompress files from a .zip archive. |
| 219 | -- @param zipfile string: pathname of .zip archive to be extracted. | 220 | -- @param zipfile string: pathname of .zip archive to be extracted. |
| 220 | -- @return boolean: true on success, false on failure. | 221 | -- @return boolean: true on success, false on failure. |
| 221 | function unzip(zipfile) | 222 | function tools.unzip(zipfile) |
| 222 | assert(zipfile) | 223 | assert(zipfile) |
| 223 | return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa x", zipfile) | 224 | return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa x", zipfile) |
| 224 | end | 225 | end |
| @@ -226,7 +227,7 @@ end | |||
| 226 | --- Test is pathname is a directory. | 227 | --- Test is pathname is a directory. |
| 227 | -- @param file string: pathname to test | 228 | -- @param file string: pathname to test |
| 228 | -- @return boolean: true if it is a directory, false otherwise. | 229 | -- @return boolean: true if it is a directory, false otherwise. |
| 229 | function is_dir(file) | 230 | function tools.is_dir(file) |
| 230 | assert(file) | 231 | assert(file) |
| 231 | return fs.execute_quiet("if not exist " .. fs.Q(file.."\\").." invalidcommandname") | 232 | return fs.execute_quiet("if not exist " .. fs.Q(file.."\\").." invalidcommandname") |
| 232 | end | 233 | end |
| @@ -234,7 +235,7 @@ end | |||
| 234 | --- Test is pathname is a regular file. | 235 | --- Test is pathname is a regular file. |
| 235 | -- @param file string: pathname to test | 236 | -- @param file string: pathname to test |
| 236 | -- @return boolean: true if it is a regular file, false otherwise. | 237 | -- @return boolean: true if it is a regular file, false otherwise. |
| 237 | function is_file(file) | 238 | function tools.is_file(file) |
| 238 | assert(file) | 239 | assert(file) |
| 239 | return fs.execute(fs.Q(vars.TEST).." -f", file) | 240 | return fs.execute(fs.Q(vars.TEST).." -f", file) |
| 240 | end | 241 | end |
| @@ -247,7 +248,7 @@ end | |||
| 247 | -- filename can be given explicitly as this second argument. | 248 | -- filename can be given explicitly as this second argument. |
| 248 | -- @return (boolean, string): true and the filename on success, | 249 | -- @return (boolean, string): true and the filename on success, |
| 249 | -- false and the error message on failure. | 250 | -- false and the error message on failure. |
| 250 | function download(url, filename, cache) | 251 | function tools.download(url, filename, cache) |
| 251 | assert(type(url) == "string") | 252 | assert(type(url) == "string") |
| 252 | assert(type(filename) == "string" or not filename) | 253 | assert(type(filename) == "string" or not filename) |
| 253 | 254 | ||
| @@ -289,7 +290,7 @@ end | |||
| 289 | -- filename extension. | 290 | -- filename extension. |
| 290 | -- @param archive string: Filename of archive. | 291 | -- @param archive string: Filename of archive. |
| 291 | -- @return boolean or (boolean, string): true on success, false and an error message on failure. | 292 | -- @return boolean or (boolean, string): true on success, false and an error message on failure. |
| 292 | function unpack_archive(archive) | 293 | function tools.unpack_archive(archive) |
| 293 | assert(type(archive) == "string") | 294 | assert(type(archive) == "string") |
| 294 | 295 | ||
| 295 | local ok | 296 | local ok |
| @@ -333,7 +334,7 @@ local md5_cmd = { | |||
| 333 | --- Get the MD5 checksum for a file. | 334 | --- Get the MD5 checksum for a file. |
| 334 | -- @param file string: The file to be computed. | 335 | -- @param file string: The file to be computed. |
| 335 | -- @return string: The MD5 checksum or nil + message | 336 | -- @return string: The MD5 checksum or nil + message |
| 336 | function get_md5(file) | 337 | function tools.get_md5(file) |
| 337 | local cmd = md5_cmd[cfg.md5checker] | 338 | local cmd = md5_cmd[cfg.md5checker] |
| 338 | if not cmd then return nil, "no MD5 checker command configured" end | 339 | if not cmd then return nil, "no MD5 checker command configured" end |
| 339 | local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) | 340 | local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) |
| @@ -349,11 +350,13 @@ end | |||
| 349 | --- Test for existance of a file. | 350 | --- Test for existance of a file. |
| 350 | -- @param file string: filename to test | 351 | -- @param file string: filename to test |
| 351 | -- @return boolean: true if file exists, false otherwise. | 352 | -- @return boolean: true if file exists, false otherwise. |
| 352 | function exists(file) | 353 | function tools.exists(file) |
| 353 | assert(file) | 354 | assert(file) |
| 354 | return fs.execute_quiet("if not exist " .. fs.Q(file) .. " invalidcommandname") | 355 | return fs.execute_quiet("if not exist " .. fs.Q(file) .. " invalidcommandname") |
| 355 | end | 356 | end |
| 356 | 357 | ||
| 357 | function browser(url) | 358 | function tools.browser(url) |
| 358 | return fs.execute(cfg.web_browser..' "Starting docs..." '..fs.Q(url)) | 359 | return fs.execute(cfg.web_browser..' "Starting docs..." '..fs.Q(url)) |
| 359 | end | 360 | end |
| 361 | |||
| 362 | return tools | ||
