Skip to content

Commit

Permalink
Big code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Karasu committed May 26, 2024
1 parent 3f6d7a3 commit 98f1843
Show file tree
Hide file tree
Showing 39 changed files with 1,249 additions and 2,264 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ You can configure separate profiles for the battery and power supply. These prof
By default, auto-cpufreq does not use a config file. If you wish to configure auto-cpufreq statically, we look for a configuration file in the following order:

1. Commandline argument: `--config <FILE>` if passed as commandline argument to `auto-cpufreq`
2. User-specific configuration: `$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`
2. User-specific configuration: `$XDG_CONFIG_HOME/auto-cpufreq.conf` or `$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`
3. System-wide configuration: `/etc/auto-cpufreq.conf`

#### Example config file contents
Expand Down
110 changes: 50 additions & 60 deletions auto-cpufreq-installer
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ AUTO_CPUFREQ_FILE="/usr/local/bin/auto-cpufreq"
AUTO_CPUFREQ_GTK_FILE=$AUTO_CPUFREQ_FILE-gtk
AUTO_CPUFREQ_GTK_DESKTOP_FILE="$(basename $AUTO_CPUFREQ_GTK_FILE).desktop"

CONFIG_FILE="/etc/auto-cpufreq.conf"
IMG_FILE="/usr/share/pixmaps/auto-cpufreq.png"
ORG_FILE="/usr/share/polkit-1/actions/org.auto-cpufreq.pkexec.policy"

