forked from AdnanHodzic/auto-cpufreq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-cpufreq-installer
executable file
·370 lines (317 loc) · 9.85 KB
/
auto-cpufreq-installer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
# auto-cpufreq-installer:
# auto-cpufreq source code based installer
SCRIPT_PATH=$(readlink -f "$0")
SCRIPT_DIR=$(dirname "${SCRIPT_PATH}")
cd "${SCRIPT_DIR}"
# check if lsb_release exists on the system before using it
if command -v lsb_release > /dev/null
then
distro="$(lsb_release -is)"
release="$(lsb_release -rs)"
codename="$(lsb_release -cs)"
fi
# functions
#separator
function separator {
local COLUMNS="`tput cols`"
echo -e "\n"
printf "%0.s─" $(seq $COLUMNS)
echo -e "\n"
}
# root check
function root_check {
if ((EUID != 0)); then
separator
echo -e "\nMust be run as root (i.e: 'sudo $0')."
separator
exit 1
fi
}
function header {
local COLUMNS="`tput cols`"
MID="$((COLUMNS / 2))"
HEADER="$1"
printf "%0.s─" $(seq $((MID-(${#HEADER}/2)- 1)))
echo -n " $HEADER "
printf "%0.s─" $(seq $((MID-(${#HEADER}/2)- 1)))
echo -e "\n"
return
}
# python packages install
function setup_venv {
venv_dir=/opt/auto-cpufreq/venv
mkdir -p "${venv_dir}"
python3 -m venv "${venv_dir}"
source "${venv_dir}/bin/activate"
python3 -m pip install --upgrade pip wheel
python3 -m pip install -r requirements.txt
}
# tool install
function install {
python3 setup.py install --record files.txt
mkdir -p /usr/local/share/auto-cpufreq/
cp -r scripts/ /usr/local/share/auto-cpufreq/
cp -r images/ /usr/local/share/auto-cpufreq/
cp images/icon.png /usr/share/pixmaps/auto-cpufreq.png
cp scripts/org.auto-cpufreq.pkexec.policy /usr/share/polkit-1/actions
# this is necessary since we need this script before we can run auto-cpufreq itself
cp scripts/auto-cpufreq-venv-wrapper /usr/local/bin/auto-cpufreq
cp scripts/start_app /usr/local/bin/auto-cpufreq-gtk
chmod a+x /usr/local/bin/auto-cpufreq
chmod a+x /usr/local/bin/auto-cpufreq-gtk
desktop-file-install --dir=/usr/share/applications scripts/auto-cpufreq-gtk.desktop
update-desktop-database /usr/share/applications
}
# First argument is the distro
function detected_distro() {
echo -e "\nDetected $1 distribution"
separator
echo -e "\nSetting up Python environment\n"
}
# merged functions pip - install - complete_msg, since it repeats
function completed () {
echo -e "\nInstalling necessary Python packages\n"
setup_venv
separator
echo -e "\ninstalling auto-cpufreq tool\n"
install
}
function complete_msg() {
separator
cat <<- _EOF_
auto-cpufreq tool successfully installed.
For list of options, run:
auto-cpufreq --help"
_EOF_
separator
}
function manual_install {
cat <<- _EOF_
Didn't detect Debian or RedHat based distro.
To complete installation, you need to:
Install: python3, pip3, python3-setuptools
Install necessary Python packages:
pip3 install psutil click distro power
Run following sequence of lines:
-----
python3 setup.py install --record files.txt
mkdir -p /usr/local/share/auto-cpufreq/
cp -r scripts/ /usr/local/share/auto-cpufreq/
-----
After which tool is installed, for full list of options run:
auto-cpufreq
_EOF_
separator
cat <<- _EOF_
Consider creating a feature request to add support for your distro:
https://github.com/AdnanHodzic/auto-cpufreq/issues/new
Make sure to include following information:
Distribution: $distro
Release: $release
Codename: $codename
_EOF_
separator
}
function tool_install {
separator
if [ -f /etc/debian_version ]; then
detected_distro "Debian based"
apt install python3-dev python3-pip python3-venv python3-setuptools dmidecode libgirepository1.0-dev libcairo2-dev -y
completed
complete_msg
elif [ -f /etc/redhat-release ]; then
detected_distro "RedHat based"
if [ -f /etc/centos-release ]; then
yum install platform-python-devel dmidecode
else
yum install python-devel dmidecode
fi
completed
complete_msg
elif [ -f /etc/solus-release ]; then
detected_distro "Solus"
eopkg install pip python3 python3-devel dmidecode
eopkg install -c system.devel
completed
complete_msg
elif [ -f /etc/os-release ];then
eval "$(cat /etc/os-release)"
separator
case $ID in
opensuse-leap)
detected_distro "OpenSUSE"
zypper install -y python3 python3-pip python3-setuptools python3-devel gcc dmidecode
completed
;;
opensuse)
detected_distro "OpenSUSE"
echo -e "\nDetected an OpenSUSE distribution\n\nSetting up Python environment\n"
zypper install -y python38 python3-pip python3-setuptools python3-devel gcc dmidecode gobject-introspection-devel python3-cairo-devel
completed
;;
arch|manjaro|endeavouros|garuda|artix)
detected_distro "Arch Linux based"
pacman -S --noconfirm --needed python python-pip python-setuptools base-devel dmidecode gobject-introspection
completed
;;
void)
detected_distro "Void Linux"
xbps-install -Sy python3 python3-pip python3-devel python3-setuptools base-devel dmidecode
completed
;;
*) #Any other distro
manual_install
completed
;;
esac
complete_msg
else # In case /etc/os-release doesn't exist
manual_install
fi
}
function tool_remove {
files="files.txt"
share_dir="/usr/local/share/auto-cpufreq/"
srv_install="/usr/local/bin/auto-cpufreq-install"
srv_install_old="/usr/bin/auto-cpufreq-install"
srv_remove="/usr/local/bin/auto-cpufreq-remove"
srv_remove_old="/usr/bin/auto-cpufreq-remove"
stats_file="/var/run/auto-cpufreq.stats"
tool_proc_rm="/usr/local/bin/auto-cpufreq --remove"
wrapper_script="/usr/local/bin/auto-cpufreq"
gui_wrapper_script="/usr/local/bin/auto-cpufreq-gtk"
unit_file="/etc/systemd/system/auto-cpufreq.service"
venv_path="/opt/auto-cpufreq"
cpufreqctl="/usr/local/bin/cpufreqctl.auto-cpufreq"
cpufreqctl_old="/usr/bin/cpufreqctl.auto-cpufreq"
desktop_file="/usr/share/applications/auto-cpufreq-gtk.desktop"
# stop any running auto-cpufreq argument (daemon/live/monitor)
tool_arg_pids=($(pgrep -f "auto-cpufreq --"))
for pid in "${tool_arg_pids[@]}"; do
if [[ $tool_arg_pids != $$ ]]; then
kill "$tool_arg_pids"
fi
done
# run uninstall in case of installed daemon
if [ -f $srv_remove -o -f $srv_remove_old -o $wrapper_script ]; then
eval $tool_proc_rm
else
separator
printf "Couldn't remove the auto-cpufreq daemon\n$srv_remove or $srv_remove_old do not exist.\n"
exit 1;
separator
fi
# remove auto-cpufreq and all its supporting files
[ -f $files ] && cat $files | xargs sudo rm -rf && rm -f $files
[ -d $share_dir ] && rm -rf $share_dir
# files cleanup
[ -f $srv_install ] && rm $srv_install
[ -f $srv_install_old ] && rm $srv_install_old
[ -f $srv_remove ] && rm $srv_remove
[ -f $srv_remove_old ] && rm $srv_remove_old
[ -f $stats_file ] && rm $stats_file
[ -f $unit_file ] && rm $unit_file
[ -f $wrapper_script ] && rm $wrapper_script
[ -f $gui_wrapper_script ] && rm $gui_wrapper_script
[ -f $cpufreqctl ] && rm $cpufreqctl
[ -f $cpufreqctl_old ] && rm $cpufreqctl_old
[ -f $desktop_file ] && rm $desktop_file
update-desktop-database /usr/share/applications
# remove python virtual environment
rm -rf "${venv_path}"
separator
echo -e "\nauto-cpufreq tool and all its supporting files successfully removed."
separator
}
function tool_update {
# Specify the repository and package name
# IT IS IMPORTANT TO NOTE THAT IF THE REPOSITORY STRUCTURE IS CHANGED, THE FOLLOWING FUNCTION NEEDS TO BE UPDATED ACCORDINGLY
repository="AdnanHodzic/auto-cpufreq"
# Fetch the latest release information from GitHub API
latest_release=$(curl -s "https://api.github.com/repos/$repository/releases/latest")
# Extract the latest release version without using jq
latest_version=$(echo "$latest_release" | grep -o '"tag_name": "[^"]*' | cut -d'"' -f4)
# Get the current version of auto-cpufreq
#installed_version=$(pip list | awk '/auto-cpufreq/ {print $2}')
installed_version=$(grep -oP "(?<=__requires__ = 'auto-cpufreq==)\d+(\.\d+)+" /opt/auto-cpufreq/venv/bin/auto-cpufreq)
#Check whether the same is installed or not
if [[ -z "$installed_version" ]]; then
echo "Current version not found, please install auto-cpufreq first"
echo $installed_version
sleep 1
exit 1
fi
installed_version="v$installed_version"
# Compare the latest version with the installed version and perform update if necessary
if [[ "$latest_version" == "$installed_version" ]]; then
echo "auto-cpufreq is up to date"
else
echo -e "Updates are available,\nCurrent version: $installed_version\nLatest version: $latest_version \nNote that your previous custom settings might be \033[1merased\033[0m with the following update \nalong with the \033[1mcurrent\033[0m directory. "
read -p "Do you want to update auto-cpufreq to the latest release? [y/n]: " ans
if [[ "$ans" == "y" ]]; then
header "Updating auto-cpufreq to the latest release\n"
tool_remove
echo -e "deleting the current directory\n"
script_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
mkdir "$script_directory/../auto-cpufreq $latest_version" && cd $_
rm -rf $script_directory
echo -e "cloning to the latest release\n"
git clone https://github.com/AdnanHodzic/auto-cpufreq.git
cd auto-cpufreq
separator
tool_install
else
separator
echo "Aborting..."
sleep 0.5
fi
fi
}
function ask_operation {
header "auto-cpufreq installer"
echo -e "Welcome to auto-cpufreq tool installer.
\nOptions:\n"
read -p \
"[I]nstall
[U]pdate
[R]emove
[Q]uit
Select a key: [i/u/r/q]: " answer
}
# End of functions
root_check
if [[ -z "${1}" ]]; then
ask_operation
else
case "${1}" in
"--install")
answer="i"
;;
"--update")
answer="u"
;;
"--remove")
answer="r"
;;
*)
answer="n"
;;
esac
fi
case $answer in
I|i)
tool_install ;;
U|u)
tool_update ;;
R|r)
tool_remove ;;
Q|q)
separator
echo ""
exit 0 ;;
*)
separator
echo -e "\nUnknown key, aborting ...\n"
exit 1 ;;
esac