0667bd99e174d55950e8785a2aaec97c38f12e7b
[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 if [[ $- != *i* ]]; then
117 echo pi "$*"
118 fi
119 if [[ $EUID == 0 ]]; then
120 DEBIAN_FRONTEND=noninteractive apt-get -y install --purge --auto-remove "$@"
121 else
122 sudo DEBIAN_FRONTEND=noninteractive apt-get -y install --purge --auto-remove "$@"
123 fi
124
125 }
126
127 pi-nostart() {
128 local ret=
129 pcheck "$@" || return 0
130 plock-wait
131 pupdate
132 local f=/usr/sbin/policy-rc.d
133 if [[ $- != *i* ]]; then
134 echo pi-nostart "$@"
135 fi
136 if [[ $EUID == 0 ]]; then
137 dd of=$f 2>/dev/null <<EOF
138 #!/bin/sh
139 exit 101
140 EOF
141 chmod +x $f
142 DEBIAN_FRONTEND=noninteractive apt-get -y install --purge --auto-remove "$@" || ret=$?
143 rm $f
144 else
145 sudo dd of=$f 2>/dev/null <<EOF
146 #!/bin/sh
147 exit 101
148 EOF
149 sudo chmod +x $f
150 sudo DEBIAN_FRONTEND=noninteractive apt-get -y install --purge --auto-remove "$@" || ret=$?
151 sudo rm $f
152 fi
153 return $ret
154 }
155 pf() {
156 # package name and descriptions
157 apt-cache search "$@"
158 }
159 pff() {
160 local s; [[ $EUID != 0 ]] && s=sudo
161 # nice aptitude search from emacs shell. package description width as
162 # wide as the screen, and package name field small aptitude
163 # manual can't figure out how wide emacs terminal is, of course
164 # it doesn't consult the $COLUMNS variable... and in a normal
165 # terminal, it makes the package name field ridiculously big
166 # also, remove that useless dash before the description
167 aptitude -F "%c%a%M %p %$((COLUMNS - 30))d" -w $COLUMNS search "$@"
168 }
169 pu() {
170 local s; [[ $EUID != 0 ]] && s=sudo
171 local needed=false
172 for arg; do
173 if dpkg -s -- "$arg" &>/dev/null; then
174 needed=true
175 break
176 fi
177 done
178 $needed || return 0
179 plock-wait
180 $s apt-get -y remove --purge --auto-remove "$@"
181 # seems slightly redundant, but it removes more stuff sometimes.
182 $s apt-get -y autoremove
183 }
184 pup() { # upgrade
185 plock-wait
186 pupdate
187 local s; [[ $EUID != 0 ]] && s=sudo
188 $s apt-get -y dist-upgrade --purge --auto-remove "$@"
189 $s apt-get -y autoremove
190 }
191 # package info
192 pl() {
193 if type -p aptitude &>/dev/null; then
194 aptitude show "$@"
195 else
196 apt-cache show "$@"
197 fi
198 }
199 pfile() {
200 # -a = search all repos
201 local -a arg all
202 all=false
203 case $1 in
204 -a)
205 all=true
206 shift
207 ;;
208 esac
209 local file=$1
210 # ucfq can tell us about config files which are not tracked
211 # with apt-file. but, for at least a few files I tested
212 # which are tracked with apt-file, ucfq doesn't show their
213 # package name. So, commenting this, waiting to find
214 # a config file only tracked by ucfq to see if it gives the
215 # package name and if I can identify this kind of file.
216 # if [[ $file == /* ]] && ! ucfq -w $file | grep ::: &>/dev/null; then
217 # ucfq $file
218
219 if [[ $file == /* ]]; then
220 dpkg -S "$file"
221 else
222 if ! $all; then
223 arg=(--filter-origins "$(positive-origins)")
224 fi
225 if [[ $file == */* ]]; then
226 apt-file "${arg[@]}" find -x "$file"\$
227 else
228 apt-file "${arg[@]}" find -x /"$file"\$
229 update-alternatives --list "$file" 2>/dev/null
230 fi
231 fi
232 }
233 pkgfiles() {
234 if dpkg -s "$1" &>/dev/null; then
235 dpkg-query -L $1
236 else
237 apt-file -x list "^$1$"
238 fi
239 }
240
241 elif command -v pacman &>/dev/null; then
242 p() {
243 pacaur "$@"
244 }
245 pi() {
246 pacaur -S --noconfirm --needed --noedit "$@"
247 }
248 pf() {
249 pacaur -Ss "$@"
250 }
251 pu() {
252 pacaur -Rs --noconfirm "$@"
253 if p=$(pacaur -Qdtq); then
254 pacaur -Rs $p
255 fi
256 }
257 aurex() {
258 p="$1"
259 aur='https://aur.archlinux.org'
260 curl -s $aur/$(curl -s "$aur/rpc.php?type=info&arg=$p" \
261 | jq -r .results.URLPath) | tar xz
262 cd "$p"
263
264 }
265 pmirror() {
266 local s; [[ $EUID != 0 ]] && s=sudo
267 local x=$(mktemp)
268 curl -s "https://www.archlinux.org/mirrorlist/\
269 ?country=US&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" \
270 | sed -r 's/^[ #]*(Server *=)/\1/' > $x
271 if (( $(stat -c %s $x ) > 10 )); then
272 $s cp $x /etc/pacman.d/mirrorlist
273 rm $x
274 fi
275 }
276 pup() { # upgrade
277 local s; [[ $EUID != 0 ]] && s=sudo
278 # file_time + 24 hours > current_time
279 if ! (( $(stat -c%Y /etc/pacman.d/mirrorlist) + 60*60*24 > $(date +%s) ))
280 then
281 pmirror
282 fi
283 pacaur -Syu --noconfirm "$@"
284 }
285 # package info
286 pl() {
287 pacaur -Si "$@"
288 }
289 pfile() {
290 pkgfile "$1"
291 }
292 pkgfiles() {
293 if pacaur -Qs "^$1$" &>/dev/null; then
294 pacman -Ql $1
295 else
296 pkgfile -l $1
297 fi
298 }
299 fi