Expand Down Expand Up @@ -82,10 +83,9 @@ function tool_install {

elif [ -f /etc/redhat-release ]; then
detected_distro "RedHat based"
if [ -f /etc/centos-release ]; then yum install platform-python-devel
else yum install python-devel
fi
yum install dmidecode gcc cairo-devel gobject-introspection-devel cairo-gobject-devel gtk3-devel
pkg="python-devel dmidecode gcc cairo-devel gobject-introspection-devel cairo-gobject-devel gtk3-devel"
if [ -f /etc/centos-release ]; then yum install platform-$pkg
else yum install $pkg; fi

elif [ -f /etc/solus-release ]; then
detected_distro "Solus"
Expand All @@ -95,7 +95,7 @@ function tool_install {
elif [ -f /etc/arch-release ]; then
detected_distro "Arch Linux based"
pacman -S --noconfirm --needed python python-pip python-setuptools base-devel dmidecode gobject-introspection gtk3 gcc

elif [ -f /etc/os-release ];then
. /etc/os-release
case $ID in
Expand All @@ -119,9 +119,7 @@ function tool_install {
;;
*) manual_install;;
esac
else # In case /etc/os-release doesn't exist
manual_install
fi
else manual_install; fi # In case /etc/os-release doesn't exist

header "Installing necessary Python packages"

Expand All @@ -138,15 +136,17 @@ function tool_install {
python -m pip install .

mkdir -p $SHARE_DIR
cp -r scripts/ $SHARE_DIR
cp -r images/ $SHARE_DIR
cp -r scripts/ $SHARE_DIR

cp $(basename $CONFIG_FILE) $CONFIG_FILE
cp images/icon.png $IMG_FILE
cp scripts/$(basename $ORG_FILE) $(dirname $ORG_FILE)

# this is necessary since we need this script before we can run auto-cpufreq itself
cp scripts/auto-cpufreq-venv-wrapper $AUTO_CPUFREQ_FILE
chmod a+x $AUTO_CPUFREQ_FILE
cp scripts/start_app $AUTO_CPUFREQ_GTK_FILE
cp scripts/start_app.sh $AUTO_CPUFREQ_GTK_FILE
chmod a+x $AUTO_CPUFREQ_GTK_FILE

desktop-file-install --dir=$APPLICATIONS_PATH scripts/$AUTO_CPUFREQ_GTK_DESKTOP_FILE
Expand All @@ -158,67 +158,57 @@ function tool_install {
}

function tool_remove {
# stop any running auto-cpufreq argument (daemon/live/monitor)
tool_arg_pids=($(pgrep -f "auto-cpufreq --"))
for pid in "${tool_arg_pids[@]}"; do [ $pid != $$ ] && kill "$pid"; done

function remove_directory {
[ -d $1 ] && rm -rf $1
}
function remove_file {
[ -f $1 ] && rm $1
}

srv_remove="$AUTO_CPUFREQ_FILE-remove"

# run uninstall in case of installed daemon
if [ -f $srv_remove -o -f $AUTO_CPUFREQ_FILE ]; then
eval "$AUTO_CPUFREQ_FILE --remove"
else
echo; echo "Couldn't remove the auto-cpufreq daemon, $srv_remove do not exist."
fi

# remove auto-cpufreq and all its supporting files
remove_directory $SHARE_DIR

remove_file "$AUTO_CPUFREQ_FILE-install"
remove_file $srv_remove
remove_file $AUTO_CPUFREQ_FILE
remove_file $AUTO_CPUFREQ_GTK_FILE
remove_file $IMG_FILE
remove_file $ORG_FILE
remove_file "/usr/local/bin/cpufreqctl.auto-cpufreq"
remove_file "/var/run/auto-cpufreq.stats"

remove_file "$APPLICATIONS_PATH/$AUTO_CPUFREQ_GTK_DESKTOP_FILE"
update-desktop-database $APPLICATIONS_PATH

# remove python virtual environment
remove_directory $venv_path

echo; echo "auto-cpufreq tool and all its supporting files successfully removed"; echo
# stop any running auto-cpufreq argument (daemon/live/monitor)
tool_arg_pids=($(pgrep -f "auto-cpufreq --"))
for pid in "${tool_arg_pids[@]}"; do [ $pid != $$ ] && kill "$pid"; done

function remove_directory { [ -d $1 ] && rm -rf $1; }
function remove_file { [ -f $1 ] && rm $1; }

# run uninstall in case of installed daemon
if [ -f $AUTO_CPUFREQ_FILE ]; then eval "$AUTO_CPUFREQ_FILE --remove"
else echo; echo "Couldn't remove the auto-cpufreq daemon, $AUTO_CPUFREQ_FILE do not exist."; fi

# remove auto-cpufreq and all its supporting files
remove_directory $SHARE_DIR
remove_directory $VENV_PATH

remove_file "$AUTO_CPUFREQ_FILE-install"
remove_file "$AUTO_CPUFREQ_FILE-remove"
remove_file $AUTO_CPUFREQ_FILE
remove_file $AUTO_CPUFREQ_GTK_FILE
remove_file $CONFIG_FILE
remove_file $IMG_FILE
remove_file $ORG_FILE
remove_file "/usr/local/bin/cpufreqctl.auto-cpufreq"
remove_file "/var/run/auto-cpufreq.stats"

remove_file "$APPLICATIONS_PATH/$AUTO_CPUFREQ_GTK_DESKTOP_FILE"
update-desktop-database $APPLICATIONS_PATH

echo; echo "auto-cpufreq tool and all its supporting files successfully removed"; echo
}

# root check
if ((EUID != 0)); then
echo; echo "Must be run as root (i.e: 'sudo $0')."; echo
exit 1
echo; echo "Must be run as root (i.e: 'sudo $0')."; echo
exit 1
fi

if [[ -z "$1" ]]; then ask_operation
else
case "$1" in
--install) answer="i";;
--remove) answer="r";;
*) ask_operation;;
esac
case "$1" in
-i|--install) answer="i";;
-r|--remove) answer="r";;
*) ask_operation;;
esac
fi

case $answer in
case "$answer" in
I|i) tool_install;;
R|r) tool_remove;;
*)
echo "Unknown key, aborting ..."; echo
exit 1
;;
echo "Unknown key, aborting ..."; echo
exit 1
;;
esac
File renamed without changes.
Empty file removed auto_cpufreq/__init__.py
Empty file.
66 changes: 66 additions & 0 deletions auto_cpufreq/batteries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
from os.path import basename
from subprocess import check_output, getoutput

