Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
*.pyc
build/
elevate.egg-info/
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ new console window. To suppress this window, use
``elevate(show_console=False)``.

On Linux and macOS, graphical prompts are tried before ``sudo`` by default. To
prevent graphical prompts, use ``elevate(graphical=False)``.
prevent graphical prompts, use ``elevate(graphical=False)``.

For macOS non-graphical prompts (i.e. sudo) you can use
``elevate(graphical=False, preserve_env=True)`` and the relaunch as root will
preserve the shell environment.
1 change: 1 addition & 0 deletions elevate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
..rfbrost.SOFTWARE.PYTHON.ELEVATE.elevate
5 changes: 3 additions & 2 deletions elevate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys


def elevate(show_console=True, graphical=True):
def elevate(show_console=True, graphical=True, preserve_env=False):
"""
Re-launch the current process with root/admin privileges

Expand All @@ -19,5 +19,6 @@ def elevate(show_console=True, graphical=True):
from elevate.windows import elevate
else:
from elevate.posix import elevate
elevate(show_console, graphical)

elevate(show_console, graphical, preserve_env)

17 changes: 11 additions & 6 deletions elevate/posix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import errno
import os
import sys

try:
from shlex import quote
except ImportError:
Expand All @@ -12,7 +13,7 @@ def quote_shell(args):


def quote_applescript(string):
charmap = {
charmap: str = {
"\n": "\\n",
"\r": "\\r",
"\t": "\\t",
Expand All @@ -22,12 +23,12 @@ def quote_applescript(string):
return '"%s"' % "".join(charmap.get(char, char) for char in string)


def elevate(show_console=True, graphical=True):
def elevate(show_console=True, graphical=True, preserve_env=False):
if os.getuid() == 0:
return

args = [sys.executable] + sys.argv
commands = []
args: str = [sys.executable] + sys.argv
commands: list = []

if graphical:
if sys.platform.startswith("darwin"):
Expand All @@ -44,11 +45,15 @@ def elevate(show_console=True, graphical=True):
commands.append(["gksudo"] + args)
commands.append(["kdesudo"] + args)

commands.append(["sudo"] + args)
if preserve_env:
pythonpath = os.environ.get('PYTHONPATH', '')
commands.append(["sudo", "-E", f"PYTHONPATH={pythonpath}"] + args)
else:
commands.append(["sudo"] + args)

for args in commands:
try:
os.execlp(args[0], *args)
except OSError as e:
if e.errno != errno.ENOENT or args[0] == "sudo":
raise
raise