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
2020-01-21 22:20:54 +01:00
import sys
2021-02-06 20:08:15 +01:00
import time
2023-09-18 19:59:46 +02:00
from click import UsageError
2020-08-06 21:58:01 +02:00
from subprocess import call , run
2020-08-04 18:22:38 +02:00
2023-10-13 08:04:49 +02:00
# sys.path.append("../")
2021-01-21 07:55:23 +01:00
from auto_cpufreq . core import *
2021-12-04 15:40:03 +01:00
from auto_cpufreq . power_helper import *
2024-02-07 06:27:56 +01:00
from auto_cpufreq . battery_scripts . battery import *
2020-01-21 22:20:54 +01:00
# cli
@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 " )
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 " )
2021-02-14 10:49:07 +01:00
@click.option ( " --stats " , is_flag = True , help = " View live stats of CPU optimizations made by daemon " )
2023-02-01 16:39:14 +01:00
@click.option ( " --force " , is_flag = False , help = " Force use of either \" powersave \" or \" performance \" governors. Setting to \" reset \" will go back to normal mode " )
@click.option ( " --get-state " , is_flag = True , hidden = True )
2021-12-26 11:01:32 +01:00
@click.option (
" --config " ,
is_flag = False ,
default = " /etc/auto-cpufreq.conf " ,
help = " Use config file at defined path " ,
)
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 " )
2023-10-14 17:46:00 +02:00
@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 ( " --log " , is_flag = True , hidden = True )
@click.option ( " --daemon " , is_flag = True , hidden = True )
2023-10-14 17:46:00 +02:00
def main ( config , daemon , debug , update , install , remove , live , log , monitor , stats , version , donate , force , get_state , completions ) :
2021-10-16 19:45:38 +02:00
# display info if config file is used
def config_info_dialog ( ) :
2021-12-26 11:01:32 +01:00
if get_config ( config ) and hasattr ( get_config , " using_cfg_file " ) :
2021-10-16 19:45:38 +02:00
print ( " \n Using settings defined in " + config + " file " )
2023-02-01 16:39:14 +01:00
# set governor override unless None or invalid
if force is not None :
not_running_daemon_check ( )
2023-06-30 08:37:19 +02:00
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
2023-02-01 16:39:14 +01: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 :
if daemon :
2021-10-16 19:45:38 +02:00
config_info_dialog ( )
2021-12-04 10:02:19 +01:00
root_check ( )
2021-02-02 21:40:55 +01:00
file_stats ( )
2020-02-08 14:32:58 +01:00
if os . getenv ( " PKG_MARKER " ) == " SNAP " and dcheck == " enabled " :
2021-12-02 21:33:02 +01:00
gnome_power_detect_snap ( )
2021-12-11 11:59:41 +01:00
tlp_service_detect_snap ( )
2024-02-07 06:27:56 +01:00
battery_setup ( )
2020-02-05 21:03:29 +01:00
while True :
2020-09-11 00:10:57 +02:00
footer ( )
2020-02-05 21:03:29 +01:00
gov_check ( )
cpufreqctl ( )
2020-09-10 23:01:30 +02:00
distro_info ( )
2020-02-05 21:03:29 +01:00
sysinfo ( )
set_autofreq ( )
2022-06-05 17:41:59 +02:00
countdown ( 2 )
2020-02-05 21:03:29 +01:00
elif os . getenv ( " PKG_MARKER " ) != " SNAP " :
2021-12-02 21:33:02 +01:00
gnome_power_detect ( )
2021-12-11 11:59:41 +01:00
tlp_service_detect ( )
2024-02-07 06:27:56 +01:00
battery_setup ( )
2020-02-05 21:03:29 +01:00
while True :
2020-09-16 21:06:14 +02:00
footer ( )
2020-02-05 21:03:29 +01:00
gov_check ( )
cpufreqctl ( )
2020-09-10 23:01:30 +02:00
distro_info ( )
2020-02-05 21:03:29 +01:00
sysinfo ( )
set_autofreq ( )
2022-06-05 17:41:59 +02:00
countdown ( 2 )
2020-02-05 21:03:29 +01:00
else :
2023-07-15 21:32:23 +02:00
pass
#"daemon_not_found" is not defined
2023-09-18 19:59:46 +02:00
#daemon_not_found()
2020-01-21 22:20:54 +01:00
elif 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 ( )
2021-12-04 10:02:19 +01:00
if os . getenv ( " PKG_MARKER " ) == " SNAP " :
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 :
2021-02-06 20:08:15 +01:00
time . sleep ( 1 )
2023-02-01 16:39:14 +01:00
running_daemon_check ( )
2021-10-17 17:52:15 +02:00
footer ( )
2020-01-21 22:20:54 +01:00
gov_check ( )
cpufreqctl ( )
2020-09-10 23:01:30 +02:00
distro_info ( )
2020-01-21 22:20:54 +01:00
sysinfo ( )
mon_autofreq ( )
2022-06-05 17:41:59 +02:00
countdown ( 2 )
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 ( )
2021-12-02 21:33:02 +01:00
if os . getenv ( " PKG_MARKER " ) == " SNAP " :
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 ( )
print ( " " )
sys . exit ( )
2021-02-02 21:40:55 +01:00
elif stats :
2023-02-01 16:39:14 +01:00
not_running_daemon_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 stats mode by pressing " ctrl+c " ' )
2021-12-04 10:02:19 +01:00
if os . getenv ( " PKG_MARKER " ) == " SNAP " :
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 ( )
2021-02-02 21:40:55 +01:00
read_stats ( )
2020-01-21 22:20:54 +01:00
elif log :
2021-02-02 21:40:55 +01:00
deprecated_log_msg ( )
2023-02-01 16:39:14 +01:00
elif get_state :
not_running_daemon_check ( )
override = get_override ( )
print ( override )
2020-08-05 23:49:23 +02:00
elif debug :
2022-08-02 09:18:29 +02:00
# ToDo: add status of GNOME Power Profile service status
2021-10-16 19:45:38 +02:00
config_info_dialog ( )
2020-09-10 23:01:30 +02:00
root_check ( )
2022-01-08 19:05:15 +01:00
cpufreqctl ( )
2020-08-05 23:49:23 +02:00
footer ( )
2020-09-09 20:02:45 +02:00
distro_info ( )
2020-12-05 21:15:15 +01:00
sysinfo ( )
2020-09-11 00:10:57 +02:00
print ( " " )
2021-02-04 23:27:19 +01:00
app_version ( )
2020-09-10 23:01:30 +02:00
print ( " " )
2020-09-10 17:44:19 +02:00
python_info ( )
2020-09-10 23:01:30 +02:00
print ( " " )
2021-02-04 08:06:41 +01:00
device_info ( )
2020-09-11 21:15:59 +02:00
if charging ( ) :
print ( " Battery is: charging " )
else :
print ( " Battery is: discharging " )
print ( " " )
app_res_use ( )
2020-09-11 08:32:16 +02:00
display_load ( )
2020-09-11 09:02:19 +02:00
get_current_gov ( )
2020-09-11 08:32:16 +02:00
get_turbo ( )
2020-08-05 23:49:23 +02:00
footer ( )
2021-02-14 10:49:07 +01:00
elif version :
footer ( )
distro_info ( )
app_version ( )
footer ( )
2021-10-16 19:38:30 +02:00
elif donate :
footer ( )
print ( " If auto-cpufreq helped you out and you find it useful ... \n " )
print ( " Show your appreciation by donating! " )
print ( " https://github.com/AdnanHodzic/auto-cpufreq/#donate " )
footer ( )
2020-01-21 22:20:54 +01:00
elif install :
2021-12-26 11:01:32 +01:00
if os . getenv ( " PKG_MARKER " ) == " SNAP " :
2020-08-05 21:12:59 +02:00
root_check ( )
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
deploy_complete_msg ( )
else :
root_check ( )
2023-02-01 16:39:14 +01:00
running_daemon_check ( )
2020-08-05 21:12:59 +02:00
gov_check ( )
deploy_daemon ( )
deploy_complete_msg ( )
2020-01-21 22:20:54 +01:00
elif remove :
2021-12-26 11:01:32 +01:00
if os . getenv ( " PKG_MARKER " ) == " SNAP " :
2020-02-05 21:03:29 +01:00
root_check ( )
2020-08-06 21:58:01 +02:00
run ( " snapctl set daemon=disabled " , shell = True )
run ( " snapctl stop --disable auto-cpufreq " , shell = True )
2021-02-02 21:40:55 +01:00
if auto_cpufreq_stats_path . exists ( ) :
if auto_cpufreq_stats_file is not None :
auto_cpufreq_stats_file . close ( )
2021-02-02 20:56:56 +01:00
2021-02-02 21:40:55 +01:00
auto_cpufreq_stats_path . unlink ( )
2023-07-15 21:32:23 +02:00
# ToDo:
# {the following snippet also used in --update, update it there too(if required)}
2021-12-02 21:33:02 +01:00
# * undo bluetooth boot disable
2021-12-04 10:02:19 +01:00
gnome_power_rm_reminder_snap ( )
2020-02-08 21:04:34 +01:00
remove_complete_msg ( )
2020-02-05 21:03:29 +01:00
else :
2020-01-21 22:20:54 +01:00
root_check ( )
2023-02-01 16:39:14 +01:00
remove_daemon ( )
2020-02-08 21:04:34 +01:00
remove_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 " )
if len ( sys . argv ) == 2 :
custom_dir = sys . argv [ 1 ]
2023-07-15 21:32:23 +02:00
if os . getenv ( " PKG_MARKER " ) == " SNAP " :
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
2023-09-10 13:53:29 +02:00
elif subprocess . run ( [ " bash " , " -c " , " command -v pacman >/dev/null 2>&1 " ] ) . returncode == 0 and subprocess . run ( [ " bash " , " -c " , " pacman -Q auto-cpufreq >/dev/null 2>&1 " ] ) . returncode == 0 :
2023-07-15 21:32:23 +02:00
print ( " Arch-based distribution with AUR support detected. Please refresh auto-cpufreq using your AUR helper. " )
else :
2023-10-27 11:38:42 +02:00
is_new_update = check_for_update ( )
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 ( )
2023-09-18 19:59:46 +02:00
if not os . path . exists ( custom_dir ) :
os . makedirs ( custom_dir )
if os . path . exists ( os . path . join ( custom_dir , " auto-cpufreq " ) ) :
shutil . 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 " ] )
2023-07-15 21:32:23 +02:00
else :
print ( " Aborted " )
2020-08-05 21:12:59 +02:00
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 " )
else :
print ( " Invalid Option, try bash|zsh|fish as argument to --completions " )
2021-12-26 11:01:32 +01:00
if __name__ == " __main__ " :
2020-08-05 21:12:59 +02:00
main ( )