Adjusted running as root checks

* Suppress pylint errors when checking uid/euid/gid
  * Helpful when checking under Windows
* Allow running wk.exe.stop_process() under Windows
This commit is contained in:
2Shirt 2020-04-26 16:24:35 -06:00
parent d0d74b8763
commit 6c775bbba7
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
2 changed files with 11 additions and 11 deletions

View file

@ -80,8 +80,9 @@ def build_cmd_kwargs(cmd, minimized=False, pipe=True, shell=False, **kwargs):
}
# Strip sudo if appropriate
if cmd[0] == 'sudo' and os.name == 'posix' and os.geteuid() == 0:
cmd.pop(0)
if cmd[0] == 'sudo':
if os.name == 'posix' and os.geteuid() == 0: # pylint: disable=no-member
cmd.pop(0)
# Add additional kwargs if applicable
for key in 'check cwd encoding errors stderr stdin stdout'.split():
@ -221,21 +222,20 @@ def stop_process(proc, graceful=True):
NOTES: proc should be a subprocess.Popen obj.
If graceful is True then a SIGTERM is sent before SIGKILL.
"""
running_as_root = os.geteuid() == 0
# Graceful exit
if graceful:
if running_as_root:
proc.terminate()
else:
if os.name == 'posix' and os.geteuid() != 0: # pylint: disable=no-member
run_program(['sudo', 'kill', str(proc.pid)], check=False)
else:
proc.terminate()
time.sleep(2)
# Force exit
if running_as_root:
proc.kill()
else:
if os.name == 'posix' and os.geteuid() != 0: # pylint: disable=no-member
run_program(['sudo', 'kill', '-9', str(proc.pid)], check=False)
else:
proc.kill()
def wait_for_procs(name, exact=True, timeout=None):

View file

@ -122,8 +122,8 @@ def mount_network_share(details, mount_point=None, read_write=False):
'-t', 'cifs',
'-o', (
f'{"rw" if read_write else "ro"}'
f',uid={os.getuid()}'
f',gid={os.getgid()}'
f',uid={os.getuid()}' # pylint: disable=no-member
f',gid={os.getgid()}' # pylint: disable=no-member
f',username={username}'
f',{"password=" if password else "guest"}{password}'
),