| |
|
| |
|
| | local tools = {}
|
| |
|
| | local fs = require("luarocks.fs")
|
| | local dir = require("luarocks.dir")
|
| | local cfg = require("luarocks.core.cfg")
|
| |
|
| | local vars = setmetatable({}, { __index = function(_,k) return cfg.variables[k] end })
|
| |
|
| | local dir_stack = {}
|
| |
|
| | do
|
| | local tool_cache = {}
|
| |
|
| | local tool_options = {
|
| | downloader = {
|
| | { var = "CURL", name = "curl" },
|
| | { var = "WGET", name = "wget" },
|
| | },
|
| | md5checker = {
|
| | { var = "MD5SUM", name = "md5sum" },
|
| | { var = "OPENSSL", name = "openssl", cmdarg = "md5", checkarg = "version" },
|
| | { var = "MD5", name = "md5", checkarg = "-shello" },
|
| | },
|
| | }
|
| |
|
| | function tools.which_tool(tooltype)
|
| | local tool = tool_cache[tooltype]
|
| | if not tool then
|
| | for _, opt in ipairs(tool_options[tooltype]) do
|
| | if fs.is_tool_available(vars[opt.var], opt.name, opt.checkarg) then
|
| | tool = opt
|
| | tool_cache[tooltype] = opt
|
| | break
|
| | end
|
| | end
|
| | end
|
| | if not tool then
|
| | return nil
|
| | end
|
| | return tool.name, vars[tool.var] .. (tool.cmdarg and " "..tool.cmdarg or "")
|
| | end
|
| | end
|
| |
|
| | do
|
| | local cache_pwd
|
| |
|
| |
|
| |
|
| | function tools.current_dir()
|
| | local current = cache_pwd
|
| | if not current then
|
| | local pipe = io.popen(fs.quiet_stderr(vars.PWD))
|
| | current = pipe:read("*l")
|
| | pipe:close()
|
| | cache_pwd = current
|
| | end
|
| | for _, directory in ipairs(dir_stack) do
|
| | current = fs.absolute_name(directory, current)
|
| | end
|
| | return current
|
| | end
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function tools.change_dir(directory)
|
| | assert(type(directory) == "string")
|
| | if fs.is_dir(directory) then
|
| | table.insert(dir_stack, directory)
|
| | return true
|
| | end
|
| | return nil, "directory not found: "..directory
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| | function tools.change_dir_to_root()
|
| | local curr_dir = fs.current_dir()
|
| | if not curr_dir or not fs.is_dir(curr_dir) then
|
| | return false
|
| | end
|
| | table.insert(dir_stack, "/")
|
| | return true
|
| | end
|
| |
|
| |
|
| | function tools.pop_dir()
|
| | local directory = table.remove(dir_stack)
|
| | return directory ~= nil
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function tools.execute_string(cmd)
|
| | local current = fs.current_dir()
|
| | if not current then return false end
|
| | cmd = fs.command_at(current, cmd)
|
| | local code = os.execute(cmd)
|
| | if code == 0 or code == true then
|
| | return true
|
| | else
|
| | return false
|
| | end
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function tools.dir_iterator(at)
|
| | local pipe = io.popen(fs.command_at(at, vars.LS, true))
|
| | for file in pipe:lines() do
|
| | if file ~= "." and file ~= ".." then
|
| | coroutine.yield(file)
|
| | end
|
| | end
|
| | pipe:close()
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | function tools.use_downloader(url, filename, cache)
|
| | assert(type(url) == "string")
|
| | assert(type(filename) == "string" or not filename)
|
| |
|
| | filename = fs.absolute_name(filename or dir.base_name(url))
|
| |
|
| | local downloader = fs.which_tool("downloader")
|
| |
|
| | local ok
|
| | if downloader == "wget" then
|
| | local wget_cmd = vars.WGET.." "..vars.WGETNOCERTFLAG.." --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet "
|
| | if cfg.connection_timeout and cfg.connection_timeout > 0 then
|
| | wget_cmd = wget_cmd .. "--timeout="..tostring(cfg.connection_timeout).." --tries=1 "
|
| | end
|
| | if cache then
|
| |
|
| |
|
| | fs.delete(filename .. ".unixtime")
|
| | fs.change_dir(dir.dir_name(filename))
|
| | ok = fs.execute_quiet(wget_cmd.." --timestamping ", url)
|
| | fs.pop_dir()
|
| | elseif filename then
|
| | ok = fs.execute_quiet(wget_cmd.." --output-document ", filename, url)
|
| | else
|
| | ok = fs.execute_quiet(wget_cmd, url)
|
| | end
|
| | elseif downloader == "curl" then
|
| | local curl_cmd = vars.CURL.." "..vars.CURLNOCERTFLAG.." -f -L --user-agent \""..cfg.user_agent.." via curl\" "
|
| | if cfg.connection_timeout and cfg.connection_timeout > 0 then
|
| | curl_cmd = curl_cmd .. "--connect-timeout "..tostring(cfg.connection_timeout).." "
|
| | end
|
| | ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename)))
|
| | else
|
| | return false, "No downloader tool available -- please install 'wget' or 'curl' in your system"
|
| | end
|
| | if ok then
|
| | return true, filename
|
| | else
|
| | os.remove(filename)
|
| | return false, "Failed downloading " .. url
|
| | end
|
| | end
|
| |
|
| |
|
| |
|
| |
|
| | function tools.get_md5(file)
|
| | local ok, md5checker = fs.which_tool("md5checker")
|
| | if ok then
|
| | local pipe = io.popen(md5checker.." "..fs.Q(fs.absolute_name(file)))
|
| | local computed = pipe:read("*l")
|
| | pipe:close()
|
| | if computed then
|
| | computed = computed:match("("..("%x"):rep(32)..")")
|
| | end
|
| | if computed then
|
| | return computed
|
| | else
|
| | return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file))
|
| | end
|
| | else
|
| | return false, "No MD5 checking tool available -- please install 'md5', 'md5sum' or 'openssl' in your system"
|
| | end
|
| | end
|
| |
|
| | return tools
|
| |
|