-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacext.sh
More file actions
executable file
·135 lines (118 loc) · 4.3 KB
/
Copy pathpacext.sh
File metadata and controls
executable file
·135 lines (118 loc) · 4.3 KB
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
#!/usr/bin/env bash
# Pacman extensions by Ben Mitchell <bjosephmitchell@gmail.com>
# https://mit-license.org/
# Copyright © 2021 Ben Mitchell
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the “Software”), to deal in the Software without
# restriction, including without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ROOT_CMD="${ROOT_CMD:-/usr/bin/sudo}"
PACMAN="${PACMAN:-/usr/bin/pacman}"
# Force LC_ALL=C
export LC_ALL=C
usage() {
echo "pacext - pacman extensions"
echo " "
echo "pacext [options] [arguments]"
echo " "
echo "options:"
echo "-h, --help show this help"
echo "-s, --summary show pacman package summary"
echo "-p, --whatprovides <file> get info on the package that provides a given file"
echo "-d, --whatdepends <package> get a list of packages that this package depends on"
echo "-r, --whatrequires <package> get a list of packages that depend on this package"
echo "-a, --autoremove [package] remove unused dependencies"
echo "-k, --check-kernel checks loaded kernel version against installed one"
exit 0
}
# Displays packages as if they were searched for in pacman
displaypackages() {
while read LINE
do
if [[ $LINE == *"Name"* ]]; then
printf "\e[1m$LINE\e[0m" | sed "s/Name *: *//g"
fi
if [[ $LINE == *"Version"* ]]; then
printf " \e[1;32m$LINE\e[0m" | sed "s/Version *: *//g"
fi
if [[ $LINE == *"Description"* ]]; then
printf "\n $LINE\n" | sed "s/Description *: *//g"
fi
done < "${1:-/dev/stdin}"
}
summary() {
printf "Package Summary:\n"
printf "Number of packages: %s\n" $(pacman -Qq "$@" | wc -l)
printf "Explicitly installed packages: %s\n" $(pacman -Qeq "$@" | wc -l)
printf "Total Storage Consumed: %s\n" \
$($PACMAN -Qi "$@" | awk '/^Installed Size/{print $4$5}' \
| numfmt --from=auto --suffix=B | awk 'BEGIN {sum=0} {sum=sum+$1} END {printf "%.0f\n", sum}' \
| numfmt --to=iec-i --suffix=B)
}
whatprovides() {
exec $PACMAN -Qoq "$@"
}
whatdepends() {
$PACMAN -Qi "$@" | awk -F'[:]' '/^Depends/ {print $2}' | xargs -n1
}
whatrequires() {
$PACMAN -Qi "$@" | awk -F'[:]' '/^Required/ {print $2}' | xargs -n1
}
autoremove() {
exec $PACMAN -Qdtq $@ | $ROOT_CMD $PACMAN -Rs -
}
check-kernel() {
KERNEL_VERSION=$(pacman -Qi linux | grep Version | sed "s/Version * : *//g;s/.arch/-arch/g")
LOADED_VERSION=$(uname -r)
printf "\e[1mKernel Version\e[0m : $KERNEL_VERSION\n"
printf "\e[1mLoaded Version\e[0m : $LOADED_VERSION\n"
printf "\e[1mMatched\e[0m : "
if [ $KERNEL_VERSION = $LOADED_VERSION ]
then
printf "\e[1;32mYes\e[0m\n"
exit 0
else
printf "\e[1;31mNo\e[0m\n"
exit 1
fi
}
case "$1" in
-h|--help)
usage
;;
-s|--summary)
shift
summary "$@"
;;
-p|--whatprovides)
shift
whatprovides "$@" | $PACMAN -Qi - | displaypackages
;;
-d|--whatdepends)
shift
whatdepends "$@" | $PACMAN -Qi - | displaypackages
;;
-r|--whatrequires)
shift
whatrequires "$@" | $PACMAN -Qi - | displaypackages
;;
-a|--autoremove)
shift
autoremove "$@"
;;
-k|--check-kernel)
check-kernel
;;
*)
printf "\e[1;31mInvalid Command\e[0m: $1\n\n"
usage
;;
esac