From fc5ac16fd6de6f8ba35d1f253aed22ed79c65044 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sat, 4 Dec 2021 18:32:50 +0100 Subject: [PATCH] Final auto-cpufreq & power_helper improvements --- auto_cpufreq/core.py | 45 ++------------- auto_cpufreq/power_helper.py | 106 +++++++++++++++++++++++++++++++---- bin/auto-cpufreq | 3 +- 3 files changed, 100 insertions(+), 54 deletions(-) diff --git a/auto_cpufreq/core.py b/auto_cpufreq/core.py index 9aa8f05..868427c 100644 --- a/auto_cpufreq/core.py +++ b/auto_cpufreq/core.py @@ -298,21 +298,8 @@ def deploy_daemon(): # deploy cpufreqctl script func call cpufreqctl() - if which("bluetoothctl") is not None: - print("* Turn off bluetooth on boot") - btconf = Path("/etc/bluetooth/main.conf") - try: - orig_set = "AutoEnable=true" - change_set = "AutoEnable=false" - with btconf.open(mode="r+") as f: - content = f.read() - f.seek(0) - f.truncate() - f.write(content.replace(orig_set, change_set)) - except Exception as e: - print(f"\nERROR:\nWas unable to turn off bluetooth on boot\n{repr(e)}") - else: - print("* Turn off bluetooth on boot [skipping] (package providing bluetooth access is not present)") + # turn off bluetooth on boot + bluetooth_disable() auto_cpufreq_stats_path.touch(exist_ok=True) @@ -329,16 +316,6 @@ def deploy_daemon(): call("/usr/bin/auto-cpufreq-install", shell=True) -# ToDo: change to be a warning and move to power_helper? -def bt_snap(): - if os.getenv("PKG_MARKER") == "SNAP": - try: - print("\n* Turn off bluetooth on boot") - call("rfkill block bluetooth", shell=True) - except: - print("n\nERROR:\nWas unable to turn off bluetooth on boot") - - # remove auto-cpufreq daemon def remove(): @@ -349,21 +326,8 @@ def remove(): print("\n" + "-" * 21 + " Removing auto-cpufreq daemon " + "-" * 22 + "\n") - if which("bluetoothctl") is not None: - print("* Turn on bluetooth on boot") - btconf = "/etc/bluetooth/main.conf" - try: - orig_set = "AutoEnable=true" - change_set = "AutoEnable=false" - with open(btconf, "r+") as f: - content = f.read() - f.seek(0) - f.truncate() - f.write(content.replace(change_set, orig_set)) - except Exception as e: - print(f"\nERROR:\nWas unable to turn on bluetooth on boot\n{repr(e)}") - else: - print("* Turn on bluetooth on boot [skipping] (package providing bluetooth access is not present)") + # turn on bluetooth on boot + bluetooth_enable() # output warning if gnome power profile is stopped gnome_power_rm_reminder() @@ -1086,6 +1050,7 @@ def daemon_running_msg(): print( 'ERROR: auto-cpufreq is running in daemon mode.\n\nMake sure to stop the deamon before running with --live or --monitor mode' ) + footer() # check if auto-cpufreq --daemon is running def running_daemon(): diff --git a/auto_cpufreq/power_helper.py b/auto_cpufreq/power_helper.py index 73d1cd4..ac2c97d 100644 --- a/auto_cpufreq/power_helper.py +++ b/auto_cpufreq/power_helper.py @@ -13,6 +13,12 @@ if sys.argv[0] == "power_helper.py": else: app_name="auto-cpufreq" +def header(): + print("\n------------------------- auto-cpufreq: Power helper -------------------------\n") + +def helper_opts(): + print("\nFor full list of options run: python3 power_helper.py --help") + # detect if gnome power profile service is running if os.getenv('PKG_MARKER') != "SNAP": gnome_power_stats = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"]) @@ -49,26 +55,86 @@ def gnome_power_disable_live(): # disable gnome >= 40 power profiles (install) def gnome_power_svc_disable(): if(gnome_power_stats == 0): - print("\n* Disabling GNOME power profiles") + print("* Disabling GNOME power profiles") call(["systemctl", "stop", "power-profiles-daemon"]) call(["systemctl", "disable", "power-profiles-daemon"]) call(["systemctl", "mask", "power-profiles-daemon"]) call(["systemctl", "daemon-reload"]) + else: + print("* GNOME power profiles already disabled") # enable gnome >= 40 power profiles (uninstall) def gnome_power_svc_enable(): if(gnome_power_stats != 0): - print("\n* Enabling GNOME power profiles") + print("* Enabling GNOME power profiles") call(["systemctl", "unmask", "power-profiles-daemon"]) call(["systemctl", "start", "power-profiles-daemon"]) call(["systemctl", "enable", "power-profiles-daemon"]) call(["systemctl", "daemon-reload"]) + else: + print("* GNOME power profiles already enabled") + # gnome power profiles current status def gnome_power_svc_status(): - print("\n* GNOME power profiles status") + print("* GNOME power profiles status") call(["systemctl", "status", "power-profiles-daemon"]) + +# disable bluetooth on boot +def bluetooth_disable(): + if os.getenv("PKG_MARKER") == "SNAP": + bluetooth_notif_snap() + elif which("bluetoothctl") is not None: + print("* Turn off bluetooth on boot") + btconf = Path("/etc/bluetooth/main.conf") + try: + orig_set = "AutoEnable=true" + change_set = "AutoEnable=false" + with btconf.open(mode="r+") as f: + content = f.read() + f.seek(0) + f.truncate() + f.write(content.replace(orig_set, change_set)) + except Exception as e: + print(f"\nERROR:\nWas unable to turn off bluetooth on boot\n{repr(e)}") + else: + print("* Turn off bluetooth on boot [skipping] (package providing bluetooth access is not present)") + + +# enable bluetooth on boot +def bluetooth_enable(): + if os.getenv("PKG_MARKER") == "SNAP": + bluetooth_on_notif_snap() + if which("bluetoothctl") is not None: + print("* Turn on bluetooth on boot") + btconf = "/etc/bluetooth/main.conf" + try: + orig_set = "AutoEnable=true" + change_set = "AutoEnable=false" + with open(btconf, "r+") as f: + content = f.read() + f.seek(0) + f.truncate() + f.write(content.replace(change_set, orig_set)) + except Exception as e: + print(f"\nERROR:\nWas unable to turn on bluetooth on boot\n{repr(e)}") + else: + print("* Turn on bluetooth on boot [skipping] (package providing bluetooth access is not present)") + + +# turn off bluetooth on snap message +def bluetooth_notif_snap(): + print("\n* Unable to turn off bluetooth on boot due to Snap package restrictions!") + print("\nSteps to perform this action using auto-cpufreq: power_helper script:") + print("python3 power_helper.py --bluetooth_boot_off") + +# turn off bluetooth on snap message +def bluetooth_on_notif_snap(): + print("\n* Unable to turn on bluetooth on boot due to Snap package restrictions!") + print("\nSteps to perform this action using auto-cpufreq: power_helper script:") + print("python3 power_helper.py --bluetooth_boot_on") + # gnome power removal reminder def gnome_power_rm_reminder(): if gnome_power_stats != 0: @@ -80,15 +146,17 @@ def gnome_power_rm_reminder(): print("cd auto-cpufreq/auto_cpufreq") print("python3 power_helper.py --gnome-power-enable") + def gnome_power_rm_reminder_snap(): print("\n----------------------------------- Warning -----------------------------------\n") - print("\nUnable to detect state of GNOME Power Profiles daemon service!") + print("Unable to detect state of GNOME Power Profiles daemon service!") print("Now it's recommended to enable this service.") print("\nSteps to perform this action using auto-cpufreq: power_helper script:") print("git clone https://github.com/AdnanHodzic/auto-cpufreq.git") print("cd auto-cpufreq/auto_cpufreq") print("python3 power_helper.py --gnome-power-enable") + def valid_options(): print("--gnome-power-enable\t\tEnable GNOME Power Profiles daemon") print("--gnome-power-disable\t\tDisable GNOME Power Profiles daemon\n") @@ -99,31 +167,45 @@ def valid_options(): @click.option("--gnome_power_enable", is_flag=True, help="Enable GNOME Power profiles service") @click.option("--gnome_power_disable", is_flag=True, help="Disable GNOME Power profiles service") @click.option("--gnome_power_status", is_flag=True, help="Get status of GNOME Power profiles service") -def main(gnome_power_enable, gnome_power_disable, gnome_power_status): +@click.option("--bluetooth_boot_on", is_flag=True, help="Turn on Bluetooth on boot") +@click.option("--bluetooth_boot_off", is_flag=True, help="Turn off Bluetooth on boot") +def main(gnome_power_enable, gnome_power_disable, gnome_power_status, bluetooth_boot_off, bluetooth_boot_on): root_check() if len(sys.argv) == 1: - print("\n------------------------- auto-cpufreq: Power helper -------------------------\n") + header() print("Unrecognized option!\n\nRun: \"" + app_name + " --help\" for list of available options.") footer() else: if gnome_power_enable: - footer() + header() root_check() - print("Enabling GNOME Power Profiles") gnome_power_svc_enable() + helper_opts() footer() elif gnome_power_disable: - footer() + header() root_check() - print("Disabling GNOME Power Profiles") gnome_power_svc_disable() + helper_opts() footer() elif gnome_power_status: - footer() + header() root_check() - print("Status of GNOME Power Profiles") gnome_power_svc_status() + helper_opts() + footer() + elif bluetooth_boot_off: + header() + root_check() + bluetooth_disable() + helper_opts() + footer() + elif bluetooth_boot_on: + header() + root_check() + bluetooth_enable() + helper_opts() footer() if __name__ == '__main__': diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index d1e63e2..5714cb2 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -154,8 +154,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don root_check() running_daemon() gnome_power_detect_snap() - # ToDo: move elsewhere after func revamp - #bt_snap() + bluetooth_notif_snap() gov_check() run("snapctl set daemon=enabled", shell=True) run("snapctl start --enable auto-cpufreq", shell=True)