aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2012-09-03 21:07:02 -0300
committerHisham Muhammad <hisham@gobolinux.org>2012-09-03 21:07:02 -0300
commita12de9fafa2d585621670d43216bd14626cb7c07 (patch)
treed80a7e206b386d2f7f907a3f07e4ed28de533997
parenta424a928396058995f0a5141cc7fd879e7fcb0d7 (diff)
downloadluarocks-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.lua62
-rw-r--r--src/luarocks/fs/win32/tools.lua161
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
11local vars = cfg.variables 11local vars = cfg.variables
12 12
13--- Run the given command. 13local 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. 15end
16-- @return boolean: true if command succeeds (status code 0), false 16
17-- otherwise. 17--- Annotate command string for quiet execution.
18function 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) 20function 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
26end 22end
27 23
28--- Obtain current directory. 24--- Obtain current directory.
@@ -38,6 +34,20 @@ function current_dir()
38 return current 34 return current
39end 35end
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.
42function 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
49end
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.
78function remove_dir_if_empty(directory) 88function 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)))
81end 91end
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.
87function remove_dir_tree_if_empty(directory) 97function 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)))
90end 100end
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.
122function copy_contents(src, dest) 132function 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
134function delete(arg) 144function 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)))
138end 148end
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
160end 170end
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)
199end 209end
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.
204function exists(file)
205 assert(file)
206 return fs.execute(vars.TEST, "-r", file)
207end
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 @@
5module("luarocks.fs.win32.tools", package.seeall) 5module("luarocks.fs.win32.tools", package.seeall)
6 6
7local fs = require("luarocks.fs") 7local fs = require("luarocks.fs")
8local cfg = require("luarocks.cfg")
9local dir = require("luarocks.dir") 8local dir = require("luarocks.dir")
9local cfg = require("luarocks.cfg")
10 10
11local dir_stack = {} 11local dir_stack = {}
12 12
@@ -32,13 +32,11 @@ local function command_at(directory, cmd)
32 return cmd 32 return cmd
33end 33end
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.
38function exists(file) 38function 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")
42end 40end
43 41
44--- Obtain current directory. 42--- Obtain current directory.
@@ -54,31 +52,18 @@ function current_dir()
54 return current 52 return current
55end 53end
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.
60function 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) 60function execute_string(cmd)
63end 61 local code = os.execute(command_at(fs.current_dir(), cmd))
64 62 if code == 0 or code == true then
65local 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
74function 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)..")")
82end 67end
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
105end 90end
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.
112function 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
119end
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.
124function is_dir(file)
125 assert(file)
126 return fs.execute(vars.TEST.." -d " .. fs.Q(file) .. " 2>NUL 1>NUL")
127end
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.
134function make_dir(directory) 97function 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
138end 101end
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.
144function remove_dir_if_empty(directory) 107function 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)))
147end 110end
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.
153function remove_dir_tree_if_empty(directory) 116function 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)))
156end 119end
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.
178function copy_contents(src, dest) 141function 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)))
196end 159end
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
246end 209end
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.
216function zip(zipfile, ...)
217 return fs.execute(vars.SEVENZ.." a -tzip", zipfile, ...)
218end
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.
223function unzip(zipfile)
224 assert(zipfile)
225 return fs.execute(vars.SEVENZ.." x", zipfile)
226end
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.
231function is_dir(file)
232 assert(file)
233 return fs.execute(fs.quiet(vars.TEST.." -d " .. fs.Q(file)))
234end
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.
239function is_file(file)
240 assert(file)
241 return fs.execute(vars.TEST.." -f", file)
242end
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
270end 266end
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.
278function zip(zipfile, ...)
279 return fs.execute(vars.SEVENZ.." a -tzip", zipfile, ...)
280end
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.
285function unzip(zipfile)
286 assert(zipfile)
287 return fs.execute(vars.SEVENZ.." x", zipfile)
288end
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
335end 313end
314
315local 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
324function 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)..")")
332end
333
334--- Test for existance of a file.
335-- @param file string: filename to test
336-- @return boolean: true if file exists, false otherwise.
337function exists(file)
338 assert(file)
339 return fs.execute(fs.quiet("if not exist " .. fs.Q(file) .. " invalidcommandname"))
340end