summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-05-24 11:45:37 +0800
committerruki <[email protected]>2017-05-24 11:45:37 +0800
commit4e52205dc696ade5ba01c8d6ccf9831b59ad6196 (patch)
tree2a2e2065ed0f65b019168d2e3f099363d4b96a26
parent0041140dbfebce83293bcd3991760c8651f7a7ee (diff)
add privilege.sudo and find sudo modules
-rw-r--r--xmake/actions/install/install.lua2
-rw-r--r--xmake/actions/install/install_admin.lua2
-rw-r--r--xmake/actions/install/main.lua9
-rw-r--r--xmake/actions/uninstall/main.lua9
-rw-r--r--xmake/actions/uninstall/uninstall.lua2
-rw-r--r--xmake/actions/uninstall/uninstall_admin.lua2
-rw-r--r--xmake/core/sandbox/modules/os.lua73
-rw-r--r--xmake/modules/detect/tool/find_sudo.lua55
-rw-r--r--xmake/modules/privilege/sudo.lua146
9 files changed, 215 insertions, 85 deletions
diff --git a/xmake/actions/install/install.lua b/xmake/actions/install/install.lua
index c1e1d9c3d..b9bd6ca79 100644
--- a/xmake/actions/install/install.lua
+++ b/xmake/actions/install/install.lua
@@ -74,7 +74,7 @@ function _install_target_and_deps(target)
end
-- install targets
-function install(targetname)
+function main(targetname)
-- init finished states
_g.finished = {}
diff --git a/xmake/actions/install/install_admin.lua b/xmake/actions/install/install_admin.lua
index 386911027..f641628ea 100644
--- a/xmake/actions/install/install_admin.lua
+++ b/xmake/actions/install/install_admin.lua
@@ -53,7 +53,7 @@ function main(targetname, installdir)
end
-- install target
- install.install(targetname)
+ install(targetname)
-- restore the previous option context
option.restore()
diff --git a/xmake/actions/install/main.lua b/xmake/actions/install/main.lua
index ae98f3eea..976478a76 100644
--- a/xmake/actions/install/main.lua
+++ b/xmake/actions/install/main.lua
@@ -27,6 +27,7 @@ import("core.base.option")
import("core.project.task")
import("core.platform.platform")
import("core.base.privilege")
+import("privilege.sudo")
import("install")
-- main
@@ -47,7 +48,7 @@ function main()
function ()
-- install target
- install.install(targetname or ifelse(option.get("all"), "__all", "__def"))
+ install(targetname or ifelse(option.get("all"), "__all", "__def"))
-- trace
cprint("${bright}install ok!${clear}${ok_hand}")
@@ -65,7 +66,7 @@ function main()
function ()
-- install target
- install.install(targetname or ifelse(option.get("all"), "__all", "__def"))
+ install(targetname or ifelse(option.get("all"), "__all", "__def"))
-- trace
cprint("${bright}install ok!${clear}${ok_hand}")
@@ -86,7 +87,7 @@ function main()
cprint("${bright red}error: ${default red}installation failed, may permission denied!")
-- continue to install with administrator permission?
- if os.feature("sudo") then
+ if sudo.has() then
-- show tips
cprint("${bright yellow}note: ${default yellow}try continue to install with administrator permission again?")
@@ -98,7 +99,7 @@ function main()
if answer == 'y' or answer == '' then
-- install target with administrator permission
- os.sudol(os.runv, path.join(os.scriptdir(), "install_admin.lua"), {targetname or ifelse(option.get("all"), "__all", "__def"), option.get("installdir")})
+ sudo.runl(path.join(os.scriptdir(), "install_admin.lua"), {targetname or ifelse(option.get("all"), "__all", "__def"), option.get("installdir")})
-- trace
cprint("${bright}install ok!${clear}${ok_hand}")
diff --git a/xmake/actions/uninstall/main.lua b/xmake/actions/uninstall/main.lua
index a0460dbe2..36b113181 100644
--- a/xmake/actions/uninstall/main.lua
+++ b/xmake/actions/uninstall/main.lua
@@ -27,6 +27,7 @@ import("core.base.option")
import("core.project.task")
import("core.platform.platform")
import("core.base.privilege")
+import("privilege.sudo")
import("uninstall")
-- main
@@ -47,7 +48,7 @@ function main()
function ()
-- uninstall target
- uninstall.uninstall(targetname)
+ uninstall(targetname)
-- trace
cprint("${bright}uninstall ok!${clear}${ok_hand}")
@@ -65,7 +66,7 @@ function main()
function ()
-- uninstall target
- uninstall.uninstall(targetname)
+ uninstall(targetname)
-- trace
cprint("${bright}uninstall ok!${clear}${ok_hand}")
@@ -86,7 +87,7 @@ function main()
cprint("${bright red}error: ${default red}failed to uninstall, may permission denied!")
-- continue to uninstall with administrator permission?
- if os.feature("sudo") then
+ if sudo.has() then
-- show tips
cprint("${bright yellow}note: ${default yellow}try continue to uninstall with administrator permission again?")
@@ -98,7 +99,7 @@ function main()
if answer == 'y' or answer == '' then
-- uninstall target with administrator permission
- os.sudol(os.runv, path.join(os.scriptdir(), "uninstall_admin.lua"), {targetname or "__all", option.get("installdir")})
+ sudo.runl(path.join(os.scriptdir(), "uninstall_admin.lua"), {targetname or "__all", option.get("installdir")})
-- trace
cprint("${bright}uninstall ok!${clear}${ok_hand}")
diff --git a/xmake/actions/uninstall/uninstall.lua b/xmake/actions/uninstall/uninstall.lua
index 6ab3d6cc1..a3a8b869f 100644
--- a/xmake/actions/uninstall/uninstall.lua
+++ b/xmake/actions/uninstall/uninstall.lua
@@ -74,7 +74,7 @@ function _uninstall_target_and_deps(target)
end
-- uninstall
-function uninstall(targetname)
+function main(targetname)
-- init finished states
_g.finished = {}
diff --git a/xmake/actions/uninstall/uninstall_admin.lua b/xmake/actions/uninstall/uninstall_admin.lua
index 8dded1cce..891b4a857 100644
--- a/xmake/actions/uninstall/uninstall_admin.lua
+++ b/xmake/actions/uninstall/uninstall_admin.lua
@@ -53,7 +53,7 @@ function main(targetname, installdir)
end
-- uninstall target
- uninstall.uninstall(ifelse(targetname ~= "__all", targetname, nil))
+ uninstall(ifelse(targetname ~= "__all", targetname, nil))
-- restore the previous option context
option.restore()
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 083181c00..00e22bfbb 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -464,79 +464,6 @@ function sandbox_os.uuid(name)
return uuid
end
--- get the feature of os
-function sandbox_os.feature(name)
-
- -- has 'sudo' feature?
- if name == "sudo" then
- return sandbox_os.host() ~= "windows"
- end
-end
-
--- sudo run shell with administrator permission
---
--- .e.g
--- os.sudo(os.run, "echo", "hello xmake!")
---
-function sandbox_os.sudo(runner, cmd, ...)
-
- -- check
- assert(sandbox_os.feature("sudo"), "no sudo!")
-
- -- run it with administrator permission
- if sandbox_os.host() == "windows" then
-
- -- TODO wrap sudo.bat
- runner(cmd, ...)
- else
- -- run it with administrator permission and preserve parent environment
- runner("sudo -E " .. cmd, ...)
- end
-end
-
--- sudo run shell with administrator permission and arguments list
---
--- .e.g
--- os.sudov(os.runv, {"echo", "hello xmake!"})
---
-function sandbox_os.sudov(runner, shellname, argv)
-
- -- check
- assert(sandbox_os.feature("sudo"), "no sudo!")
-
- -- run it with administrator permission
- if sandbox_os.host() == "windows" then
-
- -- TODO wrap sudo.bat
- runner(shellname, argv)
- else
- -- run it with administrator permission and preserve parent environment
- runner("sudo", table.join("-E", shellname, argv))
- end
-end
-
--- sudo run lua script with administrator permission and arguments list
---
--- .e.g
--- os.sudol(os.runv, "xxx.lua", {"arg1", "arg2"})
---
-function sandbox_os.sudol(runner, luafile, luaargv)
-
- -- init argv
- local argv = {"lua", "--root"}
- for _, name in ipairs({"file", "project", "backtrace", "verbose", "quiet"}) do
- local value = option.get(name)
- if type(value) == "string" then
- table.insert(argv, "--" .. name .. "=" .. value)
- elseif value then
- table.insert(argv, "--" .. name)
- end
- end
-
- -- run it with administrator permission
- sandbox_os.sudov(runner, path.join(sandbox_os.programdir(), "xmake"), table.join(argv, luafile, luaargv))
-end
-
-- get versioninfo
function sandbox_os.versioninfo()
return os.versioninfo()
diff --git a/xmake/modules/detect/tool/find_sudo.lua b/xmake/modules/detect/tool/find_sudo.lua
new file mode 100644
index 000000000..32bd86456
--- /dev/null
+++ b/xmake/modules/detect/tool/find_sudo.lua
@@ -0,0 +1,55 @@
+--!The Make-like Build Utility based on Lua
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015 - 2017, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file find_sudo.lua
+--
+
+-- imports
+import("lib.detect.find_program")
+import("lib.detect.find_programver")
+
+-- find sudo
+--
+-- @param opt the argument options, .e.g {version = true}
+--
+-- @return program, version
+--
+-- @code
+--
+-- local sudo = find_sudo()
+-- local sudo, version = find_sudo({version = true})
+--
+-- @endcode
+--
+function main(opt)
+
+ -- find program
+ local program = find_program("sudo", { "/usr/bin", "/usr/local/bin"})
+
+ -- find program version
+ local version = nil
+ if program and opt and opt.version then
+ version = find_programver(program)
+ end
+
+ -- ok?
+ return program, version
+end
diff --git a/xmake/modules/privilege/sudo.lua b/xmake/modules/privilege/sudo.lua
new file mode 100644
index 000000000..aba3a0362
--- /dev/null
+++ b/xmake/modules/privilege/sudo.lua
@@ -0,0 +1,146 @@
+--!The Make-like Build Utility based on Lua
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015 - 2017, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file sudo.lua
+--
+
+-- imports
+import("core.base.option")
+import("detect.tool.find_sudo")
+
+-- sudo run shell with administrator permission
+--
+-- .e.g
+-- _sudo(os.run, "echo", "hello xmake!")
+--
+function _sudo(runner, cmd, ...)
+
+ -- find sudo
+ local program = find_sudo()
+ assert(program, "sudo not found!")
+
+ -- TODO handle -E on ubuntu
+
+ -- run it with administrator permission and preserve parent environment
+ runner(program .. " -E " .. cmd, ...)
+end
+
+-- sudo run shell with administrator permission and arguments list
+--
+-- .e.g
+-- _sudov(os.runv, {"echo", "hello xmake!"})
+--
+function _sudov(runner, shellname, argv)
+
+ -- find sudo
+ local program = find_sudo()
+ assert(program, "sudo not found!")
+
+ -- run it with administrator permission and preserve parent environment
+ runner(program, table.join("-E", shellname, argv))
+end
+
+-- sudo run lua script with administrator permission and arguments list
+--
+-- .e.g
+-- _lua(os.runv, "xxx.lua", {"arg1", "arg2"})
+--
+function _lua(runner, luafile, luaargv)
+
+ -- init argv
+ local argv = {"lua", "--root"}
+ for _, name in ipairs({"file", "project", "backtrace", "verbose", "quiet"}) do
+ local value = option.get(name)
+ if type(value) == "string" then
+ table.insert(argv, "--" .. name .. "=" .. value)
+ elseif value then
+ table.insert(argv, "--" .. name)
+ end
+ end
+
+ -- run it with administrator permission
+ _sudov(runner, "xmake", table.join(argv, luafile, luaargv))
+end
+
+-- has sudo?
+function has()
+ return find_sudo() ~= nil
+end
+
+-- sudo run shell
+function run(cmd, ...)
+ return _sudo(os.run, cmd, ...)
+end
+
+-- sudo run shell with arguments list
+function runv(shellname, argv)
+ return _sudo(os.run, shellname, argv)
+end
+
+-- sudo quietly run shell and echo verbose info if [-v|--verbose] option is enabled
+function vrun(cmd, ...)
+ return _sudo(os.vrun, cmd, ...)
+end
+
+-- sudo quietly run shell with arguments list and echo verbose info if [-v|--verbose] option is enabled
+function vrunv(shellname, argv)
+ return _sudo(os.vrunv, shellname, argv)
+end
+
+-- sudo run shell and return output and error data
+function iorun(cmd, ...)
+ return _sudo(os.iorun, cmd, ...)
+end
+
+-- sudo run shell and return output and error data
+function iorunv(shellname, argv)
+ return _sudo(os.iorunv, shellname, argv)
+end
+
+-- sudo execute shell
+function exec(cmd, ...)
+ return _sudo(os.exec, cmd, ...)
+end
+
+-- sudo execute shell with arguments list
+function execv(shellname, argv)
+ return _sudo(os.execv, shellname, argv)
+end
+
+-- sudo run lua script
+function runl(luafile, luaargv)
+ return _lua(os.runv, luafile, luaargv)
+end
+
+-- sudo quietly run lua script and echo verbose info if [-v|--verbose] option is enabled
+function vrunl(luafile, luaargv)
+ return _lua(os.vrunv, luafile, luaargv)
+end
+
+-- sudo run lua script and return output and error data
+function iorunl(luafile, luaargv)
+ return _lua(os.iorunv, luafile, luaargv)
+end
+
+-- sudo execute lua script
+function execl(luafile, luaargv)
+ return _lua(os.execv, luafile, luaargv)
+end