From fdf546560b425ae5cfcc18b9b9d4e1e8f3995371 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sat, 8 Feb 2014 18:29:13 -0200 Subject: Refactor and improve logic for detecting base directory. --- src/luarocks/fetch.lua | 32 ++++++++++++++++++++++++++++++++ src/luarocks/new_version.lua | 18 ++++++------------ src/luarocks/write_rockspec.lua | 17 ++--------------- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index 8e08e7ad..4930ea1d 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua @@ -86,6 +86,38 @@ function fetch_url_at_temp_dir(url, tmpname, filename) end end +-- Determine base directory of a fetched URL by extracting its +-- archive and looking for a directory in the root. +-- @param file string: absolute local pathname of the fetched file +-- @param temp_dir string: temporary directory in which URL was fetched. +-- @param src_url string: URL to use when inferring base directory. +-- @param src_dir string or nil: expected base directory (inferred +-- from src_url if not given). +-- @return (string, string) or (string, nil) or (nil, string): +-- The inferred base directory and the one actually found (which may +-- be nil if not found), or nil followed by an error message. +-- The inferred dir is returned first to avoid confusion with errors, +-- because it is never nil. +function find_base_dir(file, temp_dir, src_url, src_dir) + local ok, err = fs.change_dir(temp_dir) + if not ok then return nil, err end + fs.unpack_archive(file) + local inferred_dir = src_dir or url_to_base_dir(src_url) + local found_dir = nil + if fs.exists(inferred_dir) then + found_dir = inferred_dir + else + util.printerr("Directory "..inferred_dir.." not found") + local files = fs.list_dir() + if files[1] and fs.is_dir(files[1]) then + util.printerr("Found "..files[1]) + found_dir = files[1] + end + end + fs.pop_dir() + return inferred_dir, found_dir +end + --- Obtain a rock and unpack it. -- If a directory is not given, a temporary directory will be created, -- which will be deleted on program termination. diff --git a/src/luarocks/new_version.lua b/src/luarocks/new_version.lua index 7f255d0d..4ca003f7 100644 --- a/src/luarocks/new_version.lua +++ b/src/luarocks/new_version.lua @@ -58,19 +58,13 @@ local function check_url_and_update_md5(out_rs, out_name) end util.printout("File successfully downloaded. Updating MD5 checksum...") out_rs.source.md5 = fs.get_md5(file) - local ok, err = fs.change_dir(temp_dir) - if not ok then return nil, err end - fs.unpack_archive(file) - local base_dir = out_rs.source.dir or fetch.url_to_base_dir(out_rs.source.url) - if not fs.exists(base_dir) then - util.printerr("Directory "..base_dir.." not found") - local files = fs.list_dir() - if files[1] and fs.is_dir(files[1]) then - util.printerr("Found "..files[1]) - out_rs.source.dir = files[1] - end + local inferred_dir, found_dir = fetch.find_base_dir(file, temp_dir, out_rs.source.url, out_rs.source.dir) + if not inferred_dir then + return nil, found_dir + end + if found_dir and found_dir ~= inferred_dir then + out_rs.source.dir = found_dir end - fs.pop_dir() return out_rs.source.md5 ~= old_md5 end diff --git a/src/luarocks/write_rockspec.lua b/src/luarocks/write_rockspec.lua index a27f0b63..05948922 100644 --- a/src/luarocks/write_rockspec.lua +++ b/src/luarocks/write_rockspec.lua @@ -41,7 +41,6 @@ local function open_file(name) end local function get_url(rockspec) - local url = rockspec.source.url local file, temp_dir, err_code, err_file, err_temp_dir = fetch.fetch_sources(rockspec, false) if err_code == "source.dir" then file, temp_dir = err_file, err_temp_dir @@ -54,20 +53,8 @@ local function get_url(rockspec) if fetch.is_basic_protocol(rockspec.source.protocol) then rockspec.source.md5 = fs.get_md5(file) end - local ok, err = fs.change_dir(temp_dir) - if not ok then return false end - fs.unpack_archive(file) - local base_dir = fetch.url_to_base_dir(url) - if not fs.exists(base_dir) then - util.printerr("Directory "..base_dir.." not found") - local files = fs.list_dir() - if files[1] and fs.is_dir(files[1]) then - util.printerr("Found "..files[1]) - base_dir = files[1] - end - end - fs.pop_dir() - return true, base_dir, temp_dir + local inferred_dir, found_dir = fetch.find_base_dir(file, temp_dir, rockspec.source.url) + return true, found_dir or inferred_dir, temp_dir end local function configure_lua_version(rockspec, luaver) -- cgit v1.2.3-55-g6feb