diff options
| author | Peter Melnichenko <mpeterval@gmail.com> | 2016-05-07 14:01:31 +0300 |
|---|---|---|
| committer | Peter Melnichenko <mpeterval@gmail.com> | 2016-05-22 19:50:40 +0300 |
| commit | 472e74d582055992cf1f5c75821e083131a895ac (patch) | |
| tree | 7da87bf16660a4d1873ad0e1be36f473cd7e1325 /src | |
| parent | 26ea986b8322dc3bd1e82cf6d819082c57a69024 (diff) | |
| download | luarocks-472e74d582055992cf1f5c75821e083131a895ac.tar.gz luarocks-472e74d582055992cf1f5c75821e083131a895ac.tar.bz2 luarocks-472e74d582055992cf1f5c75821e083131a895ac.zip | |
Move common implementation of fs.use_downloader into fs.tools module
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/fs.lua | 5 | ||||
| -rw-r--r-- | src/luarocks/fs/tools.lua | 56 | ||||
| -rw-r--r-- | src/luarocks/fs/unix/tools.lua | 45 | ||||
| -rw-r--r-- | src/luarocks/fs/win32/tools.lua | 45 |
4 files changed, 60 insertions, 91 deletions
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 |
| 70 | local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") | 70 | local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") |
| 71 | if ok and fs_plat_tools then load_fns(fs_plat_tools) end | 71 | if ok and fs_plat_tools then |
| 72 | load_fns(fs_plat_tools) | ||
| 73 | load_fns(require("luarocks.fs.tools")) | ||
| 74 | end | ||
| 72 | 75 | ||
| 73 | 76 | ||
| 74 | return fs | 77 | return 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. | ||
| 3 | local tools = {} | ||
| 4 | |||
| 5 | local fs = require("luarocks.fs") | ||
| 6 | local dir = require("luarocks.dir") | ||
| 7 | local cfg = require("luarocks.cfg") | ||
| 8 | |||
| 9 | local 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. | ||
| 19 | function 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 | ||
| 54 | end | ||
| 55 | |||
| 56 | return 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) |
| 233 | end | 233 | end |
| 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. | ||
| 243 | function 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 | ||
| 278 | end | ||
| 279 | |||
| 280 | function tools.chmod(pathname, mode) | 235 | function 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) |
| 243 | end | 243 | end |
| 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. | ||
| 253 | function 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 | ||
| 288 | end | ||
| 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 |
