From 6beb0ef346cc7fd097a4736a7b27755a84165287 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Tue, 10 May 2016 15:48:33 +0300 Subject: Shorten logging for `luarocks install` Instead of mentioning URL twice in Installing URL... Using URL... switching to 'build' mode simply print Installing URL and don't print URL at all when it's the argument to `luarocks install` (it's a bit redundant), only when it's a result of a search. --- src/luarocks/deps.lua | 1 + src/luarocks/install.lua | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index 812e6d18..ac294f54 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -488,6 +488,7 @@ function deps.fulfill_dependencies(rockspec, deps_mode) if not url then return nil, "Could not satisfy dependency "..deps.show_dep(dep)..": "..err end + util.printout("Installing "..url) local ok, err, errcode = install.run(url, deps.deps_mode_to_flag(deps_mode)) if not ok then return nil, "Failed installing dependency: "..url.." - "..err, errcode diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua index c938aa9f..ae162b86 100644 --- a/src/luarocks/install.lua +++ b/src/luarocks/install.lua @@ -168,7 +168,6 @@ function install.run(...) if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end if name:match("%.rockspec$") or name:match("%.src%.rock$") then - util.printout("Using "..name.."... switching to 'build' mode") local build = require("luarocks.build") return build.run(name, util.forward_flags(flags, "local", "keep", "deps-mode", "only-deps")) elseif name:match("%.rock$") then @@ -190,7 +189,7 @@ function install.run(...) if not url then return nil, err end - util.printout("Installing "..url.."...") + util.printout("Installing "..url) return install.run(url, util.forward_flags(flags)) end end -- cgit v1.2.3-55-g6feb From 4800159347ac79a30180d778ac02ee24a181e41a Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Tue, 10 May 2016 16:31:16 +0300 Subject: Use same messages for installing binary and source rocks Move successful installation announcement into an util function. Don't print that the rock was "built": sometimes there is nothing to build (pure Lua rocks), and for C rocks compilation commands are printed already. --- src/luarocks/build.lua | 10 +--------- src/luarocks/install.lua | 10 +--------- src/luarocks/util.lua | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index bdc3b57a..38a269dd 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -332,15 +332,7 @@ function build.build_rockspec(rockspec_file, need_to_fetch, minimal_mode, deps_m ok, err = manif.update_manifest(name, version, nil, deps_mode) if err then return nil, err end - local license = "" - if rockspec.description and rockspec.description.license then - license = ("(license: "..rockspec.description.license..")") - end - - local root_dir = path.root_dir(cfg.rocks_dir) - util.printout(name.." "..version.." is now built and installed in "..root_dir.." "..license) - util.printout() - + util.announce_install(rockspec) util.remove_scheduled_function(rollback) return name, version end diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua index ae162b86..8960c102 100644 --- a/src/luarocks/install.lua +++ b/src/luarocks/install.lua @@ -98,15 +98,7 @@ function install.install_binary_rock(rock_file, deps_mode) ok, err = manif.update_manifest(name, version, nil, deps_mode) if err then return nil, err end - local license = "" - if rockspec.description.license then - license = ("(license: "..rockspec.description.license..")") - end - - local root_dir = path.root_dir(cfg.rocks_dir) - util.printout() - util.printout(name.." "..version.." is now installed in "..root_dir.." "..license) - + util.announce_install(rockspec) util.remove_scheduled_function(rollback) return name, version end diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index c06c8354..a92297ce 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -505,6 +505,20 @@ function util.see_help(command, program) return "See '"..util.this_program(program or "luarocks")..' help'..(command and " "..command or "").."'." end +function util.announce_install(rockspec) + local cfg = require("luarocks.cfg") + local path = require("luarocks.path") + + local suffix = "" + if rockspec.description and rockspec.description.license then + suffix = " (license: "..rockspec.description.license..")" + end + + local root_dir = path.root_dir(cfg.rocks_dir) + util.printout(rockspec.name.." "..rockspec.version.." is now installed in "..root_dir..suffix) + util.printout() +end + -- from http://lua-users.org/wiki/SplitJoin -- by PhilippeLhoste function util.split_string(str, delim, maxNb) -- cgit v1.2.3-55-g6feb From 277b7bc9f1ce46740de1c167294e235737a6e305 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Tue, 10 May 2016 16:38:11 +0300 Subject: Don't spam 'Updating manifest for...' Installation success message is enough and is more important (what's a manifest and why users should care?) --- src/luarocks/manif.lua | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/luarocks/manif.lua b/src/luarocks/manif.lua index 05621315..bc202be3 100644 --- a/src/luarocks/manif.lua +++ b/src/luarocks/manif.lua @@ -432,8 +432,6 @@ function manif.update_manifest(name, version, repo, deps_mode) if deps_mode == "none" then deps_mode = cfg.deps_mode end - util.printout("Updating manifest for "..repo) - local manifest, err = manif.load_manifest(repo) if not manifest then util.printerr("No existing manifest. Attempting to rebuild...") -- cgit v1.2.3-55-g6feb From a26862545e0eccbbc5fba15edd2a00dedf069a3f Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Tue, 10 May 2016 17:33:44 +0300 Subject: Announce each missing dep before its install Instead of lising all missing deps before installing all of them, announce each missing dep right before installing it. Also show current rock versions for missing deps. --- src/luarocks/deps.lua | 72 ++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index ac294f54..73eab2c2 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -449,53 +449,43 @@ function deps.fulfill_dependencies(rockspec, deps_mode) end end - local _, missing, no_upgrade = deps.match_deps(rockspec, nil, deps_mode) + local first_missing_dep = true - if next(no_upgrade) then - util.printerr("Missing dependencies for "..rockspec.name.." "..rockspec.version..":") - for _, dep in pairs(no_upgrade) do - util.printerr(deps.show_dep(dep)) - end - if next(missing) then - for _, dep in pairs(missing) do - util.printerr(deps.show_dep(dep)) + for _, dep in ipairs(rockspec.dependencies) do + if not match_dep(dep, nil, deps_mode) then + if first_missing_dep then + util.printout() + first_missing_dep = false end - end - util.printerr() - for _, dep in pairs(no_upgrade) do - util.printerr("This version of "..rockspec.name.." is designed for use with") - util.printerr(deps.show_dep(dep)..", but is configured to avoid upgrading it") - util.printerr("automatically. Please upgrade "..dep.name.." with") - util.printerr(" luarocks install "..dep.name) - util.printerr("or choose an older version of "..rockspec.name.." with") - util.printerr(" luarocks search "..rockspec.name) - end - return nil, "Failed matching dependencies." - end - if next(missing) then - util.printerr() - util.printerr("Missing dependencies for "..rockspec.name..":") - for _, dep in pairs(missing) do - util.printerr(deps.show_dep(dep)) - end - util.printerr() - - for _, dep in pairs(missing) do - -- Double-check in case dependency was filled during recursion. - if not match_dep(dep, nil, deps_mode) then - local url, err = search.find_suitable_rock(dep) - if not url then - return nil, "Could not satisfy dependency "..deps.show_dep(dep)..": "..err - end - util.printout("Installing "..url) - local ok, err, errcode = install.run(url, deps.deps_mode_to_flag(deps_mode)) - if not ok then - return nil, "Failed installing dependency: "..url.." - "..err, errcode - end + local any_installed = match_dep(search.make_query(dep.name), nil, deps_mode) + local installation_type = cfg.rocks_provided[dep.name] and "provided by VM" or "installed" + local status = any_installed and any_installed.version.." "..installation_type or "missing" + util.printout(("%s %s depends on %s (%s)"):format( + rockspec.name, rockspec.version, deps.show_dep(dep), status)) + + if dep.constraints[1] and dep.constraints[1].no_upgrade then + util.printerr("This version of "..rockspec.name.." is designed for use with") + util.printerr(deps.show_dep(dep)..", but is configured to avoid upgrading it") + util.printerr("automatically. Please upgrade "..dep.name.." with") + util.printerr(" luarocks install "..dep.name) + util.printerr("or choose an older version of "..rockspec.name.." with") + util.printerr(" luarocks search "..rockspec.name) + return nil, "Failed matching dependencies" + end + + local url, search_err = search.find_suitable_rock(dep) + if not url then + return nil, "Could not satisfy dependency "..deps.show_dep(dep)..": "..search_err + end + util.printout("Installing "..url) + local ok, install_err, errcode = install.run(url, deps.deps_mode_to_flag(deps_mode)) + if not ok then + return nil, "Failed installing dependency: "..url.." - "..install_err, errcode end end end + return true end -- cgit v1.2.3-55-g6feb From 2b02e999c5b04148fd88a67a2be93378c7225956 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 14 May 2016 19:09:15 +0300 Subject: Return missing deps listing in `luarocks install` output --- src/luarocks/deps.lua | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index 73eab2c2..3c580623 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -410,6 +410,13 @@ local function values_set(tbl) return set end +local function rock_status(name, deps_mode) + local search = require("luarocks.search") + local installed = match_dep(search.make_query(name), nil, deps_mode) + local installation_type = cfg.rocks_provided[name] and "provided by VM" or "installed" + return installed and installed.version.." "..installation_type or "not installed" +end + --- Check dependencies of a rock and attempt to install any missing ones. -- Packages are installed using the LuaRocks "install" command. -- Aborts the program if a dependency could not be fulfilled. @@ -451,6 +458,19 @@ function deps.fulfill_dependencies(rockspec, deps_mode) local first_missing_dep = true + for _, dep in ipairs(rockspec.dependencies) do + if not match_dep(dep, nil, deps_mode) then + if first_missing_dep then + util.printout(("Missing dependencies for %s %s:"):format(rockspec.name, rockspec.version)) + first_missing_dep = false + end + + util.printout((" %s (%s)"):format(deps.show_dep(dep), rock_status(dep.name, deps_mode))) + end + end + + first_missing_dep = true + for _, dep in ipairs(rockspec.dependencies) do if not match_dep(dep, nil, deps_mode) then if first_missing_dep then @@ -458,11 +478,8 @@ function deps.fulfill_dependencies(rockspec, deps_mode) first_missing_dep = false end - local any_installed = match_dep(search.make_query(dep.name), nil, deps_mode) - local installation_type = cfg.rocks_provided[dep.name] and "provided by VM" or "installed" - local status = any_installed and any_installed.version.." "..installation_type or "missing" util.printout(("%s %s depends on %s (%s)"):format( - rockspec.name, rockspec.version, deps.show_dep(dep), status)) + rockspec.name, rockspec.version, deps.show_dep(dep), rock_status(dep.name, deps_mode))) if dep.constraints[1] and dep.constraints[1].no_upgrade then util.printerr("This version of "..rockspec.name.." is designed for use with") -- cgit v1.2.3-55-g6feb From 699abc334895b5e737976d7aa6c1982a692d623b Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Mon, 16 May 2016 11:03:19 +0300 Subject: Refactor match_dep() * Instead of applying blacklist and sorting versions in separate steps do a single pass over the list. * On success return just the version instead of {name = name, version = version} since rock name is stored inside dependency table already. --- src/luarocks/deps.lua | 46 +++++++++++++++------------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index 3c580623..536b6c51 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -322,48 +322,32 @@ end -- @param dep table: A dependency parsed in table format. -- @param blacklist table: Versions that can't be accepted. Table where keys -- are program versions and values are 'true'. --- @return table or nil: A table containing fields 'name' and 'version' --- representing an installed rock which matches the given dependency, +-- @return string or nil: latest installed version of the rock matching the dependency -- or nil if it could not be matched. local function match_dep(dep, blacklist, deps_mode) assert(type(dep) == "table") - local versions = cfg.rocks_provided[dep.name] + local versions if cfg.rocks_provided[dep.name] then -- provided rocks have higher priority than manifest's rocks versions = { cfg.rocks_provided[dep.name] } else versions = manif_core.get_versions(dep.name, deps_mode) end - if not versions then - return nil - end - if blacklist then - local i = 1 - while versions[i] do - if blacklist[versions[i]] then - table.remove(versions, i) - else - i = i + 1 - end - end - end - local candidates = {} + + local latest_version for _, vstring in ipairs(versions) do - local version = deps.parse_version(vstring) - if deps.match_constraints(version, dep.constraints) then - table.insert(candidates, version) + if not blacklist or not blacklist[vstring] then + local version = deps.parse_version(vstring) + if deps.match_constraints(version, dep.constraints) then + if not latest_version or version > latest_version then + latest_version = version + end + end end end - if #candidates == 0 then - return nil - else - table.sort(candidates) - return { - name = dep.name, - version = candidates[#candidates].string - } - end + + return latest_version and latest_version.string end --- Attempt to match dependencies of a rockspec to installed rocks. @@ -386,7 +370,7 @@ function deps.match_deps(rockspec, blacklist, deps_mode) local found = match_dep(dep, blacklist and blacklist[dep.name] or nil, deps_mode) if found then if not cfg.rocks_provided[dep.name] then - matched[dep] = found + matched[dep] = {name = dep.name, version = found} end else if dep.constraints[1] and dep.constraints[1].no_upgrade then @@ -414,7 +398,7 @@ local function rock_status(name, deps_mode) local search = require("luarocks.search") local installed = match_dep(search.make_query(name), nil, deps_mode) local installation_type = cfg.rocks_provided[name] and "provided by VM" or "installed" - return installed and installed.version.." "..installation_type or "not installed" + return installed and installed.." "..installation_type or "not installed" end --- Check dependencies of a rock and attempt to install any missing ones. -- cgit v1.2.3-55-g6feb From bc81a0b8f0bd9f36c0d98ec728ecde864fadd197 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Wed, 18 May 2016 19:15:10 +0300 Subject: Fix luarocks.repos module mutation Don't set repos.ok and repos.err --- src/luarocks/repos.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/luarocks/repos.lua b/src/luarocks/repos.lua index 66b1377f..b6d5825a 100644 --- a/src/luarocks/repos.lua +++ b/src/luarocks/repos.lua @@ -157,11 +157,10 @@ local function install_binary(source, target, name, version) assert(type(target) == "string") if fs.is_lua(source) then - repos.ok, repos.err = fs.wrap_script(source, target, name, version) + return fs.wrap_script(source, target, name, version) else - repos.ok, repos.err = fs.copy_binary(source, target) + return fs.copy_binary(source, target) end - return repos.ok, repos.err end local function resolve_conflict(target, deploy_dir, name, version) -- cgit v1.2.3-55-g6feb From 43c1a5925baf3e4003ca4bf070910b6615f5f829 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sun, 22 May 2016 18:04:42 +0300 Subject: Fix behaviour of `luarocks write-rockspec` with two args It accepts name, version and location. If two are given, the first one is considered the name and the second the location, but the version is also set to the location. Reset it to allow guessing it or using default (scm). --- src/luarocks/write_rockspec.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/luarocks/write_rockspec.lua b/src/luarocks/write_rockspec.lua index 972562c3..26a65808 100644 --- a/src/luarocks/write_rockspec.lua +++ b/src/luarocks/write_rockspec.lua @@ -208,6 +208,7 @@ function write_rockspec.run(...) name = nil elseif not url_or_dir then url_or_dir = version + version = nil end if flags["tag"] then -- cgit v1.2.3-55-g6feb From 7b09079887862de80757cce4d8746585fc1809f1 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sun, 22 May 2016 18:16:44 +0300 Subject: Infer name and version for `luarocks write-rockspec [path]` * Default path for `write-rockspec` is now `.`. * Use `scm` as default version in all cases. * Infer rock name as base directory of current path when passing `.` as path. As a result, `luarocks write-rockspec` with no arguments creates an scm rockspec. --- src/luarocks/write_rockspec.lua | 55 +++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/luarocks/write_rockspec.lua b/src/luarocks/write_rockspec.lua index 26a65808..abfca322 100644 --- a/src/luarocks/write_rockspec.lua +++ b/src/luarocks/write_rockspec.lua @@ -12,14 +12,16 @@ local type_check = require("luarocks.type_check") local util = require("luarocks.util") write_rockspec.help_summary = "Write a template for a rockspec file." -write_rockspec.help_arguments = "[--output= ...] [] [] {|}" +write_rockspec.help_arguments = "[--output= ...] [] [] [|]" write_rockspec.help = [[ This command writes an initial version of a rockspec file, -based on an URL or a local path. You may use a relative path such as '.'. -If a local path is given, name and version arguments are mandatory. -For URLs, LuaRocks will attempt to infer name and version if not given. - -If a repository URL is given with no version, it creates an 'scm' rock. +based on a name, a version, and a location (an URL or a local path). +If only two arguments are given, the first one is considered the name and the +second one is the location. +If only one argument is given, it must be the location. +If no arguments are given, current directory is used as location. +LuaRocks will attempt to infer name and version if not given, +using 'scm' as default version. Note that the generated file is a _starting point_ for writing a rockspec, and is not guaranteed to be complete or correct. @@ -198,19 +200,17 @@ end function write_rockspec.run(...) local flags, name, version, url_or_dir = util.parse_flags(...) - - if not name then - return nil, "Missing arguments. "..util.see_help("write_rockspec") - end - if name and not version then + if not name then + url_or_dir = "." + elseif not version then url_or_dir = name name = nil elseif not url_or_dir then url_or_dir = version version = nil end - + if flags["tag"] then if not version then version = flags["tag"]:gsub("^v", "") @@ -218,28 +218,25 @@ function write_rockspec.run(...) end local protocol, pathname = dir.split_url(url_or_dir) - if not fetch.is_basic_protocol(protocol) then - if not name then - name = dir.base_name(url_or_dir):gsub("%.[^.]+$", "") + if protocol == "file" then + if pathname == "." then + name = name or dir.base_name(fs.current_dir()) end - if not version then - version = "scm" - end - elseif protocol ~= "file" then + elseif fetch.is_basic_protocol(protocol) then local filename = dir.base_name(url_or_dir) local newname, newversion = filename:match("(.*)-([^-]+)") - if (not name) and newname then - name = newname - end - if (not version) and newversion then - version = newversion:gsub(".[a-z]+$", ""):gsub(".tar$", "") + if newname then + name = name or newname + version = version or newversion:gsub("%.[a-z]+$", ""):gsub("%.tar$", "") end - if not (name and version) then - return nil, "Missing name and version arguments. "..util.see_help("write_rockspec") - end - elseif not version then - return nil, "Missing name and version arguments. "..util.see_help("write_rockspec") + else + name = name or dir.base_name(url_or_dir):gsub("%.[^.]+$", "") + end + + if not name then + return nil, "Could not infer rock name. "..util.see_help("write_rockspec") end + version = version or "scm" local filename = flags["output"] or dir.path(fs.current_dir(), name:lower().."-"..version.."-1.rockspec") -- cgit v1.2.3-55-g6feb From 26ea986b8322dc3bd1e82cf6d819082c57a69024 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 13:52:08 +0300 Subject: Add fs.quiet_stderr function --- src/luarocks/fs/unix.lua | 7 +++++++ src/luarocks/fs/unix/tools.lua | 6 +++--- src/luarocks/fs/win32.lua | 7 +++++++ src/luarocks/fs/win32/tools.lua | 6 +++--- 4 files changed, 20 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index e2cc825b..520b3e99 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua @@ -16,6 +16,13 @@ function unix.quiet(cmd) return cmd.." 1> /dev/null 2> /dev/null" end +--- Annotate command string for execution with quiet stderr. +-- @param cmd string: A command-line string. +-- @return string: The command-line, with stderr silencing annotation. +function unix.quiet_stderr(cmd) + return cmd.." 2> /dev/null" +end + --- Return an absolute pathname from a potentially relative one. -- @param pathname string: pathname to convert. -- @param relative_to string or nil: path to prepend when making diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index ab55897e..d75d9795 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -21,7 +21,7 @@ end function tools.current_dir() local current = cfg.cache_pwd if not current then - local pipe = io.popen(fs.Q(vars.PWD).." 2> /dev/null") + local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) current = pipe:read("*l") pipe:close() cfg.cache_pwd = current @@ -183,7 +183,7 @@ function tools.find(at) return {} end local result = {} - local pipe = io.popen(command_at(at, vars.FIND.." * 2>/dev/null")) + local pipe = io.popen(command_at(at, fs.quiet_stderr(vars.FIND.." *"))) for file in pipe:lines() do table.insert(result, file) end @@ -268,7 +268,7 @@ function tools.use_downloader(url, filename, cache) if cfg.connection_timeout and cfg.connection_timeout > 0 then curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." " end - ok = fs.execute_string(curl_cmd..fs.Q(url).." 2> /dev/null 1> "..fs.Q(filename)) + ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename))) end if ok then return true, filename diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index c14c421b..74f3ed69 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -27,6 +27,13 @@ function win32.quiet(cmd) return cmd.." 2> NUL 1> NUL" end +--- Annotate command string for execution with quiet stderr. +-- @param cmd string: A command-line string. +-- @return string: The command-line, with stderr silencing annotation. +function win32.quiet_stderr(cmd) + return cmd.." 2> NUL" +end + local drive_letter = "[%.a-zA-Z]?:?[\\/]" local win_escape_chars = { diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index b9dce85c..39aa4ba1 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -39,7 +39,7 @@ end function tools.current_dir() local current = cfg.cache_pwd if not current then - local pipe = io.popen(fs.Q(vars.PWD).. " 2> NUL") + local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) current = pipe:read("*l") pipe:close() cfg.cache_pwd = current @@ -196,7 +196,7 @@ function tools.find(at) return {} end local result = {} - local pipe = io.popen(command_at(at, fs.Q(vars.FIND).." 2> NUL")) + local pipe = io.popen(command_at(at, fs.quiet_stderr(fs.Q(vars.FIND)))) for file in pipe:lines() do -- Windows find is a bit different local first_two = file:sub(1,2) @@ -278,7 +278,7 @@ function tools.use_downloader(url, filename, cache) if cfg.connection_timeout and cfg.connection_timeout > 0 then curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." " end - ok = fs.execute_string(curl_cmd..fs.Q(url).." 2> NUL 1> "..fs.Q(filename)) + ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename))) end if ok then return true, filename -- cgit v1.2.3-55-g6feb From 472e74d582055992cf1f5c75821e083131a895ac Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:01:31 +0300 Subject: Move common implementation of fs.use_downloader into fs.tools module --- Makefile.setup.inc | 2 +- src/luarocks/fs.lua | 5 +++- src/luarocks/fs/tools.lua | 56 +++++++++++++++++++++++++++++++++++++++++ src/luarocks/fs/unix/tools.lua | 45 --------------------------------- src/luarocks/fs/win32/tools.lua | 45 --------------------------------- 5 files changed, 61 insertions(+), 92 deletions(-) create mode 100644 src/luarocks/fs/tools.lua (limited to 'src') diff --git a/Makefile.setup.inc b/Makefile.setup.inc index 0a049bc4..092d0423 100644 --- a/Makefile.setup.inc +++ b/Makefile.setup.inc @@ -5,7 +5,7 @@ BINDIR ?= $(PREFIX)/bin LUADIR ?= $(PREFIX)/share/lua/$(LUA_VERSION)/ BIN_FILES = luarocks luarocks-admin -LUAROCKS_FILES = fs/unix/tools.lua fs/unix.lua fs/win32/tools.lua fs/win32.lua \ +LUAROCKS_FILES = fs/tools.lua fs/unix/tools.lua fs/unix.lua fs/win32/tools.lua fs/win32.lua \ fs/lua.lua persist.lua list.lua require.lua repos.lua dir.lua make_manifest.lua \ command_line.lua config_cmd.lua install.lua build/command.lua build/cmake.lua \ build/make.lua build/builtin.lua fetch/cvs.lua fetch/git.lua fetch/sscm.lua \ 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) -- Load platform-specific fallbacks for missing Lua modules local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") -if ok and fs_plat_tools then load_fns(fs_plat_tools) end +if ok and fs_plat_tools then + load_fns(fs_plat_tools) + load_fns(require("luarocks.fs.tools")) +end 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 @@ + +--- Common fs operations implemented with third-party tools. +local tools = {} + +local fs = require("luarocks.fs") +local dir = require("luarocks.dir") +local cfg = require("luarocks.cfg") + +local vars = cfg.variables + +--- Download a remote file. +-- @param url string: URL to be fetched. +-- @param filename string or nil: this function attempts to detect the +-- resulting local filename of the remote file as the basename of the URL; +-- if that is not correct (due to a redirection, for example), the local +-- filename can be given explicitly as this second argument. +-- @return (boolean, string): true and the filename on success, +-- false and the error message on failure. +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 ok + if cfg.downloader == "wget" then + local wget_cmd = fs.Q(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="..tonumber(cfg.connection_timeout).." --tries=1 " + end + if cache then + -- --timestamping is incompatible with --output-document, + -- but that's not a problem for our use cases. + 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 cfg.downloader == "curl" then + local curl_cmd = fs.Q(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 "..tonumber(cfg.connection_timeout).." " + end + ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename))) + end + if ok then + return true, filename + else + return false + end +end + +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) return fs.execute(vars.TEST, "-f", file) end ---- Download a remote file. --- @param url string: URL to be fetched. --- @param filename string or nil: this function attempts to detect the --- resulting local filename of the remote file as the basename of the URL; --- if that is not correct (due to a redirection, for example), the local --- filename can be given explicitly as this second argument. --- @return (boolean, string): true and the filename on success, --- false and the error message on failure. -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 ok - if cfg.downloader == "wget" then - local wget_cmd = fs.Q(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="..tonumber(cfg.connection_timeout).." --tries=1 " - end - if cache then - -- --timestamping is incompatible with --output-document, - -- but that's not a problem for our use cases. - 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 cfg.downloader == "curl" then - local curl_cmd = fs.Q(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 "..tonumber(cfg.connection_timeout).." " - end - ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename))) - end - if ok then - return true, filename - else - return false - end -end - function tools.chmod(pathname, mode) if mode then 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) return fs.execute(fs.Q(vars.TEST).." -f", file) end ---- Download a remote file. --- @param url string: URL to be fetched. --- @param filename string or nil: this function attempts to detect the --- resulting local filename of the remote file as the basename of the URL; --- if that is not correct (due to a redirection, for example), the local --- filename can be given explicitly as this second argument. --- @return (boolean, string): true and the filename on success, --- false and the error message on failure. -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 ok - if cfg.downloader == "wget" then - local wget_cmd = fs.Q(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="..tonumber(cfg.connection_timeout).." --tries=1 " - end - if cache then - -- --timestamping is incompatible with --output-document, - -- but that's not a problem for our use cases. - 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 cfg.downloader == "curl" then - local curl_cmd = fs.Q(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 "..tonumber(cfg.connection_timeout).." " - end - ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename))) - end - if ok then - return true, filename - else - return false - end -end - --- Uncompress gzip file. -- @param archive string: Filename of archive. -- @return boolean : success status -- cgit v1.2.3-55-g6feb From c79ce50f956d8d9f1454ec871dc7b0469f194c07 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:06:35 +0300 Subject: fs.tools: move common directory stack functions --- src/luarocks/fs/tools.lua | 47 +++++++++++++++++++++++++++++++++++++++++ src/luarocks/fs/unix/tools.lua | 46 ---------------------------------------- src/luarocks/fs/win32/tools.lua | 47 ----------------------------------------- 3 files changed, 47 insertions(+), 93 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 0283d8f7..08fa74a4 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -8,6 +8,53 @@ local cfg = require("luarocks.cfg") local vars = cfg.variables +local dir_stack = {} + +--- Obtain current directory. +-- Uses the module's internal directory stack. +-- @return string: the absolute pathname of the current directory. +function tools.current_dir() + local current = cfg.cache_pwd + if not current then + local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) + current = pipe:read("*l") + pipe:close() + cfg.cache_pwd = current + end + for _, directory in ipairs(dir_stack) do + current = fs.absolute_name(directory, current) + end + return current +end + +--- Change the current directory. +-- Uses the module's internal directory stack. This does not have exact +-- semantics of chdir, as it does not handle errors the same way, +-- but works well for our purposes for now. +-- @param directory string: The directory to switch to. +-- @return boolean or (nil, string): true if successful, (nil, error message) if failed. +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 + +--- Change directory to root. +-- Allows leaving a directory (e.g. for deleting it) in +-- a crossplatform way. +function tools.change_dir_to_root() + table.insert(dir_stack, "/") +end + +--- Change working directory to the previous in the directory stack. +function tools.pop_dir() + local directory = table.remove(dir_stack) + return directory ~= nil +end + --- Download a remote file. -- @param url string: URL to be fetched. -- @param filename string or nil: this function attempts to detect the diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 8eaa9361..2b5280d6 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -7,31 +7,12 @@ local fs = require("luarocks.fs") local dir = require("luarocks.dir") local cfg = require("luarocks.cfg") -local dir_stack = {} - local vars = cfg.variables local function command_at(directory, cmd) return "cd " .. fs.Q(fs.absolute_name(directory)) .. " && " .. cmd end ---- Obtain current directory. --- Uses the module's internal directory stack. --- @return string: the absolute pathname of the current directory. -function tools.current_dir() - local current = cfg.cache_pwd - if not current then - local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) - current = pipe:read("*l") - pipe:close() - cfg.cache_pwd = current - end - for _, directory in ipairs(dir_stack) do - current = fs.absolute_name(directory, current) - end - return current -end - --- Run the given command. -- The command is executed in the current directory in the directory stack. -- @param cmd string: No quoting/escaping is applied to the command. @@ -48,33 +29,6 @@ function tools.execute_string(cmd) end end ---- Change the current directory. --- Uses the module's internal directory stack. This does not have exact --- semantics of chdir, as it does not handle errors the same way, --- but works well for our purposes for now. --- @param directory string: The directory to switch to. -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 - ---- Change directory to root. --- Allows leaving a directory (e.g. for deleting it) in --- a crossplatform way. -function tools.change_dir_to_root() - table.insert(dir_stack, "/") -end - ---- Change working directory to the previous in the directory stack. -function tools.pop_dir() - local directory = table.remove(dir_stack) - return directory ~= nil -end - --- Create a directory if it does not already exist. -- If any of the higher levels in the path name does not exist -- too, they are created as well. diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index cc0da0e9..19038e1f 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -9,8 +9,6 @@ local fs = require("luarocks.fs") local dir = require("luarocks.dir") local cfg = require("luarocks.cfg") -local dir_stack = {} - local vars = cfg.variables --- Strip the last extension of a filename. @@ -33,23 +31,6 @@ local function command_at(directory, cmd) return cmd end ---- Obtain current directory. --- Uses the module's internal directory stack. --- @return string: the absolute pathname of the current directory. -function tools.current_dir() - local current = cfg.cache_pwd - if not current then - local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) - current = pipe:read("*l") - pipe:close() - cfg.cache_pwd = current - end - for _, directory in ipairs(dir_stack) do - current = fs.absolute_name(directory, current) - end - return current -end - --- Run the given command. -- The command is executed in the current directory in the directory stack. -- @param cmd string: No quoting/escaping is applied to the command. @@ -67,34 +48,6 @@ function tools.execute_string(cmd) end end ---- Change the current directory. --- Uses the module's internal directory stack. This does not have exact --- semantics of chdir, as it does not handle errors the same way, --- but works well for our purposes for now. --- @param directory string: The directory to switch to. --- @return boolean or (nil, string): true if successful, (nil, error message) if failed. -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 - ---- Change directory to root. --- Allows leaving a directory (e.g. for deleting it) in --- a crossplatform way. -function tools.change_dir_to_root() - table.insert(dir_stack, "/") -end - ---- Change working directory to the previous in the directory stack. -function tools.pop_dir() - local directory = table.remove(dir_stack) - return directory ~= nil -end - --- Create a directory if it does not already exist. -- If any of the higher levels in the path name does not exist -- too, they are created as well. -- cgit v1.2.3-55-g6feb From 1cb6bb91efe3af86335453221b0f7cdcd2cb3be5 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:12:16 +0300 Subject: fs.tools: move common 'get_md5' function --- src/luarocks/fs/tools.lua | 22 ++++++++++++++++++++++ src/luarocks/fs/unix/tools.lua | 22 ---------------------- src/luarocks/fs/win32/tools.lua | 22 ---------------------- 3 files changed, 22 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 08fa74a4..76b90383 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -100,4 +100,26 @@ function tools.use_downloader(url, filename, cache) end end +local md5_cmd = { + md5sum = fs.Q(vars.MD5SUM), + openssl = fs.Q(vars.OPENSSL).." md5", + md5 = fs.Q(vars.MD5), +} + +--- Get the MD5 checksum for a file. +-- @param file string: The file to be computed. +-- @return string: The MD5 checksum or nil + message +function tools.get_md5(file) + local cmd = md5_cmd[cfg.md5checker] + if not cmd then return nil, "no MD5 checker command configured" end + local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) + local computed = pipe:read("*a") + pipe:close() + if computed then + computed = computed:match("("..("%x"):rep(32)..")") + end + if computed then return computed end + return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) +end + return tools diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 2b5280d6..904dd0aa 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -228,28 +228,6 @@ function tools.unpack_archive(archive) return true end -local md5_cmd = { - md5sum = vars.MD5SUM, - openssl = vars.OPENSSL.." md5", - md5 = vars.MD5, -} - ---- Get the MD5 checksum for a file. --- @param file string: The file to be computed. --- @return string: The MD5 checksum -function tools.get_md5(file) - local cmd = md5_cmd[cfg.md5checker] - if not cmd then return nil, "no MD5 checker command configured" end - local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) - local computed = pipe:read("*a") - pipe:close() - if computed then - computed = computed:match("("..("%x"):rep(32)..")") - end - if computed then return computed end - return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) -end - function tools.get_permissions(filename) local pipe = io.popen(vars.STAT.." "..vars.STATFLAG.." "..fs.Q(filename)) local ret = pipe:read("*l") diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 19038e1f..f112ea3e 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -242,28 +242,6 @@ function tools.unpack_archive(archive) return true end -local md5_cmd = { - md5sum = fs.Q(vars.MD5SUM), - openssl = fs.Q(vars.OPENSSL).." md5", - md5 = fs.Q(vars.MD5), -} - ---- Get the MD5 checksum for a file. --- @param file string: The file to be computed. --- @return string: The MD5 checksum or nil + message -function tools.get_md5(file) - local cmd = md5_cmd[cfg.md5checker] - if not cmd then return nil, "no MD5 checker command configured" end - local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file))) - local computed = pipe:read("*a") - pipe:close() - if computed then - computed = computed:match("("..("%x"):rep(32)..")") - end - if computed then return computed end - return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file)) -end - --- Test for existance of a file. -- @param file string: filename to test -- @return boolean: true if file exists, false otherwise. -- cgit v1.2.3-55-g6feb From 319f8f619c9457c8ba426d8bbe0b1d2d74c02e5d Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:14:08 +0300 Subject: fs.win32.tools: move a local function closer to its usage --- src/luarocks/fs/win32/tools.lua | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index f112ea3e..aa405e14 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -11,17 +11,6 @@ local cfg = require("luarocks.cfg") local vars = cfg.variables ---- Strip the last extension of a filename. --- Example: "foo.tar.gz" becomes "foo.tar". --- If filename has no dots, returns it unchanged. --- @param filename string: The file name to strip. --- @return string: The stripped name. -local function strip_extension(filename) - assert(type(filename) == "string") - - return (filename:gsub("%.[^.]+$", "")) or filename -end - local function command_at(directory, cmd) local drive = directory:match("^([A-Za-z]:)") cmd = "cd " .. fs.Q(directory) .. " & " .. cmd @@ -195,6 +184,16 @@ function tools.is_file(file) return fs.execute(fs.Q(vars.TEST).." -f", file) end +--- Strip the last extension of a filename. +-- Example: "foo.tar.gz" becomes "foo.tar". +-- If filename has no dots, returns it unchanged. +-- @param filename string: The file name to strip. +-- @return string: The stripped name. +local function strip_extension(filename) + assert(type(filename) == "string") + return (filename:gsub("%.[^.]+$", "")) or filename +end + --- Uncompress gzip file. -- @param archive string: Filename of archive. -- @return boolean : success status -- cgit v1.2.3-55-g6feb From 470ca122e9120caac00cae44b472965a8828afb4 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:21:36 +0300 Subject: Expose tools.command_at function --- src/luarocks/fs/unix/tools.lua | 12 ++++++++---- src/luarocks/fs/win32/tools.lua | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 904dd0aa..807f8057 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -9,7 +9,11 @@ local cfg = require("luarocks.cfg") local vars = cfg.variables -local function command_at(directory, cmd) +--- Adds prefix to command to make it run from a directory. +-- @param directory string: Path to a directory. +-- @param cmd string: A command-line string. +-- @return string: The command-line with prefix. +function tools.command_at(directory, cmd) return "cd " .. fs.Q(fs.absolute_name(directory)) .. " && " .. cmd end @@ -21,7 +25,7 @@ end function tools.execute_string(cmd) local current = fs.current_dir() if not current then return false end - local code, err = os.execute(command_at(current, cmd)) + local code, err = os.execute(fs.command_at(current, cmd)) if code == 0 or code == true then return true else @@ -114,7 +118,7 @@ end -- @param at string: directory to list -- @return nil function tools.dir_iterator(at) - local pipe = io.popen(command_at(at, vars.LS)) + local pipe = io.popen(fs.command_at(at, vars.LS)) for file in pipe:lines() do if file ~= "." and file ~= ".." then coroutine.yield(file) @@ -137,7 +141,7 @@ function tools.find(at) return {} end local result = {} - local pipe = io.popen(command_at(at, fs.quiet_stderr(vars.FIND.." *"))) + local pipe = io.popen(fs.command_at(at, fs.quiet_stderr(vars.FIND.." *"))) for file in pipe:lines() do table.insert(result, file) end diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index aa405e14..f2d62f32 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -11,7 +11,11 @@ local cfg = require("luarocks.cfg") local vars = cfg.variables -local function command_at(directory, cmd) +--- Adds prefix to command to make it run from a directory. +-- @param directory string: Path to a directory. +-- @param cmd string: A command-line string. +-- @return string: The command-line with prefix. +function tools.command_at(directory, cmd) local drive = directory:match("^([A-Za-z]:)") cmd = "cd " .. fs.Q(directory) .. " & " .. cmd if drive then @@ -28,7 +32,7 @@ end function tools.execute_string(cmd) local current = fs.current_dir() if not current then return false end - cmd = command_at(current, cmd) + cmd = fs.command_at(current, cmd) local code = os.execute(cmd) if code == 0 or code == true then return true @@ -115,7 +119,7 @@ end -- @param at string: directory to list -- @return nil function tools.dir_iterator(at) - local pipe = io.popen(command_at(at, fs.Q(vars.LS))) + local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS))) for file in pipe:lines() do if file ~= "." and file ~= ".." then coroutine.yield(file) @@ -138,7 +142,7 @@ function tools.find(at) return {} end local result = {} - local pipe = io.popen(command_at(at, fs.quiet_stderr(fs.Q(vars.FIND)))) + local pipe = io.popen(fs.command_at(at, fs.quiet_stderr(fs.Q(vars.FIND)))) for file in pipe:lines() do -- Windows find is a bit different local first_two = file:sub(1,2) -- cgit v1.2.3-55-g6feb From 8630313d665904e21edcc954e4ca0d043b217665 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:22:59 +0300 Subject: Move common implementation of tools.execute_string --- src/luarocks/fs/tools.lua | 17 +++++++++++++++++ src/luarocks/fs/unix/tools.lua | 16 ---------------- src/luarocks/fs/win32/tools.lua | 17 ----------------- 3 files changed, 17 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 76b90383..6caa07f3 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -55,6 +55,23 @@ function tools.pop_dir() return directory ~= nil end +--- Run the given command. +-- The command is executed in the current directory in the directory stack. +-- @param cmd string: No quoting/escaping is applied to the command. +-- @return boolean: true if command succeeds (status code 0), false +-- otherwise. +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 + --- Download a remote file. -- @param url string: URL to be fetched. -- @param filename string or nil: this function attempts to detect the diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 807f8057..1bc307a4 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -17,22 +17,6 @@ function tools.command_at(directory, cmd) return "cd " .. fs.Q(fs.absolute_name(directory)) .. " && " .. cmd end ---- Run the given command. --- The command is executed in the current directory in the directory stack. --- @param cmd string: No quoting/escaping is applied to the command. --- @return boolean: true if command succeeds (status code 0), false --- otherwise. -function tools.execute_string(cmd) - local current = fs.current_dir() - if not current then return false end - local code, err = os.execute(fs.command_at(current, cmd)) - if code == 0 or code == true then - return true - else - return false - end -end - --- Create a directory if it does not already exist. -- If any of the higher levels in the path name does not exist -- too, they are created as well. diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index f2d62f32..f54ec9a5 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -24,23 +24,6 @@ function tools.command_at(directory, cmd) return cmd end ---- Run the given command. --- The command is executed in the current directory in the directory stack. --- @param cmd string: No quoting/escaping is applied to the command. --- @return boolean: true if command succeeds (status code 0), false --- otherwise. -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 - --- Create a directory if it does not already exist. -- If any of the higher levels in the path name does not exist -- too, they are created as well. -- cgit v1.2.3-55-g6feb From 69a6aff477fd794b3b594bc7b9f20775b8e07986 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:25:56 +0300 Subject: Move common implementation of tools.dir_iterator --- src/luarocks/fs/tools.lua | 14 ++++++++++++++ src/luarocks/fs/unix/tools.lua | 14 -------------- src/luarocks/fs/win32/tools.lua | 14 -------------- 3 files changed, 14 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 6caa07f3..ed51b545 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -72,6 +72,20 @@ function tools.execute_string(cmd) end end +--- Internal implementation function for fs.dir. +-- Yields a filename on each iteration. +-- @param at string: directory to list +-- @return nil +function tools.dir_iterator(at) + local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS))) + for file in pipe:lines() do + if file ~= "." and file ~= ".." then + coroutine.yield(file) + end + end + pipe:close() +end + --- Download a remote file. -- @param url string: URL to be fetched. -- @param filename string or nil: this function attempts to detect the diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 1bc307a4..84bd53fd 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -97,20 +97,6 @@ function tools.delete(arg) fs.execute_quiet(vars.RM, "-rf", arg) end ---- Internal implementation function for fs.dir. --- Yields a filename on each iteration. --- @param at string: directory to list --- @return nil -function tools.dir_iterator(at) - local pipe = io.popen(fs.command_at(at, vars.LS)) - for file in pipe:lines() do - if file ~= "." and file ~= ".." then - coroutine.yield(file) - end - end - pipe:close() -end - --- Recursively scan the contents of a directory. -- @param at string or nil: directory to scan (will be the current -- directory if none is given). diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index f54ec9a5..9cb6d47a 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -97,20 +97,6 @@ function tools.delete(arg) fs.execute_quiet("if exist "..fs.Q(arg.."\\").." ( RMDIR /S /Q "..fs.Q(arg).." ) else ( DEL /Q /F "..fs.Q(arg).." )") end ---- Internal implementation function for fs.dir. --- Yields a filename on each iteration. --- @param at string: directory to list --- @return nil -function tools.dir_iterator(at) - local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS))) - for file in pipe:lines() do - if file ~= "." and file ~= ".." then - coroutine.yield(file) - end - end - pipe:close() -end - --- Recursively scan the contents of a directory. -- @param at string or nil: directory to scan (will be the current -- directory if none is given). -- cgit v1.2.3-55-g6feb