From 70182a664e30bd73c72cd15e82118325d12d499b Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 8 Sep 2010 17:27:43 -0300 Subject: Modify algorithm selection of rocks tree and add --local. Drop old behavior in which the tree list was scanned to find the first writable one (which meant users ended up installing rocks locally). Now it always tries the last in the list (which is by default the global one) and warns users when they have no write permissions. (It picks the _last_ one so that the old behavior when running sudo remains the same). Also add --local as a shorthand to specify the local tree, as suggested by Steve Donovan. --- src/luarocks/build.lua | 6 ++++ src/luarocks/cfg.lua | 2 +- src/luarocks/command_line.lua | 72 ++++++++++++++++++++----------------------- src/luarocks/help.lua | 1 + src/luarocks/install.lua | 5 +++ src/luarocks/make.lua | 6 ++++ src/luarocks/remove.lua | 7 +++++ src/luarocks/rep.lua | 6 ++-- 8 files changed, 64 insertions(+), 41 deletions(-) diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index 62ac3808..c6c3a002 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -11,6 +11,7 @@ local fs = require("luarocks.fs") local dir = require("luarocks.dir") local deps = require("luarocks.deps") local manif = require("luarocks.manif") +local cfg = require("luarocks.cfg") help_summary = "Build/compile a rock." help_arguments = "{|| []}" @@ -269,6 +270,11 @@ function run(...) end assert(type(version) == "string" or not version) + if not flags["local"] and not fs.is_writable(cfg.root_dir) then + return nil, "Your user does not have write permissions in " .. cfg.root_dir .. + " \n-- you may want to run as a privileged user or use your local tree with --local." + end + if name:match("%.rockspec$") then return build_rockspec(name, true) elseif name:match("%.src%.rock$") then diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index 261411b7..67cd17b7 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua @@ -83,7 +83,7 @@ end -- Path configuration: -local sys_config_file, home_config_file, home_tree +local sys_config_file, home_config_file if detected.windows or detected.mingw32 then home = os.getenv("APPDATA") or "c:" sys_config_file = "c:/luarocks/config.lua" diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua index d501d121..71ded87d 100644 --- a/src/luarocks/command_line.lua +++ b/src/luarocks/command_line.lua @@ -33,6 +33,14 @@ local function is_writable(tree) end end +local function use_tree(tree) + cfg.root_dir = tree + cfg.rocks_dir = path.rocks_dir(tree) + cfg.deploy_bin_dir = path.deploy_bin_dir(tree) + cfg.deploy_lua_dir = path.deploy_lua_dir(tree) + cfg.deploy_lib_dir = path.deploy_lib_dir(tree) +end + --- Main command-line processor. -- Parses input arguments and calls the appropriate driver function -- to execute the action requested on the command-line, forwarding @@ -58,30 +66,39 @@ function run_command(...) local nonflags = { util.parse_flags(unpack(args)) } local flags = table.remove(nonflags, 1) cfg.flags = flags + + local command + + if flags["version"] then + print(program_name.." "..cfg.program_version) + print(program_description) + print() + os.exit(0) + elseif flags["help"] or #nonflags == 0 then + command = "help" + args = nonflags + else + command = nonflags[1] + for i, arg in ipairs(args) do + if arg == command then + table.remove(args, i) + break + end + end + end + command = command:gsub("-", "_") if flags["to"] then if flags["to"] == true then die("Argument error: use --to=") end local root_dir = fs.absolute_name(flags["to"]) - cfg.root_dir = root_dir - cfg.rocks_dir = path.rocks_dir(root_dir) - cfg.deploy_bin_dir = path.deploy_bin_dir(root_dir) - cfg.deploy_lua_dir = path.deploy_lua_dir(root_dir) - cfg.deploy_lib_dir = path.deploy_lib_dir(root_dir) + use_tree(root_dir) + elseif flags["local"] then + use_tree(cfg.home_tree) else local trees = cfg.rocks_trees - for i = #trees, 1, -1 do - local tree = trees[i] - if is_writable(tree) then - cfg.root_dir = tree - cfg.rocks_dir = path.rocks_dir(tree) - cfg.deploy_bin_dir = path.deploy_bin_dir(tree) - cfg.deploy_lua_dir = path.deploy_lua_dir(tree) - cfg.deploy_lib_dir = path.deploy_lib_dir(tree) - break - end - end + use_tree(trees[#trees]) end if type(cfg.root_dir) == "string" then @@ -111,34 +128,13 @@ function run_command(...) end cfg.rocks_servers = { flags["only-from"] } end - - local command - - if flags["version"] then - print(program_name.." "..cfg.program_version) - print(program_description) - print() - os.exit(0) - elseif flags["help"] or #nonflags == 0 then - command = "help" - args = nonflags - else - command = nonflags[1] - for i, arg in ipairs(args) do - if arg == command then - table.remove(args, i) - break - end - end - end - + if command ~= "help" then for k, v in pairs(cmdline_vars) do cfg.variables[k] = v end end - command = command:gsub("-", "_") if commands[command] then local xp, ok, err = xpcall(function() return commands[command].run(unpack(args)) end, function(err) die(debug.traceback("LuaRocks "..cfg.program_version diff --git a/src/luarocks/help.lua b/src/luarocks/help.lua index 04fea7ce..5162b917 100644 --- a/src/luarocks/help.lua +++ b/src/luarocks/help.lua @@ -40,6 +40,7 @@ can be overriden with VAR=VALUE assignments. --only-from= Fetch rocks/rockspecs from this server only (overrides any entries in the config file) --to= Which tree to operate on. +--local Use the tree in the user's home directory. Supported commands: ]]) diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua index 260c7751..74602f47 100644 --- a/src/luarocks/install.lua +++ b/src/luarocks/install.lua @@ -99,6 +99,11 @@ function run(...) return nil, "Argument missing, see help." end + if not flags["local"] and not fs.is_writable(cfg.root_dir) then + return nil, "Your user does not have write permissions in " .. cfg.root_dir .. + " \n-- you may want to run as a privileged user or use your local tree with --local." + end + if name:match("%.rockspec$") or name:match("%.src%.rock$") then local build = require("luarocks.build") return build.run(name) diff --git a/src/luarocks/make.lua b/src/luarocks/make.lua index cb2ba374..ec3772ac 100644 --- a/src/luarocks/make.lua +++ b/src/luarocks/make.lua @@ -8,6 +8,7 @@ module("luarocks.make", package.seeall) local build = require("luarocks.build") local fs = require("luarocks.fs") local util = require("luarocks.util") +local cfg = require("luarocks.cfg") help_summary = "Compile package in current directory using a rockspec." help_arguments = "[]" @@ -30,6 +31,11 @@ To install rocks, you'll normally want to use the "install" and function run(...) local flags, rockspec = util.parse_flags(...) assert(type(rockspec) == "string" or not rockspec) + + if not flags["local"] and not fs.is_writable(cfg.root_dir) then + return nil, "Your user does not have write permissions in " .. cfg.root_dir .. + " \n-- you may want to run as a privileged user or use your local tree with --local." + end if not rockspec then local files = fs.list_dir(fs.current_dir()) diff --git a/src/luarocks/remove.lua b/src/luarocks/remove.lua index afd299ae..d77f28fa 100644 --- a/src/luarocks/remove.lua +++ b/src/luarocks/remove.lua @@ -11,6 +11,7 @@ local path = require("luarocks.path") local util = require("luarocks.util") local cfg = require("luarocks.cfg") local manif = require("luarocks.manif") +local fs = require("luarocks.fs") help_summary = "Uninstall a rock." help_arguments = "[--force] []" @@ -81,6 +82,12 @@ function run(...) if type(name) ~= "string" then return nil, "Argument missing, see help." end + + if not flags["local"] and not fs.is_writable(cfg.rocks_dir) then + return nil, "Your user does not have write permissions in " .. cfg.rocks_dir .. + " \n-- you may want to run as a privileged user or use your local tree with --local." + end + local results = {} search.manifest_search(results, cfg.rocks_dir, search.make_query(name, version)) diff --git a/src/luarocks/rep.lua b/src/luarocks/rep.lua index 5da5953f..322ab166 100644 --- a/src/luarocks/rep.lua +++ b/src/luarocks/rep.lua @@ -252,10 +252,11 @@ function delete_version(name, version) local target = dir.path(deploy_dir, parent_path, file) local versioned = path.versioned_name(target, deploy_dir, name, version) if fs.exists(versioned) then - fs.delete(versioned) + local ok = fs.delete(versioned) fs.remove_dir_tree_if_empty(dir.dir_name(versioned)) + if not ok then return nil, "Failed deleting "..versioned end else - fs.delete(target) + local ok = fs.delete(target) local next_name, next_version = manif.find_next_provider(target) if next_name then local versioned = path.versioned_name(target, deploy_dir, next_name, next_version) @@ -263,6 +264,7 @@ function delete_version(name, version) fs.remove_dir_tree_if_empty(dir.dir_name(versioned)) end fs.remove_dir_tree_if_empty(dir.dir_name(target)) + if not ok then return nil, "Failed deleting "..target end end return true end -- cgit v1.2.3-55-g6feb