diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2014-02-08 18:29:13 -0200 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2014-02-08 18:29:13 -0200 |
| commit | fdf546560b425ae5cfcc18b9b9d4e1e8f3995371 (patch) | |
| tree | d24b564e0e7a29b81e3ab32474ffef2f5ecd5797 | |
| parent | 330ee40e3168cc060c6c806ec1ed90c8fb73a3ff (diff) | |
| download | luarocks-fdf546560b425ae5cfcc18b9b9d4e1e8f3995371.tar.gz luarocks-fdf546560b425ae5cfcc18b9b9d4e1e8f3995371.tar.bz2 luarocks-fdf546560b425ae5cfcc18b9b9d4e1e8f3995371.zip | |
Refactor and improve logic for detecting base directory.
| -rw-r--r-- | src/luarocks/fetch.lua | 32 | ||||
| -rw-r--r-- | src/luarocks/new_version.lua | 18 | ||||
| -rw-r--r-- | 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) | |||
| 86 | end | 86 | end |
| 87 | end | 87 | end |
| 88 | 88 | ||
| 89 | -- Determine base directory of a fetched URL by extracting its | ||
| 90 | -- archive and looking for a directory in the root. | ||
| 91 | -- @param file string: absolute local pathname of the fetched file | ||
| 92 | -- @param temp_dir string: temporary directory in which URL was fetched. | ||
| 93 | -- @param src_url string: URL to use when inferring base directory. | ||
| 94 | -- @param src_dir string or nil: expected base directory (inferred | ||
| 95 | -- from src_url if not given). | ||
| 96 | -- @return (string, string) or (string, nil) or (nil, string): | ||
| 97 | -- The inferred base directory and the one actually found (which may | ||
| 98 | -- be nil if not found), or nil followed by an error message. | ||
| 99 | -- The inferred dir is returned first to avoid confusion with errors, | ||
| 100 | -- because it is never nil. | ||
| 101 | function find_base_dir(file, temp_dir, src_url, src_dir) | ||
| 102 | local ok, err = fs.change_dir(temp_dir) | ||
| 103 | if not ok then return nil, err end | ||
| 104 | fs.unpack_archive(file) | ||
| 105 | local inferred_dir = src_dir or url_to_base_dir(src_url) | ||
| 106 | local found_dir = nil | ||
| 107 | if fs.exists(inferred_dir) then | ||
| 108 | found_dir = inferred_dir | ||
| 109 | else | ||
| 110 | util.printerr("Directory "..inferred_dir.." not found") | ||
| 111 | local files = fs.list_dir() | ||
| 112 | if files[1] and fs.is_dir(files[1]) then | ||
| 113 | util.printerr("Found "..files[1]) | ||
| 114 | found_dir = files[1] | ||
| 115 | end | ||
| 116 | end | ||
| 117 | fs.pop_dir() | ||
| 118 | return inferred_dir, found_dir | ||
| 119 | end | ||
| 120 | |||
| 89 | --- Obtain a rock and unpack it. | 121 | --- Obtain a rock and unpack it. |
| 90 | -- If a directory is not given, a temporary directory will be created, | 122 | -- If a directory is not given, a temporary directory will be created, |
| 91 | -- which will be deleted on program termination. | 123 | -- 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) | |||
| 58 | end | 58 | end |
| 59 | util.printout("File successfully downloaded. Updating MD5 checksum...") | 59 | util.printout("File successfully downloaded. Updating MD5 checksum...") |
| 60 | out_rs.source.md5 = fs.get_md5(file) | 60 | out_rs.source.md5 = fs.get_md5(file) |
| 61 | local ok, err = fs.change_dir(temp_dir) | 61 | local inferred_dir, found_dir = fetch.find_base_dir(file, temp_dir, out_rs.source.url, out_rs.source.dir) |
| 62 | if not ok then return nil, err end | 62 | if not inferred_dir then |
| 63 | fs.unpack_archive(file) | 63 | return nil, found_dir |
| 64 | local base_dir = out_rs.source.dir or fetch.url_to_base_dir(out_rs.source.url) | 64 | end |
| 65 | if not fs.exists(base_dir) then | 65 | if found_dir and found_dir ~= inferred_dir then |
| 66 | util.printerr("Directory "..base_dir.." not found") | 66 | out_rs.source.dir = found_dir |
| 67 | local files = fs.list_dir() | ||
| 68 | if files[1] and fs.is_dir(files[1]) then | ||
| 69 | util.printerr("Found "..files[1]) | ||
| 70 | out_rs.source.dir = files[1] | ||
| 71 | end | ||
| 72 | end | 67 | end |
| 73 | fs.pop_dir() | ||
| 74 | return out_rs.source.md5 ~= old_md5 | 68 | return out_rs.source.md5 ~= old_md5 |
| 75 | end | 69 | end |
| 76 | 70 | ||
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) | |||
| 41 | end | 41 | end |
| 42 | 42 | ||
| 43 | local function get_url(rockspec) | 43 | local function get_url(rockspec) |
| 44 | local url = rockspec.source.url | ||
| 45 | local file, temp_dir, err_code, err_file, err_temp_dir = fetch.fetch_sources(rockspec, false) | 44 | local file, temp_dir, err_code, err_file, err_temp_dir = fetch.fetch_sources(rockspec, false) |
| 46 | if err_code == "source.dir" then | 45 | if err_code == "source.dir" then |
| 47 | file, temp_dir = err_file, err_temp_dir | 46 | file, temp_dir = err_file, err_temp_dir |
| @@ -54,20 +53,8 @@ local function get_url(rockspec) | |||
| 54 | if fetch.is_basic_protocol(rockspec.source.protocol) then | 53 | if fetch.is_basic_protocol(rockspec.source.protocol) then |
| 55 | rockspec.source.md5 = fs.get_md5(file) | 54 | rockspec.source.md5 = fs.get_md5(file) |
| 56 | end | 55 | end |
| 57 | local ok, err = fs.change_dir(temp_dir) | 56 | local inferred_dir, found_dir = fetch.find_base_dir(file, temp_dir, rockspec.source.url) |
| 58 | if not ok then return false end | 57 | return true, found_dir or inferred_dir, temp_dir |
| 59 | fs.unpack_archive(file) | ||
| 60 | local base_dir = fetch.url_to_base_dir(url) | ||
| 61 | if not fs.exists(base_dir) then | ||
| 62 | util.printerr("Directory "..base_dir.." not found") | ||
| 63 | local files = fs.list_dir() | ||
| 64 | if files[1] and fs.is_dir(files[1]) then | ||
| 65 | util.printerr("Found "..files[1]) | ||
| 66 | base_dir = files[1] | ||
| 67 | end | ||
| 68 | end | ||
| 69 | fs.pop_dir() | ||
| 70 | return true, base_dir, temp_dir | ||
| 71 | end | 58 | end |
| 72 | 59 | ||
| 73 | local function configure_lua_version(rockspec, luaver) | 60 | local function configure_lua_version(rockspec, luaver) |
