diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2018-06-19 00:16:24 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-06-28 17:28:41 -0300 |
commit | 392f67d65a950fcf08a08d10d21e80f359160262 (patch) | |
tree | 82db039b3eacc52ca44b137abf7e8864a027fd1e /src | |
parent | 447e8aa089fc189fef8434facfea2b0d84047749 (diff) | |
download | luarocks-392f67d65a950fcf08a08d10d21e80f359160262.tar.gz luarocks-392f67d65a950fcf08a08d10d21e80f359160262.tar.bz2 luarocks-392f67d65a950fcf08a08d10d21e80f359160262.zip |
fs: perform tool detection at runtime (downloader, md5checker)
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/fs/lua.lua | 3 | ||||
-rw-r--r-- | src/luarocks/fs/tools.lua | 83 | ||||
-rw-r--r-- | src/luarocks/upload/api.lua | 2 |
3 files changed, 63 insertions, 25 deletions
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index ede7884a..b53ef8cc 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
@@ -712,7 +712,8 @@ function fs_lua.download(url, filename, cache) | |||
712 | end | 712 | end |
713 | if https_err then | 713 | if https_err then |
714 | if not downloader_warning then | 714 | if not downloader_warning then |
715 | util.warning("falling back to "..cfg.downloader.." - install luasec to get native HTTPS support") | 715 | local downloader = fs.which_tool("downloader") |
716 | util.warning("falling back to "..downloader.." - install luasec to get native HTTPS support") | ||
716 | downloader_warning = true | 717 | downloader_warning = true |
717 | end | 718 | end |
718 | return fs.use_downloader(url, filename, cache) | 719 | return fs.use_downloader(url, filename, cache) |
diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 8419f7d3..671c7ae1 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua | |||
@@ -10,21 +10,54 @@ local vars = cfg.variables | |||
10 | 10 | ||
11 | local dir_stack = {} | 11 | local dir_stack = {} |
12 | 12 | ||
13 | --- Obtain current directory. | 13 | do |
14 | -- Uses the module's internal directory stack. | 14 | local tool_cache = {} |
15 | -- @return string: the absolute pathname of the current directory. | 15 | |
16 | function tools.current_dir() | 16 | local tool_options = { |
17 | local current = cfg.cache_pwd | 17 | downloader = { |
18 | if not current then | 18 | { var = "CURL", name = "curl" }, |
19 | local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) | 19 | { var = "WGET", name = "wget" }, |
20 | current = pipe:read("*l") | 20 | }, |
21 | pipe:close() | 21 | md5checker = { |
22 | cfg.cache_pwd = current | 22 | { var = "MD5SUM", name = "md5sum" }, |
23 | { var = "OPENSSL", name = "openssl" }, | ||
24 | { var = "MD5", name = "md5", arg = "-v" }, | ||
25 | }, | ||
26 | } | ||
27 | |||
28 | function tools.which_tool(tooltype) | ||
29 | if tool_cache[tooltype] then | ||
30 | return tool_cache[tooltype] | ||
31 | end | ||
32 | |||
33 | for _, opt in ipairs(tool_options[tooltype]) do | ||
34 | if fs.is_tool_available(vars[opt.var], opt.name, opt.arg) then | ||
35 | tool_cache[tooltype] = opt.name | ||
36 | break | ||
37 | end | ||
38 | end | ||
39 | return tool_cache[tooltype] | ||
23 | end | 40 | end |
24 | for _, directory in ipairs(dir_stack) do | 41 | end |
25 | current = fs.absolute_name(directory, current) | 42 | |
43 | do | ||
44 | local cache_pwd | ||
45 | --- Obtain current directory. | ||
46 | -- Uses the module's internal directory stack. | ||
47 | -- @return string: the absolute pathname of the current directory. | ||
48 | function tools.current_dir() | ||
49 | local current = cache_pwd | ||
50 | if not current then | ||
51 | local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) | ||
52 | current = pipe:read("*l") | ||
53 | pipe:close() | ||
54 | cache_pwd = current | ||
55 | end | ||
56 | for _, directory in ipairs(dir_stack) do | ||
57 | current = fs.absolute_name(directory, current) | ||
58 | end | ||
59 | return current | ||
26 | end | 60 | end |
27 | return current | ||
28 | end | 61 | end |
29 | 62 | ||
30 | --- Change the current directory. | 63 | --- Change the current directory. |
@@ -101,9 +134,11 @@ function tools.use_downloader(url, filename, cache) | |||
101 | assert(type(filename) == "string" or not filename) | 134 | assert(type(filename) == "string" or not filename) |
102 | 135 | ||
103 | filename = fs.absolute_name(filename or dir.base_name(url)) | 136 | filename = fs.absolute_name(filename or dir.base_name(url)) |
137 | |||
138 | local downloader = fs.which_tool("downloader") | ||
104 | 139 | ||
105 | local ok | 140 | local ok |
106 | if cfg.downloader == "wget" then | 141 | if downloader == "wget" then |
107 | local wget_cmd = fs.Q(vars.WGET).." "..vars.WGETNOCERTFLAG.." --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet " | 142 | local wget_cmd = fs.Q(vars.WGET).." "..vars.WGETNOCERTFLAG.." --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet " |
108 | if cfg.connection_timeout and cfg.connection_timeout > 0 then | 143 | if cfg.connection_timeout and cfg.connection_timeout > 0 then |
109 | wget_cmd = wget_cmd .. "--timeout="..tostring(cfg.connection_timeout).." --tries=1 " | 144 | wget_cmd = wget_cmd .. "--timeout="..tostring(cfg.connection_timeout).." --tries=1 " |
@@ -119,7 +154,7 @@ function tools.use_downloader(url, filename, cache) | |||
119 | else | 154 | else |
120 | ok = fs.execute_quiet(wget_cmd, url) | 155 | ok = fs.execute_quiet(wget_cmd, url) |
121 | end | 156 | end |
122 | elseif cfg.downloader == "curl" then | 157 | elseif downloader == "curl" then |
123 | local curl_cmd = fs.Q(vars.CURL).." "..vars.CURLNOCERTFLAG.." -f -L --user-agent \""..cfg.user_agent.." via curl\" " | 158 | local curl_cmd = fs.Q(vars.CURL).." "..vars.CURLNOCERTFLAG.." -f -L --user-agent \""..cfg.user_agent.." via curl\" " |
124 | if cfg.connection_timeout and cfg.connection_timeout > 0 then | 159 | if cfg.connection_timeout and cfg.connection_timeout > 0 then |
125 | curl_cmd = curl_cmd .. "--connect-timeout "..tostring(cfg.connection_timeout).." " | 160 | curl_cmd = curl_cmd .. "--connect-timeout "..tostring(cfg.connection_timeout).." " |
@@ -144,15 +179,17 @@ local md5_cmd = { | |||
144 | -- @param file string: The file to be computed. | 179 | -- @param file string: The file to be computed. |
145 | -- @return string: The MD5 checksum or nil + message | 180 | -- @return string: The MD5 checksum or nil + message |
146 | function tools.get_md5(file) | 181 | function tools.get_md5(file) |
147 | local cmd = md5_cmd[cfg.md5checker] | 182 | local md5checker = fs.which_tool("md5checker") |
148 | if not cmd then return nil, "no MD5 checker command configured" end | 183 | if md5checker then |
149 | local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) | 184 | local cmd = md5_cmd[md5checker] |
150 | local computed = pipe:read("*l") | 185 | local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) |
151 | pipe:close() | 186 | local computed = pipe:read("*l") |
152 | if computed then | 187 | pipe:close() |
153 | computed = computed:match("("..("%x"):rep(32)..")") | 188 | if computed then |
189 | computed = computed:match("("..("%x"):rep(32)..")") | ||
190 | end | ||
191 | if computed then return computed end | ||
154 | end | 192 | end |
155 | if computed then return computed end | ||
156 | return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) | 193 | return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) |
157 | end | 194 | end |
158 | 195 | ||
diff --git a/src/luarocks/upload/api.lua b/src/luarocks/upload/api.lua index f9244258..30857158 100644 --- a/src/luarocks/upload/api.lua +++ b/src/luarocks/upload/api.lua | |||
@@ -143,7 +143,7 @@ function Api:request(url, params, post_params) | |||
143 | local json_ok, json = require_json() | 143 | local json_ok, json = require_json() |
144 | if not json_ok then return nil, "A JSON library is required for this command. "..json end | 144 | if not json_ok then return nil, "A JSON library is required for this command. "..json end |
145 | 145 | ||
146 | if cfg.downloader == "wget" then | 146 | if fs.which_tool("downloader") == "wget" then |
147 | local curl_ok, err = fs.is_tool_available(vars.CURL, "curl") | 147 | local curl_ok, err = fs.is_tool_available(vars.CURL, "curl") |
148 | if not curl_ok then | 148 | if not curl_ok then |
149 | return nil, err | 149 | return nil, err |