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
|
|
|
|
import os, sys, click
|
|
|
|
from subprocess import getoutput, call, run, check_output, DEVNULL
|
|
|
|
|
|
|
|
sys.path.append('../')
|
|
|
|
from auto_cpufreq.core import *
|
|
|
|
|
|
|
|
# app_name var
|
2021-12-04 15:40:03 +01:00
|
|
|
if sys.argv[0] == "power_helper.py":
|
|
|
|
app_name="python3 power_helper.py"
|
2021-11-28 14:06:46 +01:00
|
|
|
else:
|
|
|
|
app_name="auto-cpufreq"
|
|
|
|
|
2021-12-04 18:32:50 +01:00
|
|
|
def header():
|
|
|
|
print("\n------------------------- auto-cpufreq: Power helper -------------------------\n")
|
|
|
|
|
|
|
|
def helper_opts():
|
|
|
|
print("\nFor full list of options run: python3 power_helper.py --help")
|
|
|
|
|
2021-11-28 14:06:46 +01:00
|
|
|
# detect if gnome power profile service is running
|
2021-12-02 21:33:02 +01:00
|
|
|
if os.getenv('PKG_MARKER') != "SNAP":
|
|
|
|
gnome_power_stats = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"])
|
2021-11-28 14:06:46 +01:00
|
|
|
|
|
|
|
# alert in case gnome power profile service is running
|
|
|
|
def gnome_power_detect():
|
2021-12-02 21:33:02 +01:00
|
|
|
if gnome_power_stats == 0:
|
2021-12-04 10:02:19 +01:00
|
|
|
print("\n----------------------------------- Warning -----------------------------------\n")
|
|
|
|
print("Detected running GNOME Power Profiles daemon service!")
|
|
|
|
print("This daemon might interfere with auto-cpufreq and should be disabled.")
|
|
|
|
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")
|
2021-12-04 15:40:03 +01:00
|
|
|
print("python3 power_helper.py --gnome-power-disable")
|
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():
|
|
|
|
if gnome_power_stats == 0:
|
|
|
|
print("\n----------------------------------- Warning -----------------------------------\n")
|
|
|
|
print("Detected running GNOME Power Profiles daemon service!")
|
|
|
|
print("This daemon might interfere with auto-cpufreq and has been disabled.")
|
|
|
|
print("\nSteps to enabled disabled deamon (not recommended!) 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")
|
|
|
|
|
2021-11-28 14:06:46 +01:00
|
|
|
|
2021-12-02 21:33:02 +01:00
|
|
|
# notification on snap
|
|
|
|
def gnome_power_detect_snap():
|
2021-12-04 10:02:19 +01:00
|
|
|
print("\n----------------------------------- Warning -----------------------------------\n")
|
|
|
|
print("Unable to detect state of GNOME Power Profiles daemon service!")
|
|
|
|
print("This daemon might interfere with auto-cpufreq and should be disabled.")
|
|
|
|
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")
|
2021-12-04 15:40:03 +01:00
|
|
|
print("python3 power_helper.py --gnome-power-disable")
|
2021-12-02 21:33:02 +01:00
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
|
2021-11-28 14:06:46 +01:00
|
|
|
# disable gnome >= 40 power profiles (live)
|
|
|
|
def gnome_power_disable_live():
|
2021-11-28 14:49:14 +01:00
|
|
|
if(gnome_power_stats == 0):
|
2021-11-28 14:06:46 +01:00
|
|
|
call(["systemctl", "stop", "power-profiles-daemon"])
|
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
|
2021-11-28 14:06:46 +01:00
|
|
|
# disable gnome >= 40 power profiles (install)
|
2021-12-04 15:40:03 +01:00
|
|
|
def gnome_power_svc_disable():
|
2021-12-04 20:56:50 +01:00
|
|
|
print("\n* Disabling GNOME power profiles")
|
2021-11-28 14:06:46 +01:00
|
|
|
call(["systemctl", "stop", "power-profiles-daemon"])
|
|
|
|
call(["systemctl", "disable", "power-profiles-daemon"])
|
|
|
|
call(["systemctl", "mask", "power-profiles-daemon"])
|
2021-12-04 10:02:19 +01:00
|
|
|
call(["systemctl", "daemon-reload"])
|
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-04 20:56:50 +01:00
|
|
|
print("\n* Enabling GNOME power profiles")
|
2021-11-28 14:06:46 +01:00
|
|
|
call(["systemctl", "unmask", "power-profiles-daemon"])
|
|
|
|
call(["systemctl", "start", "power-profiles-daemon"])
|
|
|
|
call(["systemctl", "enable", "power-profiles-daemon"])
|
2021-12-04 10:02:19 +01:00
|
|
|
call(["systemctl", "daemon-reload"])
|
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-04 18:32:50 +01:00
|
|
|
print("* GNOME power profiles status")
|
2021-12-04 10:02:19 +01:00
|
|
|
call(["systemctl", "status", "power-profiles-daemon"])
|
|
|
|
|
2021-12-04 18:32:50 +01:00
|
|
|
|
|
|
|
# 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")
|
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
# gnome power removal reminder
|
|
|
|
def gnome_power_rm_reminder():
|
|
|
|
if gnome_power_stats != 0:
|
|
|
|
print("\n----------------------------------- Warning -----------------------------------\n")
|
|
|
|
print("Detected GNOME Power Profiles daemon service is stopped!")
|
2021-12-04 20:56:50 +01:00
|
|
|
print("This service will now be enabled again.")
|
2021-12-04 18:32:50 +01:00
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
def gnome_power_rm_reminder_snap():
|
|
|
|
print("\n----------------------------------- Warning -----------------------------------\n")
|
2021-12-04 18:32:50 +01:00
|
|
|
print("Unable to detect state of GNOME Power Profiles daemon service!")
|
2021-12-04 10:02:19 +01:00
|
|
|
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")
|
2021-12-04 15:40:03 +01:00
|
|
|
print("python3 power_helper.py --gnome-power-enable")
|
2021-11-28 14:06:46 +01:00
|
|
|
|
2021-12-04 18:32:50 +01:00
|
|
|
|
2021-11-28 14:06:46 +01:00
|
|
|
def valid_options():
|
2021-12-04 15:40:03 +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
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
|
2021-11-28 14:06:46 +01:00
|
|
|
# cli
|
|
|
|
@click.command()
|
2021-12-04 15:40:03 +01:00
|
|
|
@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")
|
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")
|
|
|
|
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()
|
|
|
|
if len(sys.argv) == 1:
|
2021-12-04 18:32:50 +01:00
|
|
|
header()
|
2021-11-28 14:49:14 +01:00
|
|
|
print("Unrecognized option!\n\nRun: \"" + app_name + " --help\" for list of available options.")
|
2021-11-28 14:06:46 +01:00
|
|
|
footer()
|
|
|
|
else:
|
2021-12-04 15:40:03 +01:00
|
|
|
if gnome_power_enable:
|
2021-12-04 18:32:50 +01:00
|
|
|
header()
|
2021-11-28 14:06:46 +01:00
|
|
|
root_check()
|
2021-12-04 15:40:03 +01:00
|
|
|
gnome_power_svc_enable()
|
2021-12-04 18:32:50 +01:00
|
|
|
helper_opts()
|
2021-12-04 10:02:19 +01:00
|
|
|
footer()
|
2021-12-04 15:40:03 +01:00
|
|
|
elif gnome_power_disable:
|
2021-12-04 18:32:50 +01:00
|
|
|
header()
|
2021-12-02 21:33:02 +01:00
|
|
|
root_check()
|
2021-12-04 15:40:03 +01:00
|
|
|
gnome_power_svc_disable()
|
2021-12-04 18:32:50 +01:00
|
|
|
helper_opts()
|
2021-12-04 10:02:19 +01:00
|
|
|
footer()
|
2021-12-04 15:40:03 +01:00
|
|
|
elif gnome_power_status:
|
2021-12-04 18:32:50 +01:00
|
|
|
header()
|
2021-12-04 10:02:19 +01:00
|
|
|
root_check()
|
2021-12-04 15:40:03 +01:00
|
|
|
gnome_power_svc_status()
|
2021-12-04 18:32:50 +01:00
|
|
|
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()
|
2021-12-04 10:02:19 +01:00
|
|
|
footer()
|
2021-11-28 14:06:46 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|