from auto_cpufreq.config.config import CONFIG
from auto_cpufreq.prints import print_info, print_info_block, print_warning

class Batteries:
def __init__(self) -> None:
self.batteries:tuple[str] = tuple(getoutput('find /sys/class/power_supply/BAT*').splitlines())
self.charge_threshold_files:list[tuple[str, str]] = [] # charge_start_threshold, charge_stop_threshold
for bat in self.batteries:
charge_threshold_files = getoutput(f'find {bat}/ -type f -name "charge_*_threshold" | sort').splitlines()
if len(charge_threshold_files) == 2: self.charge_threshold_files.append(tuple(charge_threshold_files))
self.conservation_mode_files:tuple[str] = tuple(getoutput('find /sys/devices/ -type f -name conservation_mode').splitlines())

def charging(self) -> bool: return any(map(lambda bat: getoutput(f'cat {bat}/status') == 'Charging', self.batteries))

def setup(self) -> None:
self.set_charge_threshold()
if CONFIG.has_option('battery', 'conservation_mode'):
self.set_conservation_mode(int(CONFIG.get_option('battery', 'conservation_mode') == 'true'))

def set_charge_threshold(self) -> None:
for charge_start_threshold_file, charge_stop_threshold_file in self.charge_threshold_files:
_set_value_in_file(_get_threshold_value('start'), charge_start_threshold_file, 'charge start threshold')
_set_value_in_file(_get_threshold_value('stop'), charge_stop_threshold_file, 'charge stop threshold')

def set_conservation_mode(self, value:int) -> None:
for conservation_mode_file in self.conservation_mode_files:
_set_value_in_file(value, conservation_mode_file, 'conservation mode')

def show_batteries_info(self) -> None:
print_info_block(
'Batteries',
*(
basename(bat)+'\n '+
_show_value_in_file(bat+'/manufacturer', 'Manufacturer')+'\n '+
_show_value_in_file(bat+'/model_name', 'Model name')+'\n '+
_show_value_in_file(bat+'/technology', 'Technology')+'\n '+
_show_value_in_file(bat+'/status', 'Status')+'\n '+
_show_value_in_file(bat+'/capacity', 'Capacity')+'\n'
for bat in self.batteries
),
*(_show_value_in_file(conservation_mode_file, 'Conservation mode', True) for conservation_mode_file in self.conservation_mode_files),
*(
_show_value_in_file(charge_start_threshold, 'Charge start threshold', True)+'\n'+
_show_value_in_file(charge_stop_threshold, 'Charge stop threshold', True)
for charge_start_threshold, charge_stop_threshold in self.charge_threshold_files
),
)

def _get_threshold_value(mode:str) -> int:
option = ('battery', mode+'_threshold')
return int(CONFIG.get_option(*option)) if CONFIG.has_option(*option) else 100*int(mode != 'start')

def _set_value_in_file(value:object, file:str, role:str) -> None:
str = f'{role} {value} in {file}'
try:
check_output(f'echo {value} | tee {file}', shell=True)
print_info('Set', str)
except: print_warning('Unable to set', str)

def _show_value_in_file(file:str, role:str, show_file_path:bool=False) -> str: return f'{role}: {getoutput("cat "+file)}\t{file*int(show_file_path)}'

BATTERIES = Batteries()
20 changes: 0 additions & 20 deletions auto_cpufreq/battery_scripts/battery.py

This file was deleted.

42 changes: 0 additions & 42 deletions auto_cpufreq/battery_scripts/ideapad_acpi.py

This file was deleted.

73 changes: 0 additions & 73 deletions auto_cpufreq/battery_scripts/ideapad_laptop.py

This file was deleted.

Loading

0 comments on commit 98f1843

Please sign in to comment.