2021-11-28 14:49:14 +01:00
|
|
|
# * add status as one of the available options
|
|
|
|
# * alert user on snap if detected and how to remove first time live/stats message starts
|
2021-11-28 14:06:46 +01:00
|
|
|
# * if daemon is disabled and auto-cpufreq is removed (snap) remind user to enable it back
|
2024-07-16 16:37:04 +02:00
|
|
|
import click
|
2024-05-12 16:16:40 +02:00
|
|
|
from shutil import which
|
2024-07-16 16:37:04 +02:00
|
|
|
from subprocess import call, DEVNULL, getoutput, STDOUT
|
|
|
|
from sys import argv
|
2021-11-28 14:06:46 +01:00
|
|
|
|
|
|
|
from auto_cpufreq.core import *
|
2024-07-16 16:37:04 +02:00
|
|
|
from auto_cpufreq.globals import GITHUB, IS_INSTALLED_WITH_SNAP
|
2021-12-11 11:59:41 +01:00
|
|
|
from auto_cpufreq.tlp_stat_parser import TLPStatusParser
|
2021-11-28 14:06:46 +01:00
|
|
|
|
|
|
|
# app_name var
|
2024-07-16 16:37:04 +02:00
|
|
|
app_name = "python3 power_helper.py" if argv[0] == "power_helper.py" else "auto-cpufreq"
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2024-05-12 16:16:40 +02:00
|
|
|
def header(): print("\n------------------------- auto-cpufreq: Power helper -------------------------\n")
|
|
|
|
def warning(): print("\n----------------------------------- Warning -----------------------------------\n")
|
2021-11-28 14:06:46 +01:00
|
|
|
|
2024-05-12 16:16:40 +02:00
|
|
|
def helper_opts(): print("\nFor full list of options run: python3 power_helper.py --help")
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-12-11 11:59:41 +01:00
|
|
|
# used to check if binary exists on the system
|
2024-05-12 16:16:40 +02:00
|
|
|
def does_command_exists(cmd): return which(cmd) is not None
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-12-11 11:59:41 +01:00
|
|
|
bluetoothctl_exists = does_command_exists("bluetoothctl")
|
2022-01-08 13:05:50 +01:00
|
|
|
powerprofilesctl_exists = does_command_exists("powerprofilesctl")
|
2024-07-16 16:37:04 +02:00
|
|
|
systemctl_exists = does_command_exists("systemctl")
|
|
|
|
tlp_stat_exists = does_command_exists("tlp-stat")
|
2021-12-11 11:59:41 +01:00
|
|
|
|
2021-11-28 14:06:46 +01:00
|
|
|
# detect if gnome power profile service is running
|
2024-07-16 16:37:04 +02:00
|
|
|
if not IS_INSTALLED_WITH_SNAP:
|
2021-12-11 11:59:41 +01:00
|
|
|
if systemctl_exists:
|
2024-05-12 16:16:40 +02:00
|
|
|
try: gnome_power_status = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"])
|
2021-12-07 08:28:58 +01:00
|
|
|
except:
|
|
|
|
print("\nUnable to determine init system")
|
|
|
|
print("If this causes any problems, please submit an issue:")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(GITHUB+"/issues")
|
2021-11-28 14:06:46 +01:00
|
|
|
|
2021-12-11 11:59:41 +01:00
|
|
|
# alert in case TLP service is running
|
|
|
|
def tlp_service_detect():
|
|
|
|
if tlp_stat_exists:
|
|
|
|
status_output = getoutput("tlp-stat -s")
|
|
|
|
tlp_status = TLPStatusParser(status_output)
|
|
|
|
if tlp_status.is_enabled():
|
2024-05-12 16:16:40 +02:00
|
|
|
warning()
|
2021-12-11 11:59:41 +01:00
|
|
|
print("Detected you are running a TLP service!")
|
2024-05-12 16:16:40 +02:00
|
|
|
print("This daemon might interfere with auto-cpufreq which can lead to unexpected results.")
|
|
|
|
print("We strongly encourage you to remove TLP unless you really know what you are doing.")
|
2021-12-11 11:59:41 +01:00
|
|
|
|
|
|
|
# alert about TLP when using snap
|
|
|
|
def tlp_service_detect_snap():
|
2024-05-12 16:16:40 +02:00
|
|
|
warning()
|
2021-12-11 11:59:41 +01:00
|
|
|
print("Unable to detect if you are using a TLP service!")
|
|
|
|
print("This daemon might interfere with auto-cpufreq which can lead to unexpected results.")
|
|
|
|
print("We strongly encourage you not to use TLP unless you really know what you are doing.")
|
|
|
|
|
2021-11-28 14:06:46 +01:00
|
|
|
# alert in case gnome power profile service is running
|
|
|
|
def gnome_power_detect():
|
2024-05-12 16:16:40 +02:00
|
|
|
if systemctl_exists and not bool(gnome_power_status):
|
|
|
|
warning()
|
|
|
|
print("Detected running GNOME Power Profiles daemon service!")
|
2024-08-18 15:44:09 +02:00
|
|
|
print("This daemon might interfere with auto-cpufreq and will be automatically disabled.")
|
2024-05-12 16:16:40 +02:00
|
|
|
print("\nSteps to perform this action using auto-cpufreq: power_helper script:")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(f"git clone {GITHUB}.git")
|
2024-05-12 16:16:40 +02:00
|
|
|
print("cd auto-cpufreq/auto_cpufreq")
|
|
|
|
print("python3 power_helper.py --gnome_power_disable")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(f"\nReference: {GITHUB}#configuring-auto-cpufreq")
|
2021-12-04 10:02:19 +01:00
|
|
|
|
2021-12-04 20:56:50 +01:00
|
|
|
# automatically disable gnome power profile service in case it's running during install
|
|
|
|
def gnome_power_detect_install():
|
2024-05-12 16:16:40 +02:00
|
|
|
if systemctl_exists and not bool(gnome_power_status):
|
|
|
|
warning()
|
|
|
|
print("Detected running GNOME Power Profiles daemon service!")
|
|
|
|
print("This daemon might interfere with auto-cpufreq and has been disabled.\n")
|
|
|
|
print('This daemon is not automatically disabled in "monitor" mode and')
|
|
|
|
print("will be enabled after auto-cpufreq is removed.\n")
|
2021-11-28 14:06:46 +01:00
|
|
|
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-12-02 21:33:02 +01:00
|
|
|
# notification on snap
|
|
|
|
def gnome_power_detect_snap():
|
2024-05-12 16:16:40 +02:00
|
|
|
warning()
|
2023-09-16 12:29:53 +02:00
|
|
|
print("Due to Snap package confinement limitations please consider installing auto-cpufreq using")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(f"auto-cpufreq-installer: {GITHUB}#auto-cpufreq-installer")
|
2024-05-12 16:16:40 +02:00
|
|
|
print()
|
2021-12-22 08:28:08 +01:00
|
|
|
print("Unable to detect state of GNOME Power Profiles daemon service!")
|
2024-08-18 15:44:09 +02:00
|
|
|
print("This daemon might interfere with auto-cpufreq and should be disabled!")
|
2021-12-22 08:28:08 +01:00
|
|
|
print("\nSteps to perform this action using auto-cpufreq: power_helper script:")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(f"git clone {GITHUB}.git")
|
2021-12-22 08:28:08 +01:00
|
|
|
print("cd auto-cpufreq/auto_cpufreq")
|
|
|
|
print("python3 power_helper.py --gnome_power_disable")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(f"\nReference: {GITHUB}#configuring-auto-cpufreq")
|
2021-12-02 21:33:02 +01:00
|
|
|
|
2022-01-08 13:05:50 +01:00
|
|
|
# stops gnome >= 40 power profiles (live)
|
|
|
|
def gnome_power_stop_live():
|
2024-05-12 16:16:40 +02:00
|
|
|
if systemctl_exists and not bool(gnome_power_status) and powerprofilesctl_exists:
|
|
|
|
call(["powerprofilesctl", "set", "balanced"])
|
|
|
|
call(["systemctl", "stop", "power-profiles-daemon"])
|
2022-01-08 13:05:50 +01:00
|
|
|
|
|
|
|
# starts gnome >= 40 power profiles (live)
|
|
|
|
def gnome_power_start_live():
|
2024-05-12 16:16:40 +02:00
|
|
|
if systemctl_exists: call(["systemctl", "start", "power-profiles-daemon"])
|
2021-11-28 14:06:46 +01:00
|
|
|
|
|
|
|
# enable gnome >= 40 power profiles (uninstall)
|
2021-12-04 15:40:03 +01:00
|
|
|
def gnome_power_svc_enable():
|
2021-12-11 11:59:41 +01:00
|
|
|
if systemctl_exists:
|
2021-12-07 08:28:58 +01:00
|
|
|
try:
|
2023-02-03 18:44:33 +01:00
|
|
|
print("* Enabling GNOME power profiles\n")
|
2021-12-07 08:28:58 +01:00
|
|
|
call(["systemctl", "unmask", "power-profiles-daemon"])
|
|
|
|
call(["systemctl", "start", "power-profiles-daemon"])
|
|
|
|
call(["systemctl", "enable", "power-profiles-daemon"])
|
|
|
|
call(["systemctl", "daemon-reload"])
|
|
|
|
except:
|
|
|
|
print("\nUnable to enable GNOME power profiles")
|
|
|
|
print("If this causes any problems, please submit an issue:")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(GITHUB+"/issues")
|
2021-12-04 18:32:50 +01:00
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
# gnome power profiles current status
|
2021-12-04 15:40:03 +01:00
|
|
|
def gnome_power_svc_status():
|
2021-12-11 11:59:41 +01:00
|
|
|
if systemctl_exists:
|
2021-12-07 08:28:58 +01:00
|
|
|
try:
|
|
|
|
print("* GNOME power profiles status")
|
|
|
|
call(["systemctl", "status", "power-profiles-daemon"])
|
|
|
|
except:
|
|
|
|
print("\nUnable to see GNOME power profiles status")
|
|
|
|
print("If this causes any problems, please submit an issue:")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(GITHUB+"/issues")
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-12-04 18:32:50 +01:00
|
|
|
# disable bluetooth on boot
|
|
|
|
def bluetooth_disable():
|
2024-07-16 16:37:04 +02:00
|
|
|
if IS_INSTALLED_WITH_SNAP: bluetooth_notif_snap()
|
2021-12-11 11:59:41 +01:00
|
|
|
elif bluetoothctl_exists:
|
2021-12-04 18:32:50 +01:00
|
|
|
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))
|
2024-05-12 16:16:40 +02:00
|
|
|
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)")
|
2021-12-04 18:32:50 +01:00
|
|
|
|
|
|
|
# enable bluetooth on boot
|
|
|
|
def bluetooth_enable():
|
2024-07-16 16:37:04 +02:00
|
|
|
if IS_INSTALLED_WITH_SNAP: bluetooth_on_notif_snap()
|
2021-12-11 11:59:41 +01:00
|
|
|
if bluetoothctl_exists:
|
2021-12-04 18:32:50 +01:00
|
|
|
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))
|
2024-05-12 16:16:40 +02:00
|
|
|
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)")
|
2021-12-04 18:32:50 +01:00
|
|
|
|
|
|
|
# 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")
|
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
# gnome power removal reminder
|
|
|
|
def gnome_power_rm_reminder():
|
2024-05-12 16:16:40 +02:00
|
|
|
if systemctl_exists and bool(gnome_power_status):
|
|
|
|
warning()
|
|
|
|
print("Detected GNOME Power Profiles daemon service is stopped!")
|
|
|
|
print("This service will now be enabled and started again.")
|
2021-12-04 18:32:50 +01:00
|
|
|
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
def gnome_power_rm_reminder_snap():
|
2024-05-12 16:16:40 +02:00
|
|
|
warning()
|
2021-12-22 08:28:08 +01:00
|
|
|
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:")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(f"git clone {GITHUB}.git")
|
2021-12-22 08:28:08 +01:00
|
|
|
print("cd auto-cpufreq/auto_cpufreq")
|
|
|
|
print("python3 power_helper.py --gnome_power_enable")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(f"\nReference: {GITHUB}#configuring-auto-cpufreq")
|
2022-08-02 09:18:29 +02:00
|
|
|
|
2021-11-28 14:06:46 +01:00
|
|
|
def valid_options():
|
2021-12-05 10:39:00 +01:00
|
|
|
print("--gnome_power_enable\t\tEnable GNOME Power Profiles daemon")
|
|
|
|
print("--gnome_power_disable\t\tDisable GNOME Power Profiles daemon\n")
|
2021-11-28 14:06:46 +01:00
|
|
|
|
2022-02-07 18:40:14 +01:00
|
|
|
def disable_power_profiles_daemon():
|
|
|
|
# always disable power-profiles-daemon
|
|
|
|
try:
|
|
|
|
print("\n* 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"])
|
|
|
|
except:
|
|
|
|
print("\nUnable to disable GNOME power profiles")
|
|
|
|
print("If this causes any problems, please submit an issue:")
|
2024-07-16 16:37:04 +02:00
|
|
|
print(GITHUB+"/issues")
|
2022-02-07 18:40:14 +01:00
|
|
|
|
2022-02-07 20:30:35 +01:00
|
|
|
# default gnome_power_svc_disable func (balanced)
|
2022-02-07 18:40:14 +01:00
|
|
|
def gnome_power_svc_disable():
|
2023-02-03 21:48:01 +01:00
|
|
|
snap_pkg_check = 0
|
2022-02-06 16:54:56 +01:00
|
|
|
if systemctl_exists:
|
2024-05-12 16:16:40 +02:00
|
|
|
if bool(gnome_power_status):
|
2022-02-27 06:59:24 +01:00
|
|
|
try:
|
2023-02-03 21:48:01 +01:00
|
|
|
# check if snap package installed
|
|
|
|
snap_pkg_check = call(['snap', 'list', '|', 'grep', 'auto-cpufreq'],
|
2024-07-16 16:37:04 +02:00
|
|
|
stdout=DEVNULL,
|
|
|
|
stderr=STDOUT)
|
2022-02-27 06:59:24 +01:00
|
|
|
# check if snapd is present and if snap package is installed | 0 is success
|
2024-05-12 16:16:40 +02:00
|
|
|
if not bool(snap_pkg_check):
|
2023-02-03 18:44:33 +01:00
|
|
|
print("GNOME Power Profiles Daemon is already disabled, it can be re-enabled by running:\n"
|
2024-05-12 16:16:40 +02:00
|
|
|
"sudo python3 power_helper.py --gnome_power_enable\n"
|
2022-02-27 06:59:24 +01:00
|
|
|
)
|
2023-02-03 18:44:33 +01:00
|
|
|
elif snap_pkg_check == 1:
|
2023-02-03 20:02:03 +01:00
|
|
|
print("auto-cpufreq snap package not installed\nGNOME Power Profiles Daemon should be enabled. run:\n\n"
|
2024-05-12 16:16:40 +02:00
|
|
|
"sudo python3 power_helper.py --gnome_power_enable"
|
2022-02-27 06:59:24 +01:00
|
|
|
)
|
2023-02-03 18:44:33 +01:00
|
|
|
except:
|
2022-02-27 06:59:24 +01:00
|
|
|
# snapd not found on the system
|
2023-02-03 18:44:33 +01:00
|
|
|
print("There was a problem, couldn't determine GNOME Power Profiles Daemon")
|
2023-02-03 21:48:01 +01:00
|
|
|
snap_pkg_check = 0
|
|
|
|
|
2024-05-12 16:16:40 +02:00
|
|
|
if not bool(gnome_power_status) and powerprofilesctl_exists:
|
2023-02-03 18:44:33 +01:00
|
|
|
if snap_pkg_check == 1:
|
2024-05-12 16:16:40 +02:00
|
|
|
print("auto-cpufreq snap package not installed.\nGNOME Power Profiles Daemon should be enabled, run:\n\n"
|
2023-02-03 20:02:03 +01:00
|
|
|
"sudo python3 power_helper.py --gnome_power_enable"
|
2024-05-12 16:16:40 +02:00
|
|
|
)
|
2023-02-03 18:44:33 +01:00
|
|
|
else:
|
|
|
|
print("auto-cpufreq snap package installed, GNOME Power Profiles Daemon should be disabled.\n")
|
|
|
|
print("Using profile: ", "balanced")
|
|
|
|
call(["powerprofilesctl", "set", "balanced"])
|
2022-02-07 18:40:14 +01:00
|
|
|
|
2023-02-03 18:44:33 +01:00
|
|
|
disable_power_profiles_daemon()
|
2022-02-06 16:54:56 +01:00
|
|
|
|
2023-02-03 18:44:33 +01:00
|
|
|
# cli
|
2021-11-28 14:06:46 +01:00
|
|
|
@click.command()
|
2023-02-03 18:44:33 +01:00
|
|
|
#@click.option("--gnome_power_disable", help="Disable GNOME Power profiles service (default: balanced), reference:\n https://bit.ly/3bjVZW1", type=click.Choice(['balanced', 'performance'], case_sensitive=False))
|
|
|
|
@click.option("--gnome_power_disable", is_flag=True, help="Disable GNOME Power profiles service")
|
2022-02-06 17:07:18 +01:00
|
|
|
# ToDo:
|
|
|
|
# * update readme/docs
|
2021-12-04 15:40:03 +01:00
|
|
|
@click.option("--gnome_power_enable", is_flag=True, help="Enable GNOME Power profiles service")
|
2022-02-06 17:07:18 +01:00
|
|
|
|
2024-05-12 16:16:40 +02:00
|
|
|
@click.option("--gnome_power_status", is_flag=True, help="Get status of GNOME Power profiles service")
|
2021-12-04 18:32:50 +01:00
|
|
|
@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")
|
2021-12-22 08:28:08 +01:00
|
|
|
def main(
|
|
|
|
gnome_power_enable,
|
|
|
|
gnome_power_disable,
|
|
|
|
gnome_power_status,
|
|
|
|
bluetooth_boot_off,
|
|
|
|
bluetooth_boot_on,
|
|
|
|
):
|
2021-11-28 14:06:46 +01:00
|
|
|
root_check()
|
2024-05-12 16:16:40 +02:00
|
|
|
header()
|
|
|
|
|
2024-07-16 16:37:04 +02:00
|
|
|
if len(argv) == 1: print('Unrecognized option!\n\nRun: "' + app_name + ' --help" for list of available options.')
|
2021-11-28 14:06:46 +01:00
|
|
|
else:
|
2024-05-12 16:16:40 +02:00
|
|
|
if gnome_power_enable: gnome_power_svc_enable()
|
|
|
|
elif gnome_power_disable: gnome_power_svc_disable()
|
|
|
|
elif gnome_power_status: gnome_power_svc_status()
|
|
|
|
elif bluetooth_boot_off: bluetooth_disable()
|
|
|
|
elif bluetooth_boot_on: bluetooth_enable()
|
|
|
|
helper_opts()
|
|
|
|
|
|
|
|
footer()
|
|
|
|
|
|
|
|
if __name__ == "__main__": main()
|