From fcba4e75b4594c018288557793a0fe5e530fc360 Mon Sep 17 00:00:00 2001 From: QwertyChouskie Date: Tue, 17 Sep 2024 21:09:36 -0700 Subject: [PATCH] Add function to add user to `dialout`/`uucp` --- REVHubInterface/__main__.py | 6 +++++- REVHubInterface/serialaccess.py | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/REVHubInterface/__main__.py b/REVHubInterface/__main__.py index 18bb3ed..a938a8c 100644 --- a/REVHubInterface/__main__.py +++ b/REVHubInterface/__main__.py @@ -16,7 +16,11 @@ # print(platform.system) # tkinter.messagebox.showerror('Drivers Not Detected', 'Please verify the correct drivers are installed. Without the correct dirvers, firmware update functionality will be unavailable.\n\n - Windows 10 and above should automatically install the correct drivers when the Expansion Hub is plugged in.\n\n - Windows 7 requires a manual install. Please see this link for the correct driver (FTDI D2xx): https://www.ftdichip.com/Drivers/CDM/CDM21228_Setup.zip\n\n - On macOS, install libftdi via Homebrew: "brew install libftdi"\n\n - On Linux, install libftdi. On Debian/Ubuntu-based systems, install it via "sudo apt install libftdi1"\n\nException Message:\n' + str(e)) if not serialaccess.hasAccess(): - tkinter.messagebox.showerror("User does not have serial access", "Your user does not have access to serial. Switch to a user that does, or add your user to a group that has serial access.\nhttps://github.com/unofficial-rev-port/REVHubInterface/blob/main/README.md#access-to-serial-on-linux") + if tkinter.messagebox.askyesno("User does not have serial access", "Your user likely does not have access to serial. Switch to a user that does, or add your user to a group that has serial access. For more details, see our documentation:\n\nhttps://github.com/unofficial-rev-port/REVHubInterface/blob/main/README.md#access-to-serial-on-linux\n\nWould you like to configure your user to have serial access?"): + try: + serialaccess.getAccess() + except Exception as e: + tkinter.messagebox.showerror(message = "Failed to add the user to the group! Please report this error to the developers:\n\n" + str(e)) def error(windowName: str, error: Exception) -> None: errName = str(error) diff --git a/REVHubInterface/serialaccess.py b/REVHubInterface/serialaccess.py index 0f646aa..dee55ce 100644 --- a/REVHubInterface/serialaccess.py +++ b/REVHubInterface/serialaccess.py @@ -10,3 +10,28 @@ def hasAccess(): except KeyError: return False else: return True + +def getAccess(): + # Get all groups that exist on the system + all_groups = grp.getgrall() + + # Check if 'dialout' or 'uucp' is in any of the available group names, and set it as the group to use if so + group_to_use = 'unknown' + for group in all_groups: + if 'dialout' in group.gr_name.lower(): + group_to_use = 'dialout' + break + if 'uucp' in group.gr_name.lower(): + group_to_use = 'uucp' + break + + if group_to_use == 'unknown': + raise Exception("Neither dialout or uucp seem to exist.") + + if os.path.exists("/usr/bin/flatpak-spawn"): + command_result = os.system("/usr/bin/flatpak-spawn --host pkexec usermod $USER -a -G dialout") + else: + command_result = os.system("pkexec usermod $USER -a -G dialout") + + if command_result != 0: + raise Exception("Unexpected command exit code: " + str(command_result))