2021-03-21 18:15:20 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
2022-01-01 12:52:33 +00:00
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
2021-03-21 18:15:20 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2021-03-21 18:15:20 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2021-03-21 18:15:20 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
2021-03-21 18:15:20 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2022-01-01 12:00:49 +00:00
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
"""Figure out the user."""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import getpass
|
|
|
|
import pwd
|
|
|
|
|
|
|
|
|
|
|
|
def get_user():
|
|
|
|
"""Try to find the user who called sudo/pkexec."""
|
|
|
|
try:
|
|
|
|
return os.getlogin()
|
|
|
|
except OSError:
|
|
|
|
# failed in some ubuntu installations and in systemd services
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2021-09-26 10:44:56 +00:00
|
|
|
user = os.environ["USER"]
|
2021-03-21 18:15:20 +00:00
|
|
|
except KeyError:
|
|
|
|
# possibly the systemd service. no sudo was used
|
|
|
|
return getpass.getuser()
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
if user == "root":
|
2021-03-21 18:15:20 +00:00
|
|
|
try:
|
2021-09-26 10:44:56 +00:00
|
|
|
return os.environ["SUDO_USER"]
|
2021-03-21 18:15:20 +00:00
|
|
|
except KeyError:
|
|
|
|
# no sudo was used
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2021-09-26 10:44:56 +00:00
|
|
|
pkexec_uid = int(os.environ["PKEXEC_UID"])
|
2021-03-21 18:15:20 +00:00
|
|
|
return pwd.getpwuid(pkexec_uid).pw_name
|
|
|
|
except KeyError:
|
|
|
|
# no pkexec was used or the uid is unknown
|
|
|
|
pass
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
2021-08-31 21:35:21 +00:00
|
|
|
def get_home(user):
|
|
|
|
"""Try to find the user's home directory."""
|
|
|
|
return pwd.getpwnam(user).pw_dir
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
USER = get_user()
|
|
|
|
|
2021-08-31 21:35:21 +00:00
|
|
|
HOME = get_home(USER)
|