From f4ca07490d68ee583ec14a973b5d9a94d17daf19 Mon Sep 17 00:00:00 2001 From: sezanzeb Date: Tue, 1 Dec 2020 11:01:11 +0100 Subject: [PATCH] ubuntu fixes --- DEBIAN/control | 2 +- keymapper/config.py | 2 +- keymapper/dev/permissions.py | 5 +++-- keymapper/paths.py | 16 +++++++++++++--- tests/testcases/permissions.py | 3 ++- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/DEBIAN/control b/DEBIAN/control index 1a4ac411..3458fa73 100644 --- a/DEBIAN/control +++ b/DEBIAN/control @@ -2,5 +2,5 @@ Package: key-mapper Version: 0.1.0 Architecture: all Maintainer: Sezanzeb -Depends: python3, python3-setuptools, python3-evdev, python3-dbus +Depends: build-essential, libpython3-dev, libdbus-1-dev, python3, python3-setuptools, python3-evdev, python3-dbus, python3-gi Description: A tool to change the mapping of your input device buttons diff --git a/keymapper/config.py b/keymapper/config.py index 6a8311c1..67cec39c 100644 --- a/keymapper/config.py +++ b/keymapper/config.py @@ -153,7 +153,7 @@ class _Config: with open(CONFIG_PATH, 'w') as file: json.dump(self._config, file, indent=4) logger.info('Saved config to %s', CONFIG_PATH) - shutil.chown(CONFIG_PATH, os.getlogin(), os.getlogin()) + shutil.chown(CONFIG_PATH, USER, USER) file.write('\n') diff --git a/keymapper/dev/permissions.py b/keymapper/dev/permissions.py index b653dfaf..fd9acd08 100644 --- a/keymapper/dev/permissions.py +++ b/keymapper/dev/permissions.py @@ -27,13 +27,14 @@ import grp import getpass from keymapper.logger import logger +from keymapper.paths import USER def can_read_devices(): """If the people ever looks into the console, make sure to help them.""" is_root = getpass.getuser() == 'root' - is_in_input_group = os.getlogin() in grp.getgrnam('input').gr_mem - is_in_plugdev_group = os.getlogin() in grp.getgrnam('plugdev').gr_mem + is_in_input_group = USER in grp.getgrnam('input').gr_mem + is_in_plugdev_group = USER in grp.getgrnam('plugdev').gr_mem def warn(group): logger.warning( diff --git a/keymapper/paths.py b/keymapper/paths.py index 6c0edd5b..9879954c 100644 --- a/keymapper/paths.py +++ b/keymapper/paths.py @@ -28,7 +28,17 @@ import shutil from keymapper.logger import logger -CONFIG = os.path.join('/home', os.getlogin(), '.config/key-mapper') + +# try to find the user who called sudo +try: + USER = os.getlogin() +except OSError: + # failed in some ubuntu installations + USER = os.environ['USER'] + if USER == 'root': + USER = os.envron.get('SUDO_USER', USER) + +CONFIG = os.path.join('/home', USER, '.config/key-mapper') def touch(path, log=True): @@ -42,7 +52,7 @@ def touch(path, log=True): mkdir(os.path.dirname(path), log=False) os.mknod(path) - shutil.chown(path, os.getlogin(), os.getlogin()) + shutil.chown(path, USER, USER) def mkdir(path, log=True): @@ -59,7 +69,7 @@ def mkdir(path, log=True): mkdir(base, log=False) os.makedirs(path) - shutil.chown(path, os.getlogin(), os.getlogin()) + shutil.chown(path, USER, USER) def get_config_path(device=None, preset=None): diff --git a/tests/testcases/permissions.py b/tests/testcases/permissions.py index 9003c9e4..b04c353e 100644 --- a/tests/testcases/permissions.py +++ b/tests/testcases/permissions.py @@ -24,6 +24,7 @@ import grp import unittest from keymapper.dev.permissions import can_read_devices +from keymapper.paths import USER class TestPermissions(unittest.TestCase): @@ -44,7 +45,7 @@ class TestPermissions(unittest.TestCase): def test_can_access(self): class Grnam: def __init__(self, group): - self.gr_mem = [os.getlogin()] + self.gr_mem = [USER] grp.getgrnam = Grnam self.assertTrue(can_read_devices())