mostly indenting
[distro-functions] / src / package-manager-abstractions
1 #!/bin/bash
2 # Copyright (C) 2014 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # basic yum/apt package manager abstraction, plus a few minor conveniences
17 if command -v yum &> /dev/null; then
18 # package manager
19 p() {
20 local s; [[ $EUID != 0 ]] && s=sudo
21 $s yum "$@"
22 }
23 # package install
24 pi() {
25 local s; [[ $EUID != 0 ]] && s=sudo
26 $s yum -y install "$@"
27 }
28 # package find
29 pf() {
30 local s; [[ $EUID != 0 ]] && s=sudo
31 $s yum search "$@"
32 }
33 # package remove/uninstall
34 pu() {
35 local s; [[ $EUID != 0 ]] && s=sudo
36 $s yum autoremove "$@"
37 }
38 pup() { # upgrade
39 local s; [[ $EUID != 0 ]] && s=sudo
40 $s yum -y distro-sync full "$@"
41 }
42 # package list info
43 pl() {
44 yum info "$@"
45 }
46 pfile() {
47 yum whatprovides \*/$1
48 }
49
50 elif command -v apt-get &>/dev/null; then
51 plock-wait() {
52 local i
53 i=0
54 while fuser /var/lib/dpkg/lock &>/dev/null; do
55 sleep 1
56 i=$(( i+1 ))
57 if (( i > 300 )); then
58 echo "error: timed out waiting for /var/lib/dpkg/lock" >&2
59 return 1
60 fi
61 done
62 }
63 pcheck() {
64 for arg; do
65 if [[ $1 == -* ]]; then
66 shift
67 else
68 break
69 fi
70 done
71 if dpkg -s -- "$@" |& grep -Fx "Status: install ok installed" &>/dev/null; then
72 return 1
73 fi
74 return 0
75 }
76 pp() { # package policy
77 apt-cache policy $@
78 }
79 p() {
80 local s; [[ $EUID != 0 ]] && s=sudo
81 case $1 in
82 install)
83 $s apt-get --purge --auto-remove "$@"
84 ;;
85 *)
86 $s apt-get "$@"
87 ;;
88 esac
89 }
90 pupdate() {
91 local now t s f cachetime limittime; [[ $EUID != 0 ]] && s=sudo
92 # update package list if its more than an 2 hours old
93 f=/var/cache/apt/pkgcache.bin
94 if [[ -r $f ]]; then
95 cachetime=$(stat -c %Y $f )
96 else
97 cachetime=0
98 fi
99 now=$(date +%s)
100 limittime=$(( now - 60*60*2 ))
101 for f in /etc/apt/sources.list /etc/apt/sources.list.d/*.list; do
102 if [[ -r $f ]]; then
103 t=$(stat -c %Y $f )
104 if (( t > limittime )); then
105 limittime=$t
106 fi
107 fi
108 done
109 if (( cachtime > limittime )); then
110 $s apt-get update
111 fi
112 }
113 pi() {
114 pcheck "$@" || return 0
115 pupdate
116 local s; [[ $EUID != 0 ]] && s=sudo
117 if [[ $- != *i* ]]; then
118 echo pi "$@"
119 fi
120 $s $PI_PREFIX apt-get -y install --purge --auto-remove "$@"
121 }
122
123 pi-nostart() {
124 local ret=
125 pcheck "$@" || return 0
126 plock-wait
127 pupdate
128 local s; [[ $EUID != 0 ]] && s=sudo
129 local f=/usr/sbin/policy-rc.d
130 $s dd of=$f 2>/dev/null <<EOF
131 #!/bin/sh
132 exit 101
133 EOF
134 $s chmod +x $f
135 if [[ $- != *i* ]]; then
136 echo pi-nostart "$@"
137 fi
138 $s apt-get -y install --purge --auto-remove "$@" || ret=$?
139 $s rm $f
140 return $ret
141 }
142 pf() {
143 # package name and descriptions
144 apt-cache search "$@"
145 }
146 pff() {
147 local s; [[ $EUID != 0 ]] && s=sudo
148 # nice aptitude search from emacs shell. package description width as
149 # wide as the screen, and package name field small aptitude
150 # manual can't figure out how wide emacs terminal is, of course
151 # it doesn't consult the $COLUMNS variable... and in a normal
152 # terminal, it makes the package name field ridiculously big
153 # also, remove that useless dash before the description
154 aptitude -F "%c%a%M %p %$((COLUMNS - 30))d" -w $COLUMNS search "$@"
155 }
156 pu() {
157 local s; [[ $EUID != 0 ]] && s=sudo
158 local needed=false
159 for arg; do
160 if dpkg -s -- "$arg" &>/dev/null; then
161 needed=true
162 break
163 fi
164 done
165 $needed || return 0
166 plock-wait
167 $s apt-get -y remove --purge --auto-remove "$@"
168 # seems slightly redundant, but it removes more stuff sometimes.
169 $s apt-get -y autoremove
170 }
171 pup() { # upgrade
172 plock-wait
173 pupdate
174 local s; [[ $EUID != 0 ]] && s=sudo
175 $s apt-get -y dist-upgrade --purge --auto-remove "$@"
176 $s apt-get -y autoremove
177 if [[ -e /usr/sbin/checkrestart ]]; then
178 $s /usr/sbin/checkrestart -p
179 fi
180 }
181 # package info
182 pl() {
183 if type -p aptitude &>/dev/null; then
184 aptitude show "$@"
185 else
186 apt-cache show "$@"
187 fi
188 }
189 pfile() {
190 local file=$1
191 # ucfq can tell us about config files which are not tracked
192 # with apt-file. but, for at least a few files I tested
193 # which are tracked with apt-file, ucfq doesn't show their
194 # package name. So, commenting this, waiting to find
195 # a config file only tracked by ucfq to see if it gives the
196 # package name and if I can identify this kind of file.
197 # if [[ $file == /* ]] && ! ucfq -w $file | grep ::: &>/dev/null; then
198 # ucfq $file
199
200 if [[ $file == */* ]]; then
201 apt-file find -x "$file"\$
202 else
203 apt-file find -x /"$file"\$
204 update-alternatives --list "$file" 2>/dev/null
205 fi
206 }
207 pkgfiles() {
208 if dpkg -s "$1" &>/dev/null; then
209 dpkg-query -L $1
210 else
211 apt-file -x list "^$1$"
212 fi
213 }
214
215 elif command -v pacman &>/dev/null; then
216 p() {
217 pacaur "$@"
218 }
219 pi() {
220 pacaur -S --noconfirm --needed --noedit "$@"
221 }
222 pf() {
223 pacaur -Ss "$@"
224 }
225 pu() {
226 pacaur -Rs --noconfirm "$@"
227 if p=$(pacaur -Qdtq); then
228 pacaur -Rs $p
229 fi
230 }
231 aurex() {
232 p="$1"
233 aur='https://aur.archlinux.org'
234 curl -s $aur/$(curl -s "$aur/rpc.php?type=info&arg=$p" \
235 | jq -r .results.URLPath) | tar xz
236 cd "$p"
237
238 }
239 pmirror() {
240 local s; [[ $EUID != 0 ]] && s=sudo
241 local x=$(mktemp)
242 curl -s "https://www.archlinux.org/mirrorlist/\
243 ?country=US&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" \
244 | sed -r 's/^[ #]*(Server *=)/\1/' > $x
245 if (( $(stat -c %s $x ) > 10 )); then
246 $s cp $x /etc/pacman.d/mirrorlist
247 rm $x
248 fi
249 }
250 pup() { # upgrade
251 local s; [[ $EUID != 0 ]] && s=sudo
252 # file_time + 24 hours > current_time
253 if ! (( $(stat -c%Y /etc/pacman.d/mirrorlist) + 60*60*24 > $(date +%s) ))
254 then
255 pmirror
256 fi
257 pacaur -Syu --noconfirm "$@"
258 }
259 # package info
260 pl() {
261 pacaur -Si "$@"
262 }
263 pfile() {
264 pkgfile "$1"
265 }
266 pkgfiles() {
267 if pacaur -Qs "^$1$" &>/dev/null; then
268 pacman -Ql $1
269 else
270 pkgfile -l $1
271 fi
272 }
273 fi