From 3a8eaaf03e620aa00b1e1a0e3c43d8ea6031a1d2 Mon Sep 17 00:00:00 2001 From: shadeyg56 Date: Sun, 26 Feb 2023 15:34:25 -0600 Subject: [PATCH] app no longer needs root to start, only asks when needed --- auto_cpufreq/gui/app.py | 5 ++--- auto_cpufreq/gui/objects.py | 19 ++++++++++++++----- scripts/org.auto-cpufreq.pkexec.policy | 6 +++--- scripts/start_app | 20 +++++++++++--------- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/auto_cpufreq/gui/app.py b/auto_cpufreq/gui/app.py index 97d55b4..6f0c81c 100644 --- a/auto_cpufreq/gui/app.py +++ b/auto_cpufreq/gui/app.py @@ -15,7 +15,7 @@ CSS_FILE = "/usr/local/share/auto-cpufreq/scripts/style.css" HBOX_PADDING = 20 -class MyWindow(Gtk.Window): +class ToolWindow(Gtk.Window): def __init__(self): super().__init__(title="auto-cpufreq") self.set_default_size(600, 480) @@ -80,8 +80,7 @@ class MyWindow(Gtk.Window): return True - -win = MyWindow() +win = ToolWindow() win.connect("destroy", Gtk.main_quit) win.show_all() GLib.set_application_name("auto-cpufreq") diff --git a/auto_cpufreq/gui/objects.py b/auto_cpufreq/gui/objects.py index cb398a2..0f9b804 100644 --- a/auto_cpufreq/gui/objects.py +++ b/auto_cpufreq/gui/objects.py @@ -9,11 +9,13 @@ import os import platform as pl sys.path.append("../../") -from subprocess import getoutput, call +from subprocess import getoutput, run, PIPE from auto_cpufreq.core import sysinfo, distro_info, set_override, get_override, get_formatted_version, dist_name, deploy_daemon, remove_daemon from io import StringIO +PKEXEC_ERROR = "Error executing command as another user: Not authorized\n\nThis incident has been reported.\n" + if os.getenv("PKG_MARKER") == "SNAP": auto_cpufreq_stats_path = "/var/snap/auto-cpufreq/current/auto-cpufreq.stats" else: @@ -32,7 +34,7 @@ def get_version(): return getoutput("echo \(Snap\) $SNAP_VERSION") # aur package elif dist_name in ["arch", "manjaro", "garuda"]: - aur_pkg_check = call("pacman -Qs auto-cpufreq > /dev/null", shell=True) + aur_pkg_check = run("pacman -Qs auto-cpufreq > /dev/null", shell=True) if aur_pkg_check == 1: return get_formatted_version() else: @@ -77,7 +79,10 @@ class RadioButtonView(Gtk.Box): def on_button_toggled(self, button, override): if button.get_active(): - set_override(override) + result = run(f"pkexec auto-cpufreq --force={override}", shell=True, stdout=PIPE, stderr=PIPE) + if result.stderr.decode() == PKEXEC_ERROR: + self.set_selected() + def set_selected(self): override = get_override() @@ -170,7 +175,9 @@ class DropDownMenu(Gtk.MenuButton): confirm.destroy() if response == Gtk.ResponseType.YES: try: - remove_daemon() + result = run("pkexec auto-cpufreq --remove", shell=True, stdout=PIPE, stderr=PIPE) + if result.stderr.decode() == PKEXEC_ERROR: + raise Exception("Authorization was cancelled") dialog = Gtk.MessageDialog( transient_for=parent, message_type=Gtk.MessageType.INFO, @@ -250,7 +257,9 @@ class DaemonNotRunningView(Gtk.Box): def install_daemon(self, button, parent): try: - deploy_daemon() + result = run("pkexec auto-cpufreq --install", shell=True, stdout=PIPE, stderr=PIPE) + if result.stderr.decode() == PKEXEC_ERROR: + raise Exception("Authorization was cancelled") dialog = Gtk.MessageDialog( transient_for=parent, message_type=Gtk.MessageType.INFO, diff --git a/scripts/org.auto-cpufreq.pkexec.policy b/scripts/org.auto-cpufreq.pkexec.policy index 6921712..d59e34b 100644 --- a/scripts/org.auto-cpufreq.pkexec.policy +++ b/scripts/org.auto-cpufreq.pkexec.policy @@ -12,8 +12,8 @@ auth_admin auth_admin - /opt/auto-cpufreq/venv/bin/python - /opt/auto-cpufreq/venv/bin/app.py - true + /opt/auto-cpufreq/venv/bin/auto-cpufreq + + \ No newline at end of file diff --git a/scripts/start_app b/scripts/start_app index d5cc3eb..a532e93 100644 --- a/scripts/start_app +++ b/scripts/start_app @@ -5,12 +5,14 @@ venv_dir=/opt/auto-cpufreq/venv . "${venv_dir}/bin/activate" python_command="${venv_dir}/bin/python ${venv_dir}/bin/app.py" -if [ "$XDG_SESSION_TYPE" = "wayland" ] ; then - # necessary for running on wayland - xhost +SI:localuser:root - pkexec ${python_command} - xhost -SI:localuser:root - xhost -else - pkexec ${python_command} -fi +# if [ "$XDG_SESSION_TYPE" = "wayland" ] ; then +# # necessary for running on wayland +# xhost +SI:localuser:root +# pkexec ${python_command} +# xhost -SI:localuser:root +# xhost +# else +# pkexec ${python_command} +# fi + +${python_command} \ No newline at end of file