diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2012-09-03 21:07:02 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2012-09-03 21:07:02 -0300 |
commit | a12de9fafa2d585621670d43216bd14626cb7c07 (patch) | |
tree | d80a7e206b386d2f7f907a3f07e4ed28de533997 | |
parent | a424a928396058995f0a5141cc7fd879e7fcb0d7 (diff) | |
download | luarocks-a12de9fafa2d585621670d43216bd14626cb7c07.tar.gz luarocks-a12de9fafa2d585621670d43216bd14626cb7c07.tar.bz2 luarocks-a12de9fafa2d585621670d43216bd14626cb7c07.zip |
Cleanup and reduce diff between unix.tools and win32.tools
-rw-r--r-- | src/luarocks/fs/unix/tools.lua | 62 | ||||
-rw-r--r-- | 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 = {} | |||
10 | 10 | ||
11 | local vars = cfg.variables | 11 | local vars = cfg.variables |
12 | 12 | ||
13 | --- Run the given command. | 13 | local function command_at(directory, cmd) |
14 | -- The command is executed in the current directory in the directory stack. | 14 | return "cd " .. fs.Q(directory) .. " && " .. cmd |
15 | -- @param cmd string: No quoting/escaping is applied to the command. | 15 | end |
16 | -- @return boolean: true if command succeeds (status code 0), false | 16 | |
17 | -- otherwise. | 17 | --- Annotate command string for quiet execution. |
18 | function execute_string(cmd) | 18 | -- @param cmd string: A command-line string. |
19 | local actual_cmd = "cd " .. fs.Q(fs.current_dir()) .. " && " .. cmd | 19 | -- @return string: The command-line, with silencing annotation. |
20 | local code = os.execute(actual_cmd) | 20 | function quiet(cmd) |
21 | if code == 0 or code == true then | 21 | return cmd.." 1> /dev/null 2> /dev/null" |
22 | return true | ||
23 | else | ||
24 | return false | ||
25 | end | ||
26 | end | 22 | end |
27 | 23 | ||
28 | --- Obtain current directory. | 24 | --- Obtain current directory. |
@@ -38,6 +34,20 @@ function current_dir() | |||
38 | return current | 34 | return current |
39 | end | 35 | end |
40 | 36 | ||
37 | --- Run the given command. | ||
38 | -- The command is executed in the current directory in the directory stack. | ||
39 | -- @param cmd string: No quoting/escaping is applied to the command. | ||
40 | -- @return boolean: true if command succeeds (status code 0), false | ||
41 | -- otherwise. | ||
42 | function execute_string(cmd) | ||
43 | local code = os.execute(command_at(fs.current_dir(), cmd)) | ||
44 | if code == 0 or code == true then | ||
45 | return true | ||
46 | else | ||
47 | return false | ||
48 | end | ||
49 | end | ||
50 | |||
41 | --- Change the current directory. | 51 | --- Change the current directory. |
42 | -- Uses the module's internal directory stack. This does not have exact | 52 | -- Uses the module's internal directory stack. This does not have exact |
43 | -- semantics of chdir, as it does not handle errors the same way, | 53 | -- semantics of chdir, as it does not handle errors the same way, |
@@ -77,7 +87,7 @@ end | |||
77 | -- @param directory string: pathname of directory to remove. | 87 | -- @param directory string: pathname of directory to remove. |
78 | function remove_dir_if_empty(directory) | 88 | function remove_dir_if_empty(directory) |
79 | assert(directory) | 89 | assert(directory) |
80 | fs.execute_string(vars.RMDIR.." "..fs.Q(directory).." 1> /dev/null 2> /dev/null") | 90 | fs.execute_string(fs.quiet(vars.RMDIR.." "..fs.Q(directory))) |
81 | end | 91 | end |
82 | 92 | ||
83 | --- Remove a directory if it is empty. | 93 | --- Remove a directory if it is empty. |
@@ -86,7 +96,7 @@ end | |||
86 | -- @param directory string: pathname of directory to remove. | 96 | -- @param directory string: pathname of directory to remove. |
87 | function remove_dir_tree_if_empty(directory) | 97 | function remove_dir_tree_if_empty(directory) |
88 | assert(directory) | 98 | assert(directory) |
89 | fs.execute_string(vars.RMDIR.." -p "..fs.Q(directory).." 1> /dev/null 2> /dev/null") | 99 | fs.execute_string(fs.quiet(vars.RMDIR.." -p "..fs.Q(directory))) |
90 | end | 100 | end |
91 | 101 | ||
92 | --- Copy a file. | 102 | --- Copy a file. |
@@ -121,7 +131,7 @@ end | |||
121 | -- plus an error message. | 131 | -- plus an error message. |
122 | function copy_contents(src, dest) | 132 | function copy_contents(src, dest) |
123 | assert(src and dest) | 133 | assert(src and dest) |
124 | if fs.execute_string(vars.CP.." -pPR "..fs.Q(src).."/* "..fs.Q(dest).." 1> /dev/null 2>/dev/null") then | 134 | if fs.execute_string(fs.quiet(vars.CP.." -pPR "..fs.Q(src).."/* "..fs.Q(dest))) then |
125 | return true | 135 | return true |
126 | else | 136 | else |
127 | return false, "Failed copying "..src.." to "..dest | 137 | return false, "Failed copying "..src.." to "..dest |
@@ -134,10 +144,10 @@ end | |||
134 | function delete(arg) | 144 | function delete(arg) |
135 | assert(arg) | 145 | assert(arg) |
136 | assert(arg:sub(1,1) == "/") | 146 | assert(arg:sub(1,1) == "/") |
137 | return fs.execute_string(vars.RM.." -rf " .. fs.Q(arg) .. " 1> /dev/null 2>/dev/null") | 147 | return fs.execute_string(fs.quiet(vars.RM.." -rf " .. fs.Q(arg))) |
138 | end | 148 | end |
139 | 149 | ||
140 | --- List the contents of a directory. | 150 | --- List the contents of a directory. |
141 | -- @param at string or nil: directory to list (will be the current | 151 | -- @param at string or nil: directory to list (will be the current |
142 | -- directory if none is given). | 152 | -- directory if none is given). |
143 | -- @return table: an array of strings with the filenames representing | 153 | -- @return table: an array of strings with the filenames representing |
@@ -151,7 +161,7 @@ function list_dir(at) | |||
151 | return {} | 161 | return {} |
152 | end | 162 | end |
153 | local result = {} | 163 | local result = {} |
154 | local pipe = io.popen("cd "..fs.Q(at).." && "..vars.LS) | 164 | local pipe = io.popen(command_at(at, vars.LS)) |
155 | for file in pipe:lines() do | 165 | for file in pipe:lines() do |
156 | table.insert(result, file) | 166 | table.insert(result, file) |
157 | end | 167 | end |
@@ -159,7 +169,7 @@ function list_dir(at) | |||
159 | return result | 169 | return result |
160 | end | 170 | end |
161 | 171 | ||
162 | --- Recursively scan the contents of a directory. | 172 | --- Recursively scan the contents of a directory. |
163 | -- @param at string or nil: directory to scan (will be the current | 173 | -- @param at string or nil: directory to scan (will be the current |
164 | -- directory if none is given). | 174 | -- directory if none is given). |
165 | -- @return table: an array of strings with the filenames representing | 175 | -- @return table: an array of strings with the filenames representing |
@@ -173,7 +183,7 @@ function find(at) | |||
173 | return {} | 183 | return {} |
174 | end | 184 | end |
175 | local result = {} | 185 | local result = {} |
176 | local pipe = io.popen("cd "..fs.Q(at).." && "..vars.FIND.." * 2>/dev/null") | 186 | local pipe = io.popen(command_at(at, vars.FIND.." * 2>/dev/null")) |
177 | for file in pipe:lines() do | 187 | for file in pipe:lines() do |
178 | table.insert(result, file) | 188 | table.insert(result, file) |
179 | end | 189 | end |
@@ -198,14 +208,6 @@ function unzip(zipfile) | |||
198 | return fs.execute(vars.UNZIP, zipfile) | 208 | return fs.execute(vars.UNZIP, zipfile) |
199 | end | 209 | end |
200 | 210 | ||
201 | --- Test for existance of a file. | ||
202 | -- @param file string: filename to test | ||
203 | -- @return boolean: true if file exists, false otherwise. | ||
204 | function exists(file) | ||
205 | assert(file) | ||
206 | return fs.execute(vars.TEST, "-r", file) | ||
207 | end | ||
208 | |||
209 | --- Test is file/directory is writable. | 211 | --- Test is file/directory is writable. |
210 | -- @param file string: filename to test | 212 | -- @param file string: filename to test |
211 | -- @return boolean: true if file exists, false otherwise. | 213 | -- @return boolean: true if file exists, false otherwise. |
@@ -278,10 +280,8 @@ function unpack_archive(archive) | |||
278 | 280 | ||
279 | local ok | 281 | local ok |
280 | if archive:match("%.tar%.gz$") or archive:match("%.tgz$") then | 282 | if archive:match("%.tar%.gz$") or archive:match("%.tgz$") then |
281 | -- ok = fs.execute("tar zxvpf ", archive) | ||
282 | ok = fs.execute_string(vars.GUNZIP.." -c "..archive.."|"..vars.TAR.." -xf -") | 283 | ok = fs.execute_string(vars.GUNZIP.." -c "..archive.."|"..vars.TAR.." -xf -") |
283 | elseif archive:match("%.tar%.bz2$") then | 284 | elseif archive:match("%.tar%.bz2$") then |
284 | -- ok = fs.execute("tar jxvpf ", archive) | ||
285 | ok = fs.execute_string(vars.BUNZIP2.." -c "..archive.."|tar -xf -") | 285 | ok = fs.execute_string(vars.BUNZIP2.." -c "..archive.."|tar -xf -") |
286 | elseif archive:match("%.zip$") then | 286 | elseif archive:match("%.zip$") then |
287 | ok = fs.execute(vars.UNZIP, archive) | 287 | 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 @@ | |||
5 | module("luarocks.fs.win32.tools", package.seeall) | 5 | module("luarocks.fs.win32.tools", package.seeall) |
6 | 6 | ||
7 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
8 | local cfg = require("luarocks.cfg") | ||
9 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
9 | local cfg = require("luarocks.cfg") | ||
10 | 10 | ||
11 | local dir_stack = {} | 11 | local dir_stack = {} |
12 | 12 | ||
@@ -32,13 +32,11 @@ local function command_at(directory, cmd) | |||
32 | return cmd | 32 | return cmd |
33 | end | 33 | end |
34 | 34 | ||
35 | --- Test for existance of a file. | 35 | --- Annotate command string for quiet execution. |
36 | -- @param file string: filename to test | 36 | -- @param cmd string: A command-line string. |
37 | -- @return boolean: true if file exists, false otherwise. | 37 | -- @return string: The command-line, with silencing annotation. |
38 | function exists(file) | 38 | function quiet(cmd) |
39 | assert(file) | 39 | return cmd.." 2> NUL 1> NUL" |
40 | return fs.execute("if not exist " .. fs.Q(file) .. | ||
41 | " invalidcommandname 2>NUL 1>NUL") | ||
42 | end | 40 | end |
43 | 41 | ||
44 | --- Obtain current directory. | 42 | --- Obtain current directory. |
@@ -54,31 +52,18 @@ function current_dir() | |||
54 | return current | 52 | return current |
55 | end | 53 | end |
56 | 54 | ||
57 | --- Test is pathname is a regular file. | 55 | --- Run the given command. |
58 | -- @param file string: pathname to test | 56 | -- The command is executed in the current directory in the directory stack. |
59 | -- @return boolean: true if it is a regular file, false otherwise. | 57 | -- @param cmd string: No quoting/escaping is applied to the command. |
60 | function is_file(file) | 58 | -- @return boolean: true if command succeeds (status code 0), false |
61 | assert(file) | 59 | -- otherwise. |
62 | return fs.execute(vars.TEST.." -f", file) | 60 | function execute_string(cmd) |
63 | end | 61 | local code = os.execute(command_at(fs.current_dir(), cmd)) |
64 | 62 | if code == 0 or code == true then | |
65 | local md5_cmd = { | 63 | return true |
66 | md5sum = vars.MD5SUM, | 64 | else |
67 | openssl = vars.OPENSSL.." md5", | 65 | return false |
68 | md5 = vars.MD5, | 66 | end |
69 | } | ||
70 | |||
71 | --- Get the MD5 checksum for a file. | ||
72 | -- @param file string: The file to be computed. | ||
73 | -- @return string: The MD5 checksum | ||
74 | function get_md5(file) | ||
75 | local cmd = md5_cmd[cfg.md5checker] | ||
76 | if not cmd then return nil end | ||
77 | local pipe = io.popen(cmd.." "..fs.absolute_name(file)) | ||
78 | local computed = pipe:read("*a") | ||
79 | pipe:close() | ||
80 | if not computed then return nil end | ||
81 | return computed:match("("..("%x"):rep(32)..")") | ||
82 | end | 67 | end |
83 | 68 | ||
84 | --- Change the current directory. | 69 | --- Change the current directory. |
@@ -104,28 +89,6 @@ function pop_dir() | |||
104 | return directory ~= nil | 89 | return directory ~= nil |
105 | end | 90 | end |
106 | 91 | ||
107 | --- Run the given command. | ||
108 | -- The command is executed in the current directory in the directory stack. | ||
109 | -- @param cmd string: No quoting/escaping is applied to the command. | ||
110 | -- @return boolean: true if command succeeds (status code 0), false | ||
111 | -- otherwise. | ||
112 | function execute_string(cmd) | ||
113 | local code = os.execute(command_at(fs.current_dir(), cmd)) | ||
114 | if code == 0 or code == true then | ||
115 | return true | ||
116 | else | ||
117 | return false | ||
118 | end | ||
119 | end | ||
120 | |||
121 | --- Test is pathname is a regular file. | ||
122 | -- @param file string: pathname to test | ||
123 | -- @return boolean: true if it is a regular file, false otherwise. | ||
124 | function is_dir(file) | ||
125 | assert(file) | ||
126 | return fs.execute(vars.TEST.." -d " .. fs.Q(file) .. " 2>NUL 1>NUL") | ||
127 | end | ||
128 | |||
129 | --- Create a directory if it does not already exist. | 92 | --- Create a directory if it does not already exist. |
130 | -- If any of the higher levels in the path name does not exist | 93 | -- If any of the higher levels in the path name does not exist |
131 | -- too, they are created as well. | 94 | -- too, they are created as well. |
@@ -133,7 +96,7 @@ end | |||
133 | -- @return boolean: true on success, false on failure. | 96 | -- @return boolean: true on success, false on failure. |
134 | function make_dir(directory) | 97 | function make_dir(directory) |
135 | assert(directory) | 98 | assert(directory) |
136 | fs.execute(vars.MKDIR.." "..fs.Q(directory).." 1> NUL 2> NUL") | 99 | fs.execute(fs.quiet(vars.MKDIR.." "..fs.Q(directory))) |
137 | return 1 | 100 | return 1 |
138 | end | 101 | end |
139 | 102 | ||
@@ -143,7 +106,7 @@ end | |||
143 | -- @param directory string: pathname of directory to remove. | 106 | -- @param directory string: pathname of directory to remove. |
144 | function remove_dir_if_empty(directory) | 107 | function remove_dir_if_empty(directory) |
145 | assert(directory) | 108 | assert(directory) |
146 | fs.execute_string(vars.RMDIR.." "..fs.Q(directory).." 1> NUL 2> NUL") | 109 | fs.execute_string(fs.quiet(vars.RMDIR.." "..fs.Q(directory))) |
147 | end | 110 | end |
148 | 111 | ||
149 | --- Remove a directory if it is empty. | 112 | --- Remove a directory if it is empty. |
@@ -152,7 +115,7 @@ end | |||
152 | -- @param directory string: pathname of directory to remove. | 115 | -- @param directory string: pathname of directory to remove. |
153 | function remove_dir_tree_if_empty(directory) | 116 | function remove_dir_tree_if_empty(directory) |
154 | assert(directory) | 117 | assert(directory) |
155 | fs.execute_string(vars.RMDIR.." "..fs.Q(directory).." 1> NUL 2> NUL") | 118 | fs.execute_string(fs.quiet(vars.RMDIR.." "..fs.Q(directory))) |
156 | end | 119 | end |
157 | 120 | ||
158 | --- Copy a file. | 121 | --- Copy a file. |
@@ -177,7 +140,7 @@ end | |||
177 | -- plus an error message. | 140 | -- plus an error message. |
178 | function copy_contents(src, dest) | 141 | function copy_contents(src, dest) |
179 | assert(src and dest) | 142 | assert(src and dest) |
180 | if fs.execute_string(vars.CP.." -a "..src.."\\*.* "..fs.Q(dest).." 1> NUL 2> NUL") then | 143 | if fs.execute_string(fs.quiet(vars.CP.." -a "..src.."\\*.* "..fs.Q(dest))) then |
181 | return true | 144 | return true |
182 | else | 145 | else |
183 | return false, "Failed copying "..src.." to "..dest | 146 | return false, "Failed copying "..src.." to "..dest |
@@ -192,7 +155,7 @@ function delete(arg) | |||
192 | assert(arg) | 155 | assert(arg) |
193 | assert(arg:match("^[\a-zA-Z]?:?[\\/]")) | 156 | assert(arg:match("^[\a-zA-Z]?:?[\\/]")) |
194 | fs.execute(vars.CHMOD.." a+rw -R ", arg) | 157 | fs.execute(vars.CHMOD.." a+rw -R ", arg) |
195 | return fs.execute_string(vars.RM.." -rf " .. fs.Q(arg) .. " 1> NUL 2> NUL") | 158 | return fs.execute_string(fs.quiet(vars.RM.." -rf " .. fs.Q(arg))) |
196 | end | 159 | end |
197 | 160 | ||
198 | --- List the contents of a directory. | 161 | --- List the contents of a directory. |
@@ -245,6 +208,39 @@ function find(at) | |||
245 | return result | 208 | return result |
246 | end | 209 | end |
247 | 210 | ||
211 | --- Compress files in a .zip archive. | ||
212 | -- @param zipfile string: pathname of .zip archive to be created. | ||
213 | -- @param ... Filenames to be stored in the archive are given as | ||
214 | -- additional arguments. | ||
215 | -- @return boolean: true on success, false on failure. | ||
216 | function zip(zipfile, ...) | ||
217 | return fs.execute(vars.SEVENZ.." a -tzip", zipfile, ...) | ||
218 | end | ||
219 | |||
220 | --- Uncompress files from a .zip archive. | ||
221 | -- @param zipfile string: pathname of .zip archive to be extracted. | ||
222 | -- @return boolean: true on success, false on failure. | ||
223 | function unzip(zipfile) | ||
224 | assert(zipfile) | ||
225 | return fs.execute(vars.SEVENZ.." x", zipfile) | ||
226 | end | ||
227 | |||
228 | --- Test is pathname is a directory. | ||
229 | -- @param file string: pathname to test | ||
230 | -- @return boolean: true if it is a directory, false otherwise. | ||
231 | function is_dir(file) | ||
232 | assert(file) | ||
233 | return fs.execute(fs.quiet(vars.TEST.." -d " .. fs.Q(file))) | ||
234 | end | ||
235 | |||
236 | --- Test is pathname is a regular file. | ||
237 | -- @param file string: pathname to test | ||
238 | -- @return boolean: true if it is a regular file, false otherwise. | ||
239 | function is_file(file) | ||
240 | assert(file) | ||
241 | return fs.execute(vars.TEST.." -f", file) | ||
242 | end | ||
243 | |||
248 | --- Download a remote file. | 244 | --- Download a remote file. |
249 | -- @param url string: URL to be fetched. | 245 | -- @param url string: URL to be fetched. |
250 | -- @param filename string or nil: this function attempts to detect the | 246 | -- @param filename string or nil: this function attempts to detect the |
@@ -269,24 +265,6 @@ function download(url, filename) | |||
269 | end | 265 | end |
270 | end | 266 | end |
271 | 267 | ||
272 | |||
273 | --- Compress files in a .zip archive. | ||
274 | -- @param zipfile string: pathname of .zip archive to be created. | ||
275 | -- @param ... Filenames to be stored in the archive are given as | ||
276 | -- additional arguments. | ||
277 | -- @return boolean: true on success, false on failure. | ||
278 | function zip(zipfile, ...) | ||
279 | return fs.execute(vars.SEVENZ.." a -tzip", zipfile, ...) | ||
280 | end | ||
281 | |||
282 | --- Uncompress files from a .zip archive. | ||
283 | -- @param zipfile string: pathname of .zip archive to be extracted. | ||
284 | -- @return boolean: true on success, false on failure. | ||
285 | function unzip(zipfile) | ||
286 | assert(zipfile) | ||
287 | return fs.execute(vars.SEVENZ.." x", zipfile) | ||
288 | end | ||
289 | |||
290 | --- Uncompress gzip file. | 268 | --- Uncompress gzip file. |
291 | -- @param archive string: Filename of archive. | 269 | -- @param archive string: Filename of archive. |
292 | -- @return boolean : success status | 270 | -- @return boolean : success status |
@@ -333,3 +311,30 @@ function unpack_archive(archive) | |||
333 | end | 311 | end |
334 | return true | 312 | return true |
335 | end | 313 | end |
314 | |||
315 | local md5_cmd = { | ||
316 | md5sum = vars.MD5SUM, | ||
317 | openssl = vars.OPENSSL.." md5", | ||
318 | md5 = vars.MD5, | ||
319 | } | ||
320 | |||
321 | --- Get the MD5 checksum for a file. | ||
322 | -- @param file string: The file to be computed. | ||
323 | -- @return string: The MD5 checksum | ||
324 | function get_md5(file) | ||
325 | local cmd = md5_cmd[cfg.md5checker] | ||
326 | if not cmd then return nil end | ||
327 | local pipe = io.popen(cmd.." "..fs.absolute_name(file)) | ||
328 | local computed = pipe:read("*a") | ||
329 | pipe:close() | ||
330 | if not computed then return nil end | ||
331 | return computed:match("("..("%x"):rep(32)..")") | ||
332 | end | ||
333 | |||
334 | --- Test for existance of a file. | ||
335 | -- @param file string: filename to test | ||
336 | -- @return boolean: true if file exists, false otherwise. | ||
337 | function exists(file) | ||
338 | assert(file) | ||
339 | return fs.execute(fs.quiet("if not exist " .. fs.Q(file) .. " invalidcommandname")) | ||
340 | end | ||