aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Melnichenko <mpeterval@gmail.com>2016-05-07 14:01:31 +0300
committerPeter Melnichenko <mpeterval@gmail.com>2016-05-22 19:50:40 +0300
commit472e74d582055992cf1f5c75821e083131a895ac (patch)
tree7da87bf16660a4d1873ad0e1be36f473cd7e1325
parent26ea986b8322dc3bd1e82cf6d819082c57a69024 (diff)
downloadluarocks-472e74d582055992cf1f5c75821e083131a895ac.tar.gz
luarocks-472e74d582055992cf1f5c75821e083131a895ac.tar.bz2
luarocks-472e74d582055992cf1f5c75821e083131a895ac.zip
Move common implementation of fs.use_downloader into fs.tools module
-rw-r--r--Makefile.setup.inc2
-rw-r--r--src/luarocks/fs.lua5
-rw-r--r--src/luarocks/fs/tools.lua56
-rw-r--r--src/luarocks/fs/unix/tools.lua45
-rw-r--r--src/luarocks/fs/win32/tools.lua45
5 files changed, 61 insertions, 92 deletions
diff --git a/Makefile.setup.inc b/Makefile.setup.inc
index 0a049bc4..092d0423 100644
--- a/Makefile.setup.inc
+++ b/Makefile.setup.inc
@@ -5,7 +5,7 @@ BINDIR ?= $(PREFIX)/bin
5LUADIR ?= $(PREFIX)/share/lua/$(LUA_VERSION)/ 5LUADIR ?= $(PREFIX)/share/lua/$(LUA_VERSION)/
6 6
7BIN_FILES = luarocks luarocks-admin 7BIN_FILES = luarocks luarocks-admin
8LUAROCKS_FILES = fs/unix/tools.lua fs/unix.lua fs/win32/tools.lua fs/win32.lua \ 8LUAROCKS_FILES = fs/tools.lua fs/unix/tools.lua fs/unix.lua fs/win32/tools.lua fs/win32.lua \
9fs/lua.lua persist.lua list.lua require.lua repos.lua dir.lua make_manifest.lua \ 9fs/lua.lua persist.lua list.lua require.lua repos.lua dir.lua make_manifest.lua \
10command_line.lua config_cmd.lua install.lua build/command.lua build/cmake.lua \ 10command_line.lua config_cmd.lua install.lua build/command.lua build/cmake.lua \
11build/make.lua build/builtin.lua fetch/cvs.lua fetch/git.lua fetch/sscm.lua \ 11build/make.lua build/builtin.lua fetch/cvs.lua fetch/git.lua fetch/sscm.lua \
diff --git a/src/luarocks/fs.lua b/src/luarocks/fs.lua
index 57302c7f..f3d86a13 100644
--- a/src/luarocks/fs.lua
+++ b/src/luarocks/fs.lua
@@ -68,7 +68,10 @@ load_fns(fs_lua)
68 68
69-- Load platform-specific fallbacks for missing Lua modules 69-- Load platform-specific fallbacks for missing Lua modules
70local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") 70local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools")
71if ok and fs_plat_tools then load_fns(fs_plat_tools) end 71if ok and fs_plat_tools then
72 load_fns(fs_plat_tools)
73 load_fns(require("luarocks.fs.tools"))
74end
72 75
73 76
74return fs 77return fs
diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua
new file mode 100644
index 00000000..0283d8f7
--- /dev/null
+++ b/src/luarocks/fs/tools.lua
@@ -0,0 +1,56 @@
1
2--- Common fs operations implemented with third-party tools.
3local tools = {}
4
5local fs = require("luarocks.fs")
6local dir = require("luarocks.dir")
7local cfg = require("luarocks.cfg")
8
9local vars = cfg.variables
10
11--- Download a remote file.
12-- @param url string: URL to be fetched.
13-- @param filename string or nil: this function attempts to detect the
14-- resulting local filename of the remote file as the basename of the URL;
15-- if that is not correct (due to a redirection, for example), the local
16-- filename can be given explicitly as this second argument.
17-- @return (boolean, string): true and the filename on success,
18-- false and the error message on failure.
19function tools.use_downloader(url, filename, cache)
20 assert(type(url) == "string")
21 assert(type(filename) == "string" or not filename)
22
23 filename = fs.absolute_name(filename or dir.base_name(url))
24
25 local ok
26 if cfg.downloader == "wget" then
27 local wget_cmd = fs.Q(vars.WGET).." "..vars.WGETNOCERTFLAG.." --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet "
28 if cfg.connection_timeout and cfg.connection_timeout > 0 then
29 wget_cmd = wget_cmd .. "--timeout="..tonumber(cfg.connection_timeout).." --tries=1 "
30 end
31 if cache then
32 -- --timestamping is incompatible with --output-document,
33 -- but that's not a problem for our use cases.
34 fs.change_dir(dir.dir_name(filename))
35 ok = fs.execute_quiet(wget_cmd.." --timestamping ", url)
36 fs.pop_dir()
37 elseif filename then
38 ok = fs.execute_quiet(wget_cmd.." --output-document ", filename, url)
39 else
40 ok = fs.execute_quiet(wget_cmd, url)
41 end
42 elseif cfg.downloader == "curl" then
43 local curl_cmd = fs.Q(vars.CURL).." "..vars.CURLNOCERTFLAG.." -f -L --user-agent \""..cfg.user_agent.." via curl\" "
44 if cfg.connection_timeout and cfg.connection_timeout > 0 then
45 curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." "
46 end
47 ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename)))
48 end
49 if ok then
50 return true, filename
51 else
52 return false
53 end
54end
55
56return tools
diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua
index d75d9795..8eaa9361 100644
--- a/src/luarocks/fs/unix/tools.lua
+++ b/src/luarocks/fs/unix/tools.lua
@@ -232,51 +232,6 @@ function tools.is_file(file)
232 return fs.execute(vars.TEST, "-f", file) 232 return fs.execute(vars.TEST, "-f", file)
233end 233end
234 234
235--- Download a remote file.
236-- @param url string: URL to be fetched.
237-- @param filename string or nil: this function attempts to detect the
238-- resulting local filename of the remote file as the basename of the URL;
239-- if that is not correct (due to a redirection, for example), the local
240-- filename can be given explicitly as this second argument.
241-- @return (boolean, string): true and the filename on success,
242-- false and the error message on failure.
243function tools.use_downloader(url, filename, cache)
244 assert(type(url) == "string")
245 assert(type(filename) == "string" or not filename)
246
247 filename = fs.absolute_name(filename or dir.base_name(url))
248
249 local ok
250 if cfg.downloader == "wget" then
251 local wget_cmd = fs.Q(vars.WGET).." "..vars.WGETNOCERTFLAG.." --no-cache --user-agent='"..cfg.user_agent.." via wget' --quiet "
252 if cfg.connection_timeout and cfg.connection_timeout > 0 then
253 wget_cmd = wget_cmd .. "--timeout="..tonumber(cfg.connection_timeout).." --tries=1 "
254 end
255 if cache then
256 -- --timestamping is incompatible with --output-document,
257 -- but that's not a problem for our use cases.
258 fs.change_dir(dir.dir_name(filename))
259 ok = fs.execute_quiet(wget_cmd.." --timestamping ", url)
260 fs.pop_dir()
261 elseif filename then
262 ok = fs.execute_quiet(wget_cmd.." --output-document ", filename, url)
263 else
264 ok = fs.execute_quiet(wget_cmd, url)
265 end
266 elseif cfg.downloader == "curl" then
267 local curl_cmd = fs.Q(vars.CURL).." "..vars.CURLNOCERTFLAG.." -f -L --user-agent '"..cfg.user_agent.." via curl' "
268 if cfg.connection_timeout and cfg.connection_timeout > 0 then
269 curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." "
270 end
271 ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename)))
272 end
273 if ok then
274 return true, filename
275 else
276 return false
277 end
278end
279
280function tools.chmod(pathname, mode) 235function tools.chmod(pathname, mode)
281 if mode then 236 if mode then
282 return fs.execute(vars.CHMOD, mode, pathname) 237 return fs.execute(vars.CHMOD, mode, pathname)
diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua
index 39aa4ba1..cc0da0e9 100644
--- a/src/luarocks/fs/win32/tools.lua
+++ b/src/luarocks/fs/win32/tools.lua
@@ -242,51 +242,6 @@ function tools.is_file(file)
242 return fs.execute(fs.Q(vars.TEST).." -f", file) 242 return fs.execute(fs.Q(vars.TEST).." -f", file)
243end 243end
244 244
245--- Download a remote file.
246-- @param url string: URL to be fetched.
247-- @param filename string or nil: this function attempts to detect the
248-- resulting local filename of the remote file as the basename of the URL;
249-- if that is not correct (due to a redirection, for example), the local
250-- filename can be given explicitly as this second argument.
251-- @return (boolean, string): true and the filename on success,
252-- false and the error message on failure.
253function tools.use_downloader(url, filename, cache)
254 assert(type(url) == "string")
255 assert(type(filename) == "string" or not filename)
256
257 filename = fs.absolute_name(filename or dir.base_name(url))
258
259 local ok
260 if cfg.downloader == "wget" then
261 local wget_cmd = fs.Q(vars.WGET).." "..vars.WGETNOCERTFLAG.." --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet "
262 if cfg.connection_timeout and cfg.connection_timeout > 0 then
263 wget_cmd = wget_cmd .. "--timeout="..tonumber(cfg.connection_timeout).." --tries=1 "
264 end
265 if cache then
266 -- --timestamping is incompatible with --output-document,
267 -- but that's not a problem for our use cases.
268 fs.change_dir(dir.dir_name(filename))
269 ok = fs.execute_quiet(wget_cmd.." --timestamping ", url)
270 fs.pop_dir()
271 elseif filename then
272 ok = fs.execute_quiet(wget_cmd.." --output-document ", filename, url)
273 else
274 ok = fs.execute_quiet(wget_cmd, url)
275 end
276 elseif cfg.downloader == "curl" then
277 local curl_cmd = fs.Q(vars.CURL).." "..vars.CURLNOCERTFLAG.." -f -L --user-agent \""..cfg.user_agent.." via curl\" "
278 if cfg.connection_timeout and cfg.connection_timeout > 0 then
279 curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." "
280 end
281 ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename)))
282 end
283 if ok then
284 return true, filename
285 else
286 return false
287 end
288end
289
290--- Uncompress gzip file. 245--- Uncompress gzip file.
291-- @param archive string: Filename of archive. 246-- @param archive string: Filename of archive.
292-- @return boolean : success status 247-- @return boolean : success status