2020-01-21 22:20:54 +01:00
#!/usr/bin/env python3
2020-01-21 23:44:42 +01:00
#
# auto-cpufreq - Automatic CPU speed & power optimizer for Linux
#
2021-12-04 10:02:19 +01:00
# Blog post: https://foolcontrol.org/?p=3124
2020-01-21 22:20:54 +01:00
2020-01-22 22:26:27 +01:00
# core import
2024-07-16 16:37:04 +02:00
import sys , time
from subprocess import run
2024-05-12 16:16:40 +02:00
from shutil import rmtree
2020-08-04 18:22:38 +02:00
2024-07-16 16:37:04 +02:00
from auto_cpufreq . battery_scripts . battery import *
from auto_cpufreq . config . config import config as conf , find_config_file
2021-01-21 07:55:23 +01:00
from auto_cpufreq . core import *
2024-07-16 16:37:04 +02:00
from auto_cpufreq . globals import GITHUB , IS_INSTALLED_WITH_AUR , IS_INSTALLED_WITH_SNAP
2021-12-04 15:40:03 +01:00
from auto_cpufreq . power_helper import *
2024-07-16 16:37:04 +02:00
2020-01-21 22:20:54 +01:00
@click.command ( )
2021-02-14 10:49:07 +01:00
@click.option ( " --monitor " , is_flag = True , help = " Monitor and see suggestions for CPU optimizations " )
2021-10-16 19:57:52 +02:00
@click.option ( " --live " , is_flag = True , help = " Monitor and make (temp.) suggested CPU optimizations " )
2024-07-16 16:37:04 +02:00
@click.option ( " --daemon " , is_flag = True , hidden = True )
2023-02-01 16:39:14 +01:00
@click.option ( " --install " , is_flag = True , help = " Install daemon for (permanent) automatic CPU optimizations " )
2023-09-18 19:59:46 +02:00
@click.option ( " --update " , is_flag = False , help = " Update daemon and package for (permanent) automatic CPU optimizations " , flag_value = " --update " )
2023-02-01 16:39:14 +01:00
@click.option ( " --remove " , is_flag = True , help = " Remove daemon for (permanent) automatic CPU optimizations " )
@click.option ( " --force " , is_flag = False , help = " Force use of either \" powersave \" or \" performance \" governors. Setting to \" reset \" will go back to normal mode " )
2024-05-12 16:16:40 +02:00
@click.option ( " --config " , is_flag = False , required = False , help = " Use config file at defined path " , )
2024-07-16 16:37:04 +02:00
@click.option ( " --stats " , is_flag = True , help = " View live stats of CPU optimizations made by daemon " )
@click.option ( " --get-state " , is_flag = True , hidden = True )
@click.option ( " --completions " , is_flag = False , help = " Enables shell completions for bash, zsh and fish. \n Possible values bash|zsh|fish " )
2021-10-16 19:57:52 +02:00
@click.option ( " --debug " , is_flag = True , help = " Show debug info (include when submitting bugs) " )
2021-02-14 10:49:07 +01:00
@click.option ( " --version " , is_flag = True , help = " Show currently installed version " )
2021-10-16 19:38:30 +02:00
@click.option ( " --donate " , is_flag = True , help = " Support the project " )
2024-07-16 16:37:04 +02:00
def main ( monitor , live , daemon , install , update , remove , force , config , stats , get_state , completions , debug , version , donate ) :
2021-10-16 19:45:38 +02:00
# display info if config file is used
2024-04-30 08:35:53 +02:00
config_path = find_config_file ( config )
conf . set_path ( config_path )
2021-10-16 19:45:38 +02:00
def config_info_dialog ( ) :
2024-04-30 08:35:53 +02:00
if conf . has_config ( ) :
print ( " \n Using settings defined in " + config_path + " file " )
2021-10-16 19:45:38 +02:00
2020-01-21 22:20:54 +01:00
if len ( sys . argv ) == 1 :
print ( " \n " + " - " * 32 + " auto-cpufreq " + " - " * 33 + " \n " )
print ( " Automatic CPU speed & power optimizer for Linux " )
2023-02-01 16:39:14 +01:00
2020-01-21 22:20:54 +01:00
print ( " \n Example usage: \n auto-cpufreq --monitor " )
print ( " \n ----- \n " )
2020-08-06 21:58:01 +02:00
run ( [ " auto-cpufreq " , " --help " ] )
2020-08-05 23:49:23 +02:00
footer ( )
2020-01-21 22:20:54 +01:00
else :
2024-07-16 16:37:04 +02:00
# set governor override unless None or invalid
if force is not None :
not_running_daemon_check ( )
root_check ( ) # Calling root_check before set_override as it will require sudo access
set_override ( force ) # Calling set override, only if force has some values
if monitor :
2021-10-16 19:45:38 +02:00
config_info_dialog ( )
2021-12-04 10:02:19 +01:00
root_check ( )
2021-12-26 11:01:32 +01:00
print ( ' \n Note: You can quit monitor mode by pressing " ctrl+c " ' )
2024-02-07 06:27:56 +01:00
battery_setup ( )
battery_get_thresholds ( )
2024-04-30 08:35:53 +02:00
conf . notifier . start ( )
2024-07-16 16:37:04 +02:00
if IS_INSTALLED_WITH_SNAP :
2021-12-04 10:02:19 +01:00
gnome_power_detect_snap ( )
2021-12-11 11:59:41 +01:00
tlp_service_detect_snap ( )
2021-12-04 10:02:19 +01:00
else :
gnome_power_detect ( )
2021-12-11 11:59:41 +01:00
tlp_service_detect ( )
2020-01-21 22:20:54 +01:00
while True :
2024-04-30 08:35:53 +02:00
try :
time . sleep ( 1 )
running_daemon_check ( )
footer ( )
gov_check ( )
cpufreqctl ( )
distro_info ( )
sysinfo ( )
mon_autofreq ( )
countdown ( 2 )
2024-05-12 16:16:40 +02:00
except KeyboardInterrupt : break
2024-04-30 08:35:53 +02:00
conf . notifier . stop ( )
2020-01-21 22:20:54 +01:00
elif live :
2021-12-04 10:02:19 +01:00
root_check ( )
2021-10-16 19:45:38 +02:00
config_info_dialog ( )
2021-12-26 11:01:32 +01:00
print ( ' \n Note: You can quit live mode by pressing " ctrl+c " ' )
2021-02-06 20:08:15 +01:00
time . sleep ( 1 )
2024-02-14 06:20:45 +01:00
battery_setup ( )
battery_get_thresholds ( )
2024-04-30 08:35:53 +02:00
conf . notifier . start ( )
2024-07-16 16:37:04 +02:00
if IS_INSTALLED_WITH_SNAP :
2021-12-02 21:33:02 +01:00
gnome_power_detect_snap ( )
2021-12-11 11:59:41 +01:00
tlp_service_detect_snap ( )
2021-12-02 21:33:02 +01:00
else :
2022-01-08 13:05:50 +01:00
gnome_power_detect_install ( )
gnome_power_stop_live ( )
2021-12-11 11:59:41 +01:00
tlp_service_detect ( )
2020-01-21 22:20:54 +01:00
while True :
2022-01-08 13:05:50 +01:00
try :
2023-02-01 16:39:14 +01:00
running_daemon_check ( )
2022-01-08 13:05:50 +01:00
footer ( )
gov_check ( )
cpufreqctl ( )
distro_info ( )
sysinfo ( )
set_autofreq ( )
2022-06-05 17:41:59 +02:00
countdown ( 2 )
2022-01-08 13:05:50 +01:00
except KeyboardInterrupt :
gnome_power_start_live ( )
2024-05-12 16:16:40 +02:00
print ( )
2024-04-30 08:35:53 +02:00
break
conf . notifier . stop ( )
2024-07-16 16:37:04 +02:00
elif daemon :
2021-10-16 19:45:38 +02:00
config_info_dialog ( )
2024-07-16 16:37:04 +02:00
root_check ( )
file_stats ( )
if IS_INSTALLED_WITH_SNAP and dcheck == " enabled " :
2021-12-04 10:02:19 +01:00
gnome_power_detect_snap ( )
2021-12-11 11:59:41 +01:00
tlp_service_detect_snap ( )
2024-07-16 16:37:04 +02:00
elif not IS_INSTALLED_WITH_SNAP :
2021-12-04 10:02:19 +01:00
gnome_power_detect ( )
2021-12-11 11:59:41 +01:00
tlp_service_detect ( )
2024-07-16 16:37:04 +02:00
battery_setup ( )
conf . notifier . start ( )
while True :
try :
footer ( )
gov_check ( )
cpufreqctl ( )
distro_info ( )
sysinfo ( )
set_autofreq ( )
countdown ( 2 )
except KeyboardInterrupt : break
conf . notifier . stop ( )
2020-01-21 22:20:54 +01:00
elif install :
2024-05-12 16:16:40 +02:00
root_check ( )
2024-07-16 16:37:04 +02:00
if IS_INSTALLED_WITH_SNAP :
2023-02-01 16:39:14 +01:00
running_daemon_check ( )
2021-12-02 21:33:02 +01:00
gnome_power_detect_snap ( )
2021-12-11 11:59:41 +01:00
tlp_service_detect_snap ( )
2021-12-04 18:32:50 +01:00
bluetooth_notif_snap ( )
2020-08-05 21:12:59 +02:00
gov_check ( )
2020-08-06 21:58:01 +02:00
run ( " snapctl set daemon=enabled " , shell = True )
run ( " snapctl start --enable auto-cpufreq " , shell = True )
2020-08-05 21:12:59 +02:00
else :
2023-02-01 16:39:14 +01:00
running_daemon_check ( )
2020-08-05 21:12:59 +02:00
gov_check ( )
deploy_daemon ( )
2024-05-12 16:16:40 +02:00
deploy_complete_msg ( )
2023-07-15 21:32:23 +02:00
elif update :
root_check ( )
2023-09-18 19:59:46 +02:00
custom_dir = " /opt/auto-cpufreq/source "
for arg in sys . argv :
if arg . startswith ( " --update= " ) :
custom_dir = arg . split ( " = " ) [ 1 ]
sys . argv . remove ( arg )
if " --update " in sys . argv :
update = True
sys . argv . remove ( " --update " )
2024-05-12 16:16:40 +02:00
if len ( sys . argv ) == 2 : custom_dir = sys . argv [ 1 ]
2023-09-18 19:59:46 +02:00
2024-07-16 16:37:04 +02:00
if IS_INSTALLED_WITH_SNAP :
2023-07-15 21:32:23 +02:00
print ( " Detected auto-cpufreq was installed using snap " )
# refresh snap directly using this command
2023-09-18 19:59:46 +02:00
# path wont work in this case
2023-07-15 21:32:23 +02:00
print ( " Please update using snap package manager, i.e: `sudo snap refresh auto-cpufreq`. " )
#check for AUR
2024-07-16 16:37:04 +02:00
elif IS_INSTALLED_WITH_AUR : print ( " Arch-based distribution with AUR support detected. Please refresh auto-cpufreq using your AUR helper. " )
2023-07-15 21:32:23 +02:00
else :
2023-10-27 11:38:42 +02:00
is_new_update = check_for_update ( )
2024-05-12 16:16:40 +02:00
if not is_new_update : return
2023-09-21 07:21:56 +02:00
ans = input ( " Do you want to update auto-cpufreq to the latest release? [Y/n]: " ) . strip ( ) . lower ( )
2024-05-12 16:16:40 +02:00
if not os . path . exists ( custom_dir ) : os . makedirs ( custom_dir )
if os . path . exists ( os . path . join ( custom_dir , " auto-cpufreq " ) ) : rmtree ( os . path . join ( custom_dir , " auto-cpufreq " ) )
2023-09-21 07:21:56 +02:00
if ans in [ ' ' , ' y ' , ' yes ' ] :
2023-07-15 21:32:23 +02:00
remove_daemon ( )
remove_complete_msg ( )
2023-09-18 19:59:46 +02:00
new_update ( custom_dir )
print ( " enabling daemon " )
run ( [ " auto-cpufreq " , " --install " ] )
print ( " auto-cpufreq is installed with the latest version " )
run ( [ " auto-cpufreq " , " --version " ] )
2024-05-12 16:16:40 +02:00
else : print ( " Aborted " )
2024-07-16 16:37:04 +02:00
elif remove :
root_check ( )
if IS_INSTALLED_WITH_SNAP :
run ( " snapctl set daemon=disabled " , shell = True )
run ( " snapctl stop --disable auto-cpufreq " , shell = True )
if auto_cpufreq_stats_path . exists ( ) :
if auto_cpufreq_stats_file is not None :
auto_cpufreq_stats_file . close ( )
auto_cpufreq_stats_path . unlink ( )
# ToDo:
# {the following snippet also used in --update, update it there too(if required)}
# * undo bluetooth boot disable
gnome_power_rm_reminder_snap ( )
else : remove_daemon ( )
remove_complete_msg ( )
elif stats :
not_running_daemon_check ( )
config_info_dialog ( )
print ( ' \n Note: You can quit stats mode by pressing " ctrl+c " ' )
if IS_INSTALLED_WITH_SNAP :
gnome_power_detect_snap ( )
tlp_service_detect_snap ( )
else :
gnome_power_detect ( )
tlp_service_detect ( )
battery_get_thresholds ( )
read_stats ( )
elif get_state :
not_running_daemon_check ( )
override = get_override ( )
print ( override )
2023-10-14 17:46:00 +02:00
elif completions :
if completions == " bash " :
print ( " Run the below command in your current shell! \n " )
print ( " echo ' eval \" $(_AUTO_CPUFREQ_COMPLETE=bash_source auto-cpufreq) \" ' >> ~/.bashrc " )
print ( " source ~/.bashrc " )
elif completions == " zsh " :
print ( " Run the below command in your current shell! \n " )
print ( " echo ' eval \" $(_AUTO_CPUFREQ_COMPLETE=zsh_source auto-cpufreq) \" ' >> ~/.zshrc " )
print ( " source ~/.zshrc " )
elif completions == " fish " :
print ( " Run the below command in your current shell! \n " )
print ( " echo ' _AUTO_CPUFREQ_COMPLETE=fish_source auto-cpufreq | source ' > ~/.config/fish/completions/auto-cpufreq.fish " )
2024-05-12 16:16:40 +02:00
else : print ( " Invalid Option, try bash|zsh|fish as argument to --completions " )
2024-07-16 16:37:04 +02:00
elif debug :
# ToDo: add status of GNOME Power Profile service status
config_info_dialog ( )
root_check ( )
battery_get_thresholds ( )
cpufreqctl ( )
footer ( )
distro_info ( )
sysinfo ( )
print ( )
app_version ( )
print ( )
python_info ( )
print ( )
device_info ( )
print ( f " Battery is: { ' ' if charging ( ) else ' dis ' } charging " )
print ( )
app_res_use ( )
get_load ( )
get_current_gov ( )
get_turbo ( )
footer ( )
elif version :
footer ( )
distro_info ( )
app_version ( )
footer ( )
elif donate :
footer ( )
print ( " If auto-cpufreq helped you out and you find it useful ... \n " )
print ( " Show your appreciation by donating! " )
print ( GITHUB + " #donate " )
footer ( )
2023-10-14 17:46:00 +02:00
2024-05-12 16:16:40 +02:00
if __name__ == " __main__ " : main ( )