Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactoring and add some features #712

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
98f1843
Big code refactoring
Angel-Karasu May 26, 2024
960444f
Compatible with many arguments
Angel-Karasu May 28, 2024
66a3232
Show help if only color arg
Angel-Karasu May 31, 2024
2c4a6ef
Refacoring code and auto run in root
Angel-Karasu May 31, 2024
d2a3ba6
Auto run in root
Angel-Karasu May 31, 2024
1291057
Add AntiX and Devuan
Angel-Karasu May 31, 2024
34337ca
Repare tabulation problem
Angel-Karasu May 31, 2024
74e0318
Remove |
Angel-Karasu Jun 1, 2024
4cabaef
Merge branch 'master' of https://github.com/Angel-Karasu/auto-cpufreq
Angel-Karasu Jun 1, 2024
31e5ee8
force python3
Angel-Karasu Jun 1, 2024
b086921
Remove -d for --daemon
Angel-Karasu Jun 1, 2024
1989c8b
Requests package 2.32
Angel-Karasu Jun 1, 2024
987fdda
Remove does_command_exist function
Angel-Karasu Jun 1, 2024
76cd1dd
Big code refactoring
Angel-Karasu May 26, 2024
505b7fe
Compatible with many arguments
Angel-Karasu May 28, 2024
303db40
Show help if only color arg
Angel-Karasu May 31, 2024
0f484dd
Refacoring code and auto run in root
Angel-Karasu May 31, 2024
d01bb0c
Auto run in root
Angel-Karasu May 31, 2024
23224cc
Add AntiX and Devuan
Angel-Karasu May 31, 2024
739170a
Remove |
Angel-Karasu Jun 1, 2024
51937ac
Repare tabulation problem
Angel-Karasu May 31, 2024
cc12af4
force python3
Angel-Karasu Jun 1, 2024
7d2b308
Remove -d for --daemon
Angel-Karasu Jun 1, 2024
e7ba3de
Requests package 2.32
Angel-Karasu Jun 1, 2024
25d4479
Remove does_command_exist function
Angel-Karasu Jun 1, 2024
3a14265
Merge pull request #1 from Angel-Karasu/AdnanHodzic-master
Angel-Karasu Jun 7, 2024
7a7785e
Repare Nix patch
Angel-Karasu Jun 7, 2024
3423acb
Merge pull request #2 from Angel-Karasu/AdnanHodzic-master
Angel-Karasu Jun 7, 2024
5ef1316
Repare prevent-install-and-copy.patch
Angel-Karasu Jun 7, 2024
1dcad42
Repare patch
Angel-Karasu Jun 7, 2024
775cc2b
Detect if batteries are not discharging
Angel-Karasu Jun 7, 2024
2bb55e9
Update prints
Angel-Karasu Jul 8, 2024
bebf55c
Repare color 0
Angel-Karasu Jul 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
65 changes: 26 additions & 39 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 @@ -135,18 +133,20 @@ function tool_install {
header "Installing auto-cpufreq tool"

git config --global --add safe.directory $(pwd)
python -m pip install .
python3 -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 @@ -162,29 +162,22 @@ function tool_remove {
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"
function remove_directory { [ -d $1 ] && rm -rf $1; }
function remove_file { [ -f $1 ] && rm $1; }

# 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
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 $srv_remove
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"
Expand All @@ -193,28 +186,22 @@ function tool_remove {
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
}

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

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

case $answer in
case "$answer" in
I|i) tool_install;;
R|r) tool_remove;;
*)
Expand Down
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') != 'Discharging', 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.

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

This file was deleted.

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

This file was deleted.

39 changes: 0 additions & 39 deletions auto_cpufreq/battery_scripts/thinkpad.py

This file was deleted.

Empty file removed auto_cpufreq/bin/__init__.py
Empty file.
Loading